nodejs-db-informix  master
nodejs bindings for Informix
 All Classes Functions Pages
query.h
1 #ifndef __QUERY_H_INCLUDED__
2 #define __QUERY_H_INCLUDED__
3 
4 #include <stdlib.h>
5 #include <node.h>
6 #include <node_buffer.h>
7 #include <node_version.h>
8 #include <algorithm>
9 #include <cctype>
10 #include <iomanip>
11 #include <string>
12 #include <sstream>
13 #include <vector>
14 #include "node_defs.h"
15 #include "connection.h"
16 #include "events.h"
17 #include "exception.h"
18 #include "result.h"
19 
20 namespace nodejs_db {
21 class Query : public EventEmitter {
22  public:
23  static void Init(v8::Handle<v8::Object> target, v8::Persistent<v8::FunctionTemplate> constructorTemplate);
24  void setConnection(Connection* connection);
25  v8::Handle<v8::Value> set(const v8::Arguments& args);
26  typedef enum {
27  NONE = 0,
28  SELECT = 0x1,
29  INSERT = 0x1 << 1,
30  UPDATE = 0x1 << 2,
31  DELETE = 0x1 << 3,
32  } query_t;
33 
34  protected:
35  struct row_t {
36  std::vector<std::string>* columns;
37  unsigned long* columnLengths;
38  };
40  v8::Persistent<v8::Object> context;
41  Query* query;
42  Result* result;
43  std::string* error;
44  uint16_t columnCount;
45  bool buffered;
46  std::vector<row_t*>* rows;
47  };
48  struct query_async_t {
49  v8::Persistent<v8::Object> context;
50  execute_request_t* request;
51  row_t* row;
52  };
54  bool flag;
55  uint32_t arg;
56  };
58  projection_clause skip;
59  projection_clause first;
60  projection_clause limit;
61  };
62  Connection* connection;
63  std::ostringstream sql;
64  query_t sqlType;
65  projection_clause_t projection;
66  std::vector< v8::Persistent<v8::Value> > values;
67  bool async;
68  bool cast;
69  bool bufferText;
70  v8::Persistent<v8::Function>* cbStart;
71  v8::Persistent<v8::Function>* cbExecute;
72  v8::Persistent<v8::Function>* cbFinish;
73 
74  Query();
75  ~Query();
76  static v8::Handle<v8::Value> Select(const v8::Arguments& args);
77  static v8::Handle<v8::Value> Skip(const v8::Arguments& args);
78  static v8::Handle<v8::Value> Limit(const v8::Arguments& args);
79  static v8::Handle<v8::Value> First(const v8::Arguments& args);
80  static v8::Handle<v8::Value> From(const v8::Arguments& args);
81  static v8::Handle<v8::Value> Join(const v8::Arguments& args);
82  static v8::Handle<v8::Value> Where(const v8::Arguments& args);
83  static v8::Handle<v8::Value> And(const v8::Arguments& args);
84  static v8::Handle<v8::Value> Or(const v8::Arguments& args);
85  static v8::Handle<v8::Value> OrderBy(const v8::Arguments& args);
86  static v8::Handle<v8::Value> Add(const v8::Arguments& args);
87  static v8::Handle<v8::Value> Insert(const v8::Arguments& args);
88  static v8::Handle<v8::Value> Into(const v8::Arguments& args);
89  static v8::Handle<v8::Value> Values(const v8::Arguments& args);
90  static v8::Handle<v8::Value> Update(const v8::Arguments& args);
91  static v8::Handle<v8::Value> Set(const v8::Arguments& args);
92  static v8::Handle<v8::Value> Delete(const v8::Arguments& args);
93  static v8::Handle<v8::Value> Sql(const v8::Arguments& args);
94  static v8::Handle<v8::Value> Execute(const v8::Arguments& args);
95  static uv_async_t g_async;
96  static void uvExecute(uv_work_t* uvRequest);
97  static void uvEmitResults(uv_async_t* uvAsync, int status);
98  static void uvExecuteFinished(uv_work_t* Rquest, int status);
99  void executeAsync(execute_request_t* request);
100  static void freeRequest(execute_request_t* request, bool freeAll = true);
101  std::string fieldName(v8::Local<v8::Value> value) const throw(Exception&);
102  std::string tableName(v8::Local<v8::Value> value, bool escape = true) const throw(Exception&);
103  v8::Handle<v8::Value> addCondition(const v8::Arguments& args, const char* separator);
104  v8::Local<v8::Object> row(Result* result, row_t* currentRow) const;
105  virtual std::string parseQuery() const throw(Exception&);
106  virtual std::vector<std::string::size_type> placeholders(std::string* parsed) const throw(Exception&);
107  virtual void addProjections() throw(Exception&);
108  virtual Result* execute() const throw(Exception&);
109  std::string value(v8::Local<v8::Value> value, bool inArray = false, bool escape = true, int precision = -1) const throw(Exception&);
110 
111  private:
112  static bool gmtDeltaLoaded;
113  static int gmtDelta;
114 
115  std::string fromDate(const double timeStamp) const throw(Exception&);
116 };
117 }
118 
119 #endif // __QUERY_H_INCLUDED__