CFx SDK Documentation  2020SP3
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_
OdError_FileNotFound
Definition: RxSystemServices.h:110
FIRSTDLL_EXPORT
#define FIRSTDLL_EXPORT
Definition: RootExport.h:39
OD::TFSQL::Statement::Row::Bool
virtual bool Bool(int i)=0
OdString
Definition: OdString.h:95
OD::TFSQL::Statement::RowIterator
Definition: TFSQL.h:68
OdTFSQLBlobPtr
OdSmartPtr< OD::TFSQL::Blob > OdTFSQLBlobPtr
Definition: TFSQL.h:144
OdRxIterator
Definition: RxIterator.h:46
sqlite3_callback
int(* sqlite3_callback)(void *, int, char **, char **)
Definition: TFSQL.h:13
OdUInt8
unsigned char OdUInt8
Definition: OdPlatformSettings.h:759
OD::TFSQL::Statement::Params::set
virtual Params & set(int at, OdUInt8 val)=0
odrxDynamicLinker
FIRSTDLL_EXPORT OdRxDynamicLinker * odrxDynamicLinker()
OD::TFSQL::BlobP
OdSmartPtr< Blob > BlobP
Definition: TFSQL.h:36
OdInt64
Definition: Int64.h:43
OD::TFSQL::Statement::RowIterator::row
Row & row()
Definition: TFSQL.h:70
OD::TFSQL::Statement
Definition: TFSQL.h:49
OD::TFSQL::Statement::Params::set
virtual Params & set(int at, const OdAnsiString &val)=0
OdRxObject
Definition: RxObject.h:564
OD::TFSQL::exe
virtual void exe(const char *sql, SQLReadCallback *cb=0)=0
OD::TFSQL::Statement::Params::set
virtual Params & set(int at, OdUInt16 val)=0
OD::TFSQL::Statement::Params::set
virtual Params & set(int at, OdUInt64 val)=0
OD::TFSQL::SQLReadCallback
Definition: TFSQL.h:122
OD::TFSQL::prepare
virtual Statement::ParamsP prepare(const char *sql, int at=-1, StatementP *stmt=0)=0
OD::TFSQL::Statement::ResultP
OdSmartPtr< Result > ResultP
Definition: TFSQL.h:74
OD::TFSQL::Statement::Params::set
virtual Params & set(int at, OdUInt32 val)=0
RxIterator.h
OD::TFSQL::Statement::RowP
OdSmartPtr< Row > RowP
Definition: TFSQL.h:66
OD::TFSQL::Statement::ParamsP
OdSmartPtr< Params > ParamsP
Definition: TFSQL.h:91
OdUInt16
unsigned short OdUInt16
Definition: OdPlatformSettings.h:760
OD::TFSQL::create
static TFSQLP create(const OdString &pathToData=OdString::kEmpty)
Definition: TFSQL.h:134
OD::TFSQL::Statement::Params::set
virtual Params & set(int at, double val)=0
RxObject.h
OdUInt32
unsigned int OdUInt32
Definition: OdPlatformSettings.h:783
OdTFSQLStatementPtr
OdSmartPtr< OdTFSQLStatement > OdTFSQLStatementPtr
Definition: TFSQL.h:146
OD::TFSQL::endTransaction
void endTransaction()
Definition: TFSQL.h:39
OD::TFSQL::beginTransaction
void beginTransaction()
Definition: TFSQL.h:38
OD::TFSQL::prepareOrReset
Statement::Params & prepareOrReset(const char *sql, int len, StatementP &stmt)
Definition: TFSQL.h:127
OD::TFSQL::Statement::Params::set
virtual Params & set(int at, const OdString &val)=0
OdTFSQLBlob
OD::TFSQL::Blob OdTFSQLBlob
Definition: TFSQL.h:143
OD::TFSQL::ODRX_DECLARE_MEMBERS
ODRX_DECLARE_MEMBERS(TFSQL)
OD::TFSQL::openR
BlobP openR(const char *tab, const char *col, OdInt64 row, const char *db="main")
Definition: TFSQL.h:44
OD::TFSQL::SQLReadCallback::on
virtual bool on(StatementRow &row)=0
OdStreamBuf.h
OD::TFSQL::Statement::Result
RowIterator Result
Definition: TFSQL.h:73
OD::TFSQL::exe
virtual void exe(const char *sql, sqlite3_callback cb, void *callbackctx)=0
OD::File::Write
@ Write
Definition: RxFS.h:62
OdSmartPtr
Definition: SmartPtr.h:58
OD::TFSQL::Statement::Row::utf16
virtual OdString utf16(int i)=0
OD::TFSQL
Definition: TFSQL.h:23
OD::TFSQL::StatementP
OdSmartPtr< Statement > StatementP
Definition: TFSQL.h:32
OD::TFSQL::abortTransaction
void abortTransaction()
Definition: TFSQL.h:40
OD::TFSQL::Statement::Params::set
virtual Params & set(int at, OdStreamBuf *val)=0
OD::TFSQLP
OdSmartPtr< TFSQL > TFSQLP
Definition: TFSQL.h:20
OD::TFSQL::close
virtual void close()=0
OD::TFSQL::Statement::Row::open
virtual BlobP open(int i, File::Access acc=File::Read)=0
OD::TFSQL::Statement::Params::exe
virtual ResultP exe()=0
DynamicLinker.h
OD::File::Read
@ Read
Definition: RxFS.h:61
OD::TFSQL::Statement::Params::set
virtual Params & set(int at, bool val)=0
OdRxDynamicLinker::loadApp
virtual OdRxModulePtr loadApp(const OdString &applicationName, bool silent=true)=0
OD::TFSQL::Statement::RowIteratorP
OdSmartPtr< RowIterator > RowIteratorP
Definition: TFSQL.h:72
OdTFSQLReadCallback
OD::TFSQL::SQLReadCallback OdTFSQLReadCallback
Definition: TFSQL.h:147
OD::TFSQL::Statement::Row::int32
virtual OdUInt32 int32(int i)=0
OD::TFSQL::Statement::reset
virtual ParamsP reset(const char *sql=0, int len=-1)=0
OdTFSQLStatement
OD::TFSQL::Statement OdTFSQLStatement
Definition: TFSQL.h:145
odrxCreateObject
FIRSTDLL_EXPORT OdRxObjectPtr odrxCreateObject(const OdString &sClassName)
OD::TFSQL::lastInsertRowId
virtual OdUInt64 lastInsertRowId()=0
OD::TFSQL::Statement::Row::int16
virtual OdUInt16 int16(int i)=0
exe
exe
Definition: DimVarDefs.h:1095
OD::TFSQL::Blob
OdStreamBuf Blob
Definition: TFSQL.h:35
OD::TFSQL::open
virtual BlobP open(const char *tab, const char *col, OdInt64 row, int mode=File::Read, const char *db="main")=0
OdStreamBuf
Definition: OdStreamBuf.h:67
OD::TFSQL::Statement::Row::realD
virtual double realD(int i)=0
OD::TFSQL::Statement::Params::set
virtual Params & set(int at, const void *val, OdUInt64 len)=0
OD::TFSQL::Statement::Row::isNull
virtual bool isNull(int i)=0
OD::TFSQL::Statement::Row::int8
virtual OdUInt8 int8(int i)=0
OD::File::Access
Access
Definition: RxFS.h:60
OD::TFSQL::Statement::Row::real
virtual float real(int i)=0
OD::TFSQL::Statement::Row::utf8
virtual OdAnsiString utf8(int i)=0
OdString::kEmpty
FIRSTDLL_EXPORT_STATIC static const OdString kEmpty
Definition: OdString.h:98
OD::TFSQL::StatementRow
OdSmartPtr< Statement::Row > StatementRow
Definition: TFSQL.h:95
OD::TFSQL::Statement::Row::blob
virtual const void * blob(int i, OdUInt64 *len=0)=0
OD::TFSQL::Statement::Params
Definition: TFSQL.h:76
OD::TFSQL::Statement::Row::int64
virtual OdUInt64 int64(int i)=0
TfIO.h
OD::TFSQL::attach
virtual void attach(const OdString &pathToData)=0
OD
Definition: OdPathUtil.h:33
OD::File::open
StreamP open(const OdString &path, int acc=Read, Creation dispos=OpenExisting, Share sh=DenyNo)
Definition: RxFS.h:239
OdSmartPtr::attach
void attach(const T *pObject)
Definition: SmartPtr.h:215
OD::TFSQL::Statement::Row
Definition: TFSQL.h:51
OD::TFSQL::path
virtual OdString path() const =0
OD::TFSQL::openRW
BlobP openRW(const char *tab, const char *col, OdInt64 row, const char *db="main")
Definition: TFSQL.h:46
OD::TFSQL::openW
BlobP openW(const char *tab, const char *col, OdInt64 row, const char *db="main")
Definition: TFSQL.h:45
OdUInt64
Definition: Int64.h:137
RxFS.h