kexi
preparedstatement.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
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
00063
00064 m_whereFields = new Field::List();
00065 for (QStringList::ConstIterator it=m_where->constBegin(); it!=m_where->constEnd(); ++it) {
00066
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 ) {
00084
00085 TableSchema *table = m_fields->fieldCount()>0 ? m_fields->field(0)->table() : 0;
00086 if (!table)
00087 return "";
00088
00089 QCString namesList;
00090 bool first = true;
00091 const bool allTableFieldsUsed = dynamic_cast<TableSchema*>(m_fields);
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
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132 void PreparedStatement::clearArguments()
00133 {
00134 m_args.clear();
00135 }
00136
|