Changeset View
Changeset View
Standalone View
Standalone View
src/parser/KDbParser_p.cpp
Show First 20 Lines • Show All 74 Lines • ▼ Show 20 Line(s) | |||||
75 | void KDbParserPrivate::setTableSchema(KDbTableSchema *table) | 75 | void KDbParserPrivate::setTableSchema(KDbTableSchema *table) | ||
76 | { | 76 | { | ||
77 | delete this->table; | 77 | delete this->table; | ||
78 | this->table = table; | 78 | this->table = table; | ||
79 | } | 79 | } | ||
80 | 80 | | |||
81 | void KDbParserPrivate::setQuerySchema(KDbQuerySchema *query) | 81 | void KDbParserPrivate::setQuerySchema(KDbQuerySchema *query) | ||
82 | { | 82 | { | ||
83 | if (this->query != query) { | ||||
83 | delete this->query; | 84 | delete this->query; | ||
85 | } | ||||
84 | this->query = query; | 86 | this->query = query; | ||
85 | } | 87 | } | ||
86 | 88 | | |||
87 | KDbQuerySchema* KDbParserPrivate::createQuery() | 89 | KDbQuerySchema* KDbParserPrivate::createQuery() | ||
88 | { | 90 | { | ||
89 | return query ? query : KDbQuerySchema::Private::createQuery(connection); | 91 | return query ? query : new KDbQuerySchema; | ||
90 | } | 92 | } | ||
91 | 93 | | |||
92 | //------------------------------------- | 94 | //------------------------------------- | ||
93 | 95 | | |||
94 | KDbParseInfo::KDbParseInfo(KDbQuerySchema *query) | 96 | KDbParseInfo::KDbParseInfo(KDbQuerySchema *query) | ||
95 | : d(new Private) | 97 | : d(new Private) | ||
96 | { | 98 | { | ||
97 | d->querySchema = query; | 99 | d->querySchema = query; | ||
▲ Show 20 Lines • Show All 140 Lines • ▼ Show 20 Line(s) | 239 | { | |||
238 | setError(KDbParser::tr("Other error"), errDesc); | 240 | setError(KDbParser::tr("Other error"), errDesc); | ||
239 | } | 241 | } | ||
240 | 242 | | |||
241 | /* this is better than assert() */ | 243 | /* this is better than assert() */ | ||
242 | #define IMPL_ERROR(errmsg) setError(KDbParser::tr("Implementation error"), QLatin1String(errmsg)) | 244 | #define IMPL_ERROR(errmsg) setError(KDbParser::tr("Implementation error"), QLatin1String(errmsg)) | ||
243 | 245 | | |||
244 | //! @internal Parses @a data for parser @a p | 246 | //! @internal Parses @a data for parser @a p | ||
245 | //! @todo Make it REENTRANT | 247 | //! @todo Make it REENTRANT | ||
246 | bool parseData(KDbParser *p, const KDbEscapedString &sql) | 248 | bool parseData() | ||
247 | { | 249 | { | ||
248 | globalParser = p; | | |||
249 | globalParser->reset(); | | |||
250 | globalField = nullptr; | | |||
251 | fieldList.clear(); | 250 | fieldList.clear(); | ||
252 | 251 | | |||
252 | const KDbEscapedString sql(globalParser->statement()); | ||||
253 | if (sql.isEmpty()) { | 253 | if (sql.isEmpty()) { | ||
254 | KDbParserError err(KDbParser::tr("Error"), | 254 | KDbParserError err(KDbParser::tr("Error"), | ||
255 | KDbParser::tr("No query statement specified."), | 255 | KDbParser::tr("No query statement specified."), | ||
256 | globalToken, globalCurrentPos); | 256 | globalToken, globalCurrentPos); | ||
257 | KDbParserPrivate::get(globalParser)->setError(err); | 257 | KDbParserPrivate::get(globalParser)->setError(err); | ||
258 | yyerror(""); | 258 | yyerror(""); | ||
259 | globalParser = nullptr; | | |||
260 | return false; | 259 | return false; | ||
261 | } | 260 | } | ||
262 | 261 | | |||
263 | const char *data = sql.constData(); | 262 | const char *data = sql.constData(); | ||
264 | tokenize(data); | 263 | tokenize(data); | ||
265 | if (!globalParser->error().type().isEmpty()) { | 264 | if (!globalParser->error().type().isEmpty()) { | ||
266 | globalParser = nullptr; | | |||
267 | return false; | 265 | return false; | ||
268 | } | 266 | } | ||
269 | 267 | | |||
270 | bool ok = yyparse() == 0; | 268 | bool ok = yyparse() == 0; | ||
271 | if (ok && globalCurrentPos < sql.length()) { | 269 | if (ok && globalCurrentPos < sql.length()) { | ||
272 | kdbDebug() << "Parse error: tokens left" | 270 | kdbDebug() << "Parse error: tokens left" | ||
273 | << "globalCurrentPos:" << globalCurrentPos << "sql.length():" << sql.length() | 271 | << "globalCurrentPos:" << globalCurrentPos << "sql.length():" << sql.length() | ||
274 | << "globalToken:" << QString::fromUtf8(globalToken); | 272 | << "globalToken:" << QString::fromUtf8(globalToken); | ||
Show All 11 Lines | 283 | /* KDbTableSchema *ts; | |||
286 | for(QDictIterator<KDbTableSchema> it(tableDict); KDbTableSchema *s = tableList.first(); s; s = tableList.next()) | 284 | for(QDictIterator<KDbTableSchema> it(tableDict); KDbTableSchema *s = tableList.first(); s; s = tableList.next()) | ||
287 | { | 285 | { | ||
288 | kdbDebug() << " " << s->name(); | 286 | kdbDebug() << " " << s->name(); | ||
289 | }*/ | 287 | }*/ | ||
290 | } else { | 288 | } else { | ||
291 | ok = false; | 289 | ok = false; | ||
292 | } | 290 | } | ||
293 | yylex_destroy(); | 291 | yylex_destroy(); | ||
294 | globalParser = nullptr; | | |||
295 | return ok; | 292 | return ok; | ||
296 | } | 293 | } | ||
297 | 294 | | |||
298 | 295 | | |||
299 | /*! Adds @a columnExpr to @a parseInfo | 296 | /*! Adds @a columnExpr to @a parseInfo | ||
300 | The column can be in a form table.field, tableAlias.field or field. | 297 | The column can be in a form table.field, tableAlias.field or field. | ||
301 | @return true on success. On error message in globalParser object is updated. | 298 | @return true on success. On error message in globalParser object is updated. | ||
302 | */ | 299 | */ | ||
▲ Show 20 Lines • Show All 180 Lines • ▼ Show 20 Line(s) | 471 | // << columnNum; | |||
483 | //----- SELECT options | 480 | //----- SELECT options | ||
484 | if (options) { | 481 | if (options) { | ||
485 | //----- WHERE expr. | 482 | //----- WHERE expr. | ||
486 | if (!options->whereExpr.isNull()) { | 483 | if (!options->whereExpr.isNull()) { | ||
487 | if (!options->whereExpr.validate(&parseInfo)) { | 484 | if (!options->whereExpr.validate(&parseInfo)) { | ||
488 | setError(parseInfo.errorMessage(), parseInfo.errorDescription()); | 485 | setError(parseInfo.errorMessage(), parseInfo.errorDescription()); | ||
489 | return nullptr; | 486 | return nullptr; | ||
490 | } | 487 | } | ||
491 | KDbQuerySchema::Private::setWhereExpressionInternal(querySchema, options->whereExpr); | 488 | KDbQuerySchemaPrivate::setWhereExpressionInternal(querySchema, options->whereExpr); | ||
492 | } | 489 | } | ||
493 | //----- ORDER BY | 490 | //----- ORDER BY | ||
494 | if (options->orderByColumns) { | 491 | if (options->orderByColumns) { | ||
495 | KDbOrderByColumnList *orderByColumnList = querySchema->orderByColumnList(); | 492 | KDbOrderByColumnList *orderByColumnList = querySchema->orderByColumnList(); | ||
496 | int count = options->orderByColumns->count(); | 493 | int count = options->orderByColumns->count(); | ||
497 | QList<OrderByColumnInternal>::ConstIterator it(options->orderByColumns->constEnd()); | 494 | QList<OrderByColumnInternal>::ConstIterator it(options->orderByColumns->constEnd()); | ||
498 | --it; | 495 | --it; | ||
499 | for (;count > 0; --it, --count) | 496 | for (;count > 0; --it, --count) | ||
500 | /*opposite direction due to parser specifics*/ | 497 | /*opposite direction due to parser specifics*/ | ||
501 | { | 498 | { | ||
502 | //first, try to find a column name or alias (outside of asterisks) | 499 | // first, try to find a column name or alias (outside of asterisks) | ||
503 | KDbQueryColumnInfo *columnInfo = querySchema->columnInfo((*it).aliasOrName, false/*outside of asterisks*/); | 500 | KDbQueryColumnInfo *columnInfo = querySchema->columnInfo( | ||
501 | globalParser->connection(), (*it).aliasOrName, | ||||
502 | KDbQuerySchema::ExpandMode::Unexpanded /*outside of asterisks*/); | ||||
504 | if (columnInfo) { | 503 | if (columnInfo) { | ||
505 | orderByColumnList->appendColumn(columnInfo, (*it).order); | 504 | orderByColumnList->appendColumn(columnInfo, (*it).order); | ||
506 | } else { | 505 | } else { | ||
507 | //failed, try to find a field name within all the tables | 506 | //failed, try to find a field name within all the tables | ||
508 | if ((*it).columnNumber != -1) { | 507 | if ((*it).columnNumber != -1) { | ||
509 | if (!orderByColumnList->appendColumn(querySchema, | 508 | if (!orderByColumnList->appendColumn(globalParser->connection(), | ||
510 | (*it).order, (*it).columnNumber - 1)) { | 509 | querySchema, (*it).order, | ||
510 | (*it).columnNumber - 1)) | ||||
511 | { | ||||
511 | setError(KDbParser::tr("Could not define sorting. Column at " | 512 | setError(KDbParser::tr("Could not define sorting. Column at " | ||
512 | "position %1 does not exist.") | 513 | "position %1 does not exist.") | ||
513 | .arg((*it).columnNumber)); | 514 | .arg((*it).columnNumber)); | ||
514 | return nullptr; | 515 | return nullptr; | ||
515 | } | 516 | } | ||
516 | } else { | 517 | } else { | ||
517 | KDbField * f = querySchema->findTableField((*it).aliasOrName); | 518 | KDbField * f = querySchema->findTableField((*it).aliasOrName); | ||
518 | if (!f) { | 519 | if (!f) { | ||
Show All 15 Lines |