00001
00004
00005 #ifndef FBwSql_HH
00006 #define FBwSql_HH
00007 #include <FBwLst.h>
00008
00009
00011
00012 {
00013
00014
00015 using std::string;
00016
00017 typedef SimList<int> SList;
00018
00019
00020 class sqParam
00021 {
00022 string p;
00023 public:
00024 sqParam(const string &s)
00025 {
00026 p=s;
00027 }
00028 string GetText() const
00029 {
00030 return p;
00031 }
00032 };
00033
00034 class sqAggregate
00035 {
00036 protected:
00037 string exp;
00038 string alias;
00039 public:
00040 sqAggregate(const string &s, const string &palias)
00041 {
00042 exp=s;
00043 alias=palias;
00044 }
00045 virtual string GetText() const = 0;
00046 string GetAlias() const;
00047 void SetAlias(const string &s)
00048 {
00049 alias=s;
00050 }
00051 };
00052
00053 class SqlTextHelper
00054 {
00055 SList *curList;
00056 public:
00057 enum clauseType { sq_select,sq_from,sq_where,sq_orderby,sq_groupby,sq_txtvars,sq_having};
00058 enum objectType { literal,aggregatefn,variable,parameter};
00059 SqlTextHelper()
00060 {
00061 curList=NULL;
00062 }
00063 void SetCurList(SList *s)
00064 {
00065 curList=s;
00066 }
00067 string ComaList(const SList &lst) const;
00068 string WhereList(const SList &lst) const;
00069 void Append(const string &var)
00070 {
00071 curList->Append(var,SqlTextHelper::literal);
00072 }
00073 void Append(const sqAggregate &var)
00074 {
00075 curList->Append(var.GetText(),SqlTextHelper::aggregatefn);
00076 }
00077 void Append(const sqParam &var)
00078 {
00079 curList->Append(var.GetText(),SqlTextHelper::parameter);
00080 }
00081 void Clear()
00082 {
00083 if( curList )
00084 curList->Destroy();
00085 }
00086 void Copy(const SList &s)
00087 {
00088 curList->Copy(s);
00089 }
00090 };
00091
00092
00093 class sqSUM : public sqAggregate
00094 {
00095 public:
00096 sqSUM(const string &s, const string &p)
00097 : sqAggregate(s,p)
00098 {
00099 }
00100 virtual string GetText() const;
00101 };
00102
00103 class sqCOUNT : public sqAggregate
00104 {
00105 public:
00106 sqCOUNT(const string &s, const string &p)
00107 : sqAggregate(s,p)
00108 {
00109 exp=s;
00110 }
00111 virtual string GetText() const;
00112 };
00113
00114 class sqMAX : public sqAggregate
00115 {
00116 public:
00117 sqMAX(const string &s, const string &p)
00118 : sqAggregate(s,p)
00119 {
00120 exp=s;
00121 }
00122 virtual string GetText() const;
00123 };
00124
00125 class sqMIN : public sqAggregate
00126 {
00127 public:
00128 sqMIN(const string s, const string &p)
00129 : sqAggregate(s,p)
00130 {
00131 exp=s;
00132 }
00133 virtual string GetText() const;
00134 };
00135
00136
00137
00138
00139
00140 class FBwSelectSql : public SqlTextHelper
00141 {
00142 SList sselect;
00143 SList sfrom;
00144 SList swhere;
00145 SList sorderby;
00146 SList sgroupby;
00147 SList txtvars;
00148 SList shaving;
00149 clauseType state;
00150 protected:
00151 public:
00152 enum objectType { literal,aggregatefn };
00153 FBwSelectSql()
00154 {
00155 state=sq_select;
00156 }
00157 virtual string GetSqlText() const;
00158 void select()
00159 {
00160 state=sq_select;
00161 SetCurList(&sselect);
00162 }
00163 void from()
00164 {
00165 state=sq_from;
00166 SetCurList(&sfrom);
00167 }
00168 void where()
00169 {
00170 state=sq_where;
00171 SetCurList(&swhere);
00172 }
00173 void orderby()
00174 {
00175 state=sq_orderby;
00176 SetCurList(&sorderby);
00177 }
00178 void groupby()
00179 {
00180 state=sq_groupby;
00181 SetCurList(&sgroupby);
00182 }
00183 void having()
00184 {
00185 state=sq_having;
00186 SetCurList(&shaving);
00187 }
00188 void vars()
00189 {
00190 state=sq_txtvars;
00191 SetCurList(&txtvars);
00192 }
00193 void ClearAll();
00194 friend FBwSelectSql &operator<<(FBwSelectSql &s, const string var);
00195 friend FBwSelectSql &operator<<(FBwSelectSql &s, const sqAggregate &var);
00196 friend FBwSelectSql &operator<<(FBwSelectSql &s, const sqParam &var);
00197 };
00198
00199
00200 class FBwInsertSql : public SqlTextHelper
00201 {
00202 string Into;
00203 SList sfields;
00204 SList svalues;
00205 public:
00206 FBwInsertSql()
00207 {
00208 }
00209 FBwInsertSql(const string &table)
00210 {
00211 Into=table;
00212 }
00213 virtual string GetSqlText() const;
00214 void fields()
00215 {
00216 SetCurList(&sfields);
00217 }
00218 void values()
00219 {
00220 SetCurList(&svalues);
00221 }
00222 void SetTable(const string &tbl)
00223 {
00224 Into=tbl;
00225 }
00226 string GetTable() const
00227 {
00228 return Into;
00229 }
00230 void ClearAll();
00231 friend FBwInsertSql &operator<<(FBwInsertSql &s, const string var);
00232 friend FBwInsertSql &operator<<(FBwInsertSql &s, const sqAggregate &var);
00233 friend FBwInsertSql &operator<<(FBwInsertSql &s, const sqParam &var);
00234 };
00235
00236
00237 class FBwUpdateSql : public SqlTextHelper
00238 {
00239 SList sfields;
00240 SList svalues;
00241 SList swhere;
00242 SList txtvars;
00243 string table;
00244 public:
00245 FBwUpdateSql()
00246 : SqlTextHelper()
00247 {
00248 }
00249 FBwUpdateSql(const string &tbl)
00250 : SqlTextHelper()
00251 {
00252 table=tbl;
00253 }
00254 virtual string GetSqlText() const;
00255 void fields()
00256 {
00257 SetCurList(&sfields);
00258 }
00259 void values()
00260 {
00261 SetCurList(&svalues);
00262 }
00263 void where()
00264 {
00265 SetCurList(&swhere);
00266 }
00267 void SetTable(const string &tbl)
00268 {
00269 table=tbl;
00270 }
00271 void ClearAll();
00272 friend FBwUpdateSql &operator<<(FBwUpdateSql &s, const string var);
00273 friend FBwUpdateSql &operator<<(FBwUpdateSql &s, const sqAggregate &var);
00274 };
00275
00276 class FBwDeleteSql : public SqlTextHelper
00277 {
00278 string from;
00279 SList swhere;
00280 public:
00281 FBwDeleteSql()
00282 {
00283 SetCurList(&swhere);
00284 };
00285 FBwDeleteSql(const string &tbl)
00286 {
00287 from=tbl;
00288 }
00289 void SetTable(const string &tbl)
00290 {
00291 from=tbl;
00292 }
00293 string GetTable() const
00294 {
00295 return from;
00296 }
00297 void where()
00298 {
00299 SetCurList(&swhere);
00300 }
00301 virtual string GetSqlText() const;
00302 void ClearAll();
00303 friend FBwDeleteSql &operator<<(FBwDeleteSql &s, const string var);
00304 friend FBwDeleteSql &operator<<(FBwDeleteSql &s, const sqAggregate &var);
00305 friend FBwDeleteSql &operator<<(FBwDeleteSql &s, const sqParam &var);
00306 };
00307
00308
00309 };
00310 #endif
00311