?? qsql_odbc.cpp
字號:
// do not query for SQL_API_SQLFETCHSCROLL because it can't be used at this time static const SQLUSMALLINT reqFunc[] = { SQL_API_SQLDESCRIBECOL, SQL_API_SQLGETDATA, SQL_API_SQLCOLUMNS, SQL_API_SQLGETSTMTATTR, SQL_API_SQLGETDIAGREC, SQL_API_SQLEXECDIRECT, SQL_API_SQLGETINFO, SQL_API_SQLTABLES, 0 }; // these functions are optional static const SQLUSMALLINT optFunc[] = { SQL_API_SQLNUMRESULTCOLS, SQL_API_SQLROWCOUNT, 0 }; SQLRETURN r; SQLUSMALLINT sup; int i; // check the required functions for (i = 0; reqFunc[i] != 0; ++i) { r = SQLGetFunctions(hDbc, reqFunc[i], &sup); if (r != SQL_SUCCESS) { qSqlWarning(QLatin1String("QODBCDriver::checkDriver: Cannot get list of supported functions"), this); return false; } if (sup == SQL_FALSE) { qWarning ("QODBCDriver::open: Warning - Driver doesn't support all needed functionality (%d). " "Please look at the Qt SQL Module Driver documentation for more information.", reqFunc[i]); return false; } } // these functions are optional and just generate a warning for (i = 0; optFunc[i] != 0; ++i) { r = SQLGetFunctions(hDbc, optFunc[i], &sup); if (r != SQL_SUCCESS) { qSqlWarning(QLatin1String("QODBCDriver::checkDriver: Cannot get list of supported functions"), this); return false; } if (sup == SQL_FALSE) { qWarning("QODBCDriver::checkDriver: Warning - Driver doesn't support some non-critical functions (%d)", optFunc[i]); return true; } }#endif //ODBC_CHECK_DRIVER return true;}void QODBCDriverPrivate::checkSchemaUsage(){ SQLRETURN r; SQLUINTEGER val; r = SQLGetInfo(hDbc, SQL_SCHEMA_USAGE, (SQLPOINTER) &val, sizeof(val), NULL); if (r == SQL_SUCCESS || r == SQL_SUCCESS_WITH_INFO) useSchema = (val != 0);}QSqlResult *QODBCDriver::createResult() const{ return new QODBCResult(this, d);}bool QODBCDriver::beginTransaction(){ if (!isOpen()) { qWarning(" QODBCDriver::beginTransaction: Database not open"); return false; } SQLUINTEGER ac(SQL_AUTOCOMMIT_OFF); SQLRETURN r = SQLSetConnectAttr(d->hDbc, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER)ac, sizeof(ac)); if (r != SQL_SUCCESS) { setLastError(qMakeError(tr("Unable to disable autocommit"), QSqlError::TransactionError, d)); return false; } return true;}bool QODBCDriver::commitTransaction(){ if (!isOpen()) { qWarning(" QODBCDriver::commitTransaction: Database not open"); return false; } SQLRETURN r = SQLEndTran(SQL_HANDLE_DBC, d->hDbc, SQL_COMMIT); if (r != SQL_SUCCESS) { setLastError(qMakeError(tr("Unable to commit transaction"), QSqlError::TransactionError, d)); return false; } return endTrans();}bool QODBCDriver::rollbackTransaction(){ if (!isOpen()) { qWarning(" QODBCDriver::rollbackTransaction: Database not open"); return false; } SQLRETURN r = SQLEndTran(SQL_HANDLE_DBC, d->hDbc, SQL_ROLLBACK); if (r != SQL_SUCCESS) { setLastError(qMakeError(tr("Unable to rollback transaction"), QSqlError::TransactionError, d)); return false; } return endTrans();}bool QODBCDriver::endTrans(){ SQLUINTEGER ac(SQL_AUTOCOMMIT_ON); SQLRETURN r = SQLSetConnectAttr(d->hDbc, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER)ac, sizeof(ac)); if (r != SQL_SUCCESS) { setLastError(qMakeError(tr("Unable to enable autocommit"), QSqlError::TransactionError, d)); return false; } return true;}QStringList QODBCDriver::tables(QSql::TableType type) const{ QStringList tl; if (!isOpen()) return tl; SQLHANDLE hStmt; SQLRETURN r = SQLAllocHandle(SQL_HANDLE_STMT, d->hDbc, &hStmt); if (r != SQL_SUCCESS) { qSqlWarning(QLatin1String("QODBCDriver::tables: Unable to allocate handle"), d); return tl; } r = SQLSetStmtAttr(hStmt, SQL_ATTR_CURSOR_TYPE, (SQLPOINTER)SQL_CURSOR_FORWARD_ONLY, SQL_IS_UINTEGER); QString tableType; if (type & QSql::Tables) tableType += QLatin1String("TABLE,"); if (type & QSql::Views) tableType += QLatin1String("VIEW,"); if (type & QSql::SystemTables) tableType += QLatin1String("SYSTEM TABLE,"); if (tableType.isEmpty()) return tl; tableType.chop(1); r = SQLTables(hStmt, NULL, 0, NULL, 0, NULL, 0,#ifdef UNICODE (SQLWCHAR*)tableType.unicode(),#else (SQLCHAR*)tableType.toLatin1().constData(),#endif tableType.length() /* characters, not bytes */); if (r != SQL_SUCCESS) qSqlWarning(QLatin1String("QODBCDriver::tables Unable to execute table list"), d); r = SQLFetchScroll(hStmt, SQL_FETCH_NEXT, 0); while (r == SQL_SUCCESS) { QString fieldVal = qGetStringData(hStmt, 2, -1, d->unicode); tl.append(fieldVal); r = SQLFetchScroll(hStmt, SQL_FETCH_NEXT, 0); } r = SQLFreeHandle(SQL_HANDLE_STMT, hStmt); if (r!= SQL_SUCCESS) qSqlWarning(QLatin1String("QODBCDriver: Unable to free statement handle") + QString::number(r), d); return tl;}QSqlIndex QODBCDriver::primaryIndex(const QString& tablename) const{ QSqlIndex index(tablename); if (!isOpen()) return index; bool usingSpecialColumns = false; QSqlRecord rec = record(tablename); SQLHANDLE hStmt; SQLRETURN r = SQLAllocHandle(SQL_HANDLE_STMT, d->hDbc, &hStmt); if (r != SQL_SUCCESS) { qSqlWarning(QLatin1String("QODBCDriver::primaryIndex: Unable to list primary key"), d); return index; } QString catalog, schema, table; d->splitTableQualifier(tablename, catalog, schema, table); r = SQLSetStmtAttr(hStmt, SQL_ATTR_CURSOR_TYPE, (SQLPOINTER)SQL_CURSOR_FORWARD_ONLY, SQL_IS_UINTEGER); r = SQLPrimaryKeys(hStmt,#ifdef UNICODE catalog.length() == 0 ? NULL : (SQLWCHAR*)catalog.unicode(),#else catalog.length() == 0 ? NULL : (SQLCHAR*)catalog.toLatin1().constData(),#endif catalog.length(),#ifdef UNICODE schema.length() == 0 ? NULL : (SQLWCHAR*)schema.unicode(),#else schema.length() == 0 ? NULL : (SQLCHAR*)schema.toLatin1().constData(),#endif schema.length(),#ifdef UNICODE (SQLWCHAR*)table.unicode(),#else (SQLCHAR*)table.toLatin1().constData(),#endif table.length() /* in characters, not in bytes */); // if the SQLPrimaryKeys() call does not succeed (e.g the driver // does not support it) - try an alternative method to get hold of // the primary index (e.g MS Access and FoxPro) if (r != SQL_SUCCESS) { r = SQLSpecialColumns(hStmt, SQL_BEST_ROWID,#ifdef UNICODE catalog.length() == 0 ? NULL : (SQLWCHAR*)catalog.unicode(),#else catalog.length() == 0 ? NULL : (SQLCHAR*)catalog.toLatin1().constData(),#endif catalog.length(),#ifdef UNICODE schema.length() == 0 ? NULL : (SQLWCHAR*)schema.unicode(),#else schema.length() == 0 ? NULL : (SQLCHAR*)schema.toLatin1().constData(),#endif schema.length(),#ifdef UNICODE (SQLWCHAR*)table.unicode(),#else (SQLCHAR*)table.toLatin1().constData(),#endif table.length(), SQL_SCOPE_CURROW, SQL_NULLABLE); if (r != SQL_SUCCESS) { qSqlWarning(QLatin1String("QODBCDriver::primaryIndex: Unable to execute primary key list"), d); } else { usingSpecialColumns = true; } } r = SQLFetchScroll(hStmt, SQL_FETCH_NEXT, 0); int fakeId = 0; QString cName, idxName; // Store all fields in a StringList because some drivers can't detail fields in this FETCH loop while (r == SQL_SUCCESS) { if (usingSpecialColumns) { cName = qGetStringData(hStmt, 1, -1, d->unicode); // column name idxName = QString::number(fakeId++); // invent a fake index name } else { cName = qGetStringData(hStmt, 3, -1, d->unicode); // column name idxName = qGetStringData(hStmt, 5, -1, d->unicode); // pk index name } index.append(rec.field(cName)); index.setName(idxName); r = SQLFetchScroll(hStmt, SQL_FETCH_NEXT, 0); } r = SQLFreeHandle(SQL_HANDLE_STMT, hStmt); if (r!= SQL_SUCCESS) qSqlWarning(QLatin1String("QODBCDriver: Unable to free statement handle") + QString::number(r), d); return index;}QSqlRecord QODBCDriver::record(const QString& tablename) const{ QSqlRecord fil; if (!isOpen()) return fil; SQLHANDLE hStmt; QString catalog, schema, table; d->splitTableQualifier(tablename, catalog, schema, table); SQLRETURN r = SQLAllocHandle(SQL_HANDLE_STMT, d->hDbc, &hStmt); if (r != SQL_SUCCESS) { qSqlWarning(QLatin1String("QODBCDriver::record: Unable to allocate handle"), d); return fil; } r = SQLSetStmtAttr(hStmt, SQL_ATTR_CURSOR_TYPE, (SQLPOINTER)SQL_CURSOR_FORWARD_ONLY, SQL_IS_UINTEGER); r = SQLColumns(hStmt,#ifdef UNICODE catalog.length() == 0 ? NULL : (SQLWCHAR*)catalog.unicode(),#else catalog.length() == 0 ? NULL : (SQLCHAR*)catalog.toLatin1().constData(),#endif catalog.length(),#ifdef UNICODE schema.length() == 0 ? NULL : (SQLWCHAR*)schema.unicode(),#else schema.length() == 0 ? NULL : (SQLCHAR*)schema.toLatin1().constData(),#endif schema.length(),#ifdef UNICODE (SQLWCHAR*)table.unicode(),#else (SQLCHAR*)table.toLatin1().constData(),#endif table.length(), NULL, 0); if (r != SQL_SUCCESS) qSqlWarning(QLatin1String("QODBCDriver::record: Unable to execute column list"), d); r = SQLFetchScroll(hStmt, SQL_FETCH_NEXT, 0); // Store all fields in a StringList because some drivers can't detail fields in this FETCH loop while (r == SQL_SUCCESS) { fil.append(qMakeFieldInfo(hStmt, d)); r = SQLFetchScroll(hStmt, SQL_FETCH_NEXT, 0); } r = SQLFreeHandle(SQL_HANDLE_STMT, hStmt); if (r!= SQL_SUCCESS) qSqlWarning(QLatin1String("QODBCDriver: Unable to free statement handle ") + QString::number(r), d); return fil;}QString QODBCDriver::formatValue(const QSqlField &field, bool trimStrings) const{ QString r; if (field.isNull()) { r = QLatin1String("NULL"); } else if (field.type() == QVariant::DateTime) { // Use an escape sequence for the datetime fields if (field.value().toDateTime().isValid()){ QDate dt = field.value().toDateTime().date(); QTime tm = field.value().toDateTime().time(); // Dateformat has to be "yyyy-MM-dd hh:mm:ss", with leading zeroes if month or day < 10 r = QLatin1String("{ ts '") + QString::number(dt.year()) + QLatin1Char('-') + QString::number(dt.month()).rightJustified(2, QLati
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -