Zserio C++ runtime library  1.3.0
Built for Zserio 2.18.0
ValidationSqliteUtil.h
Go to the documentation of this file.
1 #ifndef ZSERIO_VALIDATION_SQLITE_UTIL_H_INC
2 #define ZSERIO_VALIDATION_SQLITE_UTIL_H_INC
3 
4 #include <map>
5 
6 #include "zserio/RebindAlloc.h"
9 #include "zserio/String.h"
10 #include "zserio/StringView.h"
11 #include "zserio/Types.h"
12 
13 namespace zserio
14 {
15 
17 template <typename ALLOC>
19 {
21  using Statement = std::unique_ptr<sqlite3_stmt, SqliteFinalizer>;
22 
27  {
30  bool isNotNull;
31  bool isPrimaryKey;
32  };
33 
34  using TableSchema = std::map<string_type, ColumnDescription, std::less<string_type>,
36 
49  static size_t getNumberOfTableRows(SqliteConnection& connection, StringView attachedDbName,
50  StringView tableName, const ALLOC& allocator)
51  {
52  string_type sqlQuery(allocator);
53  sqlQuery += "SELECT count(*) FROM ";
54  if (!attachedDbName.empty())
55  {
56  sqlQuery += attachedDbName;
57  sqlQuery += ".";
58  }
59  sqlQuery += tableName;
60 
61  Statement statement(connection.prepareStatement(sqlQuery));
62  const int result = sqlite3_step(statement.get());
63  if (result != SQLITE_ROW)
64  {
65  throw SqliteException("ValidationSqliteUtils.getNumberOfTableRows: sqlite3_step() failed: ")
66  << SqliteErrorCode(result);
67  }
68 
69  return static_cast<size_t>(sqlite3_column_int64(statement.get(), 0));
70  }
71 
81  static void getTableSchema(SqliteConnection& connection, StringView attachedDbName, StringView tableName,
82  TableSchema& tableSchema, const ALLOC& allocator)
83  {
84  string_type sqlQuery(allocator);
85  sqlQuery += "PRAGMA ";
86  if (!attachedDbName.empty())
87  {
88  sqlQuery += attachedDbName;
89  sqlQuery += ".";
90  }
91  sqlQuery += "table_info(";
92  sqlQuery += tableName;
93  sqlQuery += ")";
94 
95  Statement statement(connection.prepareStatement(sqlQuery));
96 
97  int result = SQLITE_OK;
98  while ((result = sqlite3_step(statement.get())) == SQLITE_ROW)
99  {
100  const char* columnName = reinterpret_cast<const char*>(sqlite3_column_text(statement.get(), 1));
101  const char* columnType = reinterpret_cast<const char*>(sqlite3_column_text(statement.get(), 2));
102  tableSchema.emplace(string_type(columnName, allocator),
104  string_type(columnName, allocator), string_type(columnType, allocator),
105  sqlite3_column_int(statement.get(), 3) != 0, // is not null
106  sqlite3_column_int(statement.get(), 5) != 0 // is primary key
107  });
108  }
109 
110  if (result != SQLITE_DONE)
111  {
112  throw SqliteException("ValidationSqliteUtils.getTableSchema: sqlite3_step() failed: ")
113  << SqliteErrorCode(result);
114  }
115  }
116 
130  static bool isColumnInTable(SqliteConnection& connection, StringView attachedDbName, StringView tableName,
131  StringView columnName, const ALLOC& allocator)
132  {
133  // try select to check if hidden column exists
134  string_type sqlQuery(allocator);
135  sqlQuery += "SELECT ";
136  sqlQuery += columnName;
137  sqlQuery += " FROM ";
138  if (!attachedDbName.empty())
139  {
140  sqlQuery += attachedDbName;
141  sqlQuery += ".";
142  }
143  sqlQuery += tableName;
144  sqlQuery += " LIMIT 0";
145 
146  try
147  {
148  Statement statement(connection.prepareStatement(sqlQuery));
149  return sqlite3_step(statement.get()) == SQLITE_DONE;
150  }
151  catch (const SqliteException&)
152  {
153  return false;
154  }
155  }
156 
164  static const char* sqliteColumnTypeName(int columnType)
165  {
166  switch (columnType)
167  {
168  case SQLITE_INTEGER:
169  return "INTEGER";
170  case SQLITE_FLOAT:
171  return "REAL";
172  case SQLITE_TEXT:
173  return "TEXT";
174  case SQLITE_BLOB:
175  return "BLOB";
176  default:
177  return "NULL";
178  }
179  }
180 };
181 
182 } // namespace zserio
183 
184 #endif // ZSERIO_VALIDATION_SQLITE_UTIL_H_INC
constexpr bool empty() const noexcept
Definition: StringView.h:270
sqlite3_stmt * prepareStatement(StringView sqlQuery)
typename std::allocator_traits< ALLOC >::template rebind_alloc< T > RebindAlloc
Definition: RebindAlloc.h:10
std::basic_string< char, std::char_traits< char >, RebindAlloc< ALLOC, char > > string
Definition: String.h:17
static size_t getNumberOfTableRows(SqliteConnection &connection, StringView attachedDbName, StringView tableName, const ALLOC &allocator)
static void getTableSchema(SqliteConnection &connection, StringView attachedDbName, StringView tableName, TableSchema &tableSchema, const ALLOC &allocator)
static const char * sqliteColumnTypeName(int columnType)
std::map< string_type, ColumnDescription, std::less< string_type >, RebindAlloc< ALLOC, std::pair< const string_type, ColumnDescription > >> TableSchema
static bool isColumnInTable(SqliteConnection &connection, StringView attachedDbName, StringView tableName, StringView columnName, const ALLOC &allocator)
std::unique_ptr< sqlite3_stmt, SqliteFinalizer > Statement