kexidbcursor.cpp
1/***************************************************************************
2 * kexidbcursor.cpp
3 * This file is part of the KDE project
4 * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this program; see the file COPYING. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 ***************************************************************************/
19
20#include "kexidbcursor.h"
21#include "kexidbconnection.h"
22
23#include <kexidb/tableschema.h>
24#include <kexidb/queryschema.h>
25
26#include <kdebug.h>
27
28using namespace Kross::KexiDB;
29
30KexiDBCursor::KexiDBCursor(::KexiDB::Cursor* cursor)
31 : Kross::Api::Class<KexiDBCursor>("KexiDBCursor")
32 , m_cursor(cursor)
33{
34 this->addFunction0<Kross::Api::Variant>("open", this, &KexiDBCursor::open );
35 this->addFunction0<Kross::Api::Variant>("isOpened", this, &KexiDBCursor::isOpened );
36 this->addFunction0<Kross::Api::Variant>("reopen", this, &KexiDBCursor::reopen );
37 this->addFunction0<Kross::Api::Variant>("close", this, &KexiDBCursor::close );
38 this->addFunction0<Kross::Api::Variant>("moveFirst", this, &KexiDBCursor::moveFirst );
39 this->addFunction0<Kross::Api::Variant>("moveLast", this, &KexiDBCursor::moveLast );
40 this->addFunction0<Kross::Api::Variant>("movePrev", this, &KexiDBCursor::movePrev );
41 this->addFunction0<Kross::Api::Variant>("moveNext", this, &KexiDBCursor::moveNext );
42 this->addFunction0<Kross::Api::Variant>("bof", this, &KexiDBCursor::bof );
43 this->addFunction0<Kross::Api::Variant>("eof", this, &KexiDBCursor::eof );
44 this->addFunction0<Kross::Api::Variant>("at", this, &KexiDBCursor::at );
45 this->addFunction0<Kross::Api::Variant>("fieldCount", this, &KexiDBCursor::fieldCount );
46 this->addFunction1<Kross::Api::Variant, Kross::Api::Variant>("value", this, &KexiDBCursor::value );
47 this->addFunction2<Kross::Api::Variant, Kross::Api::Variant, Kross::Api::Variant>("setValue", this, &KexiDBCursor::setValue );
48 this->addFunction0<Kross::Api::Variant>("save", this, &KexiDBCursor::save );
49}
50
52{
54 //delete m_cursor;
55
56 clearBuffers();
57}
58
59void KexiDBCursor::clearBuffers()
60{
61 TQMap<TQ_LLONG, Record*>::ConstIterator
62 it( m_modifiedrecords.constBegin() ), end( m_modifiedrecords.constEnd() );
63 for( ; it != end; ++it)
64 delete it.data();
65 m_modifiedrecords.clear();
66}
67
68const TQString KexiDBCursor::getClassName() const
69{
70 return "Kross::KexiDB::KexiDBCursor";
71}
72
73bool KexiDBCursor::open() { return m_cursor->open(); }
74bool KexiDBCursor::isOpened() { return m_cursor->isOpened(); }
75bool KexiDBCursor::reopen() { return m_cursor->reopen(); }
76bool KexiDBCursor::close() { return m_cursor->close(); }
77
78bool KexiDBCursor::moveFirst() { return m_cursor->moveFirst(); }
79bool KexiDBCursor::moveLast() { return m_cursor->moveLast(); }
80bool KexiDBCursor::movePrev() { return m_cursor->movePrev(); }
81bool KexiDBCursor::moveNext() { return m_cursor->moveNext(); }
82
83bool KexiDBCursor::bof() { return m_cursor->bof(); }
84bool KexiDBCursor::eof() { return m_cursor->eof(); }
85
86TQ_LLONG KexiDBCursor::at() { return m_cursor->at(); }
87uint KexiDBCursor::fieldCount() { return m_cursor->fieldCount(); }
88
89TQVariant KexiDBCursor::value(uint index)
90{
91 return m_cursor->value(index);
92}
93
94bool KexiDBCursor::setValue(uint index, TQVariant value)
95{
96 ::KexiDB::QuerySchema* query = m_cursor->query();
97 if(! query) {
98 kdDebug() << "Invalid query in KexiDBCursor::setValue index=" << index << " value=" << value << endl;
99 return false;
100 }
101
102 ::KexiDB::QueryColumnInfo* column = query->fieldsExpanded().at(index);
103 if(! column) {
104 kdDebug() << "Invalid column in KexiDBCursor::setValue index=" << index << " value=" << value << endl;
105 return false;
106 }
107
108 const TQ_LLONG position = m_cursor->at();
109 if(! m_modifiedrecords.contains(position))
110 m_modifiedrecords.replace(position, new Record(m_cursor));
111 m_modifiedrecords[position]->buffer->insert(*column, value);
112 return true;
113}
114
115bool KexiDBCursor::save()
116{
117 if(m_modifiedrecords.count() < 1)
118 return true;
119
120 //It is needed to close the cursor before we are able to update the rows
121 //since else the database could be locked (e.g. at the case of SQLite a
122 //KexiDB: Object ERROR: 6: SQLITE_LOCKED would prevent updating).
123 //Maybe it works fine with other drivers like MySQL or Postqre?
124 m_cursor->close();
125
126 bool ok = true;
127 TQMap<TQ_LLONG, Record*>::ConstIterator
128 it( m_modifiedrecords.constBegin() ), end( m_modifiedrecords.constEnd() );
129 for( ; it != end; ++it) {
130 bool b = m_cursor->updateRow(it.data()->rowdata, * it.data()->buffer, m_cursor->isBuffered());
131 if(ok) {
132 ok = b;
133 //break;
134 }
135 }
136 //m_cursor->close();
137 clearBuffers();
138 return ok;
139}
Provides database cursor functionality.
Definition cursor.h:67
virtual bool moveLast()
Definition cursor.cpp:254
virtual bool movePrev()
Definition cursor.cpp:303
virtual bool moveNext()
Definition cursor.cpp:292
bool isOpened() const
Definition cursor.h:121
bool isBuffered() const
Definition cursor.cpp:356
TQ_LLONG at() const
Definition cursor.cpp:349
bool updateRow(RowData &data, RowEditBuffer &buf, bool useROWID=false)
Definition cursor.cpp:461
virtual bool close()
Definition cursor.cpp:178
virtual TQVariant value(uint i)=0
bool bof() const
Definition cursor.cpp:344
bool eof() const
Definition cursor.cpp:339
uint fieldCount() const
Definition cursor.h:165
bool moveFirst()
Definition cursor.cpp:205
bool reopen()
Definition cursor.cpp:198
QuerySchema * query() const
Definition cursor.h:104
Helper class that assigns additional information for the column in a query.
Definition queryschema.h:49
KexiDB::QuerySchema provides information about database query.
QueryColumnInfo::Vector fieldsExpanded(FieldsExpandedOptions options=Default)
The cursor provides a control structure for the successive traversal of records in a result set as re...
KrossKexiDB provides access to the KexiDB database functionality.