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 && 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
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
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121 void PreparedStatement::clearArguments()
00122 {
00123 m_args.clear();
00124 }
00125
|