Changeset View
Changeset View
Standalone View
Standalone View
src/KDbNativeStatementBuilder.cpp
Show First 20 Lines • Show All 158 Lines • ▼ Show 20 Line(s) | 158 | KDbLookupFieldSchema *lookupFieldSchema = (options.addVisibleLookupColumns() && f->table()) | |||
---|---|---|---|---|---|
159 | ? f->table()->lookupFieldSchema(*f) : nullptr; | 159 | ? f->table()->lookupFieldSchema(*f) : nullptr; | ||
160 | if (lookupFieldSchema && lookupFieldSchema->boundColumn() >= 0) { | 160 | if (lookupFieldSchema && lookupFieldSchema->boundColumn() >= 0) { | ||
161 | // Lookup field schema found | 161 | // Lookup field schema found | ||
162 | // Now we also need to fetch "visible" value from the lookup table, not only the value of binding. | 162 | // Now we also need to fetch "visible" value from the lookup table, not only the value of binding. | ||
163 | // -> build LEFT OUTER JOIN clause for this purpose (LEFT, not INNER because the binding can be broken) | 163 | // -> build LEFT OUTER JOIN clause for this purpose (LEFT, not INNER because the binding can be broken) | ||
164 | // "LEFT OUTER JOIN lookupTable ON thisTable.thisField=lookupTable.boundField" | 164 | // "LEFT OUTER JOIN lookupTable ON thisTable.thisField=lookupTable.boundField" | ||
165 | KDbLookupFieldSchemaRecordSource recordSource = lookupFieldSchema->recordSource(); | 165 | KDbLookupFieldSchemaRecordSource recordSource = lookupFieldSchema->recordSource(); | ||
166 | if (recordSource.type() == KDbLookupFieldSchemaRecordSource::Table) { | 166 | if (recordSource.type() == KDbLookupFieldSchemaRecordSource::Table) { | ||
167 | KDbTableSchema *lookupTable = querySchema->connection()->tableSchema(recordSource.name()); | 167 | KDbTableSchema *lookupTable = connection->tableSchema(recordSource.name()); | ||
168 | KDbFieldList* visibleColumns = nullptr; | 168 | KDbFieldList* visibleColumns = nullptr; | ||
169 | KDbField *boundField = nullptr; | 169 | KDbField *boundField = nullptr; | ||
170 | if (lookupTable | 170 | if (lookupTable | ||
171 | && lookupFieldSchema->boundColumn() < lookupTable->fieldCount() | 171 | && lookupFieldSchema->boundColumn() < lookupTable->fieldCount() | ||
172 | && (visibleColumns = lookupTable->subList(lookupFieldSchema->visibleColumns())) | 172 | && (visibleColumns = lookupTable->subList(lookupFieldSchema->visibleColumns())) | ||
173 | && (boundField = lookupTable->field(lookupFieldSchema->boundColumn()))) { | 173 | && (boundField = lookupTable->field(lookupFieldSchema->boundColumn()))) { | ||
174 | //add LEFT OUTER JOIN | 174 | //add LEFT OUTER JOIN | ||
175 | if (!s_additional_joins.isEmpty()) | 175 | if (!s_additional_joins.isEmpty()) | ||
Show All 15 Lines | |||||
191 | //! @todo Add lookup schema option for separator other than ' ' or even option for placeholders like "Name ? ?" | 191 | //! @todo Add lookup schema option for separator other than ' ' or even option for placeholders like "Name ? ?" | ||
192 | //! @todo Add possibility for joining the values at client side. | 192 | //! @todo Add possibility for joining the values at client side. | ||
193 | s_additional_fields += visibleColumns->sqlFieldsList( | 193 | s_additional_fields += visibleColumns->sqlFieldsList( | ||
194 | connection, QLatin1String(" || ' ' || "), internalUniqueTableAlias, | 194 | connection, QLatin1String(" || ' ' || "), internalUniqueTableAlias, | ||
195 | driver ? KDb::DriverEscaping : KDb::KDbEscaping); | 195 | driver ? KDb::DriverEscaping : KDb::KDbEscaping); | ||
196 | } | 196 | } | ||
197 | delete visibleColumns; | 197 | delete visibleColumns; | ||
198 | } else if (recordSource.type() == KDbLookupFieldSchemaRecordSource::Query) { | 198 | } else if (recordSource.type() == KDbLookupFieldSchemaRecordSource::Query) { | ||
199 | KDbQuerySchema *lookupQuery = querySchema->connection()->querySchema(recordSource.name()); | 199 | KDbQuerySchema *lookupQuery = connection->querySchema(recordSource.name()); | ||
200 | if (!lookupQuery) { | 200 | if (!lookupQuery) { | ||
201 | kdbWarning() << "!lookupQuery"; | 201 | kdbWarning() << "!lookupQuery"; | ||
202 | return false; | 202 | return false; | ||
203 | } | 203 | } | ||
204 | const KDbQueryColumnInfo::Vector fieldsExpanded(lookupQuery->fieldsExpanded()); | 204 | const KDbQueryColumnInfo::Vector fieldsExpanded( | ||
205 | lookupQuery->fieldsExpanded(connection)); | ||||
205 | if (lookupFieldSchema->boundColumn() >= fieldsExpanded.count()) { | 206 | if (lookupFieldSchema->boundColumn() >= fieldsExpanded.count()) { | ||
206 | kdbWarning() << "lookupFieldSchema->boundColumn() >= fieldsExpanded.count()"; | 207 | kdbWarning() << "lookupFieldSchema->boundColumn() >= fieldsExpanded.count()"; | ||
207 | return false; | 208 | return false; | ||
208 | } | 209 | } | ||
209 | KDbQueryColumnInfo *boundColumnInfo = fieldsExpanded.at(lookupFieldSchema->boundColumn()); | 210 | KDbQueryColumnInfo *boundColumnInfo = fieldsExpanded.at(lookupFieldSchema->boundColumn()); | ||
210 | if (!boundColumnInfo) { | 211 | if (!boundColumnInfo) { | ||
211 | kdbWarning() << "!boundColumnInfo"; | 212 | kdbWarning() << "!boundColumnInfo"; | ||
212 | return false; | 213 | return false; | ||
▲ Show 20 Lines • Show All 147 Lines • ▼ Show 20 Line(s) | |||||
360 | //! @todo (js) add other sql parts | 361 | //! @todo (js) add other sql parts | ||
361 | //(use wasWhere here) | 362 | //(use wasWhere here) | ||
362 | 363 | | |||
363 | // ORDER BY | 364 | // ORDER BY | ||
364 | KDbEscapedString orderByString( | 365 | KDbEscapedString orderByString( | ||
365 | querySchema->orderByColumnList()->toSqlString( | 366 | querySchema->orderByColumnList()->toSqlString( | ||
366 | !singleTable/*includeTableName*/, connection, driver ? KDb::DriverEscaping : KDb::KDbEscaping) | 367 | !singleTable/*includeTableName*/, connection, driver ? KDb::DriverEscaping : KDb::KDbEscaping) | ||
367 | ); | 368 | ); | ||
368 | const QVector<int> pkeyFieldsOrder(querySchema->pkeyFieldsOrder()); | 369 | const QVector<int> pkeyFieldsOrder(querySchema->pkeyFieldsOrder(connection)); | ||
369 | if (orderByString.isEmpty() && !pkeyFieldsOrder.isEmpty()) { | 370 | if (orderByString.isEmpty() && !pkeyFieldsOrder.isEmpty()) { | ||
370 | //add automatic ORDER BY if there is no explicitly defined (especially helps when there are complex JOINs) | 371 | //add automatic ORDER BY if there is no explicitly defined (especially helps when there are complex JOINs) | ||
371 | KDbOrderByColumnList automaticPKOrderBy; | 372 | KDbOrderByColumnList automaticPKOrderBy; | ||
372 | const KDbQueryColumnInfo::Vector fieldsExpanded(querySchema->fieldsExpanded()); | 373 | const KDbQueryColumnInfo::Vector fieldsExpanded(querySchema->fieldsExpanded(connection)); | ||
373 | foreach(int pkeyFieldsIndex, pkeyFieldsOrder) { | 374 | foreach(int pkeyFieldsIndex, pkeyFieldsOrder) { | ||
374 | if (pkeyFieldsIndex < 0) // no field mentioned in this query | 375 | if (pkeyFieldsIndex < 0) // no field mentioned in this query | ||
375 | continue; | 376 | continue; | ||
376 | if (pkeyFieldsIndex >= fieldsExpanded.count()) { | 377 | if (pkeyFieldsIndex >= fieldsExpanded.count()) { | ||
377 | kdbWarning() << "ORDER BY: (*it) >= fieldsExpanded.count() - " | 378 | kdbWarning() << "ORDER BY: (*it) >= fieldsExpanded.count() - " | ||
378 | << pkeyFieldsIndex << " >= " << fieldsExpanded.count(); | 379 | << pkeyFieldsIndex << " >= " << fieldsExpanded.count(); | ||
379 | continue; | 380 | continue; | ||
380 | } | 381 | } | ||
▲ Show 20 Lines • Show All 127 Lines • Show Last 20 Lines |