Changeset View
Changeset View
Standalone View
Standalone View
src/KDbQuerySchema.h
Show All 23 Lines | |||||
24 | #include <QHash> | 24 | #include <QHash> | ||
25 | 25 | | |||
26 | #include "KDbFieldList.h" | 26 | #include "KDbFieldList.h" | ||
27 | #include "KDbObject.h" | 27 | #include "KDbObject.h" | ||
28 | #include "KDbQueryColumnInfo.h" | 28 | #include "KDbQueryColumnInfo.h" | ||
29 | #include "KDbToken.h" | 29 | #include "KDbToken.h" | ||
30 | 30 | | |||
31 | class KDbConnection; | 31 | class KDbConnection; | ||
32 | class KDbTableSchema; | | |||
33 | class KDbRelationship; | | |||
34 | class KDbQueryAsterisk; | | |||
35 | class KDbQuerySchemaParameter; | | |||
36 | class KDbOrderByColumn; | 32 | class KDbOrderByColumn; | ||
37 | class KDbOrderByColumnList; | 33 | class KDbOrderByColumnList; | ||
34 | class KDbQueryAsterisk; | ||||
35 | class KDbQuerySchemaFieldsExpanded; | ||||
36 | class KDbQuerySchemaParameter; | ||||
37 | class KDbQuerySchemaPrivate; | ||||
38 | class KDbRelationship; | ||||
39 | class KDbTableSchema; | ||||
38 | 40 | | |||
39 | //! @short KDbQuerySchema provides information about database query | 41 | //! @short KDbQuerySchema provides information about database query | ||
40 | /*! The query that can be executed using KDb-compatible SQL database engine | 42 | /*! The query that can be executed using KDb-compatible SQL database engine | ||
41 | or used as an introspection tool. KDb parser builds KDbQuerySchema objects | 43 | or used as an introspection tool. KDb parser builds KDbQuerySchema objects | ||
42 | by parsing SQL statements. */ | 44 | by parsing SQL statements. */ | ||
43 | class KDB_EXPORT KDbQuerySchema : public KDbFieldList, public KDbObject | 45 | class KDB_EXPORT KDbQuerySchema : public KDbFieldList, public KDbObject | ||
44 | { | 46 | { | ||
45 | public: | 47 | public: | ||
Show All 13 Lines | |||||
59 | We consider that query schema based on @a table is not (a least yet) stored | 61 | We consider that query schema based on @a table is not (a least yet) stored | ||
60 | in a system table, so query connection is set to @c nullptr | 62 | in a system table, so query connection is set to @c nullptr | ||
61 | (even if @a tableSchema's connection is not @c nullptr). | 63 | (even if @a tableSchema's connection is not @c nullptr). | ||
62 | Id of the created query is set to 0. */ | 64 | Id of the created query is set to 0. */ | ||
63 | explicit KDbQuerySchema(KDbTableSchema *tableSchema); | 65 | explicit KDbQuerySchema(KDbTableSchema *tableSchema); | ||
64 | 66 | | |||
65 | /*! Copy constructor. Creates deep copy of @a querySchema. | 67 | /*! Copy constructor. Creates deep copy of @a querySchema. | ||
66 | KDbQueryAsterisk objects are deeply copied while only pointers to KDbField objects are copied. */ | 68 | KDbQueryAsterisk objects are deeply copied while only pointers to KDbField objects are copied. */ | ||
67 | KDbQuerySchema(const KDbQuerySchema& querySchema); | 69 | KDbQuerySchema(const KDbQuerySchema& querySchema, KDbConnection *conn); | ||
68 | 70 | | |||
69 | ~KDbQuerySchema() override; | 71 | ~KDbQuerySchema() override; | ||
70 | 72 | | |||
71 | /*! Inserts @a field to the columns list at @a position. | 73 | /*! Inserts @a field to the columns list at @a position. | ||
72 | Inserted field will not be owned by this KDbQuerySchema object, | 74 | Inserted field will not be owned by this KDbQuerySchema object, | ||
73 | but by the corresponding KDbTableSchema. | 75 | but by the corresponding KDbTableSchema. | ||
74 | 76 | | |||
75 | KDbQueryAsterisk can be also passed as @a field. See the KDbQueryAsterisk class | 77 | KDbQueryAsterisk can be also passed as @a field. See the KDbQueryAsterisk class | ||
▲ Show 20 Lines • Show All 110 Lines • ▼ Show 20 Line(s) | |||||
186 | 188 | | |||
187 | /*! Removes all columns and their aliases from the columns list, | 189 | /*! Removes all columns and their aliases from the columns list, | ||
188 | removes all tables and their aliases from the tables list within this query. | 190 | removes all tables and their aliases from the tables list within this query. | ||
189 | Sets master table information to @c nullptr. | 191 | Sets master table information to @c nullptr. | ||
190 | Does not destroy any objects though. Clears name and all other properties. | 192 | Does not destroy any objects though. Clears name and all other properties. | ||
191 | @see KDbFieldList::clear() */ | 193 | @see KDbFieldList::clear() */ | ||
192 | void clear() override; | 194 | void clear() override; | ||
193 | 195 | | |||
194 | /*! If query was created using a connection, | | |||
195 | returns this connection object, otherwise @c nullptr. */ | | |||
196 | KDbConnection* connection() const; | | |||
197 | | ||||
198 | /*! @return table that is master to this query. | 196 | /*! @return table that is master to this query. | ||
199 | All potentially-editable columns within this query belong just to this table. | 197 | All potentially-editable columns within this query belong just to this table. | ||
200 | This method also can return @c nullptr if there are no tables at all, | 198 | This method also can return @c nullptr if there are no tables at all, | ||
201 | or if previously assigned master table schema has been removed | 199 | or if previously assigned master table schema has been removed | ||
202 | with removeTable(). | 200 | with removeTable(). | ||
203 | Every query that has at least one table defined, should have | 201 | Every query that has at least one table defined, should have | ||
204 | assigned a master table. | 202 | assigned a master table. | ||
205 | If no master table is assigned explicitly, but only one table used in this query, | 203 | If no master table is assigned explicitly, but only one table used in this query, | ||
▲ Show 20 Lines • Show All 164 Lines • ▼ Show 20 Line(s) | |||||
370 | If one of the fields are primary keys, it will be detected | 368 | If one of the fields are primary keys, it will be detected | ||
371 | and appropriate master-detail relation will be established. | 369 | and appropriate master-detail relation will be established. | ||
372 | This functiuon does nothing if the arguments are invalid. */ | 370 | This functiuon does nothing if the arguments are invalid. */ | ||
373 | KDbRelationship* addRelationship(KDbField *field1, KDbField *field2); | 371 | KDbRelationship* addRelationship(KDbField *field1, KDbField *field2); | ||
374 | 372 | | |||
375 | /*! @return list of KDbQueryAsterisk objects defined for this query */ | 373 | /*! @return list of KDbQueryAsterisk objects defined for this query */ | ||
376 | KDbField::List* asterisks() const; | 374 | KDbField::List* asterisks() const; | ||
377 | 375 | | |||
376 | //! Mode for field() and columnInfo() | ||||
377 | //! @since 3.1 | ||||
378 | enum class ExpandMode { | ||||
379 | Unexpanded, //!< All fields are returned even if duplicated | ||||
380 | Expanded //!< Expanded list of the query fields is computed so queries with asterisks | ||||
381 | //!< are processed well | ||||
382 | }; | ||||
383 | | ||||
378 | /*! @return field for @a identifier or @c nullptr if no field for this name | 384 | /*! @return field for @a identifier or @c nullptr if no field for this name | ||
379 | was found within the query. fieldsExpanded() method is used | 385 | was found within the query. fieldsExpanded() method is used | ||
380 | to lookup expanded list of the query fields, so queries with asterisks | 386 | to lookup expanded list of the query fields, so queries with asterisks | ||
381 | are processed well. | 387 | are processed well. | ||
382 | If a field has alias defined, name is not taken into account, | 388 | If a field has alias defined, name is not taken into account, | ||
383 | but only its alias. If a field has no alias: | 389 | but only its alias. If a field has no alias: | ||
384 | - field's name is checked | 390 | - field's name is checked | ||
385 | - field's table and field's name are checked in a form of "tablename.fieldname", | 391 | - field's table and field's name are checked in a form of "tablename.fieldname", | ||
Show All 10 Lines | |||||
396 | Expanded list of columns for the query is: T.B AS X, T.A, T.B, T.C. | 402 | Expanded list of columns for the query is: T.B AS X, T.A, T.B, T.C. | ||
397 | - Calling field("B") will return a pointer to third query column (not the first, | 403 | - Calling field("B") will return a pointer to third query column (not the first, | ||
398 | because it is covered by "X" alias). Additionally, calling field("X") | 404 | because it is covered by "X" alias). Additionally, calling field("X") | ||
399 | will return the same pointer. | 405 | will return the same pointer. | ||
400 | - Calling field("T.A") will return the same pointer as field("A"). | 406 | - Calling field("T.A") will return the same pointer as field("A"). | ||
401 | 407 | | |||
402 | This method is also a product of inheritance from KDbFieldList. | 408 | This method is also a product of inheritance from KDbFieldList. | ||
403 | */ | 409 | */ | ||
404 | const KDbField* field(const QString& identifier) const override; | 410 | const KDbField *field(KDbConnection *conn, const QString &identifier, | ||
411 | ExpandMode mode = ExpandMode::Expanded) const; | ||||
405 | 412 | | |||
406 | /** | 413 | /** | ||
407 | * @overload const KDbField* field(const QString& identifier) const | 414 | * @overload | ||
408 | */ | 415 | */ | ||
409 | KDbField* field(const QString& identifier) override; | 416 | KDbField *field(KDbConnection *conn, const QString &identifier, | ||
410 | 417 | ExpandMode mode = ExpandMode::Expanded); | |||
411 | /** | | |||
412 | * An overloaded method KDbField* field(const QString& identifier) | | |||
413 | * where unexpanded list of fields is used to find a field. | | |||
414 | */ | | |||
415 | const KDbField* unexpandedField(const QString& identifier) const; | | |||
416 | | ||||
417 | /** | | |||
418 | * @overload const KDbField* unexpandedField(const QString& identifier) const | | |||
419 | */ | | |||
420 | KDbField* unexpandedField(const QString& identifier); | | |||
421 | 418 | | |||
422 | /*! @return field id or @c nullptr if there is no such a field. */ | 419 | /*! @return field id or @c nullptr if there is no such a field. */ | ||
423 | KDbField* field(int id) override; | 420 | KDbField* field(int id) override; | ||
424 | 421 | | |||
422 | using KDbFieldList::field; | ||||
423 | | ||||
425 | /*! @overload KDbField* field(int id) */ | 424 | /*! @overload KDbField* field(int id) */ | ||
426 | const KDbField* field(int id) const override; | 425 | const KDbField* field(int id) const override; | ||
427 | 426 | | |||
428 | /*! Like KDbQuerySchema::field(const QString& name) but returns not only KDbField | 427 | /*! Like KDbQuerySchema::field(const QString& name) but returns not only KDbField | ||
429 | object for @a identifier but entire KDbQueryColumnInfo object. | 428 | object for @a identifier but entire KDbQueryColumnInfo object. | ||
430 | @a identifier can be: | 429 | @a identifier can be: | ||
431 | - a fieldname | 430 | - a fieldname | ||
432 | - an aliasname | 431 | - an aliasname | ||
433 | - a tablename.fieldname | 432 | - a tablename.fieldname | ||
434 | - a tablename.aliasname | 433 | - a tablename.aliasname | ||
435 | Note that if there are two occurrrences of the same name, | 434 | Note that if there are two occurrrences of the same name, | ||
436 | only the first is accessible using this method. For instance, | 435 | only the first is accessible using this method. For instance, | ||
437 | calling columnInfo("name") for "SELECT t1.name, t2.name FROM t1, t2" statement | 436 | calling columnInfo("name") for "SELECT t1.name, t2.name FROM t1, t2" statement | ||
438 | will only return the column related to t1.name and not t2.name, so you'll need to | 437 | will only return the column related to t1.name and not t2.name, so you'll need to | ||
439 | explicitly specify "t2.name" as the identifier to get the second column. */ | 438 | explicitly specify "t2.name" as the identifier to get the second column. */ | ||
440 | KDbQueryColumnInfo* columnInfo(const QString& identifier, bool expanded = true) const; | 439 | KDbQueryColumnInfo *columnInfo(KDbConnection *conn, const QString &identifier, | ||
440 | ExpandMode mode = ExpandMode::Expanded) const; | ||||
441 | 441 | | |||
442 | /*! Options used in fieldsExpanded() and visibleFieldsExpanded(). */ | 442 | //! Mode for fieldsExpanded() and visibleFieldsExpanded() | ||
443 | enum FieldsExpandedOptions { | 443 | //! @since 3.1 | ||
444 | enum class FieldsExpandedMode { | ||||
444 | Default, //!< All fields are returned even if duplicated | 445 | Default, //!< All fields are returned even if duplicated | ||
445 | Unique, //!< Unique list of fields is returned | 446 | Unique, //!< Unique list of fields is returned | ||
446 | WithInternalFields, //!< Like Default but internal fields (for lookup) are appended | 447 | WithInternalFields, //!< Like Default but internal fields (for lookup) are appended | ||
447 | WithInternalFieldsAndRecordId //!< Like WithInternalFields but record ID (big int type) field | 448 | WithInternalFieldsAndRecordId //!< Like WithInternalFields but record ID (big int type) field | ||
448 | //!< is appended after internal fields | 449 | //!< is appended after internal fields | ||
449 | }; | 450 | }; | ||
450 | 451 | | |||
451 | /*! @return fully expanded list of fields. | 452 | /*! @return fully expanded list of fields. | ||
Show All 33 Lines | |||||
485 | if there are multiple occurrences of one or more (options == Default). | 486 | if there are multiple occurrences of one or more (options == Default). | ||
486 | 487 | | |||
487 | Note: You should assign the resulted vector in your space - it will be shared | 488 | Note: You should assign the resulted vector in your space - it will be shared | ||
488 | and implicity copied on any modification. | 489 | and implicity copied on any modification. | ||
489 | This method's result is cached by KDbQuerySchema object. | 490 | This method's result is cached by KDbQuerySchema object. | ||
490 | @todo js: UPDATE CACHE! | 491 | @todo js: UPDATE CACHE! | ||
491 | */ | 492 | */ | ||
492 | inline KDbQueryColumnInfo::Vector fieldsExpanded( | 493 | inline KDbQueryColumnInfo::Vector fieldsExpanded( | ||
493 | FieldsExpandedOptions options = Default) const | 494 | KDbConnection *conn, FieldsExpandedMode mode = FieldsExpandedMode::Default) const | ||
494 | { | 495 | { | ||
495 | return fieldsExpandedInternal(options, false); | 496 | return fieldsExpandedInternal(conn, mode, false); | ||
496 | } | 497 | } | ||
497 | 498 | | |||
498 | /*! Like fieldsExpanded() but returns only visible fields. */ | 499 | /*! Like fieldsExpanded() but returns only visible fields. */ | ||
499 | inline KDbQueryColumnInfo::Vector visibleFieldsExpanded( | 500 | inline KDbQueryColumnInfo::Vector visibleFieldsExpanded( | ||
500 | FieldsExpandedOptions options = Default) const | 501 | KDbConnection *conn, FieldsExpandedMode options = FieldsExpandedMode::Default) const | ||
501 | { | 502 | { | ||
502 | return fieldsExpandedInternal(options, true); | 503 | return fieldsExpandedInternal(conn, options, true); | ||
503 | } | 504 | } | ||
504 | 505 | | |||
505 | /*! @return list of internal fields used for lookup columns. */ | 506 | /*! @return list of internal fields used for lookup columns. */ | ||
506 | KDbQueryColumnInfo::Vector internalFields() const; | 507 | KDbQueryColumnInfo::Vector internalFields(KDbConnection *conn) const; | ||
507 | 508 | | |||
508 | /*! @return info for expanded of internal field at index @a index. | 509 | /*! @return info for expanded of internal field at index @a index. | ||
509 | The returned field can be either logical or internal (for lookup), | 510 | The returned field can be either logical or internal (for lookup), | ||
510 | the latter case is @c true if @a index >= fieldsExpanded().count(). | 511 | the latter case is @c true if @a index >= fieldsExpanded().count(). | ||
511 | Equivalent of KDbQuerySchema::fieldsExpanded(WithInternalFields).at(index). */ | 512 | Equivalent of KDbQuerySchema::fieldsExpanded(WithInternalFields).at(index). */ | ||
512 | KDbQueryColumnInfo* expandedOrInternalField(int index) const; | 513 | KDbQueryColumnInfo* expandedOrInternalField(KDbConnection *conn, int index) const; | ||
513 | 514 | | |||
514 | /*! Options used in columnsOrder(). */ | 515 | //! Mode for columnsOrder() | ||
515 | enum ColumnsOrderOptions { | 516 | //! @since 3.1 | ||
517 | enum class ColumnsOrderMode { | ||||
516 | UnexpandedList, //!< A map for unexpanded list is created | 518 | UnexpandedList, //!< A map for unexpanded list is created | ||
517 | UnexpandedListWithoutAsterisks, //!< A map for unexpanded list is created, with asterisks skipped | 519 | UnexpandedListWithoutAsterisks, //!< A map for unexpanded list is created, with asterisks skipped | ||
518 | ExpandedList //!< A map for expanded list is created | 520 | ExpandedList //!< A map for expanded list is created | ||
519 | }; | 521 | }; | ||
520 | 522 | | |||
521 | /*! @return a hash for fast lookup of query columns' order. | 523 | /*! @return a hash for fast lookup of query columns' order. | ||
522 | - If @a options is UnexpandedList, each KDbQueryColumnInfo pointer is mapped to the index | 524 | - If @a options is UnexpandedList, each KDbQueryColumnInfo pointer is mapped to the index | ||
523 | within (unexpanded) list of fields, i.e. "*" or "table.*" asterisks are considered | 525 | within (unexpanded) list of fields, i.e. "*" or "table.*" asterisks are considered | ||
Show All 18 Lines | 543 | - columnsOrder(ExpandedList) will return the following map: KDbQueryColumnInfo(id)->0, | |||
542 | KDbQueryColumnInfo(name)->1, KDbQueryColumnInfo(surname)->2. | 544 | KDbQueryColumnInfo(name)->1, KDbQueryColumnInfo(surname)->2. | ||
543 | - columnsOrder(UnexpandedList) will return the following map: KDbQueryColumnInfo(id)->0, | 545 | - columnsOrder(UnexpandedList) will return the following map: KDbQueryColumnInfo(id)->0, | ||
544 | KDbQueryColumnInfo(name)->0, KDbQueryColumnInfo(surname)->0 because the column | 546 | KDbQueryColumnInfo(name)->0, KDbQueryColumnInfo(surname)->0 because the column | ||
545 | list is not expanded. This way you can use the returned index to get KDbField* | 547 | list is not expanded. This way you can use the returned index to get KDbField* | ||
546 | pointer using field(int) method of KDbFieldList superclass. | 548 | pointer using field(int) method of KDbFieldList superclass. | ||
547 | - columnsOrder(UnexpandedListWithoutAsterisks) will return the following map: | 549 | - columnsOrder(UnexpandedListWithoutAsterisks) will return the following map: | ||
548 | KDbQueryColumnInfo(id)->0, | 550 | KDbQueryColumnInfo(id)->0, | ||
549 | */ | 551 | */ | ||
550 | QHash<KDbQueryColumnInfo*, int> columnsOrder(ColumnsOrderOptions options = ExpandedList) const; | 552 | QHash<KDbQueryColumnInfo *, int> columnsOrder(KDbConnection *conn, | ||
553 | ColumnsOrderMode mode = ColumnsOrderMode::ExpandedList) const; | ||||
551 | 554 | | |||
552 | /*! @return table describing order of primary key (PKEY) fields within the query. | 555 | /*! @return table describing order of primary key (PKEY) fields within the query. | ||
553 | Indexing is performed against vector returned by fieldsExpanded(). | 556 | Indexing is performed against vector returned by fieldsExpanded(). | ||
554 | It is usable for e.g. Connection::updateRecord(), when we need | 557 | It is usable for e.g. Connection::updateRecord(), when we need | ||
555 | to locate each primary key's field in a constant time. | 558 | to locate each primary key's field in a constant time. | ||
556 | 559 | | |||
557 | Returned vector is owned and cached by KDbQuerySchema object. When you assign it, | 560 | Returned vector is owned and cached by KDbQuerySchema object. When you assign it, | ||
558 | it is implicity shared. Its size is equal to number of primary key | 561 | it is implicity shared. Its size is equal to number of primary key | ||
559 | fields defined for master table (masterTable()->primaryKey()->fieldCount()). | 562 | fields defined for master table (masterTable()->primaryKey()->fieldCount()). | ||
560 | 563 | | |||
561 | Each element of the returned vector: | 564 | Each element of the returned vector: | ||
562 | - can belong to [0..fieldsExpanded().count()-1] if there is such | 565 | - can belong to [0..fieldsExpanded().count()-1] if there is such | ||
563 | primary key's field in the fieldsExpanded() list. | 566 | primary key's field in the fieldsExpanded() list. | ||
564 | - can be equal to -1 if there is no such primary key's field | 567 | - can be equal to -1 if there is no such primary key's field | ||
565 | in the fieldsExpanded() list. | 568 | in the fieldsExpanded() list. | ||
566 | 569 | | |||
567 | If there are more than one primary key's field included in the query, | 570 | If there are more than one primary key's field included in the query, | ||
568 | only first-found column (oin the fieldsExpanded() list) for each pkey's field is included. | 571 | only first-found column (oin the fieldsExpanded() list) for each pkey's field is included. | ||
569 | 572 | | |||
570 | Returns empty vector if there is no master table or no master table's pkey. | 573 | Returns empty vector if there is no master table or no master table's pkey. | ||
571 | @see example for pkeyFieldCount(). | 574 | @see example for pkeyFieldCount(). | ||
572 | @todo js: UPDATE CACHE! | 575 | @todo js: UPDATE CACHE! | ||
573 | */ | 576 | */ | ||
574 | QVector<int> pkeyFieldsOrder() const; | 577 | QVector<int> pkeyFieldsOrder(KDbConnection *conn) const; | ||
575 | 578 | | |||
576 | /*! @return number of master table's primary key fields included in this query. | 579 | /*! @return number of master table's primary key fields included in this query. | ||
577 | This method is useful to quickly check whether the vector returned by pkeyFieldsOrder() | 580 | This method is useful to quickly check whether the vector returned by pkeyFieldsOrder() | ||
578 | if filled completely. | 581 | if filled completely. | ||
579 | 582 | | |||
580 | User e.g. in KDbConnection::updateRecord() to check if entire primary | 583 | User e.g. in KDbConnection::updateRecord() to check if entire primary | ||
581 | key information is specified. | 584 | key information is specified. | ||
582 | 585 | | |||
583 | Examples: let table T has (ID1 INTEGER, ID2 INTEGER, A INTEGER) fields, | 586 | Examples: let table T has (ID1 INTEGER, ID2 INTEGER, A INTEGER) fields, | ||
584 | and let (ID1, ID2) is T's primary key. | 587 | and let (ID1, ID2) is T's primary key. | ||
585 | -# The query defined by "SELECT * FROM T" statement contains all T's | 588 | -# The query defined by "SELECT * FROM T" statement contains all T's | ||
586 | primary key's fields as T is the master table, and thus pkeyFieldCount() | 589 | primary key's fields as T is the master table, and thus pkeyFieldCount() | ||
587 | will return 2 (both primary key's fields are in the fieldsExpanded() list), | 590 | will return 2 (both primary key's fields are in the fieldsExpanded() list), | ||
588 | and pkeyFieldsOrder() will return vector {0, 1}. | 591 | and pkeyFieldsOrder() will return vector {0, 1}. | ||
589 | -# The query defined by "SELECT A, ID2 FROM T" statement, and thus pkeyFieldCount() | 592 | -# The query defined by "SELECT A, ID2 FROM T" statement, and thus pkeyFieldCount() | ||
590 | will return 1 (only one primary key's field is in the fieldsExpanded() list), | 593 | will return 1 (only one primary key's field is in the fieldsExpanded() list), | ||
591 | and pkeyFieldsOrder() will return vector {-1, 1}, as second primary key's field | 594 | and pkeyFieldsOrder() will return vector {-1, 1}, as second primary key's field | ||
592 | is at position #1 and first field is not specified at all within the query. | 595 | is at position #1 and first field is not specified at all within the query. | ||
593 | */ | 596 | */ | ||
594 | int pkeyFieldCount(); | 597 | int pkeyFieldCount(KDbConnection *conn); | ||
595 | 598 | | |||
596 | /*! @return a list of field infos for all auto-incremented fields | 599 | /*! @return a list of field infos for all auto-incremented fields | ||
597 | from master table of this query. This result is cached for efficiency. | 600 | from master table of this query. This result is cached for efficiency. | ||
598 | fieldsExpanded() is used for that. | 601 | fieldsExpanded() is used for that. | ||
599 | */ | 602 | */ | ||
600 | KDbQueryColumnInfo::List* autoIncrementFields() const; | 603 | KDbQueryColumnInfo::List* autoIncrementFields(KDbConnection *conn) const; | ||
601 | 604 | | |||
602 | /*! @return a preset statement (if any). */ | 605 | /*! @return a preset statement (if any). */ | ||
603 | KDbEscapedString statement() const; | 606 | KDbEscapedString statement() const; | ||
604 | 607 | | |||
605 | /*! Forces a raw SQL statement @a sql for the query. This means that no statement is composed | 608 | /*! Forces a raw SQL statement @a sql for the query. This means that no statement is composed | ||
606 | * from KDbQuerySchema's content. */ | 609 | * from KDbQuerySchema's content. */ | ||
607 | void setStatement(const KDbEscapedString& sql); | 610 | void setStatement(const KDbEscapedString& sql); | ||
608 | 611 | | |||
▲ Show 20 Lines • Show All 67 Lines • ▼ Show 20 Line(s) | |||||
676 | Read notes for @ref setOrderByColumnList(). */ | 679 | Read notes for @ref setOrderByColumnList(). */ | ||
677 | KDbOrderByColumnList* orderByColumnList(); | 680 | KDbOrderByColumnList* orderByColumnList(); | ||
678 | 681 | | |||
679 | /*! @see orderByColumnList() */ | 682 | /*! @see orderByColumnList() */ | ||
680 | const KDbOrderByColumnList* orderByColumnList() const; | 683 | const KDbOrderByColumnList* orderByColumnList() const; | ||
681 | 684 | | |||
682 | /*! @return query schema parameters. These are taked from the WHERE section | 685 | /*! @return query schema parameters. These are taked from the WHERE section | ||
683 | (a tree of expression items). */ | 686 | (a tree of expression items). */ | ||
684 | QList<KDbQuerySchemaParameter> parameters() const; | 687 | QList<KDbQuerySchemaParameter> parameters(KDbConnection *conn) const; | ||
685 | 688 | | |||
686 | //! @return @c true if this query is valid | 689 | //! @return @c true if this query is valid | ||
687 | /*! Detailed validation is performed in the same way as parsing of query statements | 690 | /*! Detailed validation is performed in the same way as parsing of query statements | ||
688 | * by the KDbParser. | 691 | * by the KDbParser. | ||
689 | * Example :Let the query be "SELECT <fields> FROM <tables> WHERE <whereExpression>". | 692 | * Example :Let the query be "SELECT <fields> FROM <tables> WHERE <whereExpression>". | ||
690 | * First each field from <fields> (@see fields()) is validated using | 693 | * First each field from <fields> (@see fields()) is validated using | ||
691 | * KDbField::expression().validate(). Then the <whereExpression> (@see | 694 | * KDbField::expression().validate(). Then the <whereExpression> (@see | ||
692 | * whereExpression()) | 695 | * whereExpression()) | ||
693 | * is validated using KDbExpression::validate(). | 696 | * is validated using KDbExpression::validate(). | ||
694 | * | 697 | * | ||
695 | * On error a string pointed by @a errorMessage (if provided) is set to a general | 698 | * On error a string pointed by @a errorMessage (if provided) is set to a general | ||
696 | * error message and a string pointed by @a errorDescription (if provided) is set to a | 699 | * error message and a string pointed by @a errorDescription (if provided) is set to a | ||
697 | * detailed description of the error. | 700 | * detailed description of the error. | ||
698 | */ | 701 | */ | ||
699 | //! @todo add tests | 702 | //! @todo add tests | ||
700 | bool validate(QString *errorMessage = nullptr, QString *errorDescription = nullptr); | 703 | bool validate(QString *errorMessage = nullptr, QString *errorDescription = nullptr); | ||
701 | 704 | | |||
702 | class Private; // Protected not private because of the parser | | |||
703 | | ||||
704 | protected: | 705 | protected: | ||
705 | //! @internal associates @a conn with this query so it's possible to find tables | 706 | KDbQuerySchemaFieldsExpanded *computeFieldsExpanded(KDbConnection *conn) const; | ||
706 | explicit KDbQuerySchema(KDbConnection *conn); | | |||
707 | 707 | | |||
708 | void computeFieldsExpanded() const; | 708 | //! Used by fieldsExpanded(KDbConnection*, FieldsExpandedMode) | ||
709 | 709 | //! and visibleFieldsExpanded(KDbConnection*, FieldsExpandedMode). | |||
710 | //! Used by fieldsExpanded(FieldsExpandedOptions) | 710 | KDbQueryColumnInfo::Vector fieldsExpandedInternal(KDbConnection *conn, | ||
711 | //! and visibleFieldsExpanded(FieldsExpandedOptions options). | 711 | FieldsExpandedMode mode, | ||
712 | KDbQueryColumnInfo::Vector fieldsExpandedInternal(FieldsExpandedOptions options, | | |||
713 | bool onlyVisible) const; | 712 | bool onlyVisible) const; | ||
714 | 713 | | |||
715 | /** Internal method used by all insert*Field methods. | 714 | /** Internal method used by all insert*Field methods. | ||
716 | * The new column can also be explicitly bound to a specific position on tables list. | 715 | * The new column can also be explicitly bound to a specific position on tables list. | ||
717 | * @a bindToTable is a table index within the query for which the field should be bound. | 716 | * @a bindToTable is a table index within the query for which the field should be bound. | ||
718 | * If @a bindToTable is -1, no particular table will be bound. | 717 | * If @a bindToTable is -1, no particular table will be bound. | ||
719 | * @see tableBoundToColumn(int columnPosition) | 718 | * @see tableBoundToColumn(int columnPosition) | ||
720 | */ | 719 | */ | ||
Show All 9 Lines | |||||
730 | * Appends expression @a expr at the and of columns list, sets visibility. | 729 | * Appends expression @a expr at the and of columns list, sets visibility. | ||
731 | */ | 730 | */ | ||
732 | bool addExpressionInternal(const KDbExpression& expr, bool visible); | 731 | bool addExpressionInternal(const KDbExpression& expr, bool visible); | ||
733 | 732 | | |||
734 | /** Internal method used by a query parser. | 733 | /** Internal method used by a query parser. | ||
735 | */ | 734 | */ | ||
736 | void setWhereExpressionInternal(const KDbExpression &expr); | 735 | void setWhereExpressionInternal(const KDbExpression &expr); | ||
737 | 736 | | |||
738 | Private * const d; | 737 | friend class KDbQuerySchemaPrivate; | ||
738 | | ||||
739 | Q_DISABLE_COPY(KDbQuerySchema) | ||||
740 | KDbQuerySchemaPrivate * const d; | ||||
739 | }; | 741 | }; | ||
740 | 742 | | |||
741 | //! Sends query schema information @a query to debug output @a dbg. | 743 | //! A pair (connection, table-or-schema) for QDebug operator<< | ||
742 | KDB_EXPORT QDebug operator<<(QDebug dbg, const KDbQuerySchema& query); | 744 | //! @since 3.1 | ||
745 | typedef std::tuple<KDbConnection*, const KDbQuerySchema&> KDbConnectionAndQuerySchema; | ||||
746 | | ||||
747 | //! Sends connection and query schema information @a connectionAndSchema to debug output @a dbg. | ||||
748 | //! @since 3.1 | ||||
749 | KDB_EXPORT QDebug operator<<(QDebug dbg, | ||||
750 | const KDbConnectionAndQuerySchema &connectionAndSchema); | ||||
743 | 751 | | |||
744 | #endif | 752 | #endif |