kexi

preparedstatement.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2005 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "preparedstatement.h"
00021 
00022 #include <kexidb/connection.h>
00023 #include <kexidb/connection_p.h>
00024 #include <kdebug.h>
00025 
00026 using namespace KexiDB;
00027 
00028 PreparedStatement::PreparedStatement(StatementType type, ConnectionInternal& conn, 
00029     FieldList& fields, const QStringList& where)
00030  : KShared()
00031  , m_type(type)
00032  , m_fields(&fields)
00033  , m_where(where.isEmpty() ? new QStringList(where) : 0)
00034  , m_whereFields(0)
00035 {
00036     Q_UNUSED(conn);
00037 }
00038 
00039 PreparedStatement::~PreparedStatement()
00040 {
00041     delete m_where;
00042     delete m_whereFields;
00043 }
00044 
00045 QCString PreparedStatement::generateStatementString()
00046 {
00047     QCString s(1024);
00048     if (m_type == SelectStatement) {
00050         s = "SELECT ";
00051         bool first = true;
00052 //      for (uint i=0; i<m_fields->fieldCount(); i++) {
00053         for (Field::ListIterator it(m_fields->fieldsIterator()); it.current(); ++it) {
00054             if (first)
00055                 first = false;
00056             else
00057                 s.append(", ");
00058             s.append(it.current()->name().latin1());
00059         }
00060         first = true;
00061         s.append(" WHERE ");
00062 //      for (uint i=0; i<m_fields->fieldCount(); i++) {
00063 
00064         m_whereFields = new Field::List();
00065         for (QStringList::ConstIterator it=m_where->constBegin(); it!=m_where->constEnd(); ++it) {
00066 //      for (Field::ListIterator it(m_fields->fieldsIterator()); it.current(); ++it) {
00067             if (first)
00068                 first = false;
00069             else
00070                 s.append(" AND ");
00071             Field *f = m_fields->field(*it);
00072             if (!f) {
00073                 KexiDBWarn << "PreparedStatement::generateStatementString(): no '" 
00074                     << *it << "' field found" << endl;
00075                 continue;
00076             }
00077             m_whereFields->append(f);
00078             s.append((*it).latin1());
00079             s.append("=?");
00080         }
00081     }
00082     else if (m_type == InsertStatement && dynamic_cast<TableSchema*>(m_fields)) {
00084             
00085         s = QCString("INSERT INTO ")+dynamic_cast<TableSchema*>(m_fields)->name().latin1()
00086             +" VALUES (";
00087         bool first = true;
00088 //      for (Field::ListIterator it(m_fields->fieldsIterator()); it.current(); ++it) {
00089         for (uint i=0; i<m_fields->fieldCount(); i++) {
00090             if (first) {
00091                 s.append( "?" );
00092                 first = false;
00093             } else {
00094                 s.append( ",?" );
00095             }
00096         }
00097         s.append(")");
00098     }
00099     return s;
00100 }
00101 
00102 PreparedStatement& PreparedStatement::operator<< ( const QVariant& value )
00103 {
00104     m_args.append(value);
00105     return *this;
00106 }
00107 
00108 /*bool PreparedStatement::insert()
00109 {
00110     const bool res = m_conn->drv_prepareStatement(this);
00111     const bool res = m_conn->drv_insertRecord(this);
00112     clearArguments();
00113     return res;
00114 }
00115 
00116 bool PreparedStatement::select()
00117 {
00118     const bool res = m_conn->drv_bindArgumentForPreparedStatement(this, m_args.count()-1);
00119 }*/
00120 
00121 void PreparedStatement::clearArguments()
00122 {
00123     m_args.clear();
00124 }
00125 
KDE Home | KDE Accessibility Home | Description of Access Keys