-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqliteutil.cpp
More file actions
116 lines (99 loc) · 3.07 KB
/
Copy pathsqliteutil.cpp
File metadata and controls
116 lines (99 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "sqliteutil.h"
#include "utils.h"
#include "sqlite/sqlite3.h"
#include "sqlitevariant.h"
#include <cstring>
void SQLEscapeString(std::string& str)
{
std::string::size_type pos = 0;
for(; pos < str.length() &&
(pos = str.find("'", pos)) != std::string::npos;)
{
str.replace(pos, 1, "''");
pos += 2;
}
}
void SQLStripString(std::string& str)
{
std::string::size_type pos = 0;
for(; pos < str.length() &&
(pos = str.find("'", pos)) != std::string::npos;)
{
str.replace(pos, 1, "");
++pos;
}
}
int DoesSQLiteTableExist(sqlite3* pDB, const char* tablename)
{
sqlite3_stmt* query = 0;
static const char* checktablequery = "SELECT name FROM sqlite_master WHERE type='table' AND name=$tablename;";
dbgprintf("SQLiteTable check query: %s\n", checktablequery);
if(SQLITE_OK != sqlite3_prepare_v2(pDB, checktablequery, -1, &query, 0))
{
dbgprintf("sqlite prepare failed: %s\n",
sqlite3_errmsg(pDB));
return -1;
}
if(SQLITE_OK != sqlite3_bind_text(query, 1, tablename, strlen(tablename), 0))
{
dbgprintf("sqlite bind failed\n");
sqlite3_finalize(query);
return -1;
}
int result = sqlite3_step(query) == SQLITE_ROW;
sqlite3_finalize(query);
return result;
}
int GetTableColumns(sqlite3* pDB, const char* tablename, std::set<std::string>& columnset)
{
//Do not call this before verifying the table exists. (pragmas cannot use bound parameters).
std::string tablenamestr(tablename);
SQLStripString(tablenamestr);
std::string columnquerystr = "pragma table_info(" + tablenamestr + ");";
sqlite3_stmt* columnquery = 0;
if(SQLITE_OK != sqlite3_prepare_v2(pDB, columnquerystr.c_str(), -1, &columnquery, 0))
{
dbgprintf("Failed to prepare sql statement\n");
return -1;
}
while(sqlite3_step(columnquery) != SQLITE_DONE)
{
std::string row((char*) sqlite3_column_text(columnquery, 1));
columnset.insert(row);
}
sqlite3_finalize(columnquery);
return SQLITE_OK;
}
int ExecSQLiteStatement(sqlite3* pDB, const char* createtablequery)
{
sqlite3_stmt* query = 0;
int result = 0;
result = sqlite3_prepare_v2(pDB, createtablequery, -1, &query, 0);
if(SQLITE_OK != result)
{
dbgprintf("Failed preparing create table sql statement.\n");
return result;
}
result = sqlite3_step(query);
sqlite3_finalize(query);
return result;
}
int AddColumnToSQLiteTable(sqlite3* pDB, const char* tablename, const char* colname, const char* coltype)
{
dbgprintf("Trying to add column %s with type %s to table %s\n", colname, coltype, tablename);
sqlite3_stmt* query = 0;
int result = 0;
//SQLite does not support binding parameters to alter table, either.
//The values here are not user supplied, but are taken from the game scripts
std::string atqstr = "alter table " + std::string(tablename) + " add column " + std::string(colname) + " " + std::string(coltype);
SQLStripString(atqstr); //Strip all apostrophes from this string
result = sqlite3_prepare_v2(pDB, atqstr.c_str(), -1, &query, 0);
if(SQLITE_OK != result)
{
dbgprintf("Add Column Prepare failed: %d\n", result);
return result;
}
result = sqlite3_step(query);
sqlite3_finalize(query);
return result;
}