summaryrefslogtreecommitdiffstats
path: root/kexi/kexidb/preparedstatement.cpp
blob: f1ab679c286141bcbba1032b6e0753d9112f8a9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* This file is part of the KDE project
   Copyright (C) 2005 Jaroslaw Staniek <js@iidea.pl>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include "preparedstatement.h"

#include <kexidb/connection.h>
#include <kexidb/connection_p.h>
#include <kdebug.h>

using namespace KexiDB;

PreparedStatement::PreparedStatement(StatementType type, ConnectionInternal& conn, 
	FieldList& fields, const TQStringList& where)
 : TDEShared()
 , m_type(type)
 , m_fields(&fields)
 , m_where(where.isEmpty() ? new TQStringList(where) : 0)
 , m_whereFields(0)
{
	Q_UNUSED(conn);
}

PreparedStatement::~PreparedStatement()
{
	delete m_where;
	delete m_whereFields;
}

TQCString PreparedStatement::generateStatementString()
{
	TQCString s(1024);
	if (m_type == SelectStatement) {
//! @todo only tables and trivial queries supported for select...
		s = "SELECT ";
		bool first = true;
//		for (uint i=0; i<m_fields->fieldCount(); i++) {
		for (Field::ListIterator it(m_fields->fieldsIterator()); it.current(); ++it) {
			if (first)
				first = false;
			else
				s.append(", ");
			s.append(it.current()->name().latin1());
		}
		first = true;
		s.append(" WHERE ");
//		for (uint i=0; i<m_fields->fieldCount(); i++) {

		m_whereFields = new Field::List();
		for (TQStringList::ConstIterator it=m_where->constBegin(); it!=m_where->constEnd(); ++it) {
//		for (Field::ListIterator it(m_fields->fieldsIterator()); it.current(); ++it) {
			if (first)
				first = false;
			else
				s.append(" AND ");
			Field *f = m_fields->field(*it);
			if (!f) {
				KexiDBWarn << "PreparedStatement::generateStatementString(): no '" 
					<< *it << "' field found" << endl;
				continue;
			}
			m_whereFields->append(f);
			s.append((*it).latin1());
			s.append("=?");
		}
	}
	else if (m_type == InsertStatement /*&& dynamic_cast<TableSchema*>(m_fields)*/) {
//! @todo only tables supported for insert; what about views?
		
		TableSchema *table = m_fields->fieldCount()>0 ? m_fields->field(0)->table() : 0;
		if (!table)
			return ""; //err

		TQCString namesList;
		bool first = true;
		const bool allTableFieldsUsed = dynamic_cast<TableSchema*>(m_fields); //we are using a selection of fields only
		Field::ListIterator it = m_fields->fieldsIterator();
		for (uint i=0; i<m_fields->fieldCount(); i++, ++it) {
			if (first) {
				s.append( "?" );
				if (!allTableFieldsUsed)
					namesList = it.current()->name().latin1();
				first = false;
			} else {
				s.append( ",?" );
				if (!allTableFieldsUsed)
					namesList.append(TQCString(", ")+it.current()->name().latin1());
			}
		}
		s.append(")");
		s.prepend(TQCString("INSERT INTO ") + table->name().latin1()
			+ (allTableFieldsUsed ? TQCString() : (" (" + namesList + ")"))
			+ " VALUES (");
	}
	return s;
}

PreparedStatement& PreparedStatement::operator<< ( const TQVariant& value )
{
	m_args.append(value);
	return *this;
}

/*bool PreparedStatement::insert()
{
	const bool res = m_conn->drv_prepareStatement(this);
	const bool res = m_conn->drv_insertRecord(this);
	clearArguments();
	return res;
}

bool PreparedStatement::select()
{
	const bool res = m_conn->drv_bindArgumentForPreparedStatement(this, m_args.count()-1);
}*/

void PreparedStatement::clearArguments()
{
	m_args.clear();
}