kexi
mysqlcursor.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "mysqlcursor.h"
00022 #include "mysqlconnection.h"
00023 #include "mysqlconnection_p.h"
00024 #include <kexidb/error.h>
00025 #include <klocale.h>
00026 #include <kdebug.h>
00027 #include <limits.h>
00028
00029 #define BOOL bool
00030
00031 using namespace KexiDB;
00032
00033 MySqlCursor::MySqlCursor(KexiDB::Connection* conn, const QString& statement, uint cursor_options)
00034 : Cursor(conn,statement,cursor_options)
00035 , d( new MySqlCursorData(conn) )
00036 {
00037 m_options |= Buffered;
00038 d->mysql = static_cast<MySqlConnection*>(conn)->d->mysql;
00039
00040 }
00041
00042 MySqlCursor::MySqlCursor(Connection* conn, QuerySchema& query, uint options )
00043 : Cursor( conn, query, options )
00044 , d( new MySqlCursorData(conn) )
00045 {
00046 m_options |= Buffered;
00047 d->mysql = static_cast<MySqlConnection*>(conn)->d->mysql;
00048
00049 }
00050
00051 MySqlCursor::~MySqlCursor() {
00052 close();
00053 }
00054
00055 bool MySqlCursor::drv_open() {
00056
00057
00058
00059 if(mysql_real_query(d->mysql, m_sql.utf8(), strlen(m_sql.utf8())) == 0) {
00060 if(mysql_errno(d->mysql) == 0) {
00061 d->mysqlres= mysql_store_result(d->mysql);
00062 m_fieldCount=mysql_num_fields(d->mysqlres);
00063 d->numRows=mysql_num_rows(d->mysqlres);
00064 m_at=0;
00065
00066 m_opened=true;
00067 m_records_in_buf = d->numRows;
00068 m_buffering_completed = true;
00069 m_afterLast=false;
00070 return true;
00071 }
00072 }
00073
00074 setError(ERR_DB_SPECIFIC,QString::fromUtf8(mysql_error(d->mysql)));
00075 return false;
00076 }
00077
00078 bool MySqlCursor::drv_close() {
00079 mysql_free_result(d->mysqlres);
00080 d->mysqlres=0;
00081 d->mysqlrow=0;
00082
00083 d->lengths=0;
00084 m_opened=false;
00085 d->numRows=0;
00086 return true;
00087 }
00088
00089
00090
00091
00092
00093 void MySqlCursor::drv_getNextRecord() {
00094
00095 if (at() < d->numRows && at() >=0) {
00096 d->lengths=mysql_fetch_lengths(d->mysqlres);
00097 m_result=FetchOK;
00098 }
00099 else if (at() >= d->numRows) {
00100 m_result = FetchEnd;
00101 }
00102 else {
00103 m_result = FetchError;
00104 }
00105 }
00106
00107
00108 QVariant MySqlCursor::value(uint pos) {
00109 if (!d->mysqlrow || pos>=m_fieldCount || d->mysqlrow[pos]==0)
00110 return QVariant();
00111
00112 KexiDB::Field *f = (m_fieldsExpanded && pos<m_fieldsExpanded->count())
00113 ? m_fieldsExpanded->at(pos)->field : 0;
00114
00116
00117
00118 if (!f || f->isTextType())
00119 return QVariant( QString::fromUtf8((const char*)d->mysqlrow[pos]) );
00120 else if (f->isIntegerType())
00121 return QVariant( QCString((const char*)d->mysqlrow[pos]).toInt() );
00122 else if (f->isFPNumericType())
00123 return QVariant( QCString((const char*)d->mysqlrow[pos]).toDouble() );
00124
00125
00126 return QVariant(QString::fromUtf8((const char*)d->mysqlrow[pos]));
00127 }
00128
00129
00130
00131
00132
00133 void MySqlCursor::storeCurrentRow(RowData &data) const {
00134
00135 if (d->numRows<=0)
00136 return;
00137
00140
00141 data.resize(m_fieldCount);
00142 const uint fieldsExpandedCount = m_fieldsExpanded ? m_fieldsExpanded->count() : UINT_MAX;
00143 const uint realCount = QMIN(fieldsExpandedCount, m_fieldCount);
00144 for( uint i=0; i<realCount; i++) {
00145 Field *f = m_fieldsExpanded ? m_fieldsExpanded->at(i)->field : 0;
00146 if (m_fieldsExpanded && !f)
00147 continue;
00148 if (f && f->type()==Field::BLOB) {
00149 QByteArray ba;
00150 ba.duplicate(d->mysqlrow[i], d->mysqlres->lengths[i]);
00151 data[i] = ba;
00152 KexiDBDbg << data[i].toByteArray().size() << endl;
00153 }
00156 else {
00157 data[i] = QVariant(QString::fromUtf8((const char*)d->mysqlrow[i]));
00158 }
00159 }
00160 }
00161
00162 void MySqlCursor::drv_appendCurrentRecordToBuffer() {
00163 }
00164
00165
00166 void MySqlCursor::drv_bufferMovePointerNext() {
00167 d->mysqlrow=mysql_fetch_row(d->mysqlres);
00168 d->lengths=mysql_fetch_lengths(d->mysqlres);
00169 }
00170
00171 void MySqlCursor::drv_bufferMovePointerPrev() {
00172
00173 mysql_data_seek(d->mysqlres,m_at-1);
00174 d->mysqlrow=mysql_fetch_row(d->mysqlres);
00175 d->lengths=mysql_fetch_lengths(d->mysqlres);
00176 }
00177
00178
00179 void MySqlCursor::drv_bufferMovePointerTo(Q_LLONG to) {
00180
00181 mysql_data_seek(d->mysqlres, to);
00182 d->mysqlrow=mysql_fetch_row(d->mysqlres);
00183 d->lengths=mysql_fetch_lengths(d->mysqlres);
00184 }
00185
00186 const char** MySqlCursor::rowData() const {
00188 return 0;
00189 }
00190
00191 int MySqlCursor::serverResult()
00192 {
00193 return d->res;
00194 }
00195
00196 QString MySqlCursor::serverResultName()
00197 {
00198 return QString::null;
00199 }
00200
00201 void MySqlCursor::drv_clearServerResult()
00202 {
00203 if (!d)
00204 return;
00205 d->res = 0;
00206 }
00207
00208 QString MySqlCursor::serverErrorMsg()
00209 {
00210 return d->errmsg;
00211 }
|