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         TableSchema *table = m_fields->fieldCount()>0 ? m_fields->field(0)->table() : 0;
00086         if (!table)
00087             return ""; //err
00088 
00089         QCString namesList;
00090         bool first = true;
00091         const bool allTableFieldsUsed = dynamic_cast<TableSchema*>(m_fields); //we are using a selection of fields only
00092         Field::ListIterator it = m_fields->fieldsIterator();
00093         for (uint i=0; i<m_fields->fieldCount(); i++, ++it) {
00094             if (first) {
00095                 s.append( "?" );
00096                 if (!allTableFieldsUsed)
00097                     namesList = it.current()->name().latin1();
00098                 first = false;
00099             } else {
00100                 s.append( ",?" );
00101                 if (!allTableFieldsUsed)
00102                     namesList.append(QCString(", ")+it.current()->name().latin1());
00103             }
00104         }
00105         s.append(")");
00106         s.prepend(QCString("INSERT INTO ") + table->name().latin1()
00107             + (allTableFieldsUsed ? "" : (" (" + namesList + ")"))
00108             + " VALUES (");
00109     }
00110     return s;
00111 }
00112 
00113 PreparedStatement& PreparedStatement::operator<< ( const QVariant& value )
00114 {
00115     m_args.append(value);
00116     return *this;
00117 }
00118 
00119 /*bool PreparedStatement::insert()
00120 {
00121     const bool res = m_conn->drv_prepareStatement(this);
00122     const bool res = m_conn->drv_insertRecord(this);
00123     clearArguments();
00124     return res;
00125 }
00126 
00127 bool PreparedStatement::select()
00128 {
00129     const bool res = m_conn->drv_bindArgumentForPreparedStatement(this, m_args.count()-1);
00130 }*/
00131 
00132 void PreparedStatement::clearArguments()
00133 {
00134     m_args.clear();
00135 }
00136 
KDE Home | KDE Accessibility Home | Description of Access Keys