⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qsql_ibase.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 4 页
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtSql module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file.  Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qsql_ibase.h"#include <qcoreapplication.h>#include <qdatetime.h>#include <qvariant.h>#include <qsqlerror.h>#include <qsqlfield.h>#include <qsqlindex.h>#include <qsqlquery.h>#include <qstringlist.h>#include <qlist.h>#include <qvector.h>#include <qtextcodec.h>#include <stdlib.h>#include <limits.h>#include <math.h>    #define FBVERSION SQL_DIALECT_V6#ifndef SQLDA_CURRENT_VERSION#define SQLDA_CURRENT_VERSION SQLDA_VERSION1#endifenum { QIBaseChunkSize = SHRT_MAX / 2 };static bool getIBaseError(QString& msg, ISC_STATUS* status, ISC_LONG &sqlcode,                          QTextCodec *tc){    if (status[0] != 1 || status[1] <= 0)        return false;    sqlcode = isc_sqlcode(status);    char buf[512];    isc_sql_interprete(sqlcode, buf, 512);    if (tc)        msg = tc->toUnicode(buf);    else        msg = QString::fromUtf8(buf);    return true;}static void createDA(XSQLDA *&sqlda){    sqlda = (XSQLDA *) malloc(XSQLDA_LENGTH(1));    sqlda->sqln = 1;    sqlda->sqld = 0;    sqlda->version = SQLDA_CURRENT_VERSION;    sqlda->sqlvar[0].sqlind = 0;    sqlda->sqlvar[0].sqldata = 0;}static void enlargeDA(XSQLDA *&sqlda, int n){    free(sqlda);    sqlda = (XSQLDA *) malloc(XSQLDA_LENGTH(n));    sqlda->sqln = n;    sqlda->version = SQLDA_CURRENT_VERSION;}static void initDA(XSQLDA *sqlda){    for (int i = 0; i < sqlda->sqld; ++i) {        switch (sqlda->sqlvar[i].sqltype & ~1) {        case SQL_INT64:        case SQL_LONG:        case SQL_SHORT:        case SQL_FLOAT:        case SQL_DOUBLE:        case SQL_TIMESTAMP:        case SQL_TYPE_TIME:        case SQL_TYPE_DATE:        case SQL_TEXT:        case SQL_BLOB:            sqlda->sqlvar[i].sqldata = (char*)malloc(sqlda->sqlvar[i].sqllen);            break;        case SQL_ARRAY:            sqlda->sqlvar[i].sqldata = (char*)malloc(sizeof(ISC_QUAD));            memset(sqlda->sqlvar[i].sqldata, 0, sizeof(ISC_QUAD));            break;        case SQL_VARYING:            sqlda->sqlvar[i].sqldata = (char*)malloc(sqlda->sqlvar[i].sqllen + sizeof(short));            break;        default:            // not supported - do not bind.            sqlda->sqlvar[i].sqldata = 0;            break;        }        if (sqlda->sqlvar[i].sqltype & 1) {            sqlda->sqlvar[i].sqlind = (short*)malloc(sizeof(short));            *(sqlda->sqlvar[i].sqlind) = 0;        } else {            sqlda->sqlvar[i].sqlind = 0;        }    }}static void delDA(XSQLDA *&sqlda){    if (!sqlda)        return;    for (int i = 0; i < sqlda->sqld; ++i) {        free(sqlda->sqlvar[i].sqlind);        free(sqlda->sqlvar[i].sqldata);    }    free(sqlda);    sqlda = 0;}static QVariant::Type qIBaseTypeName(int iType, bool hasScale){    switch (iType) {    case blr_varying:    case blr_varying2:    case blr_text:    case blr_cstring:    case blr_cstring2:        return QVariant::String;    case blr_sql_time:        return QVariant::Time;    case blr_sql_date:        return QVariant::Date;    case blr_timestamp:        return QVariant::DateTime;    case blr_blob:        return QVariant::ByteArray;    case blr_quad:    case blr_short:    case blr_long:        return (hasScale ? QVariant::Double : QVariant::Int);    case blr_int64:        return (hasScale ? QVariant::Double : QVariant::LongLong);    case blr_float:    case blr_d_float:    case blr_double:        return QVariant::Double;    }    qWarning("qIBaseTypeName: unknown datatype: %d", iType);    return QVariant::Invalid;}static QVariant::Type qIBaseTypeName2(int iType, bool hasScale){    switch(iType & ~1) {    case SQL_VARYING:    case SQL_TEXT:        return QVariant::String;    case SQL_LONG:    case SQL_SHORT:        return (hasScale ? QVariant::Double : QVariant::Int);    case SQL_INT64:        return (hasScale ? QVariant::Double : QVariant::LongLong);    case SQL_FLOAT:    case SQL_DOUBLE:        return QVariant::Double;    case SQL_TIMESTAMP:        return QVariant::DateTime;    case SQL_TYPE_TIME:        return QVariant::Time;    case SQL_TYPE_DATE:        return QVariant::Date;    case SQL_ARRAY:        return QVariant::List;    case SQL_BLOB:        return QVariant::ByteArray;    default:        return QVariant::Invalid;    }}static ISC_TIMESTAMP toTimeStamp(const QDateTime &dt){    static const QTime midnight(0, 0, 0, 0);    static const QDate basedate(1858, 11, 17);    ISC_TIMESTAMP ts;    ts.timestamp_time = midnight.msecsTo(dt.time()) * 10;    ts.timestamp_date = basedate.daysTo(dt.date());    return ts;}static QDateTime fromTimeStamp(char *buffer){    static const QDate bd(1858, 11, 17);    QTime t;    QDate d;    // have to demangle the structure ourselves because isc_decode_time    // strips the msecs    t = t.addMSecs(int(((ISC_TIMESTAMP*)buffer)->timestamp_time / 10));    d = bd.addDays(int(((ISC_TIMESTAMP*)buffer)->timestamp_date));    return QDateTime(d, t);}static ISC_TIME toTime(const QTime &t){    static const QTime midnight(0, 0, 0, 0);    return (ISC_TIME)midnight.msecsTo(t) * 10;}static QTime fromTime(char *buffer){    QTime t;    // have to demangle the structure ourselves because isc_decode_time    // strips the msecs    t = t.addMSecs(int((*(ISC_TIME*)buffer) / 10));    return t;}static ISC_DATE toDate(const QDate &t){    static const QDate basedate(1858, 11, 17);    ISC_DATE date;    date = basedate.daysTo(t);    return date;}static QDate fromDate(char *buffer){    static const QDate bd(1858, 11, 17);    QDate d;    // have to demangle the structure ourselves because isc_decode_time    // strips the msecs    d = bd.addDays(int(((ISC_TIMESTAMP*)buffer)->timestamp_date));    return d;}static QByteArray encodeString(QTextCodec *tc, const QString &str){    if (tc)        return tc->fromUnicode(str);    return str.toUtf8();}class QIBaseDriverPrivate{public:    QIBaseDriverPrivate(QIBaseDriver *d) : q(d), ibase(0), trans(0), tc(0) {}    bool isError(const char *msg, QSqlError::ErrorType typ = QSqlError::UnknownError)    {        QString imsg;        ISC_LONG sqlcode;        if (!getIBaseError(imsg, status, sqlcode, tc))            return false;        //qDebug() << "ERROR" << msg << imsg << typ;        q->setLastError(QSqlError(QCoreApplication::translate("QIBaseDriver", msg),                        imsg, typ, int(sqlcode)));        return true;    }public:    QIBaseDriver* q;    isc_db_handle ibase;    isc_tr_handle trans;    QTextCodec *tc;    ISC_STATUS status[20];};class QIBaseResultPrivate{public:    QIBaseResultPrivate(QIBaseResult *d, const QIBaseDriver *ddb);    ~QIBaseResultPrivate() { cleanup(); }    void cleanup();    bool isError(const char *msg, QSqlError::ErrorType typ = QSqlError::UnknownError)    {        QString imsg;        ISC_LONG sqlcode;        if (!getIBaseError(imsg, status, sqlcode, tc))            return false;        q->setLastError(QSqlError(QCoreApplication::translate("QIBaseResult", msg),                        imsg, typ, int(sqlcode)));        return true;    }    bool transaction();    bool commit();    bool isSelect();    QVariant fetchBlob(ISC_QUAD *bId);    bool writeBlob(int i, const QByteArray &ba);    QVariant fetchArray(int pos, ISC_QUAD *arr);    bool writeArray(int i, const QList<QVariant> &list);public:    QIBaseResult *q;    const QIBaseDriver *db;    ISC_STATUS status[20];    isc_tr_handle trans;    //indicator whether we have a local transaction or a transaction on driver level    bool localTransaction;    isc_stmt_handle stmt;    isc_db_handle ibase;    XSQLDA *sqlda; // output sqlda    XSQLDA *inda; // input parameters    int queryType;    QTextCodec *tc;};QIBaseResultPrivate::QIBaseResultPrivate(QIBaseResult *d, const QIBaseDriver *ddb):    q(d), db(ddb), trans(0), stmt(0), ibase(ddb->d->ibase), sqlda(0), inda(0), queryType(-1), tc(ddb->d->tc){    localTransaction = (ddb->d->ibase == 0);}void QIBaseResultPrivate::cleanup(){    commit();    if (!localTransaction)        trans = 0;    if (stmt) {        isc_dsql_free_statement(status, &stmt, DSQL_drop);        stmt = 0;    }    delDA(sqlda);    delDA(inda);    queryType = -1;    q->cleanup();}bool QIBaseResultPrivate::writeBlob(int i, const QByteArray &ba){    isc_blob_handle handle = 0;    ISC_QUAD *bId = (ISC_QUAD*)inda->sqlvar[i].sqldata;    isc_create_blob2(status, &ibase, &trans, &handle, bId, 0, 0);    if (!isError(QT_TRANSLATE_NOOP("QIBaseResult", "Unable to create BLOB"),                 QSqlError::StatementError)) {        int i = 0;        while (i < ba.size()) {            isc_put_segment(status, &handle, qMin(ba.size() - i, int(QIBaseChunkSize)),                            const_cast<char*>(ba.data()) + i);            if (isError(QT_TRANSLATE_NOOP("QIBaseResult", "Unable to write BLOB")))                return false;            i += qMin(ba.size() - i, int(QIBaseChunkSize));        }    }    isc_close_blob(status, &handle);    return true;}QVariant QIBaseResultPrivate::fetchBlob(ISC_QUAD *bId){    isc_blob_handle handle = 0;    isc_open_blob2(status, &ibase, &trans, &handle, bId, 0, 0);    if (isError(QT_TRANSLATE_NOOP("QIBaseResult", "Unable to open BLOB"),

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -