OSDN Git Service

fix of aff0c8f.
[hmh/hhml.git] / ext / ml-sqlite3.h
1 #ifndef ML_SQLITE3_H
2 #define ML_SQLITE3_H
3
4 #include "ml.h"
5 #include "ml-id.h"
6 #include "ustring.h"
7 #include "motorconst.h"
8 #include <boost/ptr_container/ptr_vector.hpp>
9 extern "C" {
10 #ifdef BDB5
11 #include <db5/dbsql.h>
12 #else
13 #include "sqlite3.h"
14 #endif
15 }
16
17 class  MNode;
18 class  MlEnv;
19
20 class  ustringptr_vector: public std::vector<ustring*> {
21  public:
22     ustringptr_vector () {};
23     virtual  ~ustringptr_vector () {
24         clear ();
25     };
26     virtual void  clear () {
27         ustringptr_vector::const_iterator  it;
28         for (it = begin (); it < end (); it ++) {
29             if (*it != NULL) {
30                 delete (*it);
31             }
32         }
33         std::vector<ustring*>::clear ();
34     };
35 //    virtual ustring*  at (size_type pos);
36 //    virtual ustring*  back ();
37 //      ...
38 };
39
40 class  MLSqlite3: public MLFunc {
41  public:
42     typedef ustringptr_vector  namearray;
43     class  fsqlParam {
44     public:
45         ustring  sql;
46         MLSqlite3::namearray  bindName;
47         MLSqlite3::namearray  bindValue;
48         enum {
49             COL_LIST,
50             COL_LIST_LIST,
51             COL_VECTOR,
52             COL_VECTOR_VECTOR,
53             COL_LIST_VECTOR,
54         }  answertype;
55         
56         fsqlParam () {
57             answertype = COL_LIST;
58         };
59         virtual  ~fsqlParam () {};
60     };
61
62     sqlite3*  dbh;
63     int  limit;
64     ustring  voverflow;
65     sqlite3_stmt*  dbst;
66     int  ncols;
67     bool  fcreate;
68     MNodePtr  errfunc;
69     MNodePtr  initfunc;
70     bool  finit;
71     bool  fbegin;
72     int  retry;
73     MNodePtr  blockSym;
74
75     MLSqlite3 (MlEnv* _mlenv): MLFunc (cMLSqlite3ID, _mlenv) {
76         dbh = NULL;
77         limit = kARRAYMAX;
78         dbst = NULL;
79         fcreate = false;
80         finit = false;
81         fbegin = false;
82         retry = 0;
83     };
84     virtual  ~MLSqlite3 () {
85         close ();
86     };
87
88     virtual int  open (ustring& name);
89     virtual void  close ();
90     virtual void  finalize ();
91     virtual int  prepare (const ustring& sql);
92     virtual void  bind (namearray& name, namearray& value);
93     virtual void  bind (namearray& value);
94     virtual void  exec ();
95     virtual void  step ();
96     virtual bool  isReady () {
97         return (dbst != NULL);
98     };
99     virtual MNode*  answer_list ();
100     virtual MNode*  answer_list_ary ();
101     virtual MNode*  answer_vector ();
102     virtual MNode*  answer_vector_ary ();
103     virtual MNode*  answer_vectorlist_ary ();
104     virtual sqlite3_int64  rowid ();
105     virtual void  postError (ustring msg);
106 //    virtual int  sql (ustring query);
107     virtual int  sql_s (const ustring& query);
108     virtual void  setBreak ();
109     virtual ustring  errmsg ();
110 };
111
112 MNode*  ml_sqlite3 (MNode* cell, MlEnv* mlenv);
113 MNode*  ml_sqlite3_sql (MNode* cell, MlEnv* mlenv, MLFunc* mobj);
114 MNode*  ml_sqlite3_sql_ary (MNode* cell, MlEnv* mlenv, MLFunc* mobj);
115 MNode*  ml_sqlite3_sqlv (MNode* cell, MlEnv* mlenv, MLFunc* mobj);
116 MNode*  ml_sqlite3_sqlv_ary (MNode* cell, MlEnv* mlenv, MLFunc* mobj);
117 MNode*  ml_sqlite3_sqllv_ary (MNode* cell, MlEnv* mlenv, MLFunc* mobj);
118 MNode*  ml_sqlite3_rowid (MNode* cell, MlEnv* mlenv, MLFunc* mobj);
119 MNode*  ml_sqlite3_escape_like (MNode* cell, MlEnv* mlenv, MLFunc* mobj);
120 MNode*  ml_sqlite3_begin_transaction (MNode* cell, MlEnv* mlenv, MLFunc* mobj);
121 MNode*  ml_sqlite3_rollback (MNode* cell, MlEnv* mlenv, MLFunc* mobj);
122
123 #endif /* ML_SQLITE3_H */