ffead.server.doc
Cibernate.h
1 /*
2  Copyright 2009-2012, Sumeet Chhetri
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 /*
17  * Cibernate.h
18  *
19  * Created on: Jan 5, 2010
20  * Author: sumeet
21  */
22 
23 #ifndef CIBERNATE_H_
24 #define CIBERNATE_H_
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <sql.h>
28 #include <sqlext.h>
29 #include <sqltypes.h>
30 #include "Reflector.h"
31 #include "AfcUtil.h"
32 #include <iostream>
33 #include "Object.h"
34 #include "Timer.h"
35 #include "CibernateConnPools.h"
36 #include "DateFormat.h"
37 #include "BinaryData.h"
38 #include "Logger.h"
39 #include "CibernateQuery.h"
40 #include "RegexUtil.h"
41 
42 typedef map<string,Object*> Params;
43 class Cibernate {
44 private:
45  Logger logger;
46  string demangle(const char *mangled);
48  Mapping* mapping;
49  string appName;
50  Connection *conn;
51  SQLHENV V_OD_Env;// Handle ODBC environment
52  SQLHDBC V_OD_hdbc;// Handle connection
53  SQLHSTMT V_OD_hstmt;//statement
54  Params params,params1;
55  bool igPaWC;
56  bool allocateStmt(bool);
57  map<string,string> ntmap,ntmap1;
58  void clearMaps(){ntmap.clear();params.clear();ntmap1.clear();params1.clear();}
59  void* getElements();
60  void* getElements(string clasName);
61  void* getElements(vector<string> cols,string clasName);
62  void* executeQuery(CibernateQuery query);
63  void bindQueryParams(CibernateQuery& query);
64  void* executeQuery(CibernateQuery query, vector<string> cols);
65  bool executeInsert(CibernateQuery cquery, vector<string> cols, void* t);
66  bool executeUpdate(CibernateQuery cquery, vector<string> cols, void* t);
67  int storeProperty(ClassInfo clas, void* t, int var, string fieldName);
68  int getProperty(int dataType, int columnSize, map<string, void*>& colValMap, string colName, int var);
69  void* sqlfuncs(string type,string clasName);
70  bool init;
71 public:
72  Cibernate();
73  Cibernate(string);
74  Cibernate(string,string,string,int);
75  virtual ~Cibernate();
76  void close();
77  bool startTransaction();
78  bool commit();
79  bool rollback();
80  void procedureCall(string);
81  void addParam(string name,string type,Object &obj){ntmap[name]=type;params[name]=&obj;}
82  void addParam(string name,Object &obj){params[name]=&obj;}
83  Object getParam(string name){return *params[name];}
84  void addParam1(string name,string type,Object &obj){ntmap1[name]=type;params1[name]=&obj;}
85  void addParam1(string name,Object &obj){params1[name]=&obj;}
86  Object getParam1(string name){return *params1[name];}
87  vector<map<string,void*> > getARSCW(string tableName,vector<string> cols,vector<string> coltypes);
88  void truncate(string clasName);
89  void empty(string clasName);
90 
91 
92  vector<map<string, void*> > execute(CibernateQuery query);
93  template<class T> vector<T> getList(CibernateQuery query)
94  {
95  T t;
96  const char *mangled = typeid(t).name();
97  string clasName = demangle(mangled);
98  if(clasName!=query.className)
99  {
100  vector<T> vecT;
101  return vecT;
102  }
103  vector<T> vecT;
104  void* vect = executeQuery(query);
105  if(vect!=NULL)
106  {
107  vecT = *(vector<T>*)vect;
108  }
109  return vecT;
110  }
111 
112  template<class T> T get(CibernateQuery query)
113  {
114  T t;
115  const char *mangled = typeid(t).name();
116  string clasName = demangle(mangled);
117  if(clasName!=query.className)
118  {
119  vector<T> vecT;
120  return vecT;
121  }
122  vector<T> vecT;
123  void* vect = executeQuery(query);
124  if(vect!=NULL)
125  {
126  vecT = *(vector<T>*)vect;
127  if(vecT.size()>0)
128  {
129  t = vecT.at(0);
130  }
131  delete vect;
132  }
133  return t;
134  }
135 
136  template<class T> bool executeUpdate(CibernateQuery query)
137  {
138  T t;
139  const char *mangled = typeid(t).name();
140  string clasName = demangle(mangled);
141  if(clasName!=query.className)
142  {
143  return false;
144  }
145  void* vect = executeQuery(query);
146  if(vect!=NULL)
147  {
148  bool* flag = (bool*)vect;
149  bool ffl = *flag;
150  delete flag;
151  return ffl;
152  }
153  return false;
154  }
155 
156  template<class T> vector<T> getAll()
157  {
158  T t;
159  const char *mangled = typeid(t).name();
160  string clasName = demangle(mangled);
161  vector<string> cols;
162  CibernateQuery query("", clasName);
163  vector<T> vecT;
164  void* vect = executeQuery(query, cols);
165  if(vect!=NULL)
166  {
167  vecT = *(vector<T>*)vect;
168  delete vect;
169  }
170  return vecT;
171  }
172 
173  template<class T> T get(int id)
174  {
175  Object oid;
176  oid << id;
177  vector<string> cols;
178  T t;
179  const char *mangled = typeid(t).name();
180  string clasName = demangle(mangled);
181  CibernateQuery query("", clasName);
182  query.addColumnBinding("id", oid);
183  vector<T> vecT;
184  void* vect = executeQuery(query, cols);
185  if(vect!=NULL)
186  {
187  vecT = *(vector<T>*)vect;
188  if(vecT.size()>0)
189  {
190  t = vecT.at(0);
191  }
192  delete vect;
193  }
194  return t;
195  }
196 
197  template<class T, class R> vector<R> getColumnValues(string name)
198  {
199  vector<string> cols;
200  cols.push_back(name);
201  T t;
202  const char *mangled = typeid(t).name();
203  string clasName = demangle(mangled);
204  CibernateQuery query("", clasName);
205  vector<T> vecT;
206  void* vect = executeQuery(query, cols);
207  vector<R> vecR;
208  if(vect!=NULL)
209  {
210  vecT = *(vector<T>*)vect;
211  delete vect;
212  if(vecT.size()>0)
213  {
214  Reflector reflector;
215  ClassInfo clas = reflector.getClassInfo(clasName);
216  args argus;
217  string methname = "get"+AfcUtil::camelCased(name);
218  Method meth = clas.getMethod(methname,argus);
219  vals valus;
220  for (int var = 0; var < (int)vecT.size(); ++var) {
221  void *ns = reflector.invokeMethodGVP(&(vecT.at(var)),meth,valus);
222  vecR.push_back(*(R*)ns);
223  delete ns;
224  }
225  }
226  }
227  return vecR;
228  }
229  template<class T, class R> vector<R> getColumnValues(string name, PosParameters propPosVaues)
230  {
231  vector<string> cols;
232  cols.push_back(name);
233  T t;
234  const char *mangled = typeid(t).name();
235  string clasName = demangle(mangled);
236  CibernateQuery query("", clasName);
237  query.propPosVaues = propPosVaues;
238  vector<T> vecT;
239  void* vect = executeQuery(query, cols);
240  vector<R> vecR;
241  if(vect!=NULL)
242  {
243  vecT = *(vector<T>*)vect;
244  delete vect;
245  if(vecT.size()>0)
246  {
247  Reflector reflector;
248  ClassInfo clas = reflector.getClassInfo(clasName);
249  args argus;
250  string methname = "get"+AfcUtil::camelCased(name);
251  Method meth = clas.getMethod(methname,argus);
252  vals valus;
253  for (int var = 0; var < (int)vecT.size(); ++var) {
254  void *ns = reflector.invokeMethodGVP(&(vecT.at(var)),meth,valus);
255  vecR.push_back(*(R*)ns);
256  delete ns;
257  }
258  }
259  }
260  return vecR;
261  }
262  template<class T, class R> vector<R> getColumnValues(string name, Parameters propNameVaues)
263  {
264  vector<string> cols;
265  cols.push_back(name);
266  T t;
267  const char *mangled = typeid(t).name();
268  string clasName = demangle(mangled);
269  CibernateQuery query("", clasName);
270  query.propNameVaues = propNameVaues;
271  vector<T> vecT;
272  void* vect = executeQuery(query, cols);
273  vector<R> vecR;
274  if(vect!=NULL)
275  {
276  vecT = *(vector<T>*)vect;
277  delete vect;
278  if(vecT.size()>0)
279  {
280  Reflector reflector;
281  ClassInfo clas = reflector.getClassInfo(clasName);
282  args argus;
283  string methname = "get"+AfcUtil::camelCased(name);
284  Method meth = clas.getMethod(methname,argus);
285  vals valus;
286  for (int var = 0; var < (int)vecT.size(); ++var) {
287  void *ns = reflector.invokeMethodGVP(&(vecT.at(var)),meth,valus);
288  vecR->push_back(*(R*)ns);
289  delete ns;
290  }
291  delete vecT;
292  }
293  }
294  return vecR;
295  }
296 
297  template<class T> void insert(T t)
298  {
299  const char *mangled = typeid(t).name();
300  string clasName = demangle(mangled);
301  CibernateQuery cquery("", clasName);
302  vector<string> cols;
303  executeInsert(cquery, cols, &t);
304  }
305 
306  template<class T> void bulkInsert(vector<T> vecT)
307  {
308  for(unsigned int k=0;k<vecT.size();k++)
309  {
310  T t = vecT.at(k);
311  insert<T>(t);
312  }
313  }
314 
315  template<class T> void update(T t)
316  {
317  const char *mangled = typeid(t).name();
318  string clasName = demangle(mangled);
319  CibernateQuery cquery("", clasName);
320  vector<string> cols;
321  executeUpdate(cquery, cols, &t);
322  }
323 
324  template<class T> void truncate()
325  {
326  T t;
327  const char *mangled = typeid(t).name();
328  string clasName = demangle(mangled);
329  empty(clasName);
330  }
331 
332  template<class T> void empty()
333  {
334  T t;
335  const char *mangled = typeid(t).name();
336  string clasName = demangle(mangled);
337  empty(clasName);
338  }
339 
340  template<class T> long getNumRows()
341  {
342  T t;
343  const char *mangled = typeid(t).name();
344  string clasName = demangle(mangled);
345  long size = *(long*)sqlfuncs("COUNT(*)",clasName);
346  return size;
347  }
348 
349  template<class T> int getSumValue(string col)
350  {
351  igPaWC = true;
352  T t;
353  const char *mangled = typeid(t).name();
354  string clasName = demangle(mangled);
355  int size = *(int*)sqlfuncs("SUM("+col+")","int");
356  igPaWC = false;
357  return size;
358  }
359 
360  template<class T> int getAvgValue(string col)
361  {
362  igPaWC = true;
363  T t;
364  const char *mangled = typeid(t).name();
365  string clasName = demangle(mangled);
366  int size = *(int*)sqlfuncs("AVG("+col+")","int");
367  igPaWC = false;
368  return size;
369  }
370 
371  template<class T> T getFirstValue(string col)
372  {
373  igPaWC = true;
374  T t;
375  const char *mangled = typeid(t).name();
376  string clasName = demangle(mangled);
377  t = *(T*)sqlfuncs("first("+col+")",clasName);
378  igPaWC = false;
379  return t;
380  }
381 
382  template<class T> T getLastValue(string col)
383  {
384  igPaWC = true;
385  T t;
386  const char *mangled = typeid(t).name();
387  string clasName = demangle(mangled);
388  t = *(T*)sqlfuncs("last("+col+")",clasName);
389  igPaWC = false;
390  return t;
391  }
392 
393  template<class T> T getMinValue(string col)
394  {
395  igPaWC = true;
396  T t;
397  const char *mangled = typeid(t).name();
398  string clasName = demangle(mangled);
399  t = *(T*)sqlfuncs("min("+col+")",clasName);
400  igPaWC = false;
401  return t;
402  }
403 
404  template<class T> T getMaxValue(string col)
405  {
406  igPaWC = true;
407  T t;
408  const char *mangled = typeid(t).name();
409  string clasName = demangle(mangled);
410  t = *(T*)sqlfuncs("max("+col+")",clasName);
411  igPaWC = false;
412  return t;
413  }
414 };
415 
416 #endif /* CIBERNATE_H_ */