Changeset View
Changeset View
Standalone View
Standalone View
src/KDbConnection.cpp
Show All 26 Lines | |||||
27 | #include "KDbNativeStatementBuilder.h" | 27 | #include "KDbNativeStatementBuilder.h" | ||
28 | #include "KDbQuerySchema.h" | 28 | #include "KDbQuerySchema.h" | ||
29 | #include "KDbQuerySchema_p.h" | 29 | #include "KDbQuerySchema_p.h" | ||
30 | #include "KDbRecordData.h" | 30 | #include "KDbRecordData.h" | ||
31 | #include "KDbRecordEditBuffer.h" | 31 | #include "KDbRecordEditBuffer.h" | ||
32 | #include "KDbRelationship.h" | 32 | #include "KDbRelationship.h" | ||
33 | #include "KDbSqlRecord.h" | 33 | #include "KDbSqlRecord.h" | ||
34 | #include "KDbSqlResult.h" | 34 | #include "KDbSqlResult.h" | ||
35 | #include "KDbTableOrQuerySchema.h" | ||||
35 | #include "KDbTableSchemaChangeListener.h" | 36 | #include "KDbTableSchemaChangeListener.h" | ||
36 | #include "KDbTransactionData.h" | 37 | #include "KDbTransactionData.h" | ||
37 | #include "KDbTransactionGuard.h" | 38 | #include "KDbTransactionGuard.h" | ||
38 | #include "kdb_debug.h" | 39 | #include "kdb_debug.h" | ||
39 | 40 | | |||
40 | #include <QDir> | 41 | #include <QDir> | ||
41 | #include <QFileInfo> | 42 | #include <QFileInfo> | ||
42 | #include <QDomDocument> | 43 | #include <QDomDocument> | ||
▲ Show 20 Lines • Show All 367 Lines • ▼ Show 20 Line(s) | 409 | ERR_SQL_PARSE_ERROR, tr("<p>Could not load definition for query \"%1\". " | |||
410 | "<p>This query can be edited only in Text View.</p>") | 411 | "<p>This query can be edited only in Text View.</p>") | ||
411 | .arg(query->name(), sql)); | 412 | .arg(query->name(), sql)); | ||
412 | return nullptr; | 413 | return nullptr; | ||
413 | } | 414 | } | ||
414 | insertQuery(query); | 415 | insertQuery(query); | ||
415 | return newQuery.take(); | 416 | return newQuery.take(); | ||
416 | } | 417 | } | ||
417 | 418 | | |||
419 | KDbQuerySchemaFieldsExpanded *KDbConnectionPrivate::fieldsExpanded(const KDbQuerySchema *query) | ||||
420 | { | ||||
421 | return m_fieldsExpandedCache[query]; | ||||
422 | } | ||||
423 | | ||||
424 | void KDbConnectionPrivate::insertFieldsExpanded(const KDbQuerySchema *query, KDbQuerySchemaFieldsExpanded *cache) | ||||
425 | { | ||||
426 | m_fieldsExpandedCache.insert(query, cache); | ||||
427 | } | ||||
428 | | ||||
418 | //================================================ | 429 | //================================================ | ||
419 | 430 | | |||
420 | namespace { | 431 | namespace { | ||
421 | //! @internal static: list of internal KDb system table names | 432 | //! @internal static: list of internal KDb system table names | ||
422 | class SystemTables : public QStringList | 433 | class SystemTables : public QStringList | ||
423 | { | 434 | { | ||
424 | public: | 435 | public: | ||
425 | SystemTables() | 436 | SystemTables() | ||
▲ Show 20 Lines • Show All 2508 Lines • ▼ Show 20 Line(s) | |||||
2934 | 2945 | | |||
2935 | KDbQuerySchema* KDbConnection::querySchema(const QString& aQueryName) | 2946 | KDbQuerySchema* KDbConnection::querySchema(const QString& aQueryName) | ||
2936 | { | 2947 | { | ||
2937 | QString queryName = aQueryName.toLower(); | 2948 | QString queryName = aQueryName.toLower(); | ||
2938 | KDbQuerySchema *q = d->query(queryName); | 2949 | KDbQuerySchema *q = d->query(queryName); | ||
2939 | if (q) | 2950 | if (q) | ||
2940 | return q; | 2951 | return q; | ||
2941 | //not found: retrieve schema | 2952 | //not found: retrieve schema | ||
2942 | QScopedPointer<KDbQuerySchema> newQuery(KDbQuerySchema::Private::createQuery(this)); | 2953 | QScopedPointer<KDbQuerySchema> newQuery(new KDbQuerySchema); | ||
2943 | clearResult(); | 2954 | clearResult(); | ||
2944 | if (true != loadObjectData(KDb::QueryObjectType, aQueryName, newQuery.data())) { | 2955 | if (true != loadObjectData(KDb::QueryObjectType, aQueryName, newQuery.data())) { | ||
2945 | return nullptr; | 2956 | return nullptr; | ||
2946 | } | 2957 | } | ||
2947 | return d->setupQuerySchema(newQuery.take()); | 2958 | return d->setupQuerySchema(newQuery.take()); | ||
2948 | } | 2959 | } | ||
2949 | 2960 | | |||
2950 | KDbQuerySchema* KDbConnection::querySchema(int queryId) | 2961 | KDbQuerySchema* KDbConnection::querySchema(int queryId) | ||
2951 | { | 2962 | { | ||
2952 | KDbQuerySchema *q = d->query(queryId); | 2963 | KDbQuerySchema *q = d->query(queryId); | ||
2953 | if (q) | 2964 | if (q) | ||
2954 | return q; | 2965 | return q; | ||
2955 | //not found: retrieve schema | 2966 | //not found: retrieve schema | ||
2956 | QScopedPointer<KDbQuerySchema> newQuery(KDbQuerySchema::Private::createQuery(this)); | 2967 | QScopedPointer<KDbQuerySchema> newQuery(new KDbQuerySchema); | ||
2957 | clearResult(); | 2968 | clearResult(); | ||
2958 | if (true != loadObjectData(KDb::QueryObjectType, queryId, newQuery.data())) { | 2969 | if (true != loadObjectData(KDb::QueryObjectType, queryId, newQuery.data())) { | ||
2959 | return nullptr; | 2970 | return nullptr; | ||
2960 | } | 2971 | } | ||
2961 | return d->setupQuerySchema(newQuery.take()); | 2972 | return d->setupQuerySchema(newQuery.take()); | ||
2962 | } | 2973 | } | ||
2963 | 2974 | | |||
2964 | bool KDbConnection::setQuerySchemaObsolete(const QString& queryName) | 2975 | bool KDbConnection::setQuerySchemaObsolete(const QString& queryName) | ||
Show All 35 Lines | |||||
3000 | } | 3011 | } | ||
3001 | 3012 | | |||
3002 | void KDbConnection::setAvailableDatabaseName(const QString& dbName) | 3013 | void KDbConnection::setAvailableDatabaseName(const QString& dbName) | ||
3003 | { | 3014 | { | ||
3004 | d->availableDatabaseName = dbName; | 3015 | d->availableDatabaseName = dbName; | ||
3005 | } | 3016 | } | ||
3006 | 3017 | | |||
3007 | //! @internal used in updateRecord(), insertRecord(), | 3018 | //! @internal used in updateRecord(), insertRecord(), | ||
3008 | inline static void updateRecordDataWithNewValues(KDbQuerySchema* query, KDbRecordData* data, | 3019 | inline static void updateRecordDataWithNewValues( | ||
3020 | KDbConnection *conn, KDbQuerySchema* query, KDbRecordData* data, | ||||
3009 | const KDbRecordEditBuffer::DbHash& b, | 3021 | const KDbRecordEditBuffer::DbHash& b, | ||
3010 | QHash<KDbQueryColumnInfo*, int>* columnsOrderExpanded) | 3022 | QHash<KDbQueryColumnInfo*, int>* columnsOrderExpanded) | ||
3011 | { | 3023 | { | ||
3012 | *columnsOrderExpanded = query->columnsOrder(KDbQuerySchema::ExpandedList); | 3024 | *columnsOrderExpanded | ||
3025 | = query->columnsOrder(conn, KDbQuerySchema::ColumnsOrderMode::ExpandedList); | ||||
3013 | QHash<KDbQueryColumnInfo*, int>::ConstIterator columnsOrderExpandedIt; | 3026 | QHash<KDbQueryColumnInfo*, int>::ConstIterator columnsOrderExpandedIt; | ||
3014 | for (KDbRecordEditBuffer::DbHash::ConstIterator it = b.constBegin();it != b.constEnd();++it) { | 3027 | for (KDbRecordEditBuffer::DbHash::ConstIterator it = b.constBegin();it != b.constEnd();++it) { | ||
3015 | columnsOrderExpandedIt = columnsOrderExpanded->constFind(it.key()); | 3028 | columnsOrderExpandedIt = columnsOrderExpanded->constFind(it.key()); | ||
3016 | if (columnsOrderExpandedIt == columnsOrderExpanded->constEnd()) { | 3029 | if (columnsOrderExpandedIt == columnsOrderExpanded->constEnd()) { | ||
3017 | kdbWarning() << "(KDbConnection) \"now also assign new value in memory\" step" | 3030 | kdbWarning() << "(KDbConnection) \"now also assign new value in memory\" step" | ||
3018 | "- could not find item" << it.key()->aliasOrName(); | 3031 | "- could not find item" << it.key()->aliasOrName(); | ||
3019 | continue; | 3032 | continue; | ||
3020 | } | 3033 | } | ||
▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Line(s) | 3078 | if (!sqlset.isEmpty()) | |||
3066 | sqlset += ','; | 3079 | sqlset += ','; | ||
3067 | KDbField* currentField = it.key()->field(); | 3080 | KDbField* currentField = it.key()->field(); | ||
3068 | const bool affectedFieldsAddOk = affectedFields.addField(currentField); | 3081 | const bool affectedFieldsAddOk = affectedFields.addField(currentField); | ||
3069 | Q_ASSERT(affectedFieldsAddOk); | 3082 | Q_ASSERT(affectedFieldsAddOk); | ||
3070 | sqlset += KDbEscapedString(escapeIdentifier(currentField->name())) + '=' + | 3083 | sqlset += KDbEscapedString(escapeIdentifier(currentField->name())) + '=' + | ||
3071 | d->driver->valueToSql(currentField, it.value()); | 3084 | d->driver->valueToSql(currentField, it.value()); | ||
3072 | } | 3085 | } | ||
3073 | if (pkey) { | 3086 | if (pkey) { | ||
3074 | const QVector<int> pkeyFieldsOrder(query->pkeyFieldsOrder()); | 3087 | const QVector<int> pkeyFieldsOrder(query->pkeyFieldsOrder(this)); | ||
3075 | //kdbDebug() << pkey->fieldCount() << " ? " << query->pkeyFieldCount(); | 3088 | //kdbDebug() << pkey->fieldCount() << " ? " << query->pkeyFieldCount(); | ||
3076 | if (pkey->fieldCount() != query->pkeyFieldCount()) { //sanity check | 3089 | if (pkey->fieldCount() != query->pkeyFieldCount(this)) { //sanity check | ||
3077 | kdbWarning() << " -- NO ENTIRE MASTER TABLE's PKEY SPECIFIED!"; | 3090 | kdbWarning() << " -- NO ENTIRE MASTER TABLE's PKEY SPECIFIED!"; | ||
3078 | m_result = KDbResult(ERR_UPDATE_NO_ENTIRE_MASTER_TABLES_PKEY, | 3091 | m_result = KDbResult(ERR_UPDATE_NO_ENTIRE_MASTER_TABLES_PKEY, | ||
3079 | tr("Could not update record because it does not contain entire primary key of master table.")); | 3092 | tr("Could not update record because it does not contain entire primary key of master table.")); | ||
3080 | return false; | 3093 | return false; | ||
3081 | } | 3094 | } | ||
3082 | if (!pkey->fields()->isEmpty()) { | 3095 | if (!pkey->fields()->isEmpty()) { | ||
3083 | int i = 0; | 3096 | int i = 0; | ||
3084 | foreach(KDbField *f, *pkey->fields()) { | 3097 | foreach(KDbField *f, *pkey->fields()) { | ||
Show All 30 Lines | |||||
3115 | 3128 | | |||
3116 | if (!res) { | 3129 | if (!res) { | ||
3117 | m_result = KDbResult(ERR_UPDATE_SERVER_ERROR, | 3130 | m_result = KDbResult(ERR_UPDATE_SERVER_ERROR, | ||
3118 | tr("Record updating on the server failed.")); | 3131 | tr("Record updating on the server failed.")); | ||
3119 | return false; | 3132 | return false; | ||
3120 | } | 3133 | } | ||
3121 | //success: now also assign new values in memory: | 3134 | //success: now also assign new values in memory: | ||
3122 | QHash<KDbQueryColumnInfo*, int> columnsOrderExpanded; | 3135 | QHash<KDbQueryColumnInfo*, int> columnsOrderExpanded; | ||
3123 | updateRecordDataWithNewValues(query, data, b, &columnsOrderExpanded); | 3136 | updateRecordDataWithNewValues(this, query, data, b, &columnsOrderExpanded); | ||
3124 | return true; | 3137 | return true; | ||
3125 | } | 3138 | } | ||
3126 | 3139 | | |||
3127 | bool KDbConnection::insertRecord(KDbQuerySchema* query, KDbRecordData* data, KDbRecordEditBuffer* buf, bool getRecordId) | 3140 | bool KDbConnection::insertRecord(KDbQuerySchema* query, KDbRecordData* data, KDbRecordEditBuffer* buf, bool getRecordId) | ||
3128 | { | 3141 | { | ||
3129 | // Each SQL identifier needs to be escaped in the generated query. | 3142 | // Each SQL identifier needs to be escaped in the generated query. | ||
3130 | clearResult(); | 3143 | clearResult(); | ||
3131 | //--get PKEY | 3144 | //--get PKEY | ||
Show All 20 Lines | |||||
3152 | 3165 | | |||
3153 | //insert the record: | 3166 | //insert the record: | ||
3154 | KDbEscapedString sql; | 3167 | KDbEscapedString sql; | ||
3155 | sql.reserve(4096); | 3168 | sql.reserve(4096); | ||
3156 | sql = KDbEscapedString("INSERT INTO ") + escapeIdentifier(mt->name()) + " ("; | 3169 | sql = KDbEscapedString("INSERT INTO ") + escapeIdentifier(mt->name()) + " ("; | ||
3157 | KDbRecordEditBuffer::DbHash b = buf->dbBuffer(); | 3170 | KDbRecordEditBuffer::DbHash b = buf->dbBuffer(); | ||
3158 | 3171 | | |||
3159 | // add default values, if available (for any column without value explicitly set) | 3172 | // add default values, if available (for any column without value explicitly set) | ||
3160 | const KDbQueryColumnInfo::Vector fieldsExpanded(query->fieldsExpanded(KDbQuerySchema::Unique)); | 3173 | const KDbQueryColumnInfo::Vector fieldsExpanded( | ||
3174 | query->fieldsExpanded(this, KDbQuerySchema::FieldsExpandedMode::Unique)); | ||||
3161 | int fieldsExpandedCount = fieldsExpanded.count(); | 3175 | int fieldsExpandedCount = fieldsExpanded.count(); | ||
3162 | for (int i = 0; i < fieldsExpandedCount; i++) { | 3176 | for (int i = 0; i < fieldsExpandedCount; i++) { | ||
3163 | KDbQueryColumnInfo *ci = fieldsExpanded.at(i); | 3177 | KDbQueryColumnInfo *ci = fieldsExpanded.at(i); | ||
3164 | if (ci->field() && KDb::isDefaultValueAllowed(*ci->field()) | 3178 | if (ci->field() && KDb::isDefaultValueAllowed(*ci->field()) | ||
3165 | && !ci->field()->defaultValue().isNull() | 3179 | && !ci->field()->defaultValue().isNull() | ||
3166 | && !b.contains(ci)) | 3180 | && !b.contains(ci)) | ||
3167 | { | 3181 | { | ||
3168 | //kdbDebug() << "adding default value" << ci->field->defaultValue().toString() << "for column" << ci->field->name(); | 3182 | //kdbDebug() << "adding default value" << ci->field->defaultValue().toString() << "for column" << ci->field->name(); | ||
3169 | b.insert(ci, ci->field()->defaultValue()); | 3183 | b.insert(ci, ci->field()->defaultValue()); | ||
3170 | } | 3184 | } | ||
3171 | } | 3185 | } | ||
3172 | 3186 | | |||
3173 | //collect fields which have values in KDbRecordEditBuffer | 3187 | //collect fields which have values in KDbRecordEditBuffer | ||
3174 | KDbFieldList affectedFields; | 3188 | KDbFieldList affectedFields; | ||
3175 | 3189 | | |||
3176 | if (b.isEmpty()) { | 3190 | if (b.isEmpty()) { | ||
3177 | // empty record inserting requested: | 3191 | // empty record inserting requested: | ||
3178 | if (!getRecordId && !pkey) { | 3192 | if (!getRecordId && !pkey) { | ||
3179 | kdbWarning() << "MASTER TABLE's PKEY REQUIRED FOR INSERTING EMPTY RECORDS: INSERT CANCELLED"; | 3193 | kdbWarning() << "MASTER TABLE's PKEY REQUIRED FOR INSERTING EMPTY RECORDS: INSERT CANCELLED"; | ||
3180 | m_result = KDbResult(ERR_INSERT_NO_MASTER_TABLES_PKEY, | 3194 | m_result = KDbResult(ERR_INSERT_NO_MASTER_TABLES_PKEY, | ||
3181 | tr("Could not insert record because master table has no primary key specified.")); | 3195 | tr("Could not insert record because master table has no primary key specified.")); | ||
3182 | return false; | 3196 | return false; | ||
3183 | } | 3197 | } | ||
3184 | if (pkey) { | 3198 | if (pkey) { | ||
3185 | const QVector<int> pkeyFieldsOrder(query->pkeyFieldsOrder()); | 3199 | const QVector<int> pkeyFieldsOrder(query->pkeyFieldsOrder(this)); | ||
3186 | // kdbDebug() << pkey->fieldCount() << " ? " << query->pkeyFieldCount(); | 3200 | // kdbDebug() << pkey->fieldCount() << " ? " << query->pkeyFieldCount(); | ||
3187 | if (pkey->fieldCount() != query->pkeyFieldCount()) { // sanity check | 3201 | if (pkey->fieldCount() != query->pkeyFieldCount(this)) { // sanity check | ||
3188 | kdbWarning() << "NO ENTIRE MASTER TABLE's PKEY SPECIFIED!"; | 3202 | kdbWarning() << "NO ENTIRE MASTER TABLE's PKEY SPECIFIED!"; | ||
3189 | m_result = KDbResult(ERR_INSERT_NO_ENTIRE_MASTER_TABLES_PKEY, | 3203 | m_result = KDbResult(ERR_INSERT_NO_ENTIRE_MASTER_TABLES_PKEY, | ||
3190 | tr("Could not insert record because it does not contain " | 3204 | tr("Could not insert record because it does not contain " | ||
3191 | "entire master table's primary key.")); | 3205 | "entire master table's primary key.")); | ||
3192 | return false; | 3206 | return false; | ||
3193 | } | 3207 | } | ||
3194 | } | 3208 | } | ||
3195 | //at least one value is needed for VALUES section: find it and set to NULL: | 3209 | //at least one value is needed for VALUES section: find it and set to NULL: | ||
Show All 34 Lines | 3241 | // kdbDebug() << " -- SQL == " << sql; | |||
3230 | QSharedPointer<KDbSqlResult> result = insertRecordInternal(mt->name(), &affectedFields, sql); | 3244 | QSharedPointer<KDbSqlResult> result = insertRecordInternal(mt->name(), &affectedFields, sql); | ||
3231 | if (!result) { | 3245 | if (!result) { | ||
3232 | m_result = KDbResult(ERR_INSERT_SERVER_ERROR, | 3246 | m_result = KDbResult(ERR_INSERT_SERVER_ERROR, | ||
3233 | tr("Record inserting on the server failed.")); | 3247 | tr("Record inserting on the server failed.")); | ||
3234 | return false; | 3248 | return false; | ||
3235 | } | 3249 | } | ||
3236 | //success: now also assign a new value in memory: | 3250 | //success: now also assign a new value in memory: | ||
3237 | QHash<KDbQueryColumnInfo*, int> columnsOrderExpanded; | 3251 | QHash<KDbQueryColumnInfo*, int> columnsOrderExpanded; | ||
3238 | updateRecordDataWithNewValues(query, data, b, &columnsOrderExpanded); | 3252 | updateRecordDataWithNewValues(this, query, data, b, &columnsOrderExpanded); | ||
3239 | 3253 | | |||
3240 | //fetch autoincremented values | 3254 | //fetch autoincremented values | ||
3241 | KDbQueryColumnInfo::List *aif_list = query->autoIncrementFields(); | 3255 | KDbQueryColumnInfo::List *aif_list = query->autoIncrementFields(this); | ||
3242 | quint64 recordId = 0; | 3256 | quint64 recordId = 0; | ||
3243 | if (pkey && !aif_list->isEmpty()) { | 3257 | if (pkey && !aif_list->isEmpty()) { | ||
3244 | //! @todo now only if PKEY is present, this should also work when there's no PKEY | 3258 | //! @todo now only if PKEY is present, this should also work when there's no PKEY | ||
3245 | KDbQueryColumnInfo *id_columnInfo = aif_list->first(); | 3259 | KDbQueryColumnInfo *id_columnInfo = aif_list->first(); | ||
3246 | //! @todo safe to cast it? | 3260 | //! @todo safe to cast it? | ||
3247 | quint64 last_id | 3261 | quint64 last_id | ||
3248 | = KDb::lastInsertedAutoIncValue(result, id_columnInfo->field()->name(), | 3262 | = KDb::lastInsertedAutoIncValue(result, id_columnInfo->field()->name(), | ||
3249 | id_columnInfo->field()->table()->name(), &recordId); | 3263 | id_columnInfo->field()->table()->name(), &recordId); | ||
▲ Show 20 Lines • Show All 60 Lines • ▼ Show 20 Line(s) | 3316 | //! @todo allow to delete from a table without pkey | |||
3310 | //update the record: | 3324 | //update the record: | ||
3311 | KDbEscapedString sql; | 3325 | KDbEscapedString sql; | ||
3312 | sql.reserve(4096); | 3326 | sql.reserve(4096); | ||
3313 | sql = KDbEscapedString("DELETE FROM ") + escapeIdentifier(mt->name()) + " WHERE "; | 3327 | sql = KDbEscapedString("DELETE FROM ") + escapeIdentifier(mt->name()) + " WHERE "; | ||
3314 | KDbEscapedString sqlwhere; | 3328 | KDbEscapedString sqlwhere; | ||
3315 | sqlwhere.reserve(1024); | 3329 | sqlwhere.reserve(1024); | ||
3316 | 3330 | | |||
3317 | if (pkey) { | 3331 | if (pkey) { | ||
3318 | const QVector<int> pkeyFieldsOrder(query->pkeyFieldsOrder()); | 3332 | const QVector<int> pkeyFieldsOrder(query->pkeyFieldsOrder(this)); | ||
3319 | //kdbDebug() << pkey->fieldCount() << " ? " << query->pkeyFieldCount(); | 3333 | //kdbDebug() << pkey->fieldCount() << " ? " << query->pkeyFieldCount(); | ||
3320 | if (pkey->fieldCount() != query->pkeyFieldCount()) { //sanity check | 3334 | if (pkey->fieldCount() != query->pkeyFieldCount(this)) { //sanity check | ||
3321 | kdbWarning() << " -- NO ENTIRE MASTER TABLE's PKEY SPECIFIED!"; | 3335 | kdbWarning() << " -- NO ENTIRE MASTER TABLE's PKEY SPECIFIED!"; | ||
3322 | m_result = KDbResult(ERR_DELETE_NO_ENTIRE_MASTER_TABLES_PKEY, | 3336 | m_result = KDbResult(ERR_DELETE_NO_ENTIRE_MASTER_TABLES_PKEY, | ||
3323 | tr("Could not delete record because it does not contain entire master table's primary key.")); | 3337 | tr("Could not delete record because it does not contain entire master table's primary key.")); | ||
3324 | return false; | 3338 | return false; | ||
3325 | } | 3339 | } | ||
3326 | int i = 0; | 3340 | int i = 0; | ||
3327 | foreach(KDbField *f, *pkey->fields()) { | 3341 | foreach(KDbField *f, *pkey->fields()) { | ||
3328 | if (!sqlwhere.isEmpty()) | 3342 | if (!sqlwhere.isEmpty()) | ||
▲ Show 20 Lines • Show All 42 Lines • ▼ Show 20 Line(s) | 3371 | { | |||
3371 | if (!executeSql(sql)) { | 3385 | if (!executeSql(sql)) { | ||
3372 | m_result = KDbResult(ERR_DELETE_SERVER_ERROR, | 3386 | m_result = KDbResult(ERR_DELETE_SERVER_ERROR, | ||
3373 | tr("Record deletion on the server failed.")); | 3387 | tr("Record deletion on the server failed.")); | ||
3374 | return false; | 3388 | return false; | ||
3375 | } | 3389 | } | ||
3376 | return true; | 3390 | return true; | ||
3377 | } | 3391 | } | ||
3378 | 3392 | | |||
3393 | int KDbConnection::recordCount(const KDbEscapedString& sql) | ||||
3394 | { | ||||
3395 | int count = -1; //will be changed only on success of querySingleNumber() | ||||
3396 | const tristate result = querySingleNumber( | ||||
3397 | KDbEscapedString("SELECT COUNT() FROM (") + sql + ") AS kdb__subquery", &count); | ||||
3398 | if (~result) { | ||||
3399 | count = 0; | ||||
3400 | } | ||||
3401 | return count; | ||||
3402 | } | ||||
3403 | | ||||
3404 | int KDbConnection::recordCount(const KDbTableSchema& tableSchema) | ||||
3405 | { | ||||
3406 | //! @todo does not work with non-SQL data sources | ||||
3407 | int count = -1; // will be changed only on success of querySingleNumber() | ||||
3408 | const tristate result | ||||
3409 | = querySingleNumber(KDbEscapedString("SELECT COUNT(*) FROM ") | ||||
3410 | + tableSchema.connection()->escapeIdentifier(tableSchema.name()), | ||||
3411 | &count); | ||||
3412 | if (~result) { | ||||
3413 | count = 0; | ||||
3414 | } | ||||
3415 | return count; | ||||
3416 | } | ||||
3417 | | ||||
3418 | int KDbConnection::recordCount(KDbQuerySchema* querySchema, const QList<QVariant>& params) | ||||
3419 | { | ||||
3420 | //! @todo does not work with non-SQL data sources | ||||
3421 | int count = -1; //will be changed only on success of querySingleNumber() | ||||
3422 | KDbNativeStatementBuilder builder(this); | ||||
3423 | KDbEscapedString subSql; | ||||
3424 | if (!builder.generateSelectStatement(&subSql, querySchema, params)) { | ||||
3425 | return -1; | ||||
3426 | } | ||||
3427 | const tristate result = querySingleNumber( | ||||
3428 | KDbEscapedString("SELECT COUNT(*) FROM (") + subSql + ") AS kdb__subquery", &count); | ||||
3429 | if (~result) { | ||||
3430 | count = 0; | ||||
3431 | } | ||||
3432 | return count; | ||||
3433 | } | ||||
3434 | | ||||
3435 | int KDbConnection::recordCount(KDbTableOrQuerySchema* tableOrQuery, const QList<QVariant>& params) | ||||
3436 | { | ||||
3437 | if (tableOrQuery) { | ||||
3438 | if (tableOrQuery->table()) | ||||
3439 | return recordCount(*tableOrQuery->table()); | ||||
3440 | if (tableOrQuery->query()) | ||||
3441 | return recordCount(tableOrQuery->query(), params); | ||||
3442 | } | ||||
3443 | return -1; | ||||
3444 | } | ||||
3445 | | ||||
3379 | KDbConnectionOptions* KDbConnection::options() | 3446 | KDbConnectionOptions* KDbConnection::options() | ||
3380 | { | 3447 | { | ||
3381 | return &d->options; | 3448 | return &d->options; | ||
3382 | } | 3449 | } | ||
3383 | 3450 | | |||
3384 | void KDbConnection::addCursor(KDbCursor* cursor) | 3451 | void KDbConnection::addCursor(KDbCursor* cursor) | ||
3385 | { | 3452 | { | ||
3386 | d->cursors.insert(cursor); | 3453 | d->cursors.insert(cursor); | ||
Show All 34 Lines |