-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLGen.cpp
More file actions
285 lines (234 loc) · 6.54 KB
/
SQLGen.cpp
File metadata and controls
285 lines (234 loc) · 6.54 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include "SQLGen.h"
#include <algorithm>
#include <cctype>
#include <sstream>
using namespace QLang;
using namespace std;
string SQLGen::tableNameToSQL( const string &structName )
{
// Convert PascalCase to lowercase: User -> user, UserPost -> user_post
string result;
for ( size_t i = 0; i < structName.size(); i++ )
{
char c = structName[i];
if ( isupper( c ) )
{
if ( i > 0 ) result += '_';
result += (char)tolower( c );
}
else
{
result += c;
}
}
return result;
}
string SQLGen::blangTypeToSQL( const string &blangType )
{
if ( blangType == "int" || blangType == "long" || blangType == "short" )
return "INTEGER";
if ( blangType == "float" || blangType == "double" )
return "REAL";
if ( blangType == "string" )
return "TEXT";
if ( blangType == "bool" )
return "INTEGER"; // SQLite stores bools as integers
if ( blangType == "char" )
return "TEXT";
return "TEXT"; // default
}
string SQLGen::exprToSQL( const Expression *expr, vector<string> ¶ms )
{
// Handle query field references: .field -> table.field
const QueryFieldExpression *field = dynamic_cast<const QueryFieldExpression*>( expr );
if ( field )
return field->getFieldName();
// Handle binary operations: a == b -> a = b, a != b -> a <> b
const OperationsExpression *ops = dynamic_cast<const OperationsExpression*>( expr );
if ( ops )
{
string left = exprToSQL( ops->mOp1, params );
string right = exprToSQL( ops->mOp2, params );
string op = ops->mOperation;
// Map BLang operators to SQL operators
if ( op == "==" ) op = "=";
else if ( op == "!=" ) op = "<>";
else if ( op == "&&" ) op = "AND";
else if ( op == "||" ) op = "OR";
return left + " " + op + " " + right;
}
// Handle constant integers
const ConstInteger *ci = dynamic_cast<const ConstInteger*>( expr );
if ( ci )
{
return to_string( ci->mValue );
}
// Handle constant strings
const ConstString *cs = dynamic_cast<const ConstString*>( expr );
if ( cs )
{
params.push_back( cs->mValue );
return "?";
}
// Handle constant floats
const ConstFloat *cf = dynamic_cast<const ConstFloat*>( expr );
if ( cf )
{
return to_string( cf->mValue );
}
// Handle variable references (bind as parameters)
const VariableExpression *ve = dynamic_cast<const VariableExpression*>( expr );
if ( ve )
{
// Variable references in SQL context become parameter placeholders
return "?";
}
return "?";
}
SQLStatement SQLGen::generateSelect( QueryExpression *query, Module *mod )
{
SQLStatement result;
string tableName = tableNameToSQL( query->mTableName );
stringstream ss;
ss << "SELECT * FROM " << tableName;
string joinClause;
string whereClause;
string orderByClause;
string limitClause;
for ( auto &step : query->mSteps )
{
switch ( step.mType )
{
case QueryPipelineStep::WHERE:
if ( !whereClause.empty() ) whereClause += " AND ";
whereClause += exprToSQL( step.mExpression, result.params );
break;
case QueryPipelineStep::ORDER_BY:
orderByClause = exprToSQL( step.mExpression, result.params );
break;
case QueryPipelineStep::LIMIT:
{
ConstInteger *ci = dynamic_cast<ConstInteger*>( (Expression*)step.mExpression );
if ( ci )
limitClause = to_string( ci->mValue );
else
limitClause = "?";
break;
}
case QueryPipelineStep::JOIN:
{
string joinTable = tableNameToSQL( step.mJoinTable );
string joinCond = exprToSQL( step.mExpression, result.params );
joinClause += " JOIN " + joinTable + " ON " + joinCond;
break;
}
case QueryPipelineStep::FIRST:
limitClause = "1";
break;
default:
break;
}
}
if ( !joinClause.empty() ) ss << joinClause;
if ( !whereClause.empty() ) ss << " WHERE " << whereClause;
if ( !orderByClause.empty() ) ss << " ORDER BY " << orderByClause;
if ( !limitClause.empty() ) ss << " LIMIT " << limitClause;
result.sql = ss.str();
return result;
}
SQLStatement SQLGen::generateInsert( InsertExpression *insert, Module *mod )
{
SQLStatement result;
string tableName = tableNameToSQL( insert->mTableName );
stringstream ss;
ss << "INSERT INTO " << tableName << " (";
for ( size_t i = 0; i < insert->mFieldNames.size(); i++ )
{
if ( i > 0 ) ss << ", ";
ss << insert->mFieldNames[i];
}
ss << ") VALUES (";
for ( size_t i = 0; i < insert->mFieldValues.size(); i++ )
{
if ( i > 0 ) ss << ", ";
ss << "?";
}
ss << ")";
result.sql = ss.str();
return result;
}
SQLStatement SQLGen::generateUpdate( UpdateExpression *update, Module *mod )
{
SQLStatement result;
string tableName = tableNameToSQL( update->mTableName );
stringstream ss;
ss << "UPDATE " << tableName;
string setClauses;
string whereClause;
for ( auto &step : update->mSteps )
{
switch ( step.mType )
{
case QueryPipelineStep::WHERE:
if ( !whereClause.empty() ) whereClause += " AND ";
whereClause += exprToSQL( step.mExpression, result.params );
break;
case QueryPipelineStep::SET:
for ( auto &field : step.mSetFields )
{
if ( !setClauses.empty() ) setClauses += ", ";
setClauses += field.first + " = " +
exprToSQL( field.second, result.params );
}
break;
default:
break;
}
}
if ( !setClauses.empty() ) ss << " SET " << setClauses;
if ( !whereClause.empty() ) ss << " WHERE " << whereClause;
result.sql = ss.str();
return result;
}
SQLStatement SQLGen::generateDelete( DeleteExpression *del, Module *mod )
{
SQLStatement result;
string tableName = tableNameToSQL( del->mTableName );
stringstream ss;
ss << "DELETE FROM " << tableName;
string whereClause;
for ( auto &step : del->mSteps )
{
if ( step.mType == QueryPipelineStep::WHERE )
{
if ( !whereClause.empty() ) whereClause += " AND ";
whereClause += exprToSQL( step.mExpression, result.params );
}
}
if ( !whereClause.empty() ) ss << " WHERE " << whereClause;
result.sql = ss.str();
return result;
}
string SQLGen::generateCreateTable( StructDefinition *structDef )
{
string tableName = tableNameToSQL( structDef->getName() );
stringstream ss;
ss << "CREATE TABLE IF NOT EXISTS " << tableName << " (";
const auto &fields = structDef->getFields();
for ( size_t i = 0; i < fields.size(); i++ )
{
if ( i > 0 ) ss << ", ";
ss << fields[i]->getName() << " " << blangTypeToSQL( fields[i]->getVariableType()->getName() );
// Mark 'id' fields as PRIMARY KEY
if ( fields[i]->getName() == "id" )
ss << " PRIMARY KEY";
}
ss << ")";
return ss.str();
}
string SQLGen::generateAddColumn( const string &tableName, const string &columnName,
const string &columnType )
{
return "ALTER TABLE " + tableNameToSQL( tableName ) +
" ADD COLUMN " + columnName + " " + blangTypeToSQL( columnType );
}