CFx SDK Documentation  2023 SP0
TFSQL.h
Go to the documentation of this file.
1 #ifndef _OD_TFSQL_INCLUDED_
2 #define _OD_TFSQL_INCLUDED_
3 
4 
5 #include "RxObject.h"
6 #include "OdStreamBuf.h"
7 #include "RxIterator.h"
8 #include "RxFS.h"
9 #include "TfIO.h"
10 #include "DynamicLinker.h"
11 
12 #ifndef OD_SQLITE3_DEFINED
13 typedef int (*sqlite3_callback)(void*,int,char**, char**);
14 #endif
15 
16 // pasts static constant string and it's length to function call parameter list
17 #define ODSZ_CONS(str_const) str_const,(sizeof(str_const)-1)
18 
19 namespace OD {
20  class TFSQL;
22 
23  class FIRSTDLL_EXPORT TFSQL : public OdRxObject {
24  public:
26 
27  static TFSQLP create(const OdString& pathToData = OdString::kEmpty);
28  virtual void attach(const OdString& pathToData) = 0;
29  virtual OdString path() const = 0;
30  virtual void close() = 0;
31 
32  class Statement;
34 
35  typedef OdStreamBuf Blob;
37 
38  /* */ void beginTransaction() { exe("BEGIN;PRAGMA read_uncommitted=true;"); }
39  /* */ void endTransaction() { exe("COMMIT"); }
40  /* */ void abortTransaction() { exe("ROLLBACK"); }
41 
42 
43  virtual BlobP open (const char* tab, const char* col, OdInt64 row, int mode = File::Read, const char* db = "main") = 0;
44  /* */ BlobP openR (const char* tab, const char* col, OdInt64 row, const char* db = "main") {return open(tab,col,row,File::Read ,db);}
45  /* */ BlobP openW (const char* tab, const char* col, OdInt64 row, const char* db = "main") {return open(tab,col,row,File::Write ,db);}
46  /* */ BlobP openRW (const char* tab, const char* col, OdInt64 row, const char* db = "main") {return open(tab,col,row,File::Read|File::Write ,db);}
47  /* *///void touch (const char* tab, const char* col, OdInt64 row, const char* db = "main") {return blob(tab,col,row,Blob::Touch ,db);}
48 
49  class Statement : public OdRxObject {
50  public:
51  class Row : public OdRxObject {
52  public:
53  virtual bool isNull(int i) = 0;
54  virtual bool Bool (int i) = 0;
55  virtual OdUInt8 int8 (int i) = 0;
56  virtual OdUInt16 int16 (int i) = 0;
57  virtual OdUInt32 int32 (int i) = 0;
58  virtual OdUInt64 int64 (int i) = 0;
59  virtual double realD (int i) = 0;
60  virtual float real (int i) = 0;
61  virtual OdString utf16 (int i) = 0;
62  virtual OdAnsiString utf8 (int i) = 0;
63  virtual const void* blob (int i, OdUInt64 *len = 0) = 0;
64  virtual BlobP open (int i, File::Access acc = File::Read) = 0;
65  };
67 
68  class RowIterator : public OdRxIterator {
69  public:
70  Row& row() { return (Row&)*object().get(); }
71  };
75 
76  class Params : public OdRxObject {
77  public:
78  //virtual Params& setNull(int at) = 0;
79  virtual Params& set(int at, bool val) = 0;
80  virtual Params& set(int at, OdUInt8 val) = 0;
81  virtual Params& set(int at, OdUInt16 val) = 0;
82  virtual Params& set(int at, OdUInt32 val) = 0;
83  virtual Params& set(int at, OdUInt64 val) = 0;
84  virtual Params& set(int at, double val) = 0;
85  virtual Params& set(int at, const OdString& val) = 0;
86  virtual Params& set(int at, const OdAnsiString& val) = 0;
87  virtual Params& set(int at, const void* val, OdUInt64 len) = 0;
88  virtual Params& set(int at, OdStreamBuf* val) = 0;
89  virtual ResultP exe() = 0;
90  };
92 
93  virtual ParamsP reset(const char* sql = 0, int len = -1) = 0;
94  };
96 
97 
98  /*
99  ** A function to executes one or more statements of SQL.
100  **
101  ** If one or more of the SQL statements are queries, then
102  ** the callback function specified by the 3rd parameter is
103  ** invoked once for each row of the query result. This callback
104  ** should normally return 0. If the callback returns a non-zero
105  ** value then the query is aborted, all subsequent SQL statements
106  ** are skipped and the sqlite3_exec() function returns the SQLITE_ABORT.
107  **
108  ** The 1st parameter is an arbitrary pointer that is passed
109  ** to the callback function as its first parameter.
110  **
111  ** The 2nd parameter to the callback function is the number of
112  ** columns in the query result. The 3rd parameter to the callback
113  ** is an array of strings holding the values for each column.
114  ** The 4th parameter to the callback is an array of strings holding
115  ** the names of each column.
116  **
117  ** The callback function may be NULL, even for queries. A NULL
118  ** callback is not an error. It just means that no callback
119  ** will be invoked.
120  */
121  virtual void exe(const char* sql, sqlite3_callback cb, void* callbackctx) = 0;
122  class SQLReadCallback : public OdRxObject { public: virtual bool on(StatementRow& row) = 0; };
123  virtual void exe(const char* sql, SQLReadCallback* cb = 0) = 0;
124  virtual OdUInt64 lastInsertRowId() = 0;
125 
126  virtual Statement::ParamsP prepare(const char* sql, int at = -1, StatementP* stmt = 0) = 0;
127  Statement::Params& prepareOrReset(const char* sql, int len, StatementP& stmt) {
128  if(stmt.isNull())
129  return *prepare(sql, len, &stmt);
130  return *stmt->reset();
131  }
132  };
133 
134  inline OD::TFSQLP TFSQL::create(const OdString& pathToData) {
135  ::odrxDynamicLinker()->loadApp("TFSQLite", false);
136  OD::TFSQLP strg = odrxCreateObject("TFSQLite");
137  if(strg.isNull())
138  throw OdError_FileNotFound("rx:TFSQLite.tx/TFSQLite");
139  strg->attach(pathToData);
140  return strg;
141  }
142 }
148 
149 
150 #endif // _OD_TFSQL_INCLUDED_
exe
Definition: DimVarDefs.h:1095
FIRSTDLL_EXPORT OdRxDynamicLinker * odrxDynamicLinker()
unsigned int OdUInt32
unsigned short OdUInt16
unsigned char OdUInt8
#define FIRSTDLL_EXPORT
Definition: RootExport.h:39
FIRSTDLL_EXPORT OdRxObjectPtr odrxCreateObject(const OdString &sClassName)
OdSmartPtr< OD::TFSQL::Blob > OdTFSQLBlobPtr
Definition: TFSQL.h:144
OdSmartPtr< OdTFSQLStatement > OdTFSQLStatementPtr
Definition: TFSQL.h:146
OD::TFSQL::Statement OdTFSQLStatement
Definition: TFSQL.h:145
int(* sqlite3_callback)(void *, int, char **, char **)
Definition: TFSQL.h:13
OD::TFSQL::Blob OdTFSQLBlob
Definition: TFSQL.h:143
OD::TFSQL::SQLReadCallback OdTFSQLReadCallback
Definition: TFSQL.h:147
virtual bool on(StatementRow &row)=0
virtual Params & set(int at, const void *val, OdUInt64 len)=0
virtual Params & set(int at, const OdAnsiString &val)=0
virtual Params & set(int at, OdUInt64 val)=0
virtual Params & set(int at, OdUInt8 val)=0
virtual Params & set(int at, bool val)=0
virtual Params & set(int at, const OdString &val)=0
virtual Params & set(int at, OdUInt32 val)=0
virtual Params & set(int at, OdStreamBuf *val)=0
virtual Params & set(int at, double val)=0
virtual ResultP exe()=0
virtual Params & set(int at, OdUInt16 val)=0
virtual double realD(int i)=0
virtual bool isNull(int i)=0
virtual const void * blob(int i, OdUInt64 *len=0)=0
virtual OdUInt8 int8(int i)=0
virtual OdAnsiString utf8(int i)=0
virtual OdUInt64 int64(int i)=0
virtual BlobP open(int i, File::Access acc=File::Read)=0
virtual OdString utf16(int i)=0
virtual OdUInt16 int16(int i)=0
virtual bool Bool(int i)=0
virtual OdUInt32 int32(int i)=0
virtual float real(int i)=0
OdSmartPtr< RowIterator > RowIteratorP
Definition: TFSQL.h:72
OdSmartPtr< Params > ParamsP
Definition: TFSQL.h:91
RowIterator Result
Definition: TFSQL.h:73
virtual ParamsP reset(const char *sql=0, int len=-1)=0
OdSmartPtr< Row > RowP
Definition: TFSQL.h:66
OdSmartPtr< Result > ResultP
Definition: TFSQL.h:74
BlobP openRW(const char *tab, const char *col, OdInt64 row, const char *db="main")
Definition: TFSQL.h:46
void beginTransaction()
Definition: TFSQL.h:38
BlobP openW(const char *tab, const char *col, OdInt64 row, const char *db="main")
Definition: TFSQL.h:45
virtual void close()=0
OdSmartPtr< Statement::Row > StatementRow
Definition: TFSQL.h:95
virtual OdString path() const =0
OdSmartPtr< Statement > StatementP
Definition: TFSQL.h:32
static TFSQLP create(const OdString &pathToData=OdString::kEmpty)
Definition: TFSQL.h:134
virtual void exe(const char *sql, SQLReadCallback *cb=0)=0
virtual BlobP open(const char *tab, const char *col, OdInt64 row, int mode=File::Read, const char *db="main")=0
ODRX_DECLARE_MEMBERS(TFSQL)
void abortTransaction()
Definition: TFSQL.h:40
OdStreamBuf Blob
Definition: TFSQL.h:35
OdSmartPtr< Blob > BlobP
Definition: TFSQL.h:36
Statement::Params & prepareOrReset(const char *sql, int len, StatementP &stmt)
Definition: TFSQL.h:127
BlobP openR(const char *tab, const char *col, OdInt64 row, const char *db="main")
Definition: TFSQL.h:44
virtual void attach(const OdString &pathToData)=0
virtual Statement::ParamsP prepare(const char *sql, int at=-1, StatementP *stmt=0)=0
void endTransaction()
Definition: TFSQL.h:39
virtual OdUInt64 lastInsertRowId()=0
virtual void exe(const char *sql, sqlite3_callback cb, void *callbackctx)=0
bool isNull() const
Definition: BaseObjectPtr.h:70
Definition: Int64.h:43
virtual OdRxModulePtr loadApp(const OdString &applicationName, bool silent=true)=0
void attach(const T *pObject)
Definition: SmartPtr.h:215
FIRSTDLL_EXPORT_STATIC static const OdString kEmpty
Definition: OdString.h:98
Access
Definition: RxFS.h:60
@ Read
Definition: RxFS.h:61
@ Write
Definition: RxFS.h:62
StreamP open(const OdString &path, int acc=Read, Creation dispos=OpenExisting, Share sh=DenyNo)
Definition: RxFS.h:239
Definition: OdPathUtil.h:33
OdSmartPtr< TFSQL > TFSQLP
Definition: TFSQL.h:20