-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathSqlImporter.cpp
More file actions
172 lines (130 loc) · 3.46 KB
/
SqlImporter.cpp
File metadata and controls
172 lines (130 loc) · 3.46 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* Copyright (C) 2012-2013 Allan SIMON (Sysko) <allan.simon@supinfo.com>
* See accompanying file COPYING.TXT file for licensing details.
*
* @category Cppcms-skeleton
* @author Allan SIMON <allan.simon@supinfo.com>
* @package Models
*
*/
#include <fstream>
#include <iostream>
#include <string>
#include <booster/log.h>
#include "SqlImporter.h"
namespace cppcmsskel {
namespace models {
/**
*
*/
SqlImporter::SqlImporter(cppdb::session sqliteDbParam):
currentPosition(0),
sqliteDb(sqliteDbParam)
{
}
/**
*
*/
bool SqlImporter::from_file(
const std::string &sqlFilePath
) {
std::ifstream fileStream(sqlFilePath.c_str());
if (!fileStream.good()) {
BOOSTER_ERROR("cppcms") << "file is not readable" << std::endl;
std::cerr << "file is not readable" << std::endl;
return false;
}
std::string fileStr(
(std::istreambuf_iterator<char>(fileStream)),
std::istreambuf_iterator<char>()
);
return from_string(fileStr);
}
/**
*
*/
bool SqlImporter::from_string(
const std::string &sqlStringParam
) {
sqlString = sqlStringParam;
try {
// TODO we will ignore what's after the last ; , or everything
// if there's no ; that's a workaround because for the moment
// create_statement ... << exec will launch a "no error" exception
// if you give it an empty string
//
while (next_sql_block() != std::string::npos) {
// we split the string
sqliteDb.create_statement(currentFragment) << cppdb::exec;
}
} catch(std::exception const &e) {
std::cerr << e.what() << std::endl;
BOOSTER_ERROR("cppcms") << e.what();
return false;
}
return true;
}
/**
*
*/
size_t SqlImporter::next_sql_block() {
// we first get the position of the next ";"
size_t next = sqlString.find( ";", currentPosition );
// we split the string
currentFragment = sqlString.substr(
currentPosition,
next - currentPosition
);
if (!contains_trigger_begin(currentFragment)) {
currentPosition = next + 1;
return next;
}
next = find_trigger_end();
if (next != std::string::npos) {
currentFragment = sqlString.substr(
currentPosition,
next - currentPosition
);
currentPosition = next + 1;
}
return next;
}
/**
*
*/
bool SqlImporter::contains_trigger_begin(
const std::string &sqlFragment
) {
// we don't handle the case in which somebody has written
// cReATe
// if necessary it can be easily implemented by doing a
// "to_lower" on sqlFragment
if (sqlFragment.find("create") == std::string::npos) {
if (sqlFragment.find("CREATE") == std::string::npos) {
return false;
}
}
if (sqlFragment.find("trigger") == std::string::npos) {
if (sqlFragment.find("TRIGGER") == std::string::npos) {
return false;
}
}
return true;
}
/**
*
*/
size_t SqlImporter::find_trigger_end() {
// we first look for end/END
size_t next = sqlString.find( "end", currentPosition );
if (next == std::string::npos) {
next = sqlString.find( "END", currentPosition );
if (next == std::string::npos) {
return next;
}
}
// if we find it we look for the next ';'
return sqlString.find( ";", next);
}
} // end namespace models
} // end namespace cppcmsskel