Eneboo - Documentación para desarrolladores
|
00001 /*************************************************************************** 00002 FLSqlQuery.h 00003 ------------------- 00004 begin : sáb jun 22 2002 00005 copyright : (C) 2002-2005 by InfoSiAL S.L. 00006 email : mail@infosial.com 00007 ***************************************************************************/ 00008 /*************************************************************************** 00009 * This program is free software; you can redistribute it and/or modify * 00010 * it under the terms of the GNU General Public License as published by * 00011 * the Free Software Foundation; version 2 of the License. * 00012 ***************************************************************************/ 00013 /*************************************************************************** 00014 Este programa es software libre. Puede redistribuirlo y/o modificarlo 00015 bajo los términos de la Licencia Pública General de GNU en su 00016 versión 2, publicada por la Free Software Foundation. 00017 ***************************************************************************/ 00018 00019 #ifndef FLSQLQUERY_H 00020 #define FLSQLQUERY_H 00021 00022 #include "FLTableMetaData.h" 00023 00024 #include <qsqlquery.h> 00025 #include <qvariant.h> 00026 #include <qdict.h> 00027 #include <qregexp.h> 00028 #include <qstringlist.h> 00029 #include <qinputdialog.h> 00030 #include <qdict.h> 00031 00032 class FLParameterQuery; 00033 class FLGroupByQuery; 00034 class FLFieldMetaData; 00035 class FLSqlDatabase; 00036 class FLSqlQueryPrivate; 00037 00046 class FL_EXPORT FLSqlQuery: public QObject 00047 { 00048 00049 Q_OBJECT 00050 00051 public: 00052 00056 FLSqlQuery(QObject *parent = 0, const QString &connectionName = "default"); 00057 00061 ~FLSqlQuery(); 00062 00066 bool exec(); 00067 00073 void addParameter(const FLParameterQuery *p); 00074 00080 void addGroup(const FLGroupByQuery *g); 00081 00085 typedef QDict<FLParameterQuery> FLParameterQueryDict; 00086 00090 typedef QDict<FLGroupByQuery> FLGroupByQueryDict; 00091 00097 void setName(const QString &n); 00098 00102 QString name() const; 00103 00107 QString select() const; 00108 00112 QString from() const; 00113 00117 QString where() const; 00118 00122 QString orderBy() const; 00123 00135 void setSelect(const QString &s, const QString &sep = QString::fromLatin1(",")); 00136 00143 void setFrom(const QString &f); 00144 00151 void setWhere(const QString &w); 00152 00159 void setOrderBy(const QString &w); 00160 00170 QString sql(); 00171 00177 FLParameterQueryDict *parameterDict() const; 00178 00184 FLGroupByQueryDict *groupDict() const; 00185 00192 QStringList fieldList() const; 00193 00206 void setGroupDict(FLGroupByQueryDict *gd); 00207 00220 void setParameterDict(FLParameterQueryDict *pd); 00221 00227 void showDebug(); 00228 00241 QVariant value(const QString &n, bool raw = false) const; 00242 00243 00253 bool isNull(const QString &n) const; 00254 00260 QVariant value(int i, bool raw = false) const; 00261 00270 QString posToFieldName(const int p) const; 00271 00278 int fieldNameToPos(const QString &n) const; 00279 00286 QStringList tablesList() const; 00287 00294 void setTablesList(const QString &tl); 00295 00302 void setValueParam(const QString &name, const QVariant &v); 00303 00309 QVariant valueParam(const QString &name) const; 00310 00314 int size() const; 00315 00321 FLTableMetaData::FLFieldMetaDataList *fieldMetaDataList(); 00322 00323 #ifdef FL_DEBUG 00324 static long countRefQuery; 00325 #endif 00326 00330 FLSqlDatabase *db() const; 00331 00332 private: 00333 00337 FLSqlQueryPrivate *d; 00338 00339 public: 00340 00341 // 00342 // QSqlQuery 00343 // 00344 00345 bool isValid() const { 00346 return qry_.isValid(); 00347 } 00348 bool isActive() const { 00349 return qry_.isActive(); 00350 } 00351 bool isNull(int field) const { 00352 return qry_.isNull(field); 00353 } 00354 int at() const { 00355 return qry_.at(); 00356 } 00357 QString lastQuery() const { 00358 return qry_.lastQuery(); 00359 } 00360 int numRowsAffected() const { 00361 return qry_.numRowsAffected(); 00362 } 00363 QSqlError lastError() const { 00364 return qry_.lastError(); 00365 } 00366 bool isSelect() const { 00367 return qry_.isSelect(); 00368 } 00369 int QSqlQuery_size() const { 00370 return qry_.size(); 00371 } 00372 const QSqlDriver *driver() const { 00373 return qry_.driver(); 00374 } 00375 const QSqlResult *result() const { 00376 return qry_.result(); 00377 } 00378 bool isForwardOnly() const { 00379 return qry_.isForwardOnly(); 00380 } 00381 void setForwardOnly(bool forward) { 00382 qry_.setForwardOnly(forward); 00383 } 00384 00385 bool exec(const QString &query) { 00386 return qry_.exec(query); 00387 } 00388 QVariant QSqlQuery_value(int i) const { 00389 return qry_.value(i); 00390 } 00391 00392 bool seek(int i, bool relative = FALSE) { 00393 return qry_.seek(i, relative); 00394 } 00395 bool next() { 00396 return qry_.next(); 00397 } 00398 bool prev() { 00399 return qry_.prev(); 00400 } 00401 bool first() { 00402 return qry_.first(); 00403 } 00404 bool last() { 00405 return qry_.last(); 00406 } 00407 00408 // prepared query support 00409 bool prepare(const QString &query) { 00410 return qry_.prepare(query); 00411 } 00412 void bindValue(const QString &placeholder, const QVariant &val) { 00413 qry_.bindValue(placeholder, val); 00414 } 00415 void bindValue(int pos, const QVariant &val) { 00416 qry_.bindValue(pos, val); 00417 } 00418 void addBindValue(const QVariant &val) { 00419 qry_.addBindValue(val); 00420 } 00421 // remove these overloads in 4.0 00422 void bindValue(const QString &placeholder, const QVariant &val, QSql::ParameterType type) { 00423 qry_.bindValue(placeholder, val, type); 00424 } 00425 void bindValue(int pos, const QVariant &val, QSql::ParameterType type) { 00426 qry_.bindValue(pos, val, type); 00427 } 00428 void addBindValue(const QVariant &val, QSql::ParameterType type) { 00429 qry_.addBindValue(val, type); 00430 } 00431 QVariant boundValue(const QString &placeholder) const { 00432 return qry_.boundValue(placeholder); 00433 } 00434 QVariant boundValue(int pos) const { 00435 return qry_.boundValue(pos); 00436 } 00437 QMap<QString, QVariant> boundValues() const { 00438 return qry_.boundValues(); 00439 } 00440 QString executedQuery() const { 00441 return qry_.executedQuery(); 00442 } 00443 00444 private: 00445 00446 QSqlQuery qry_; 00447 }; 00448 00449 class FLSqlQueryPrivate 00450 { 00451 public: 00452 00453 FLSqlQueryPrivate(); 00454 ~FLSqlQueryPrivate(); 00455 00459 QString name_; 00460 00464 QString select_; 00465 00469 QString from_; 00470 00474 QString where_; 00475 00479 QString orderBy_; 00480 00484 QStringList fieldList_; 00485 00489 FLSqlQuery::FLParameterQueryDict *parameterDict_; 00490 00494 FLSqlQuery::FLGroupByQueryDict *groupDict_; 00495 00500 QStringList tablesList_; 00501 00505 FLTableMetaData::FLFieldMetaDataList *fieldMetaDataList_; 00506 00510 FLSqlDatabase *db_; 00511 }; 00512 00513 inline FLSqlDatabase *FLSqlQuery::db() const 00514 { 00515 return d->db_; 00516 } 00517 00518 inline void FLSqlQuery::setName(const QString &n) 00519 { 00520 d->name_ = n; 00521 } 00522 00523 inline QString FLSqlQuery::name() const 00524 { 00525 return d->name_; 00526 } 00527 00528 inline QString FLSqlQuery::select() const 00529 { 00530 return d->select_; 00531 } 00532 00533 inline QString FLSqlQuery::from() const 00534 { 00535 return d->from_; 00536 } 00537 00538 inline QString FLSqlQuery::where() const 00539 { 00540 return d->where_; 00541 } 00542 00543 inline QString FLSqlQuery::orderBy() const 00544 { 00545 return d->orderBy_; 00546 } 00547 00548 inline FLSqlQuery::FLParameterQueryDict *FLSqlQuery::parameterDict() const 00549 { 00550 return d->parameterDict_; 00551 } 00552 00553 inline FLSqlQuery::FLGroupByQueryDict *FLSqlQuery::groupDict() const 00554 { 00555 return d->groupDict_; 00556 } 00557 00558 inline QStringList FLSqlQuery::fieldList() const 00559 { 00560 return d->fieldList_; 00561 } 00562 00563 inline QStringList FLSqlQuery::tablesList() const 00564 { 00565 return d->tablesList_; 00566 } 00567 00568 #endif