ithewei 6 ani în urmă
părinte
comite
a4c64560f6
4 a modificat fișierele cu 105 adăugiri și 76 ștergeri
  1. 6 66
      sqlite/db.cpp
  2. 2 10
      sqlite/db.h
  3. 78 0
      sqlite/sql.cpp
  4. 19 0
      sqlite/sql.h

+ 6 - 66
sqlite/db.cpp

@@ -77,16 +77,10 @@ int dbtable_exist(HDB hdb, const char* table_name) {
     where += '\'';
     return dbtable_count(hdb, "sqlite_master", where.c_str());
 }
-// select count(*) from $table_name where $where;
+
 int dbtable_count(HDB hdb, const char* table_name, const char* where) {
     std::string sql;
-    sql = "select count(*) from ";
-    sql += table_name;
-    if (where) {
-        sql += " where ";
-        sql += where;
-    }
-    sql += ';';
+    sql_count(sql, table_name, where);
     DBTable table;
     if (db_exec_with_result(hdb, sql.c_str(), &table) == SQL_OK) {
         return atoi(table[1][0].c_str());
@@ -94,81 +88,27 @@ int dbtable_count(HDB hdb, const char* table_name, const char* where) {
     return 0;
 }
 
-// select keys from $table_name where $where limit $limit order by $column ASC|DESC;
 int dbtable_select(HDB hdb, const char* table_name, const char* keys, const char* where, DBTable* table, const KeyVal* options) {
     std::string sql;
-    sql = "select ";
-    if (keys) {
-        sql += keys;
-    }
-    else {
-        sql += '*';
-    }
-    sql += " from ";
-    sql += table_name;
-    if (where) {
-        sql += " where ";
-        sql += where;
-    }
-    if (options) {
-        for (KeyVal::const_iterator iter = options->begin(); iter != options->end(); ++iter) {
-            sql += ' ';
-            sql += iter->first;
-            sql += ' ';
-            sql += iter->second;
-        }
-    }
-    sql += ';';
+    sql_select(sql, table_name, keys, where, options);
     return db_exec_with_result(hdb, sql.c_str(), table);
 }
 
-// insert into $table_name ($keys) values ($values);
 int dbtable_insert(HDB hdb, const char* table_name, const char* keys, const char* values) {
     std::string sql;
-    sql = "insert into ";
-    sql += table_name;
-    if (keys) {
-        sql += " (";
-        sql += keys;
-        sql += ')';
-    }
-    if (values) {
-        sql += " values ";
-        sql += '(';
-        sql += values;
-        sql += ')';
-    }
-    sql += ';';
+    sql_insert(sql, table_name, keys, values);
     return db_exec(hdb, sql.c_str());
 }
 
-// update $table_name set $set where $where;
 int dbtable_update(HDB hdb, const char* table_name, const char* set, const char* where) {
     std::string sql;
-    sql = "update ";
-    sql += table_name;
-    if (set) {
-        sql += " set ";
-        sql += set;
-    }
-    if (where) {
-        sql += " where ";
-        sql += where;
-    }
-    sql += ';';
+    sql_update(sql, table_name, set, where);
     return db_exec(hdb, sql.c_str());
 }
 
-// delete from $table_name where $where;
 int dbtable_delete(HDB hdb, const char* table_name, const char* where) {
     std::string sql;
-    sql = "delete from ";
-    sql += table_name;
-    if (where) {
-        sql += " where ";
-        sql += where;
-    }
-    sql += ';';
+    sql_delete(sql, table_name, where);
     return db_exec(hdb, sql.c_str());
 }
 ////////////////////////////////////////////////////////////////////////////////

+ 2 - 10
sqlite/db.h

@@ -2,17 +2,16 @@
 #define SQLITE_DB_H_
 
 #include <vector>
-#include <string>
-#include <map>
 
 #include "sqlite3.h"
 
+#include "sql.h"
+
 #define SQL_OK  SQLITE_OK
 #define SQL_ERR SQLITE_ERROR
 
 typedef sqlite3* HDB;
 typedef int (*db_callback)(void* userdata, int rows, char** values, char** keys);
-typedef std::map<std::string, std::string> KeyVal;
 typedef KeyVal DBRecord;
 typedef KeyVal DBOption;
 typedef std::vector<std::string> DBRow;
@@ -26,20 +25,13 @@ int db_exec(HDB hdb, const char* sql);
 int db_exec_with_result(HDB hdb, const char* sql, DBTable* table);
 int db_exec_cb(HDB hdb, const char* sql, db_callback cb, void* userdata);
 
-// select count(*) from sqlite_master where type='table' and name='$table_name';
 int dbtable_exist(HDB hdb, const char* table_name);
-// select count(*) from $table_name where $where;
 int dbtable_count(HDB hdb, const char* table_name, const char* where);
-// select keys from $table_name where $where limit $limit order by $column ASC|DESC;
 int dbtable_select(HDB hdb, const char* table_name, const char* keys, const char* where, DBTable* table, const KeyVal* options=NULL);
-// insert into $table_name ($keys) values ($values);
 int dbtable_insert(HDB hdb, const char* table_name, const char* keys, const char* values);
-// update $table_name set $set where $where;
 int dbtable_update(HDB hdb, const char* table_name, const char* set, const char* where);
-// delete from $table_name where $where;
 int dbtable_delete(HDB hdb, const char* table_name, const char* where);
 
 int dbtable_get_index(const char* key, const DBTable& table);
 
 #endif  // SQLITE_DB_H_
-

+ 78 - 0
sqlite/sql.cpp

@@ -0,0 +1,78 @@
+#include "sql.h"
+
+// select count(*) from $table_name where $where;
+void sql_count(std::string& sql, const char* table_name, const char* where) {
+    sql = "select count(*) from ";
+    sql += table_name;
+    if (where) {
+        sql += " where ";
+        sql += where;
+    }
+    sql += ';';
+}
+// select $keys from $table_name where $where limit $limit order by $column ASC|DESC;
+void sql_select(std::string& sql, const char* table_name, const char* keys, const char* where, const KeyVal* options) {
+    sql = "select ";
+    if (keys) {
+        sql += keys;
+    }
+    else {
+        sql += '*';
+    }
+    sql += " from ";
+    sql += table_name;
+    if (where) {
+        sql += " where ";
+        sql += where;
+    }
+    if (options) {
+        for (KeyVal::const_iterator iter = options->begin(); iter != options->end(); ++iter) {
+            sql += ' ';
+            sql += iter->first;
+            sql += ' ';
+            sql += iter->second;
+        }
+    }
+    sql += ';';
+}
+// insert into $table_name ($keys) values ($values);
+void sql_insert(std::string& sql, const char* table_name, const char* keys, const char* values) {
+    sql = "insert into ";
+    sql += table_name;
+    if (keys) {
+        sql += " (";
+        sql += keys;
+        sql += ')';
+    }
+    if (values) {
+        sql += " values ";
+        sql += '(';
+        sql += values;
+        sql += ')';
+    }
+    sql += ';';
+}
+// update $table_name set $set where $where;
+void sql_update(std::string& sql, const char* table_name, const char* set, const char* where) {
+    sql = "update ";
+    sql += table_name;
+    if (set) {
+        sql += " set ";
+        sql += set;
+    }
+    if (where) {
+        sql += " where ";
+        sql += where;
+    }
+    sql += ';';
+}
+// delete from $table_name where $where;
+void sql_delete(std::string& sql, const char* table_name, const char* where) {
+    sql = "delete from ";
+    sql += table_name;
+    if (where) {
+        sql += " where ";
+        sql += where;
+    }
+    sql += ';';
+}

+ 19 - 0
sqlite/sql.h

@@ -0,0 +1,19 @@
+#ifndef HW_SQL_H_
+#define HW_SQL_H_
+
+#include <string>
+#include <map>
+typedef std::map<std::string, std::string> KeyVal;
+
+// select count(*) from $table_name where $where;
+void sql_count(std::string& sql, const char* table_name, const char* where = NULL);
+// select $keys from $table_name where $where limit $limit order by $column ASC|DESC;
+void sql_select(std::string& sql, const char* table_name, const char* keys = "*", const char* where = NULL, const KeyVal* options = NULL);
+// insert into $table_name ($keys) values ($values);
+void sql_insert(std::string& sql, const char* table_name, const char* keys, const char* values);
+// update $table_name set $set where $where;
+void sql_update(std::string& sql, const char* table_name, const char* set, const char* where = NULL);
+// delete from $table_name where $where;
+void sql_delete(std::string& sql, const char* table_name, const char* where = NULL);
+
+#endif // HW_SQL_H_