Changeset View
Changeset View
Standalone View
Standalone View
src/KDbNativeStatementBuilder.cpp
Show First 20 Lines • Show All 383 Lines • ▼ Show 20 Line(s) | 350 | //! @todo: we're using WHERE for joins now; use INNER/LEFT/RIGHT JOIN later | |||
---|---|---|---|---|---|
384 | } | 384 | } | ||
385 | if (!s_where.isEmpty()) | 385 | if (!s_where.isEmpty()) | ||
386 | sql += " WHERE " + s_where; | 386 | sql += " WHERE " + s_where; | ||
387 | //! @todo (js) add other sql parts | 387 | //! @todo (js) add other sql parts | ||
388 | //(use wasWhere here) | 388 | //(use wasWhere here) | ||
389 | 389 | | |||
390 | // ORDER BY | 390 | // ORDER BY | ||
391 | KDbEscapedString orderByString(querySchema->orderByColumnList()->toSqlString( | 391 | KDbEscapedString orderByString(querySchema->orderByColumnList()->toSqlString( | ||
392 | !singleTable /*includeTableName*/, connection, dialect)); | 392 | !singleTable /*includeTableName*/, connection, querySchema, dialect)); | ||
393 | const QVector<int> pkeyFieldsOrder(querySchema->pkeyFieldsOrder(connection)); | 393 | const QVector<int> pkeyFieldsOrder(querySchema->pkeyFieldsOrder(connection)); | ||
394 | if (dialect == KDb::DriverEscaping && orderByString.isEmpty() && !pkeyFieldsOrder.isEmpty()) { | 394 | if (dialect == KDb::DriverEscaping && orderByString.isEmpty() && !pkeyFieldsOrder.isEmpty()) { | ||
395 | // Native only: add automatic ORDER BY if there is no explicitly defined one | 395 | // Native only: add automatic ORDER BY if there is no explicitly defined one | ||
396 | // (especially helps when there are complex JOINs) | 396 | // (especially helps when there are complex JOINs) | ||
397 | KDbOrderByColumnList automaticPKOrderBy; | 397 | KDbOrderByColumnList automaticPKOrderBy; | ||
398 | const KDbQueryColumnInfo::Vector fieldsExpanded(querySchema->fieldsExpanded(connection)); | 398 | const KDbQueryColumnInfo::Vector fieldsExpanded(querySchema->fieldsExpanded(connection)); | ||
399 | foreach(int pkeyFieldsIndex, pkeyFieldsOrder) { | 399 | foreach(int pkeyFieldsIndex, pkeyFieldsOrder) { | ||
400 | if (pkeyFieldsIndex < 0) // no field mentioned in this query | 400 | if (pkeyFieldsIndex < 0) // no field mentioned in this query | ||
401 | continue; | 401 | continue; | ||
402 | if (pkeyFieldsIndex >= fieldsExpanded.count()) { | 402 | if (pkeyFieldsIndex >= fieldsExpanded.count()) { | ||
403 | kdbWarning() << "ORDER BY: (*it) >= fieldsExpanded.count() - " | 403 | kdbWarning() << "ORDER BY: (*it) >= fieldsExpanded.count() - " | ||
404 | << pkeyFieldsIndex << " >= " << fieldsExpanded.count(); | 404 | << pkeyFieldsIndex << " >= " << fieldsExpanded.count(); | ||
405 | continue; | 405 | continue; | ||
406 | } | 406 | } | ||
407 | KDbQueryColumnInfo *ci = fieldsExpanded[ pkeyFieldsIndex ]; | 407 | KDbQueryColumnInfo *ci = fieldsExpanded[ pkeyFieldsIndex ]; | ||
408 | automaticPKOrderBy.appendColumn(ci); | 408 | automaticPKOrderBy.appendColumn(ci); | ||
409 | } | 409 | } | ||
410 | orderByString = automaticPKOrderBy.toSqlString(!singleTable /*includeTableName*/, | 410 | orderByString = automaticPKOrderBy.toSqlString(!singleTable /*includeTableName*/, | ||
411 | connection, dialect); | 411 | connection, querySchema, dialect); | ||
412 | } | 412 | } | ||
413 | if (!orderByString.isEmpty()) | 413 | if (!orderByString.isEmpty()) | ||
414 | sql += (" ORDER BY " + orderByString); | 414 | sql += (" ORDER BY " + orderByString); | ||
415 | 415 | | |||
416 | //kdbDebug() << sql; | 416 | //kdbDebug() << sql; | ||
417 | *target = sql; | 417 | *target = sql; | ||
418 | return true; | 418 | return true; | ||
419 | } | 419 | } | ||
▲ Show 20 Lines • Show All 114 Lines • Show Last 20 Lines |