Add functions required during db debug.
[platform/core/security/key-manager.git] / src / manager / service / db-crypto.cpp
index 04f4022..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>
@@ -44,7 +45,7 @@ namespace {
          * increment and update DB_VERSION_CURRENT,
          * then provide migration mechanism!
          */
-        DB_VERSION_CURRENT             = 3
+        DB_VERSION_CURRENT             = 4
     };
 
     const char *SCRIPT_CREATE_SCHEMA                = "create_schema";
@@ -87,10 +88,11 @@ namespace {
             "INSERT INTO OBJECTS("
             "   exportable, dataType,"
             "   algorithmType, encryptionScheme,"
-            "   iv, dataSize, data, tag, idx) "
+            "   iv, dataSize, data, tag, idx, backendId) "
             "   VALUES(?001, ?002, ?003, ?004, ?005, "
             "          ?006, ?007, ?008,"
-            "          (SELECT idx FROM NAMES WHERE name=?101 and label=?102)"
+            "          (SELECT idx FROM NAMES WHERE name=?101 and label=?102),"
+            "          ?009"
             "         );";
 
     const char *DB_CMD_OBJECT_SELECT_BY_NAME_AND_LABEL =
@@ -422,6 +424,7 @@ namespace DB {
         row.dataSize = selectCommand->GetColumnInteger(7);
         row.data = selectCommand->GetColumnBlob(8);
         row.tag = selectCommand->GetColumnBlob(9);
+        row.backendId = static_cast<CryptoBackend>(selectCommand->GetColumnInteger(11));
         return row;
     }
 
@@ -816,6 +819,7 @@ namespace DB {
         insertObjectCommand->BindInteger(6, row.dataSize);
         insertObjectCommand->BindBlob   (7, row.data);
         insertObjectCommand->BindBlob   (8, row.tag);
+        insertObjectCommand->BindInteger(9, static_cast<int>(row.backendId));
 
         // name table reference
         insertObjectCommand->BindString (101, row.name.c_str());
@@ -823,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