Add functions required during db debug. 53/45953/3
authorBartlomiej Grzelewski <b.grzelewski@samsung.com>
Wed, 12 Aug 2015 09:12:40 +0000 (11:12 +0200)
committerkyungwook tak <k.tak@samsung.com>
Mon, 24 Aug 2015 07:35:31 +0000 (00:35 -0700)
Add Crypto::getSchema() and Crypto::getContent()

Change-Id: I46739eaef891edaa5d4f190a8adcadd2958dcc24

src/manager/dpl/db/include/dpl/db/sql_connection.h
src/manager/dpl/db/src/sql_connection.cpp
src/manager/service/db-crypto.cpp
src/manager/service/db-crypto.h

index b6aced5..4492dec 100644 (file)
@@ -368,6 +368,17 @@ class SqlConnection
          * @throw Exception::InvalidColumn
          */
         boost::optional<RawBuffer> GetColumnOptionalBlob(ColumnIndex column);
+
+        /**
+         * Get number of column.
+         */
+        ColumnIndex GetColumnCount();
+        /**
+         * Get type of the column.
+         *
+         * @throw Exception::InvalidColumn
+         */
+        int GetColumnType(ColumnIndex column);
     };
 
     // Move on copy constructor. No copy semantics
index 59d7877..110eca0 100644 (file)
@@ -397,6 +397,17 @@ void SqlConnection::DataCommand::Reset()
     LogPedantic("SQL data command reset");
 }
 
+SqlConnection::ColumnIndex SqlConnection::DataCommand::GetColumnCount() {
+    return sqlcipher3_column_count(m_stmt);
+}
+
+int SqlConnection::DataCommand::GetColumnType(
+    SqlConnection::ColumnIndex column)
+{
+    CheckColumnIndex(column);
+    return sqlcipher3_column_type(m_stmt, column);
+}
+
 void SqlConnection::DataCommand::CheckColumnIndex(
     SqlConnection::ColumnIndex column)
 {
index 8a5b57b..ce9d750 100644 (file)
@@ -21,6 +21,7 @@
  */
 
 #include <fstream>
+#include <sstream>
 #include <db-crypto.h>
 #include <dpl/db/sql_connection.h>
 #include <dpl/log/log.h>
@@ -826,6 +827,74 @@ namespace DB {
 
         insertObjectCommand->Step();
     }
+
+    std::string Crypto::getSchema() {
+        SqlConnection::DataCommandUniquePtr schema =
+                m_connection->PrepareDataCommand("SELECT sql FROM "
+                        "(SELECT * FROM sqlcipher_master UNION ALL "
+                        "SELECT * FROM sqlcipher_temp_master) "
+                        "WHERE type!='meta' "
+                        "ORDER BY tbl_name, type DESC, name;");
+
+        std::stringstream ss;
+        while(schema->Step()) {
+            ss << schema->GetColumnString(0) << std::endl;
+        }
+        return ss.str();
+    }
+
+    std::string Crypto::getContent() {
+        SqlConnection::DataCommandUniquePtr tableSelect =
+                m_connection->PrepareDataCommand(
+                        "SELECT name FROM sqlcipher_master "
+                        "WHERE type IN ('table','view') AND name NOT LIKE 'sqlcipher_%' "
+                        "UNION ALL "
+                        "SELECT name FROM sqlcipher_temp_master "
+                        "WHERE type IN ('table','view') "
+                        "ORDER BY 1; ");
+
+        std::vector<std::string> tables;
+        while(tableSelect->Step()) {
+            tables.push_back(tableSelect->GetColumnString(0));
+        }
+
+        std::stringstream ss;
+
+        for (auto &e : tables) {
+            ss << "Table " << e << std::endl;
+            std::string query = "select * from " + e + ";";
+            SqlConnection::DataCommandUniquePtr result =
+                m_connection->PrepareDataCommand(query.c_str());
+            while(result->Step()) {
+                int maxColumn = result->GetColumnCount();
+                for (int i = 0; i < maxColumn; ++i) {
+                    switch(result->GetColumnType(i)) {
+                        case 1: // int64
+                            ss << result->GetColumnInteger(i) << " | ";
+                            break;
+                        case 2: // float
+                            ss << result->GetColumnFloat(i) << " | ";
+                            break;
+                        case 3: // string
+                            ss << result->GetColumnString(i) << " | ";
+                            break;
+                        case 4: // Blob
+                            {
+                            auto buffer = result->GetColumnBlob(i);
+                            ss << "BLOB (Size: " << buffer.size() << ") | ";
+                            break;
+                            }
+                        case 5: // NULL
+                            ss << "NULL | ";
+                            break;
+                    }
+                }
+                ss << std::endl;
+            }
+        }
+
+        return ss.str();
+    }
 } // namespace DB
 } // namespace CKM
 
index ec3281d..193570d 100644 (file)
@@ -115,6 +115,10 @@ namespace DB {
                     const Name &name,
                     const Label &ownerLabel);
 
+            std::string getSchema();
+
+            std::string getContent();
+
             // keys
             void saveKey(const Label& label, const RawBuffer &key);
             RawBufferOptional getKey(const Label& label);