kexi

preparedstatement.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2005 Jaroslaw Staniek <js@iidea.pl>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. 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 "preparedstatement.h"
21
22#include <kexidb/connection.h>
23#include <kexidb/connection_p.h>
24#include <kdebug.h>
25
26using namespace KexiDB;
27
29 FieldList& fields, const TQStringList& where)
30 : TDEShared()
31 , m_type(type)
32 , m_fields(&fields)
33 , m_where(where.isEmpty() ? new TQStringList(where) : 0)
34 , m_whereFields(0)
35{
36 Q_UNUSED(conn);
37}
38
39PreparedStatement::~PreparedStatement()
40{
41 delete m_where;
42 delete m_whereFields;
43}
44
46{
47 TQCString s(1024);
48 if (m_type == SelectStatement) {
50 s = "SELECT ";
51 bool first = true;
52// for (uint i=0; i<m_fields->fieldCount(); i++) {
53 for (Field::ListIterator it(m_fields->fieldsIterator()); it.current(); ++it) {
54 if (first)
55 first = false;
56 else
57 s.append(", ");
58 s.append(it.current()->name().latin1());
59 }
60 first = true;
61 s.append(" WHERE ");
62// for (uint i=0; i<m_fields->fieldCount(); i++) {
63
64 m_whereFields = new Field::List();
65 for (TQStringList::ConstIterator it=m_where->constBegin(); it!=m_where->constEnd(); ++it) {
66// for (Field::ListIterator it(m_fields->fieldsIterator()); it.current(); ++it) {
67 if (first)
68 first = false;
69 else
70 s.append(" AND ");
71 Field *f = m_fields->field(*it);
72 if (!f) {
73 KexiDBWarn << "PreparedStatement::generateStatementString(): no '"
74 << *it << "' field found" << endl;
75 continue;
76 }
77 m_whereFields->append(f);
78 s.append((*it).latin1());
79 s.append("=?");
80 }
81 }
82 else if (m_type == InsertStatement /*&& dynamic_cast<TableSchema*>(m_fields)*/) {
84
85 TableSchema *table = m_fields->fieldCount()>0 ? m_fields->field(0)->table() : 0;
86 if (!table)
87 return ""; //err
88
89 TQCString namesList;
90 bool first = true;
91 const bool allTableFieldsUsed = dynamic_cast<TableSchema*>(m_fields); //we are using a selection of fields only
92 Field::ListIterator it = m_fields->fieldsIterator();
93 for (uint i=0; i<m_fields->fieldCount(); i++, ++it) {
94 if (first) {
95 s.append( "?" );
96 if (!allTableFieldsUsed)
97 namesList = it.current()->name().latin1();
98 first = false;
99 } else {
100 s.append( ",?" );
101 if (!allTableFieldsUsed)
102 namesList.append(TQCString(", ")+it.current()->name().latin1());
103 }
104 }
105 s.append(")");
106 s.prepend(TQCString("INSERT INTO ") + table->name().latin1()
107 + (allTableFieldsUsed ? TQCString() : (" (" + namesList + ")"))
108 + " VALUES (");
109 }
110 return s;
111}
112
114{
115 m_args.append(value);
116 return *this;
117}
118
119/*bool PreparedStatement::insert()
120{
121 const bool res = m_conn->drv_prepareStatement(this);
122 const bool res = m_conn->drv_insertRecord(this);
123 clearArguments();
124 return res;
125}
126
127bool PreparedStatement::select()
128{
129 const bool res = m_conn->drv_bindArgumentForPreparedStatement(this, m_args.count()-1);
130}*/
131
133{
134 m_args.clear();
135}
136
uint fieldCount() const
Definition fieldlist.h:58
Meta-data for a field.
Definition field.h:73
TQPtrList< Field > List
list of fields
Definition field.h:75
TQPtrListIterator< Field > ListIterator
iterator for list of fields
Definition field.h:77
StatementType
Defines type of the prepared statement.
@ InsertStatement
INSERT statement will be prepared end executed.
@ SelectStatement
SELECT statement will be prepared end executed.
PreparedStatement(StatementType type, ConnectionInternal &conn, FieldList &fields, const TQStringList &where=TQStringList())
Creates Prepared statement. In your code use KexiDB::Connection:prepareStatement() instead.
void clearArguments()
Clears arguments of the prepared statement. Usually used after execute().
PreparedStatement & operator<<(const TQVariant &value)
Appends argument value to the statement.
High-level database connectivity library with database backend drivers.