├── Include └── SQLite3 │ ├── Backup.mqh │ ├── Blob.mqh │ ├── Common.mqh │ ├── Defines.mqh │ ├── Imports.mqh │ ├── SQLite3.mqh │ ├── Statement.mqh │ └── Vfs.mqh ├── LICENSE ├── Library ├── MT4 │ └── sqlite3.dll └── MT5 │ └── sqlite3.dll ├── README.md └── Scripts └── Test └── TestSQLite3.mq4 /Include/SQLite3/Backup.mqh: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| Backup.mqh | 3 | //| Copyright 2017, Li Ding | 4 | //| dingmaotu@126.com | 5 | //+------------------------------------------------------------------+ 6 | #property strict 7 | 8 | #include "SQLite3.mqh" 9 | //+------------------------------------------------------------------+ 10 | //| Backup operation | 11 | //+------------------------------------------------------------------+ 12 | class Backup 13 | { 14 | private: 15 | intptr_t m_ref; 16 | public: 17 | Backup(const SQLite3 &dest,string destDbName,const SQLite3 &src,string srcDbName) 18 | { 19 | uchar destDbNameBuf[]; 20 | uchar srcDbNameBuf[]; 21 | StringToUtf8(destDbName,destDbNameBuf); 22 | StringToUtf8(srcDbName,srcDbNameBuf); 23 | m_ref=sqlite3_backup_init(dest.ref(),destDbNameBuf,src.ref(),srcDbNameBuf); 24 | } 25 | 26 | ~Backup() {sqlite3_backup_finish(m_ref);} 27 | bool isValid() const {return m_ref!=NULL;} 28 | intptr_t ref() const {return m_ref;} 29 | 30 | // SQLITE_OK | SQLITE_DONE for success completion 31 | // SQLITE_READONLY | SQLITE_IOERR_* | SQLITE_NOMEM is condidered fatal 32 | // SQLITE_BUSY if timeout waiting resource lock 33 | int step(int pages) {return sqlite3_backup_step(m_ref,pages);} 34 | int getPageCount() const {return sqlite3_backup_pagecount(m_ref);} 35 | int getRemaining() const {return sqlite3_backup_remaining(m_ref);} 36 | }; 37 | //+------------------------------------------------------------------+ 38 | -------------------------------------------------------------------------------- /Include/SQLite3/Blob.mqh: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| Blob.mqh | 3 | //| Copyright 2017, Li Ding | 4 | //| dingmaotu@126.com | 5 | //+------------------------------------------------------------------+ 6 | #property strict 7 | 8 | #include "SQLite3.mqh" 9 | //+------------------------------------------------------------------+ 10 | //| Blob for streaming IO | 11 | //+------------------------------------------------------------------+ 12 | class Blob 13 | { 14 | private: 15 | bool m_valid; 16 | intptr_t m_ref; 17 | public: 18 | Blob(const SQLite3 &conn,string db,string table,string column,long row,bool readonly=true) 19 | { 20 | char zDb[]; 21 | char zTable[]; 22 | char zColumn[]; 23 | StringToUtf8(db,zDb); 24 | StringToUtf8(table,zTable); 25 | StringToUtf8(column,zColumn); 26 | 27 | int res=sqlite3_blob_open(conn.ref(),zDb,zTable,zColumn,row,readonly?0:1,m_ref); 28 | m_valid=(res==SQLITE_OK); 29 | } 30 | ~Blob() {if(m_valid) sqlite3_blob_close(m_ref);} 31 | 32 | bool isValid() const {return m_valid;} 33 | intptr_t ref() const {return m_ref;} 34 | 35 | int size() const {return sqlite3_blob_bytes(m_ref); } 36 | 37 | int moveTo(long rowId) {return sqlite3_blob_reopen(m_ref,rowId);} 38 | int read(uchar &buf[],int size,int offset) {return sqlite3_blob_read(m_ref,buf,size,offset);} 39 | int write(const uchar &buf[],int size,int offset) {return sqlite3_blob_write(m_ref,buf,size,offset);} 40 | }; 41 | //+------------------------------------------------------------------+ 42 | -------------------------------------------------------------------------------- /Include/SQLite3/Common.mqh: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| Common.mqh | 3 | //| Copyright 2016-2017, Li Ding | 4 | //| dingmaotu@126.com | 5 | //+------------------------------------------------------------------+ 6 | #property strict 7 | 8 | // Assume MT5 is 64bit, which is the default. 9 | // Even though MT5 can be 32bit, there is no way to detect this 10 | // by using preprocessor macros. Instead, MetaQuotes provides a 11 | // function called IsX64 to detect this dynamically 12 | 13 | // This is just absurd. Why do you want to know the bitness of 14 | // the runtime? To define pointer related entities at compile time! 15 | // All integer types in MQL is uniform on both 32bit or 64bit 16 | // architectures, so it is almost useless to have a runtime function IsX64. 17 | 18 | // Why not a __X64__? 19 | #ifdef __MQL5__ 20 | #define __X64__ 21 | #endif 22 | 23 | #ifdef __X64__ 24 | #define intptr_t long 25 | #define uintptr_t ulong 26 | #define size_t long 27 | #else 28 | #define intptr_t int 29 | #define uintptr_t uint 30 | #define size_t int 31 | #endif 32 | 33 | #import "kernel32.dll" 34 | void RtlMoveMemory(intptr_t dest,const uchar &array[],size_t length); 35 | void RtlMoveMemory(uchar &array[],intptr_t src,size_t length); 36 | int lstrlen(intptr_t psz); 37 | int MultiByteToWideChar(uint codePage, 38 | uint flags, 39 | const intptr_t multiByteString, 40 | int lengthMultiByte, 41 | string &str, 42 | int length 43 | ); 44 | #import 45 | //+------------------------------------------------------------------+ 46 | //| Copy the memory contents pointed by src to array | 47 | //| array parameter should be initialized to the desired size | 48 | //+------------------------------------------------------------------+ 49 | void ArrayFromPointer(uchar &array[],intptr_t src,int count=WHOLE_ARRAY) 50 | { 51 | int size=(count==WHOLE_ARRAY)?ArraySize(array):count; 52 | RtlMoveMemory(array,src,(size_t)size); 53 | } 54 | //+------------------------------------------------------------------+ 55 | //| Copy array to the memory pointed by dest | 56 | //+------------------------------------------------------------------+ 57 | void ArrayToPointer(const uchar &array[],intptr_t dest,int count=WHOLE_ARRAY) 58 | { 59 | int size=(count==WHOLE_ARRAY)?ArraySize(array):count; 60 | RtlMoveMemory(dest,array,(size_t)size); 61 | } 62 | //+------------------------------------------------------------------+ 63 | //| Read a valid utf-8 string to the MQL environment | 64 | //| With this function, there is no need to copy the string to char | 65 | //| array, and convert with CharArrayToString | 66 | //+------------------------------------------------------------------+ 67 | string StringFromUtf8Pointer(intptr_t psz,int len) 68 | { 69 | if(len < 0) return NULL; 70 | string res; 71 | int required=MultiByteToWideChar(CP_UTF8,0,psz,len,res,0); 72 | StringInit(res,required); 73 | int resLength = MultiByteToWideChar(CP_UTF8,0,psz,len,res,required); 74 | if(resLength != required) 75 | { 76 | return NULL; 77 | } 78 | else 79 | { 80 | return res; 81 | } 82 | } 83 | //+------------------------------------------------------------------+ 84 | //| for null-terminated string | 85 | //+------------------------------------------------------------------+ 86 | string StringFromUtf8Pointer(intptr_t psz) 87 | { 88 | if(psz==0) return NULL; 89 | int len=lstrlen(psz); 90 | if(len==0) return NULL; 91 | return StringFromUtf8Pointer(psz, len); 92 | } 93 | //+------------------------------------------------------------------+ 94 | //| Convert a utf-8 byte array to a string | 95 | //+------------------------------------------------------------------+ 96 | string StringFromUtf8(const uchar &utf8[]) 97 | { 98 | return CharArrayToString(utf8, 0, -1, CP_UTF8); 99 | } 100 | //+------------------------------------------------------------------+ 101 | //| Convert a string to a utf-8 byte array | 102 | //+------------------------------------------------------------------+ 103 | void StringToUtf8(const string str,uchar &utf8[],bool ending=true) 104 | { 105 | if(!ending && str=="") return; 106 | int count=ending ? -1 : StringLen(str); 107 | StringToCharArray(str,utf8,0,count,CP_UTF8); 108 | } 109 | //+------------------------------------------------------------------+ 110 | -------------------------------------------------------------------------------- /Include/SQLite3/Defines.mqh: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| Defines.mqh | 3 | //| Copyright 2017, Li Ding | 4 | //| dingmaotu@126.com | 5 | //+------------------------------------------------------------------+ 6 | #property strict 7 | 8 | // The defines come from sqlite3.h 9 | 10 | #define SQLITE_VERSION "3.18.0" 11 | #define SQLITE_VERSION_NUMBER 3018000 12 | #define SQLITE_SOURCE_ID "2017-03-28 18:48:43 424a0d380332858ee55bdebc4af3789f74e70a2b3ba1cf29d84b9b4bcf3e2e37" 13 | 14 | /* 15 | ** CAPI3REF: Fundamental Datatypes 16 | ** KEYWORDS: SQLITE_TEXT 17 | ** 18 | ** (Every value in SQLite has one of five fundamental datatypes: 19 | ** 20 | ** 1. 64-bit signed integer 21 | ** 2. 64-bit IEEE floating point number 22 | ** 3. string 23 | ** 4. BLOB 24 | ** 5. NULL 25 | */ 26 | #define SQLITE_INTEGER 1 27 | #define SQLITE_FLOAT 2 28 | #define SQLITE_BLOB 4 29 | #define SQLITE_NULL 5 30 | #define SQLITE_TEXT 3 31 | #define SQLITE3_TEXT 3 // use only if link with both sqlite2 and sqlite3 32 | 33 | /* 34 | ** CAPI3REF: Text Encodings 35 | ** 36 | ** These constant define integer codes that represent the various 37 | ** text encodings supported by SQLite. 38 | */ 39 | #define SQLITE_UTF8 1 /* IMP: R-37514-35566 */ 40 | #define SQLITE_UTF16LE 2 /* IMP: R-03371-37637 */ 41 | #define SQLITE_UTF16BE 3 /* IMP: R-51971-34154 */ 42 | #define SQLITE_UTF16 4 /* Use native byte order */ 43 | #define SQLITE_ANY 5 /* Deprecated */ 44 | #define SQLITE_UTF16_ALIGNED 8 /* sqlite3_create_collation only */ 45 | 46 | /* 47 | ** CAPI3REF: Result Codes 48 | ** KEYWORDS: {result code definitions} 49 | ** 50 | ** Many SQLite functions return an integer result code from the set shown 51 | ** here in order to indicate success or failure. 52 | ** 53 | ** New error codes may be added in future versions of SQLite. 54 | ** 55 | ** See also: [extended result code definitions] 56 | */ 57 | #define SQLITE_OK 0 /* Successful result */ 58 | /* beginning-of-error-codes */ 59 | #define SQLITE_ERROR 1 /* SQL error or missing database */ 60 | #define SQLITE_INTERNAL 2 /* Internal logic error in SQLite */ 61 | #define SQLITE_PERM 3 /* Access permission denied */ 62 | #define SQLITE_ABORT 4 /* Callback routine requested an abort */ 63 | #define SQLITE_BUSY 5 /* The database file is locked */ 64 | #define SQLITE_LOCKED 6 /* A table in the database is locked */ 65 | #define SQLITE_NOMEM 7 /* A malloc() failed */ 66 | #define SQLITE_READONLY 8 /* Attempt to write a readonly database */ 67 | #define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite3_interrupt()*/ 68 | #define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */ 69 | #define SQLITE_CORRUPT 11 /* The database disk image is malformed */ 70 | #define SQLITE_NOTFOUND 12 /* Unknown opcode in sqlite3_file_control() */ 71 | #define SQLITE_FULL 13 /* Insertion failed because database is full */ 72 | #define SQLITE_CANTOPEN 14 /* Unable to open the database file */ 73 | #define SQLITE_PROTOCOL 15 /* Database lock protocol error */ 74 | #define SQLITE_EMPTY 16 /* Database is empty */ 75 | #define SQLITE_SCHEMA 17 /* The database schema changed */ 76 | #define SQLITE_TOOBIG 18 /* String or BLOB exceeds size limit */ 77 | #define SQLITE_CONSTRAINT 19 /* Abort due to constraint violation */ 78 | #define SQLITE_MISMATCH 20 /* Data type mismatch */ 79 | #define SQLITE_MISUSE 21 /* Library used incorrectly */ 80 | #define SQLITE_NOLFS 22 /* Uses OS features not supported on host */ 81 | #define SQLITE_AUTH 23 /* Authorization denied */ 82 | #define SQLITE_FORMAT 24 /* Auxiliary database format error */ 83 | #define SQLITE_RANGE 25 /* 2nd parameter to sqlite3_bind out of range */ 84 | #define SQLITE_NOTADB 26 /* File opened that is not a database file */ 85 | #define SQLITE_NOTICE 27 /* Notifications from sqlite3_log() */ 86 | #define SQLITE_WARNING 28 /* Warnings from sqlite3_log() */ 87 | #define SQLITE_ROW 100 /* sqlite3_step() has another row ready */ 88 | #define SQLITE_DONE 101 /* sqlite3_step() has finished executing */ 89 | /* end-of-error-codes */ 90 | 91 | /* 92 | ** CAPI3REF: Extended Result Codes 93 | ** KEYWORDS: {extended result code definitions} 94 | ** 95 | ** In its default configuration, SQLite API routines return one of 30 integer 96 | ** [result codes]. However, experience has shown that many of 97 | ** these result codes are too coarse-grained. They do not provide as 98 | ** much information about problems as programmers might like. In an effort to 99 | ** address this, newer versions of SQLite (version 3.3.8 [dateof:3.3.8] 100 | ** and later) include 101 | ** support for additional result codes that provide more detailed information 102 | ** about errors. These [extended result codes] are enabled or disabled 103 | ** on a per database connection basis using the 104 | ** [sqlite3_extended_result_codes()] API. Or, the extended code for 105 | ** the most recent error can be obtained using 106 | ** [sqlite3_extended_errcode()]. 107 | */ 108 | #define SQLITE_IOERR_READ (SQLITE_IOERR | (1<<8)) 109 | #define SQLITE_IOERR_SHORT_READ (SQLITE_IOERR | (2<<8)) 110 | #define SQLITE_IOERR_WRITE (SQLITE_IOERR | (3<<8)) 111 | #define SQLITE_IOERR_FSYNC (SQLITE_IOERR | (4<<8)) 112 | #define SQLITE_IOERR_DIR_FSYNC (SQLITE_IOERR | (5<<8)) 113 | #define SQLITE_IOERR_TRUNCATE (SQLITE_IOERR | (6<<8)) 114 | #define SQLITE_IOERR_FSTAT (SQLITE_IOERR | (7<<8)) 115 | #define SQLITE_IOERR_UNLOCK (SQLITE_IOERR | (8<<8)) 116 | #define SQLITE_IOERR_RDLOCK (SQLITE_IOERR | (9<<8)) 117 | #define SQLITE_IOERR_DELETE (SQLITE_IOERR | (10<<8)) 118 | #define SQLITE_IOERR_BLOCKED (SQLITE_IOERR | (11<<8)) 119 | #define SQLITE_IOERR_NOMEM (SQLITE_IOERR | (12<<8)) 120 | #define SQLITE_IOERR_ACCESS (SQLITE_IOERR | (13<<8)) 121 | #define SQLITE_IOERR_CHECKRESERVEDLOCK (SQLITE_IOERR | (14<<8)) 122 | #define SQLITE_IOERR_LOCK (SQLITE_IOERR | (15<<8)) 123 | #define SQLITE_IOERR_CLOSE (SQLITE_IOERR | (16<<8)) 124 | #define SQLITE_IOERR_DIR_CLOSE (SQLITE_IOERR | (17<<8)) 125 | #define SQLITE_IOERR_SHMOPEN (SQLITE_IOERR | (18<<8)) 126 | #define SQLITE_IOERR_SHMSIZE (SQLITE_IOERR | (19<<8)) 127 | #define SQLITE_IOERR_SHMLOCK (SQLITE_IOERR | (20<<8)) 128 | #define SQLITE_IOERR_SHMMAP (SQLITE_IOERR | (21<<8)) 129 | #define SQLITE_IOERR_SEEK (SQLITE_IOERR | (22<<8)) 130 | #define SQLITE_IOERR_DELETE_NOENT (SQLITE_IOERR | (23<<8)) 131 | #define SQLITE_IOERR_MMAP (SQLITE_IOERR | (24<<8)) 132 | #define SQLITE_IOERR_GETTEMPPATH (SQLITE_IOERR | (25<<8)) 133 | #define SQLITE_IOERR_CONVPATH (SQLITE_IOERR | (26<<8)) 134 | #define SQLITE_IOERR_VNODE (SQLITE_IOERR | (27<<8)) 135 | #define SQLITE_IOERR_AUTH (SQLITE_IOERR | (28<<8)) 136 | #define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8)) 137 | #define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8)) 138 | #define SQLITE_BUSY_SNAPSHOT (SQLITE_BUSY | (2<<8)) 139 | #define SQLITE_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8)) 140 | #define SQLITE_CANTOPEN_ISDIR (SQLITE_CANTOPEN | (2<<8)) 141 | #define SQLITE_CANTOPEN_FULLPATH (SQLITE_CANTOPEN | (3<<8)) 142 | #define SQLITE_CANTOPEN_CONVPATH (SQLITE_CANTOPEN | (4<<8)) 143 | #define SQLITE_CORRUPT_VTAB (SQLITE_CORRUPT | (1<<8)) 144 | #define SQLITE_READONLY_RECOVERY (SQLITE_READONLY | (1<<8)) 145 | #define SQLITE_READONLY_CANTLOCK (SQLITE_READONLY | (2<<8)) 146 | #define SQLITE_READONLY_ROLLBACK (SQLITE_READONLY | (3<<8)) 147 | #define SQLITE_READONLY_DBMOVED (SQLITE_READONLY | (4<<8)) 148 | #define SQLITE_ABORT_ROLLBACK (SQLITE_ABORT | (2<<8)) 149 | #define SQLITE_CONSTRAINT_CHECK (SQLITE_CONSTRAINT | (1<<8)) 150 | #define SQLITE_CONSTRAINT_COMMITHOOK (SQLITE_CONSTRAINT | (2<<8)) 151 | #define SQLITE_CONSTRAINT_FOREIGNKEY (SQLITE_CONSTRAINT | (3<<8)) 152 | #define SQLITE_CONSTRAINT_FUNCTION (SQLITE_CONSTRAINT | (4<<8)) 153 | #define SQLITE_CONSTRAINT_NOTNULL (SQLITE_CONSTRAINT | (5<<8)) 154 | #define SQLITE_CONSTRAINT_PRIMARYKEY (SQLITE_CONSTRAINT | (6<<8)) 155 | #define SQLITE_CONSTRAINT_TRIGGER (SQLITE_CONSTRAINT | (7<<8)) 156 | #define SQLITE_CONSTRAINT_UNIQUE (SQLITE_CONSTRAINT | (8<<8)) 157 | #define SQLITE_CONSTRAINT_VTAB (SQLITE_CONSTRAINT | (9<<8)) 158 | #define SQLITE_CONSTRAINT_ROWID (SQLITE_CONSTRAINT |(10<<8)) 159 | #define SQLITE_NOTICE_RECOVER_WAL (SQLITE_NOTICE | (1<<8)) 160 | #define SQLITE_NOTICE_RECOVER_ROLLBACK (SQLITE_NOTICE | (2<<8)) 161 | #define SQLITE_WARNING_AUTOINDEX (SQLITE_WARNING | (1<<8)) 162 | #define SQLITE_AUTH_USER (SQLITE_AUTH | (1<<8)) 163 | #define SQLITE_OK_LOAD_PERMANENTLY (SQLITE_OK | (1<<8)) 164 | 165 | /* 166 | ** CAPI3REF: Flags For File Open Operations 167 | ** 168 | ** These bit values are intended for use in the 169 | ** 3rd parameter to the [sqlite3_open_v2()] interface and 170 | ** in the 4th parameter to the [sqlite3_vfs.xOpen] method. 171 | */ 172 | #define SQLITE_OPEN_READONLY 0x00000001 /* Ok for sqlite3_open_v2() */ 173 | #define SQLITE_OPEN_READWRITE 0x00000002 /* Ok for sqlite3_open_v2() */ 174 | #define SQLITE_OPEN_CREATE 0x00000004 /* Ok for sqlite3_open_v2() */ 175 | #define SQLITE_OPEN_DELETEONCLOSE 0x00000008 /* VFS only */ 176 | #define SQLITE_OPEN_EXCLUSIVE 0x00000010 /* VFS only */ 177 | #define SQLITE_OPEN_AUTOPROXY 0x00000020 /* VFS only */ 178 | #define SQLITE_OPEN_URI 0x00000040 /* Ok for sqlite3_open_v2() */ 179 | #define SQLITE_OPEN_MEMORY 0x00000080 /* Ok for sqlite3_open_v2() */ 180 | #define SQLITE_OPEN_MAIN_DB 0x00000100 /* VFS only */ 181 | #define SQLITE_OPEN_TEMP_DB 0x00000200 /* VFS only */ 182 | #define SQLITE_OPEN_TRANSIENT_DB 0x00000400 /* VFS only */ 183 | #define SQLITE_OPEN_MAIN_JOURNAL 0x00000800 /* VFS only */ 184 | #define SQLITE_OPEN_TEMP_JOURNAL 0x00001000 /* VFS only */ 185 | #define SQLITE_OPEN_SUBJOURNAL 0x00002000 /* VFS only */ 186 | #define SQLITE_OPEN_MASTER_JOURNAL 0x00004000 /* VFS only */ 187 | #define SQLITE_OPEN_NOMUTEX 0x00008000 /* Ok for sqlite3_open_v2() */ 188 | #define SQLITE_OPEN_FULLMUTEX 0x00010000 /* Ok for sqlite3_open_v2() */ 189 | #define SQLITE_OPEN_SHAREDCACHE 0x00020000 /* Ok for sqlite3_open_v2() */ 190 | #define SQLITE_OPEN_PRIVATECACHE 0x00040000 /* Ok for sqlite3_open_v2() */ 191 | #define SQLITE_OPEN_WAL 0x00080000 /* VFS only */ 192 | 193 | /* Reserved: 0x00F00000 */ 194 | 195 | /* 196 | ** CAPI3REF: Configuration Options 197 | ** KEYWORDS: {configuration option} 198 | ** 199 | ** These constants are the available integer configuration options that 200 | ** can be passed as the first argument to the [sqlite3_config()] interface. 201 | ** 202 | ** New configuration options may be added in future releases of SQLite. 203 | ** Existing configuration options might be discontinued. Applications 204 | ** should check the return code from [sqlite3_config()] to make sure that 205 | ** the call worked. The [sqlite3_config()] interface will return a 206 | ** non-zero [error code] if a discontinued or unsupported configuration option 207 | ** is invoked. 208 | ** 209 | */ 210 | #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */ 211 | #define SQLITE_CONFIG_MULTITHREAD 2 /* nil */ 212 | #define SQLITE_CONFIG_SERIALIZED 3 /* nil */ 213 | #define SQLITE_CONFIG_MALLOC 4 /* sqlite3_mem_methods* */ 214 | #define SQLITE_CONFIG_GETMALLOC 5 /* sqlite3_mem_methods* */ 215 | #define SQLITE_CONFIG_SCRATCH 6 /* void*, int sz, int N */ 216 | #define SQLITE_CONFIG_PAGECACHE 7 /* void*, int sz, int N */ 217 | #define SQLITE_CONFIG_HEAP 8 /* void*, int nByte, int min */ 218 | #define SQLITE_CONFIG_MEMSTATUS 9 /* boolean */ 219 | #define SQLITE_CONFIG_MUTEX 10 /* sqlite3_mutex_methods* */ 220 | #define SQLITE_CONFIG_GETMUTEX 11 /* sqlite3_mutex_methods* */ 221 | /* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */ 222 | #define SQLITE_CONFIG_LOOKASIDE 13 /* int int */ 223 | #define SQLITE_CONFIG_PCACHE 14 /* no-op */ 224 | #define SQLITE_CONFIG_GETPCACHE 15 /* no-op */ 225 | #define SQLITE_CONFIG_LOG 16 /* xFunc, void* */ 226 | #define SQLITE_CONFIG_URI 17 /* int */ 227 | #define SQLITE_CONFIG_PCACHE2 18 /* sqlite3_pcache_methods2* */ 228 | #define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */ 229 | #define SQLITE_CONFIG_COVERING_INDEX_SCAN 20 /* int */ 230 | #define SQLITE_CONFIG_SQLLOG 21 /* xSqllog, void* */ 231 | #define SQLITE_CONFIG_MMAP_SIZE 22 /* sqlite3_int64, sqlite3_int64 */ 232 | #define SQLITE_CONFIG_WIN32_HEAPSIZE 23 /* int nByte */ 233 | #define SQLITE_CONFIG_PCACHE_HDRSZ 24 /* int *psz */ 234 | #define SQLITE_CONFIG_PMASZ 25 /* unsigned int szPma */ 235 | #define SQLITE_CONFIG_STMTJRNL_SPILL 26 /* int nByte */ 236 | /* 237 | ** CAPI3REF: Database Connection Configuration Options 238 | ** 239 | ** These constants are the available integer configuration options that 240 | ** can be passed as the second argument to the [sqlite3_db_config()] interface. 241 | ** 242 | ** New configuration options may be added in future releases of SQLite. 243 | ** Existing configuration options might be discontinued. Applications 244 | ** should check the return code from [sqlite3_db_config()] to make sure that 245 | ** the call worked. ^The [sqlite3_db_config()] interface will return a 246 | ** non-zero [error code] if a discontinued or unsupported configuration option 247 | ** is invoked. 248 | */ 249 | #define SQLITE_DBCONFIG_MAINDBNAME 1000 /* const char* */ 250 | #define SQLITE_DBCONFIG_LOOKASIDE 1001 /* void* int int */ 251 | #define SQLITE_DBCONFIG_ENABLE_FKEY 1002 /* int int* */ 252 | #define SQLITE_DBCONFIG_ENABLE_TRIGGER 1003 /* int int* */ 253 | #define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004 /* int int* */ 254 | #define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */ 255 | #define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE 1006 /* int int* */ 256 | 257 | /* 258 | ** CAPI3REF: Run-Time Limit Categories 259 | ** KEYWORDS: {limit category} {*limit categories} 260 | ** 261 | ** These constants define various performance limits 262 | ** that can be lowered at run-time using [sqlite3_limit()]. 263 | ** The synopsis of the meanings of the various limits is shown below. 264 | ** Additional information is available at [limits | Limits in SQLite]. 265 | */ 266 | #define SQLITE_LIMIT_LENGTH 0 267 | #define SQLITE_LIMIT_SQL_LENGTH 1 268 | #define SQLITE_LIMIT_COLUMN 2 269 | #define SQLITE_LIMIT_EXPR_DEPTH 3 270 | #define SQLITE_LIMIT_COMPOUND_SELECT 4 271 | #define SQLITE_LIMIT_VDBE_OP 5 272 | #define SQLITE_LIMIT_FUNCTION_ARG 6 273 | #define SQLITE_LIMIT_ATTACHED 7 274 | #define SQLITE_LIMIT_LIKE_PATTERN_LENGTH 8 275 | #define SQLITE_LIMIT_VARIABLE_NUMBER 9 276 | #define SQLITE_LIMIT_TRIGGER_DEPTH 10 277 | #define SQLITE_LIMIT_WORKER_THREADS 11 278 | 279 | // Constants Defining Special Destructor Behavior 280 | #define SQLITE_STATIC 0 281 | #define SQLITE_TRANSIENT -1 282 | 283 | // Statment status counters 284 | #define SQLITE_STMTSTATUS_FULLSCAN_STEP 1 285 | #define SQLITE_STMTSTATUS_SORT 2 286 | #define SQLITE_STMTSTATUS_AUTOINDEX 3 287 | #define SQLITE_STMTSTATUS_VM_STEP 4 288 | 289 | // Checkpoint mode 290 | #define SQLITE_CHECKPOINT_PASSIVE 0 /* Do as much as possible w/o blocking */ 291 | #define SQLITE_CHECKPOINT_FULL 1 /* Wait for writers, then checkpoint */ 292 | #define SQLITE_CHECKPOINT_RESTART 2 /* Like FULL but wait for for readers */ 293 | #define SQLITE_CHECKPOINT_TRUNCATE 3 /* Like RESTART but also truncate WAL */ 294 | //+------------------------------------------------------------------+ 295 | -------------------------------------------------------------------------------- /Include/SQLite3/Imports.mqh: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| Imports.mqh | 3 | //| Copyright 2017, Li Ding | 4 | //| dingmaotu@126.com | 5 | //+------------------------------------------------------------------+ 6 | #property strict 7 | 8 | // always use UTF-8 for all character strings 9 | // always use v2 for advanced features 10 | // only import essential APIs and implement others in MQL 11 | #include "Common.mqh" 12 | #include "Defines.mqh" 13 | 14 | #import "sqlite3.dll" 15 | 16 | intptr_t sqlite3_libversion(void); 17 | intptr_t sqlite3_sourceid(void); 18 | int sqlite3_libversion_number(void); 19 | 20 | int sqlite3_config(int); 21 | int sqlite3_config(int,int); 22 | int sqlite3_config(int,long); 23 | int sqlite3_config(int,int,int); 24 | 25 | int sqlite3_threadsafe(void); 26 | int sqlite3_enable_shared_cache(bool); 27 | 28 | int sqlite3_extended_result_codes(intptr_t,bool onoff); 29 | int sqlite3_enable_load_extension(intptr_t,bool onoff); 30 | int sqlite3_load_extension( 31 | intptr_t,/* Load the extension into this database connection */ 32 | const char &zFile[],/* Name of the shared library containing extension */ 33 | const char &zProc[],/* Entry point. Derived from zFile if 0 */ 34 | intptr_t &pzErrMsg /* Put error message here if not 0 */ 35 | ); 36 | int sqlite3_load_extension( 37 | intptr_t,/* Load the extension into this database connection */ 38 | const char &zFile[],/* Name of the shared library containing extension */ 39 | intptr_t zProc,/* Entry point. Derived from zFile if 0 */ 40 | intptr_t &pzErrMsg /* Put error message here if not 0 */ 41 | ); 42 | 43 | int sqlite3_get_autocommit(intptr_t); 44 | 45 | int sqlite3_initialize(void); 46 | int sqlite3_shutdown(void); 47 | 48 | int sqlite3_release_memory(int); 49 | long sqlite3_soft_heap_limit64(long); 50 | 51 | long sqlite3_memory_used(void); 52 | long sqlite3_memory_highwater(bool resetFlag); 53 | 54 | void sqlite3_free(intptr_t); 55 | 56 | // sqlite3 connection management 57 | int sqlite3_open_v2( 58 | const char &filename[],/* Database filename (UTF-8) */ 59 | intptr_t &pDb,/* OUT: SQLite db handle (sqlite3**) */ 60 | int flags,/* Flags */ 61 | intptr_t zVfs/* Name of VFS module to use */ 62 | ); 63 | int sqlite3_open_v2( 64 | const char &filename[],/* Database filename (UTF-8) */ 65 | intptr_t &pDb,/* OUT: SQLite db handle (sqlite3**) */ 66 | int flags,/* Flags */ 67 | const char &zVfs[]/* Name of VFS module to use */ 68 | ); 69 | int sqlite3_limit(intptr_t,int id,int newVal); 70 | int sqlite3_busy_timeout(intptr_t,int ms); 71 | 72 | intptr_t sqlite3_db_filename(intptr_t,const char &zDbName[]); 73 | int sqlite3_db_readonly(intptr_t,const char &zDbName[]); 74 | int sqlite3_table_column_metadata( 75 | intptr_t,/* Connection handle */ 76 | const char &zDbName[],/* Database name or NULL */ 77 | const char &zTableName[],/* Table name */ 78 | const char &zColumnName[],/* Column name */ 79 | intptr_t &pzDataType,/* OUTPUT: Declared data type */ 80 | intptr_t &pzCollSeq,/* OUTPUT: Collation sequence name */ 81 | bool &isNotNull,/* OUTPUT: True if NOT NULL constraint exists */ 82 | bool &isPrimaryKey,/* OUTPUT: True if column part of PK */ 83 | bool &isAutoinc/* OUTPUT: True if column is auto-increment */ 84 | ); 85 | 86 | int sqlite3_status(int op,int &pCurrent,int &pHighwater,bool reset=false); 87 | int sqlite3_status64(int op,long &pCurrent,long &pHighwater,bool reset=false); 88 | int sqlite3_complete(const char &sql[]); 89 | 90 | void sqlite3_interrupt(intptr_t); 91 | int sqlite3_db_cacheflush(intptr_t); 92 | int sqlite3_db_release_memory(intptr_t); 93 | 94 | int sqlite3_db_status(intptr_t,int op,int &pCurrent,int &pHighwater,bool reset=false); 95 | int sqlite3_db_config(intptr_t,int op,int,intptr_t&); 96 | int sqlite3_db_config(intptr_t,int op,bool,intptr_t); 97 | int sqlite3_db_config(intptr_t,int op,const uchar &dbName[]); 98 | int sqlite3_db_config(intptr_t,int op,intptr_t,int,int); 99 | 100 | long sqlite3_last_insert_rowid(intptr_t); 101 | void sqlite3_set_last_insert_rowid(intptr_t,long); 102 | int sqlite3_changes(intptr_t); 103 | int sqlite3_total_changes(intptr_t); 104 | 105 | int sqlite3_errcode(intptr_t); 106 | int sqlite3_extended_errcode(intptr_t); 107 | intptr_t sqlite3_errmsg(intptr_t); 108 | intptr_t sqlite3_errstr(int); 109 | 110 | int sqlite3_wal_autocheckpoint(intptr_t,int); 111 | int sqlite3_wal_checkpoint_v2( 112 | intptr_t,/* Database handle */ 113 | const char &zDb[],/* Name of attached database (or NULL) */ 114 | int eMode,/* SQLITE_CHECKPOINT_* value */ 115 | int &pnLog,/* OUT: Size of WAL log in frames */ 116 | int &pnCkpt/* OUT: Total number of frames checkpointed */ 117 | ); 118 | int sqlite3_wal_checkpoint_v2( 119 | intptr_t,/* Database handle */ 120 | intptr_t,/* Name of attached database (or NULL) */ 121 | int eMode,/* SQLITE_CHECKPOINT_* value */ 122 | int &pnLog,/* OUT: Size of WAL log in frames */ 123 | int &pnCkpt/* OUT: Total number of frames checkpointed */ 124 | ); 125 | 126 | int sqlite3_close(intptr_t); /* sqlite* */ 127 | 128 | // sqlite3 statement management 129 | int sqlite3_prepare_v2( 130 | intptr_t handle,/* Database handle */ 131 | const char &sql[],/* SQL statement, UTF-8 encoded */ 132 | int bytes,/* Maximum length of sql in bytes. */ 133 | intptr_t &pStmt,/* OUT: Statement handle (sqlite3_stmt **) */ 134 | intptr_t pzTail/* OUT: Pointer to unused portion of zSql (not used and always NULL) */ 135 | ); 136 | int sqlite3_reset(intptr_t pStmt); /* reset a prepared statement */ 137 | int sqlite3_clear_bindings(intptr_t pStmt); /* clear bindings of a prepared statement */ 138 | intptr_t sqlite3_db_handle(intptr_t pStmt); 139 | intptr_t sqlite3_sql(intptr_t pStmt); 140 | intptr_t sqlite3_expanded_sql(intptr_t pStmt); 141 | 142 | int sqlite3_bind_parameter_count(intptr_t pStmt); 143 | intptr_t sqlite3_bind_parameter_name(intptr_t pStmt,int); 144 | int sqlite3_bind_parameter_index(intptr_t pStmt,const char &name[]); 145 | 146 | int sqlite3_bind_blob(intptr_t,int,const uchar &value[],int,intptr_t); 147 | int sqlite3_bind_blob64(intptr_t,int,const uchar &value[],ulong,intptr_t); 148 | int sqlite3_bind_text(intptr_t,int,const char &value[],int,intptr_t); 149 | int sqlite3_bind_text64(intptr_t,int,const char &value[],ulong,intptr_t,uchar encoding); 150 | 151 | int sqlite3_bind_double(intptr_t,int,double); 152 | int sqlite3_bind_int(intptr_t,int,int); 153 | int sqlite3_bind_int64(intptr_t,int,long); 154 | int sqlite3_bind_null(intptr_t,int); 155 | int sqlite3_bind_zeroblob(intptr_t,int,int); 156 | int sqlite3_bind_zeroblob64(intptr_t,int,ulong); 157 | 158 | bool sqlite3_stmt_busy(intptr_t pStmt); 159 | bool sqlite3_stmt_readonly(intptr_t pStmt); 160 | int sqlite3_step(intptr_t pStmt); /* execute a prepared statement */ 161 | int sqlite3_stmt_status(intptr_t,int op,bool resetFlg); 162 | 163 | int sqlite3_data_count(intptr_t pStmt); 164 | int sqlite3_column_count(intptr_t pStmt); 165 | intptr_t sqlite3_column_name(intptr_t pStmt,int); 166 | int sqlite3_column_type(intptr_t pStmt,int i); 167 | int sqlite3_column_bytes(intptr_t pStmt,int i); 168 | 169 | double sqlite3_column_double(intptr_t pStmt,int i); 170 | int sqlite3_column_int(intptr_t pStmt,int i); 171 | long sqlite3_column_int64(intptr_t pStmt,int i); 172 | intptr_t sqlite3_column_text(intptr_t pStmt,int i); 173 | intptr_t sqlite3_column_blob(intptr_t pStmt,int i); 174 | 175 | intptr_t sqlite3_column_decltype(intptr_t pStmt,int); 176 | intptr_t sqlite3_column_database_name(intptr_t,int); 177 | intptr_t sqlite3_column_table_name(intptr_t,int); 178 | intptr_t sqlite3_column_origin_name(intptr_t,int); 179 | 180 | int sqlite3_finalize(intptr_t pStmt); /* destruct sqlite3_stmt */ 181 | 182 | // blob io streaming api 183 | int sqlite3_blob_open( 184 | intptr_t,/* pointer to sqlite3 connection */ 185 | const char &zDb[], 186 | const char &zTable[], 187 | const char &zColumn[], 188 | long row, 189 | int flags, 190 | intptr_t &pBlob 191 | ); 192 | int sqlite3_blob_reopen(intptr_t,long row); 193 | int sqlite3_blob_bytes(intptr_t); 194 | int sqlite3_blob_read(intptr_t,uchar &buf[],int n,int offset); 195 | int sqlite3_blob_write(intptr_t,const uchar &buf[],int n,int offset); 196 | int sqlite3_blob_close(intptr_t); 197 | 198 | // online backup api 199 | intptr_t sqlite3_backup_init( 200 | intptr_t pDest,/* Destination database handle */ 201 | const char &zDestName[],/* Destination database name */ 202 | intptr_t pSource,/* Source database handle */ 203 | const char &zSourceName[]/* Source database name */ 204 | ); 205 | int sqlite3_backup_step(intptr_t,int page); 206 | int sqlite3_backup_finish(intptr_t); 207 | int sqlite3_backup_remaining(intptr_t); 208 | int sqlite3_backup_pagecount(intptr_t); 209 | 210 | intptr_t sqlite3_vfs_find(const char &zVfsName[]); 211 | intptr_t sqlite3_vfs_find(intptr_t); 212 | int sqlite3_vfs_register(intptr_t,bool); 213 | int sqlite3_vfs_unregister(intptr_t); 214 | #import 215 | //+------------------------------------------------------------------+ 216 | //| Basic wrapper for sqlite3_open_v2 | 217 | //+------------------------------------------------------------------+ 218 | int sqlite3_open(const string &filename,intptr_t &handle,int flags,string vfs="") 219 | { 220 | uchar u8filename[]; 221 | StringToUtf8(filename,u8filename); 222 | int res=0; 223 | if(vfs=="") 224 | res=sqlite3_open_v2(u8filename,handle,flags,0); 225 | else 226 | { 227 | uchar u8vfs[]; 228 | StringToUtf8(vfs,u8vfs); 229 | res=sqlite3_open_v2(u8filename,handle,flags,u8vfs); 230 | } 231 | return res; 232 | } 233 | //+------------------------------------------------------------------+ 234 | //| Basic wrapper for sqlite3_prepare_v2 | 235 | //+------------------------------------------------------------------+ 236 | int sqlite3_prepare(const intptr_t handle,const string &sql,intptr_t &stmt) 237 | { 238 | uchar u8sql[]; 239 | StringToUtf8(sql,u8sql); 240 | int res=sqlite3_prepare_v2(handle,u8sql,ArraySize(u8sql),stmt,0); 241 | return res; 242 | } 243 | //+------------------------------------------------------------------+ 244 | -------------------------------------------------------------------------------- /Include/SQLite3/SQLite3.mqh: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| SQLite3.mqh | 3 | //| Copyright 2017, Li Ding | 4 | //| dingmaotu@126.com | 5 | //+------------------------------------------------------------------+ 6 | #property strict 7 | 8 | #include "Imports.mqh" 9 | //+------------------------------------------------------------------+ 10 | //| Represents column metadata | 11 | //+------------------------------------------------------------------+ 12 | struct ColumnInfo 13 | { 14 | public: 15 | string DataType; 16 | string CollationSequence; 17 | bool NotNull; 18 | bool PrimaryKey; 19 | bool AutoIncrement; 20 | }; 21 | //+------------------------------------------------------------------+ 22 | //| sqlite3 object representing a connection to a db | 23 | //+------------------------------------------------------------------+ 24 | class SQLite3 25 | { 26 | private: 27 | int m_valid; 28 | intptr_t m_ref; 29 | uchar m_dbName[]; 30 | public: 31 | // version numbers 32 | static int getVersionNumber() {return sqlite3_libversion_number();} 33 | static string getVersion() {return StringFromUtf8Pointer(sqlite3_libversion());} 34 | static string getSourceId() {return StringFromUtf8Pointer(sqlite3_sourceid());} 35 | 36 | // compile time thread safety setting 37 | static bool isThreadSafe() {return sqlite3_threadsafe()!=0;} 38 | 39 | // global configuration before initialize 40 | static int setSingleThreaded() {return sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);} 41 | static int setMultiThreaded() {return sqlite3_config(SQLITE_CONFIG_MULTITHREAD);} 42 | static int setSerialized() {return sqlite3_config(SQLITE_CONFIG_SERIALIZED);} 43 | 44 | static int setMemStatus(bool enabled) {return sqlite3_config(SQLITE_CONFIG_MEMSTATUS,int(enabled));} 45 | static int setUriHandling(bool enabled) {return sqlite3_config(SQLITE_CONFIG_URI,int(enabled));} 46 | static int setLookAside(int before,int after) {return sqlite3_config(SQLITE_CONFIG_LOOKASIDE,before,after);} 47 | 48 | static int setMmapSize(long size) {return sqlite3_config(SQLITE_CONFIG_MMAP_SIZE,size);} 49 | static int setWin32HeapSize(int size) {return sqlite3_config(SQLITE_CONFIG_WIN32_HEAPSIZE,size);} 50 | static int setStatementJournalThreshold(int size) {return sqlite3_config(SQLITE_CONFIG_STMTJRNL_SPILL,size);} 51 | 52 | static int initialize() {return sqlite3_initialize();} 53 | static int shutdown() {return sqlite3_shutdown();} 54 | 55 | static string errorCode2Msg(int code) {return StringFromUtf8Pointer(sqlite3_errstr(code));} 56 | static int status(int op,long ¤t,long &highwater,bool reset=false) {return sqlite3_status64(op,current,highwater,reset);} 57 | 58 | static int releaseMemoryInBytes(int bytes) {return sqlite3_release_memory(bytes);} 59 | static long setSoftHeapLimit(long bytes) {return sqlite3_soft_heap_limit64(bytes);} 60 | 61 | static long getMemoryUsed() {return sqlite3_memory_used(); } 62 | static long getMemoryHighwater() {return sqlite3_memory_highwater(false);} 63 | static long resetMemoryHighwater() {return sqlite3_memory_highwater(true);} 64 | 65 | SQLite3(string filename,int flags,string vfs="") 66 | { 67 | m_valid=sqlite3_open(filename,m_ref,flags,vfs); 68 | if(m_valid!=SQLITE_OK) 69 | { 70 | Print(">>> Error opening database [",filename,"]: ",SQLite3::errorCode2Msg(m_valid)); 71 | } 72 | } 73 | ~SQLite3() 74 | { 75 | if(isValid()) 76 | { 77 | int ret=sqlite3_close(m_ref); 78 | if(ret!=SQLITE_OK) 79 | { 80 | Print(">>> Error close connection: ",getErrorMsg()); 81 | } 82 | } 83 | } 84 | 85 | bool isValid() const {return m_valid==SQLITE_OK;} 86 | 87 | bool isAutoCommit() const { return sqlite3_get_autocommit(m_ref)!=0;} 88 | 89 | bool isReadonly(string db) const 90 | { 91 | char buf[]; 92 | StringToUtf8(db,buf); 93 | return 1==sqlite3_db_readonly(m_ref,buf); 94 | } 95 | bool hasDb(string db) const 96 | { 97 | char buf[]; 98 | StringToUtf8(db,buf); 99 | return -1==sqlite3_db_readonly(m_ref,buf); 100 | } 101 | string getDbFilename(string db) const 102 | { 103 | char buf[]; 104 | StringToUtf8(db,buf); 105 | return StringFromUtf8Pointer(sqlite3_db_filename(m_ref,buf)); 106 | } 107 | int getDbColumnMetadata(string db,string table,string column,ColumnInfo &info) 108 | { 109 | char zDbName[]; 110 | char zTableName[]; 111 | char zColumnName[]; 112 | StringToUtf8(db,zDbName); 113 | StringToUtf8(table,zTableName); 114 | StringToUtf8(column,zColumnName); 115 | intptr_t pDataType; 116 | intptr_t pCollSeq; 117 | int res=sqlite3_table_column_metadata(m_ref,zDbName,zTableName,zColumnName,pDataType,pCollSeq,info.NotNull,info.PrimaryKey,info.AutoIncrement); 118 | if(res==SQLITE_OK) 119 | { 120 | info.DataType=StringFromUtf8Pointer(pDataType); 121 | info.CollationSequence=StringFromUtf8Pointer(pCollSeq); 122 | } 123 | return res; 124 | } 125 | 126 | int setLimit(int id,int value) {return sqlite3_limit(m_ref,id,value);} 127 | int setBusyTimeout(int millis) {return sqlite3_busy_timeout(m_ref, millis);} 128 | 129 | int flush() {return sqlite3_db_cacheflush(m_ref);} 130 | int releaseMemory() {return sqlite3_db_release_memory(m_ref);} 131 | 132 | void interrupt() {sqlite3_interrupt(m_ref);} 133 | 134 | long getLastInsertRowId() {return sqlite3_last_insert_rowid(m_ref);} 135 | void setLastInsertRowId(long id) {sqlite3_set_last_insert_rowid(m_ref,id);} 136 | int getChanges() const {return sqlite3_changes(m_ref);} 137 | int getTotalChanges() const {return sqlite3_total_changes(m_ref);} 138 | 139 | int getErrorCode() const {return sqlite3_errcode(m_ref);} 140 | int getExtendedErrorCode() const {return sqlite3_extended_errcode(m_ref);} 141 | string getErrorMsg() const {return StringFromUtf8Pointer(sqlite3_errmsg(m_ref));} 142 | 143 | int getStatus(int op,int ¤t,int &highwater,bool reset=false) {return sqlite3_db_status(m_ref,op,current,highwater,reset);} 144 | 145 | int setSharedCache(bool value) {return sqlite3_enable_shared_cache(value);} 146 | int setLoadExtension(bool value) {return sqlite3_enable_load_extension(m_ref,value);} 147 | 148 | int setExtendedResultCodes(bool value) {return sqlite3_extended_result_codes(m_ref,value);} 149 | 150 | bool loadExtension(string name,string entry) 151 | { 152 | char u8name[],u8entry[]; 153 | StringToUtf8(name,u8name); 154 | StringToUtf8(name,u8entry); 155 | intptr_t errMsg; 156 | int res= sqlite3_load_extension(m_ref,u8name,u8entry,errMsg); 157 | if(res == SQLITE_OK) return true; 158 | else 159 | { 160 | PrintFormat(">>> Error loading extension module %s: %s",name,StringFromUtf8Pointer(errMsg)); 161 | sqlite3_free(errMsg); 162 | return false; 163 | } 164 | } 165 | 166 | int setAutoCheckpoint(int frameThreshold) {return sqlite3_wal_autocheckpoint(m_ref,frameThreshold);} 167 | int checkpoint(string db,int mode,int &pnLog,int &pnCkpt) 168 | { 169 | if(db==NULL) 170 | return sqlite3_wal_checkpoint_v2(m_ref, 0, mode, pnLog, pnCkpt); 171 | else 172 | { 173 | uchar u8db[]; 174 | StringToUtf8(db,u8db); 175 | return sqlite3_wal_checkpoint_v2(m_ref, u8db, mode, pnLog, pnCkpt); 176 | } 177 | } 178 | 179 | // db config 180 | #define DB_CONFIG(Name,MACRO) \ 181 | bool is##Name##Enabled() const {int res;sqlite3_db_config(m_ref,MACRO,-1,res);return res!=0;}\ 182 | void set##Name##Enabled(bool value) {sqlite3_db_config(m_ref,MACRO,value,0);} 183 | 184 | DB_CONFIG(Trigger,SQLITE_DBCONFIG_ENABLE_TRIGGER) 185 | DB_CONFIG(ForeignKey,SQLITE_DBCONFIG_ENABLE_FKEY) 186 | DB_CONFIG(FTS3Tokenizer,SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER) 187 | DB_CONFIG(LoadExtension,SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION) 188 | DB_CONFIG(CheckpointsOnClose,SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE) 189 | 190 | bool setMainDbName(string name) 191 | { 192 | if(ArraySize(m_dbName) > 0) return false; 193 | StringToUtf8(name,m_dbName); 194 | sqlite3_db_config(m_ref,SQLITE_DBCONFIG_MAINDBNAME,m_dbName); 195 | return true; 196 | } 197 | 198 | int setDbLookAside(int slotSize,int nSlots) {return sqlite3_db_config(m_ref,SQLITE_DBCONFIG_LOOKASIDE,0,slotSize,nSlots);} 199 | // end db config 200 | 201 | //--- intended for internal use 202 | intptr_t ref() const {return m_ref;} 203 | }; 204 | //+------------------------------------------------------------------+ 205 | -------------------------------------------------------------------------------- /Include/SQLite3/Statement.mqh: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| Statement.mqh | 3 | //| Copyright 2017, Li Ding | 4 | //| dingmaotu@126.com | 5 | //+------------------------------------------------------------------+ 6 | #property strict 7 | 8 | #include "SQLite3.mqh" 9 | //+------------------------------------------------------------------+ 10 | //| Wraps sqlite3_stmt object | 11 | //+------------------------------------------------------------------+ 12 | class Statement 13 | { 14 | private: 15 | int m_valid; 16 | intptr_t m_ref; 17 | public: 18 | 19 | static bool isComplete(string sql) 20 | { 21 | uchar buf[]; 22 | StringToUtf8(sql,buf); 23 | return 1==sqlite3_complete(buf); 24 | } 25 | 26 | Statement(const SQLite3 &conn,string sql) 27 | { 28 | m_valid=sqlite3_prepare(conn.ref(),sql,m_ref); 29 | } 30 | ~Statement() {if(isValid())sqlite3_finalize(m_ref);} 31 | 32 | bool setSql(string sql) 33 | { 34 | if(isValid()) 35 | { 36 | intptr_t h=getConnectionHandle(); 37 | sqlite3_finalize(m_ref); 38 | m_valid=sqlite3_prepare(h,sql,m_ref); 39 | return m_valid==SQLITE_OK; 40 | } 41 | return false; 42 | } 43 | string getSql() const {return StringFromUtf8Pointer(sqlite3_sql(m_ref));} 44 | string getExpandedSql() const {return StringFromUtf8Pointer(sqlite3_expanded_sql(m_ref));} 45 | intptr_t getConnectionHandle() const {return sqlite3_db_handle(m_ref);} 46 | 47 | bool isValid() const {return m_valid==SQLITE_OK;} 48 | 49 | bool isBusy() const {return sqlite3_stmt_busy(m_ref);} 50 | bool isReadonly() const {return sqlite3_stmt_readonly(m_ref);} 51 | 52 | int getCounter(int op) const {return sqlite3_stmt_status(m_ref,op,false);} 53 | void resetCounter(int op) { sqlite3_stmt_status(m_ref,op,true);} 54 | 55 | int step() { return sqlite3_step(m_ref);} 56 | 57 | int reset() {return sqlite3_reset(m_ref);} 58 | int clearBindings() {return sqlite3_clear_bindings(m_ref);} 59 | 60 | int getParameterCount() const {return sqlite3_bind_parameter_count(m_ref);} 61 | string getParameterName(int i) const {return StringFromUtf8Pointer(sqlite3_bind_parameter_name(m_ref,i));} 62 | int getParameterIndex(string name) const 63 | { 64 | uchar u8name[]; 65 | StringToUtf8(name,u8name); 66 | return sqlite3_bind_parameter_index(m_ref,u8name); 67 | } 68 | 69 | int bind(int i,const uchar &value[],bool copy=true) 70 | { 71 | return sqlite3_bind_blob(m_ref,i,value,ArraySize(value),copy?SQLITE_TRANSIENT:SQLITE_STATIC); 72 | } 73 | int bind(int i,string text) 74 | { 75 | uchar u8text[]; 76 | StringToUtf8(text,u8text); 77 | return sqlite3_bind_text(m_ref,i,u8text,ArraySize(u8text),SQLITE_TRANSIENT); 78 | } 79 | 80 | int bind(int i,double value) {return sqlite3_bind_double(m_ref,i,value);} 81 | int bind(int i,int value) {return sqlite3_bind_int(m_ref,i,value);} 82 | int bind(int i,long value) {return sqlite3_bind_int64(m_ref,i,value);} 83 | int bind(int i) {return sqlite3_bind_null(m_ref,i);} 84 | 85 | int zero(int i,int bytes) {return sqlite3_bind_zeroblob(m_ref,i,bytes);} 86 | int zero(int i,ulong bytes) {return sqlite3_bind_zeroblob64(m_ref,i,bytes);} 87 | 88 | int getDataCount() const {return sqlite3_data_count(m_ref);} 89 | 90 | int getColumnCount() const {return sqlite3_column_count(m_ref);} 91 | string getColumnName(int i) const {return StringFromUtf8Pointer(sqlite3_column_name(m_ref,i));} 92 | int getColumnType(int i) const {return sqlite3_column_type(m_ref,i);} 93 | string getColumnDeclareType(int i) const {return StringFromUtf8Pointer(sqlite3_column_decltype(m_ref,i));} 94 | int getColumnBytes(int i) const {return sqlite3_column_bytes(m_ref,i);} 95 | 96 | string getColumnDatabaseName(int i) const {return StringFromUtf8Pointer(sqlite3_column_database_name(m_ref,i));} 97 | string getColumnTableName(int i) const {return StringFromUtf8Pointer(sqlite3_column_table_name(m_ref,i));} 98 | string getColumnOriginName(int i) const {return StringFromUtf8Pointer(sqlite3_column_origin_name(m_ref,i));} 99 | 100 | void getColumn(int i,double &value) const {value=sqlite3_column_double(m_ref,i);} 101 | void getColumn(int i,int &value) const {value=sqlite3_column_int(m_ref,i);} 102 | void getColumn(int i,long &value) const {value=sqlite3_column_int64(m_ref,i);} 103 | void getColumn(int i,string &value) const 104 | { 105 | intptr_t u8p=0; 106 | u8p=sqlite3_column_text(m_ref,i); 107 | if(u8p>0) value=StringFromUtf8Pointer(u8p); 108 | else value=NULL; 109 | } 110 | void getColumn(int i,uchar &blob[]) const 111 | { 112 | int len=sqlite3_column_bytes(m_ref,i); 113 | if(len==0) 114 | {ArrayFree(blob);return;} 115 | intptr_t blobp=sqlite3_column_blob(m_ref,i); 116 | ArrayResize(blob,len); 117 | ArrayFromPointer(blob,blobp,len); 118 | } 119 | }; 120 | //+------------------------------------------------------------------+ 121 | -------------------------------------------------------------------------------- /Include/SQLite3/Vfs.mqh: -------------------------------------------------------------------------------- 1 | //+------------------------------------------------------------------+ 2 | //| Vfs.mqh | 3 | //| Copyright 2017, Li Ding | 4 | //| dingmaotu@126.com | 5 | //+------------------------------------------------------------------+ 6 | #property strict 7 | 8 | #include "Imports.mqh" 9 | //+------------------------------------------------------------------+ 10 | //| Virtual file system operations | 11 | //+------------------------------------------------------------------+ 12 | struct Vfs 13 | { 14 | public: 15 | intptr_t handle; 16 | 17 | static Vfs getDefault() {Vfs vfs(sqlite3_vfs_find(0)); return vfs;} 18 | static bool find(string name,Vfs &vfs) {char u8[];StringToUtf8(name,u8);vfs.handle=sqlite3_vfs_find(u8);return vfs.handle!=0;} 19 | 20 | Vfs(intptr_t ptr):handle(ptr){} 21 | Vfs(const Vfs&rhs):handle(rhs.handle) {} 22 | void operator=(const Vfs&rhs) {handle=rhs.handle;} 23 | 24 | int register() {return sqlite3_vfs_register(handle,false);} 25 | int registerDefault() {return sqlite3_vfs_register(handle,true);} 26 | int unregister() {return sqlite3_vfs_unregister(handle);} 27 | }; 28 | //+------------------------------------------------------------------+ 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Ding Li 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Library/MT4/sqlite3.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dingmaotu/mql-sqlite3/1b5186f7c3c3d4cd27cc648f5273d76eaa8dc593/Library/MT4/sqlite3.dll -------------------------------------------------------------------------------- /Library/MT5/sqlite3.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dingmaotu/mql-sqlite3/1b5186f7c3c3d4cd27cc648f5273d76eaa8dc593/Library/MT5/sqlite3.dll -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # mql-sqlite3 2 | 3 | SQLite3 binding for the MQL language (both 32bit MT4 and 64bit MT5) 4 | 5 | ## Introduction 6 | 7 | This is a complete binding of the [SQLite3](http://sqlite.org/) library (version 8 | 3180000) for the MQL4/5 language used by MetaTrader4/5. 9 | 10 | SQLite is an fast embeded SQL engine written in C 11 | and [widely used](http://sqlite.org/mostdeployed.html) by a lot of projects. We 12 | always have the need to persistent states of Expert Advisors and SQLite seems to 13 | be the best solution. 14 | 15 | This binding tries to remain compatible between MQL4/5. Users of both versions 16 | can use this binding, with a single set of headers. MQL4 and MQL5 are basically 17 | the same in that they are merged in recent versions. The difference is in the 18 | runtime environment (MetaTrader5 is 64bit by default, while MetaTrader4 is 19 | 32bit). The trading system is also different, but it is no concern of this 20 | binding. 21 | 22 | ## Files 23 | 24 | This binding contains three sets of files: 25 | 26 | 1. The binding itself is in the `Include/SQLite3` directory. 27 | 28 | 2. There is a simple testing script called `TestSQLite3.mq4` in `Scripts/Test` 29 | directory. The script files are mq4 by default, but you can change the 30 | extension to mq5 to use them in MetaTrader5. Currently there is only one 31 | script. I am going to add more in the future. 32 | 33 | 3. Precompiled DLLs of both 64bit (`Library/MT5`) and 32bit (`Library/MT4`) are 34 | provided. Copy the corresponding DLLs to the `Library` folder of your 35 | MetaTrader terminal. If you are using MetaTrader5 32bit, use the 32bit 36 | version from `Library/MT4`. The DLLs have no special dependencies and should 37 | work in any Windows version after NT. *Note* that these DLLs are copied from 38 | official binary release, without any modification. You can download/compile 39 | your own if you don't trust these binaries. 40 | 41 | ## API comparison 42 | 43 | Below is a detailed comparison table of MQL and C/C++ APIs. If you are reading 44 | the official documentation, and you want to find the corresponding MQL API for 45 | the C API, use this table. This table also has comments about why a particular 46 | API is not included in this binding. This table might change if new APIs get added. 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 473 | 474 | 475 | 476 | 477 | 478 | 479 | 480 | 481 | 482 | 483 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 530 | 531 | 532 | 533 | 534 | 535 | 536 | 537 | 538 | 539 | 540 | 541 | 542 | 543 | 544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | 552 | 553 | 554 | 555 | 556 | 557 | 558 | 559 | 560 | 561 | 562 | 563 | 564 | 565 | 566 | 567 | 568 | 569 | 570 | 571 | 572 | 573 | 574 | 575 | 576 | 577 | 578 | 579 | 580 | 581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | 597 | 598 | 599 | 600 | 601 | 602 | 603 | 604 | 605 | 606 | 607 | 608 | 609 | 610 | 611 | 612 | 613 | 614 | 615 | 616 | 617 | 618 | 619 | 620 | 621 | 622 | 623 | 624 | 625 | 626 | 627 | 628 | 629 | 630 | 631 | 632 | 633 | 634 | 635 | 636 | 637 | 638 | 639 | 640 | 641 | 643 | 644 | 645 | 646 | 647 | 648 | 649 | 650 | 651 | 652 | 653 | 654 | 655 | 656 | 657 | 658 | 659 | 660 | 661 | 662 | 663 | 664 | 665 | 666 | 667 | 668 | 669 | 670 | 671 | 672 | 673 | 674 | 675 | 676 | 677 | 678 | 679 | 680 | 681 | 682 | 683 | 684 | 685 | 686 | 687 | 688 | 689 | 690 | 691 | 692 | 693 | 694 | 695 | 696 | 697 | 698 | 699 | 700 | 701 | 702 | 703 | 704 | 705 | 706 | 707 | 708 | 709 | 710 | 711 | 712 | 713 | 714 | 715 | 716 | 717 | 718 | 719 | 720 | 721 | 722 | 723 | 724 | 725 | 726 | 727 | 728 | 729 | 730 | 731 | 732 | 733 | 734 | 735 | 736 | 737 | 738 | 739 | 740 | 741 | 742 | 743 | 744 | 745 | 746 | 747 | 748 | 749 | 750 | 751 | 752 | 753 | 754 | 755 | 756 | 757 | 758 | 759 | 760 | 761 | 762 | 763 | 764 | 765 | 766 | 767 | 768 | 769 | 770 | 771 | 772 | 773 | 774 | 775 | 776 | 777 | 778 | 779 | 780 | 781 | 782 | 783 | 784 | 785 | 786 | 787 | 788 | 789 | 790 | 791 | 792 | 793 | 794 | 795 | 796 | 797 | 798 | 799 | 800 | 801 | 802 | 803 | 804 | 805 | 806 | 807 | 808 | 809 | 810 | 811 | 812 | 813 | 814 | 815 | 816 | 817 | 818 | 819 | 820 | 821 | 822 | 823 | 824 | 825 | 826 | 827 | 828 | 829 | 830 | 831 | 832 | 833 | 834 | 835 | 836 | 837 | 838 | 839 | 840 | 841 | 842 | 843 | 844 | 845 | 846 | 847 | 848 | 849 | 850 | 851 | 852 | 853 | 854 | 855 | 856 | 857 | 858 | 859 | 860 | 861 | 862 | 863 | 864 | 865 | 866 | 867 | 868 | 869 | 870 | 871 | 872 | 873 | 874 | 875 | 876 | 877 | 878 | 879 | 880 | 881 | 882 | 883 | 884 | 885 | 886 | 887 | 888 | 889 | 890 | 891 | 892 | 893 | 894 | 895 | 896 | 897 | 898 | 899 | 900 | 901 | 902 | 903 | 904 | 905 | 906 | 907 | 908 | 909 | 910 | 911 | 912 | 913 | 914 | 915 | 916 | 917 | 918 | 919 | 920 | 921 | 922 | 923 | 924 | 925 | 926 | 927 | 928 | 929 | 930 | 931 | 932 | 933 | 934 | 935 | 936 | 937 | 938 | 939 | 940 | 941 | 942 | 943 | 944 | 945 | 946 | 947 | 948 | 949 | 950 | 951 | 952 | 953 | 954 | 955 | 956 | 957 | 958 | 959 | 960 | 961 | 962 | 963 | 964 | 965 | 966 | 967 | 968 | 969 | 970 | 971 | 972 | 973 | 974 | 975 | 976 | 977 | 978 | 979 | 980 | 981 | 982 | 983 | 984 | 985 | 986 | 987 | 988 | 989 | 990 | 991 | 992 | 993 | 994 | 995 | 996 | 997 | 998 | 999 | 1000 | 1001 | 1002 | 1003 | 1004 | 1005 | 1006 | 1007 | 1008 | 1009 | 1010 | 1011 | 1012 | 1013 | 1014 | 1015 | 1016 | 1017 | 1018 | 1019 | 1020 | 1021 | 1022 | 1023 | 1024 | 1025 | 1026 | 1027 | 1028 | 1029 | 1030 | 1031 | 1033 | 1034 | 1035 | 1036 | 1037 | 1038 | 1039 | 1040 | 1041 | 1042 | 1043 | 1044 | 1045 | 1046 | 1047 | 1048 | 1049 | 1050 | 1051 | 1052 | 1053 | 1054 | 1055 | 1056 | 1057 | 1058 | 1059 | 1060 | 1061 | 1062 | 1063 | 1064 | 1065 | 1066 | 1067 | 1068 | 1069 | 1070 | 1071 | 1072 | 1073 | 1074 | 1075 | 1076 | 1077 | 1078 | 1079 | 1080 | 1081 | 1082 | 1083 | 1084 | 1085 | 1086 | 1087 | 1088 | 1089 | 1090 | 1091 | 1092 | 1093 | 1094 | 1095 | 1096 | 1097 | 1098 | 1099 | 1100 | 1101 | 1102 | 1103 | 1104 | 1105 | 1106 | 1107 | 1108 | 1109 | 1110 | 1111 | 1112 | 1113 | 1114 | 1115 | 1116 | 1117 | 1118 | 1119 | 1120 | 1121 | 1122 | 1123 | 1124 | 1125 | 1126 | 1127 | 1128 | 1129 | 1130 | 1131 | 1132 | 1133 | 1134 | 1135 | 1136 | 1137 | 1138 | 1139 | 1140 | 1141 | 1142 | 1143 | 1144 | 1145 | 1146 | 1147 | 1148 | 1149 | 1150 | 1151 | 1152 | 1153 | 1154 | 1155 | 1156 | 1157 | 1158 | 1159 | 1160 | 1161 | 1162 | 1163 | 1164 | 1165 | 1166 | 1167 | 1168 | 1169 | 1170 | 1171 | 1172 | 1173 | 1174 | 1175 | 1176 | 1177 | 1178 | 1179 | 1180 | 1181 | 1182 | 1183 | 1184 | 1185 | 1186 | 1187 | 1188 | 1189 | 1190 | 1191 | 1192 | 1193 | 1194 | 1195 | 1196 | 1197 | 1198 | 1199 | 1200 | 1201 | 1202 | 1203 | 1204 | 1205 | 1206 | 1207 | 1208 | 1209 | 1210 | 1211 | 1212 | 1213 | 1214 | 1215 | 1216 | 1217 | 1219 | 1220 | 1221 | 1222 | 1223 | 1224 | 1225 | 1226 | 1227 | 1228 | 1229 | 1230 | 1231 | 1232 | 1233 | 1234 | 1235 | 1236 | 1237 | 1238 | 1239 | 1240 | 1241 | 1242 | 1243 | 1244 | 1245 | 1246 | 1247 | 1248 | 1249 | 1250 | 1251 | 1252 | 1253 | 1254 | 1255 | 1256 | 1257 | 1258 | 1259 | 1260 | 1261 | 1262 | 1263 | 1264 | 1265 | 1266 | 1267 |
SQLite3 C APImql-sqlite3Comment
sqlite3_aggregate_contextN/Afor extension
sqlite3_aggregate_countN/Adeprecated
sqlite3_auto_extensionN/Afor extension
sqlite3_backup_finishBackup::~Backup 
sqlite3_backup_initBackup::Backup 
sqlite3_backup_pagecountBackup::getPageCount 
sqlite3_backup_remainingBackup::getRemaining 
sqlite3_backup_stepBackup::step 
sqlite3_bind_blobStatement::bind 
sqlite3_bind_blob64N/Atoo large for ordinary use
sqlite3_bind_doubleStatement::bind 
sqlite3_bind_intStatement::bind 
sqlite3_bind_int64Statement::bind 
sqlite3_bind_nullStatement::bind 
sqlite3_bind_parameter_countStatement::getParameterCount 
sqlite3_bind_parameter_indexStatement::getParameterIndex 
sqlite3_bind_parameter_nameStatement::getParameterName 
sqlite3_bind_textStatement::bind 
sqlite3_bind_text16N/Aalways UTF-8
sqlite3_bind_text64N/Atoo large for ordinary use
sqlite3_bind_valueN/Afor extension
sqlite3_bind_zeroblobStatement::zero 
sqlite3_bind_zeroblob64Statement::zero 
sqlite3_blob_bytesBlob::size 
sqlite3_blob_closeBlob::~Blob 
sqlite3_blob_openBlob::Blob 
sqlite3_blob_readBlob::read 
sqlite3_blob_reopenBlob::moveTo 
sqlite3_blob_writeBlob::write 
sqlite3_busy_handlerN/ANeed callback
sqlite3_busy_timeoutSQLite3::setBusyTimeout 
sqlite3_cancel_auto_extensionN/Afor extension
sqlite3_changesSQLite3::getChanges 
sqlite3_clear_bindingsStatement::clearBindings 
sqlite3_closeN/Ause v2
sqlite3_close_v2SQLite3::~SQLite3 
sqlite3_collation_neededN/Afor extension
sqlite3_collation_needed16N/Afor extension
sqlite3_column_blobStatement::getColumn 
sqlite3_column_bytesStatement::getColumnBytes 
sqlite3_column_bytes16N/Aalways UTF-8
sqlite3_column_countStatement::getColumnCount 
sqlite3_column_database_nameStatement::getColumnDatabaseName 
sqlite3_column_database_name16N/Aalways UTF-8
sqlite3_column_decltypeStatement::getColumnDeclareType 
sqlite3_column_decltype16N/Aalways UTF-8
sqlite3_column_doubleStatement::getColumn 
sqlite3_column_intStatement::getColumn 
sqlite3_column_int64Statement::getColumn 
sqlite3_column_nameStatement::getColumnName 
sqlite3_column_name16N/Aalways UTF-8
sqlite3_column_origin_nameStatement::getColumnOriginName 
sqlite3_column_origin_name16N/Aalways UTF-8
sqlite3_column_table_nameStatement::getColumnTableName 
sqlite3_column_table_name16N/Aalways UTF-8
sqlite3_column_textStatement::getColumn 
sqlite3_column_text16N/Aalways UTF-8
sqlite3_column_typeStatement::getColumnType 
sqlite3_column_valueN/Afor extension
sqlite3_commit_hookN/Aneed callback
sqlite3_compileoption_getN/Adiagnostic/optional
sqlite3_compileoption_usedN/Adiagnostic/optional
sqlite3_completeStatement::isComplete 
sqlite3_complete16N/Aalways UTF-8
sqlite3_configSQLite3::set* static methodspartial
sqlite3_context_db_handleN/Afor extension
sqlite3_create_collationN/Afor extension
sqlite3_create_collation16N/Afor extension
sqlite3_create_collation_v2N/Afor extension
sqlite3_create_functionN/Afor extension
sqlite3_create_function16N/Afor extension
sqlite3_create_function_v2N/Afor extension
sqlite3_create_moduleN/Afor extension
sqlite3_create_module_v2N/Afor extension
sqlite3_data_countStatement::getDataCount 
sqlite3_db_cacheflushSQLite3::flush 
sqlite3_db_configSQLite3::setDbLookAside
440 | SQLite3::setMainDbName
441 | SQLite3::isTriggerEnabled
442 | SQLite3::setTriggerEnabled
443 | SQLite3::isForeignKeyEnabled
444 | SQLite3::setForeignKeyEnabled
445 | SQLite3::isFTS3TokenizerEnabled
446 | SQLite3::setFTS3TokenizerEnabled
447 | SQLite3::isLoadExtensionEnabled
448 | SQLite3::setLoadExtensionEnabled
449 | SQLite3::isCheckpointsOnCloseEnabled
450 | SQLite3::setCheckpointsOnCloseEnabled
 
sqlite3_db_filenameSQLite3::getDbFilename 
sqlite3_db_handleStatement::getConnectionHandle 
sqlite3_db_mutexN/Ainternal use only
sqlite3_db_readonlySQLite3::isReadonlySQLite3::hasDb check if 472 | contains db
sqlite3_db_release_memorySQLite3::releaseMemory 
sqlite3_db_statusSQLite3::getStatus 
sqlite3_declare_vtabN/Afor extension
sqlite3_enable_load_extensionSQLite3::setLoadExtension 
sqlite3_enable_shared_cacheSQLite3::setSharedCache 
sqlite3_errcodeSQLite3::getErrorCode 
sqlite3_errmsgSQLite3::getErrorMsg 
sqlite3_errmsg16N/Aalways UTF-8
sqlite3_errstrSQLite3::errorCode2Msg 
sqlite3_execN/Aconvenience wrapper only
sqlite3_expanded_sqlSQLite3::getExpandedSql 
sqlite3_expiredN/Adeprecated
sqlite3_extended_errcodeSQLite3::getExtendedErrorCode 
sqlite3_extended_result_codesSQLite3::setResultCodes 
sqlite3_file_controlN/Atoo low level
sqlite3_finalizeSQLite3::~SQLite3 
sqlite3_freeN/Ainternal use only
sqlite3_free_tableN/Aconvenience wrapper only
sqlite3_get_autocommitSQLite3::isAutoCommit 
sqlite3_get_auxdataN/Afor extension
sqlite3_get_tableN/Aconvenience wrapper only
sqlite3_global_recoverN/Adeprecated
sqlite3_initializeSQLite3::initializestatic
sqlite3_interruptSQLite3::interrupt 
sqlite3_last_insert_rowidSQLite3::getLastInsertRowId 
sqlite3_libversionSQLite3::getVersionstatic
sqlite3_libversion_numberSQLite3::getVersionNumberstatic
sqlite3_limitSQLite3::setLimit 
sqlite3_load_extensionSQLite3::loadExtension 
sqlite3_logN/Afor extension
sqlite3_mallocN/Ainternal use only
sqlite3_malloc64N/Ainternal use only
sqlite3_memory_alarmN/Adeprecated
sqlite3_memory_highwaterSQLite3::getMemoryHighwater, 642 | SQLite3::resetMemoryHighwater
sqlite3_memory_usedSQLite3::getMemoryUsed 
sqlite3_mprintfN/Ainternal use only
sqlite3_msizeN/Ainternal use only
sqlite3_mutex_allocN/Ainternal use only
sqlite3_mutex_enterN/Ainternal use only
sqlite3_mutex_freeN/Ainternal use only
sqlite3_mutex_heldN/Ainternal use only
sqlite3_mutex_leaveN/Ainternal use only
sqlite3_mutex_notheldN/Ainternal use only
sqlite3_mutex_tryN/Ainternal use only
sqlite3_next_stmtN/Ano use
sqlite3_openN/Ause v2
sqlite3_open16N/Aalways UTF-8
sqlite3_open_v2SQLite3::SQLite3 
sqlite3_os_endN/Ainternal use only
sqlite3_os_initN/Ainternal use only
sqlite3_overload_functionN/Afor extension
sqlite3_prepareN/Ause v2
sqlite3_prepare16N/Aalways UTF-8
sqlite3_prepare16_v2N/Aalways UTF-8
sqlite3_prepare_v2Statement::Statement 
sqlite3_preupdate_countN/Afor extension
sqlite3_preupdate_depthN/Afor extension
sqlite3_preupdate_hookN/Afor extension
sqlite3_preupdate_newN/Afor extension
sqlite3_preupdate_oldN/Afor extension
sqlite3_profileN/Adeprecated
sqlite3_progress_handlerN/Aneed callback
sqlite3_randomnessN/Ause MQL Random
sqlite3_reallocN/Ainternal use only
sqlite3_realloc64N/Ainternal use only
sqlite3_release_memorySQLite3::releaseMemoryInBytesstatic
sqlite3_resetStatement::reset 
sqlite3_reset_auto_extensionN/Afor extension
sqlite3_result_blobN/Afor extension
sqlite3_result_blob64N/Afor extension
sqlite3_result_doubleN/Afor extension
sqlite3_result_errorN/Afor extension
sqlite3_result_error16N/Afor extension
sqlite3_result_error_codeN/Afor extension
sqlite3_result_error_nomemN/Afor extension
sqlite3_result_error_toobigN/Afor extension
sqlite3_result_intN/Afor extension
sqlite3_result_int64N/Afor extension
sqlite3_result_nullN/Afor extension
sqlite3_result_subtypeN/Afor extension
sqlite3_result_textN/Afor extension
sqlite3_result_text16N/Afor extension
sqlite3_result_text16beN/Afor extension
sqlite3_result_text16leN/Afor extension
sqlite3_result_text64N/Afor extension
sqlite3_result_valueN/Afor extension
sqlite3_result_zeroblobN/Afor extension
sqlite3_result_zeroblob64N/Afor extension
sqlite3_rollback_hookN/Aneed callback
sqlite3_set_authorizerN/Aneed callback
sqlite3_set_auxdataN/Afor extension
sqlite3_set_last_insert_rowidSQLite3::setLastInsertRowId 
sqlite3_shutdownSQLite3::shutdown 
sqlite3_sleepN/Ause MQL Sleep
sqlite3_snapshot_cmp(exp)N/Aexperimental
sqlite3_snapshot_free(exp)N/Aexperimental
sqlite3_snapshot_get(exp)N/Aexperimental
sqlite3_snapshot_open(exp)N/Aexperimental
sqlite3_snapshot_recover(exp)N/Aexperimental
sqlite3_snprintfN/Ainternal use only
sqlite3_soft_heap_limitN/Adeprecated
sqlite3_soft_heap_limit64SQLite3::setSoftHeapLimitstatic
sqlite3_sourceidSQLite3::getSourceIdstatic
sqlite3_sqlStatement::getSql 
sqlite3_statusN/Ause sqlite3_status64
sqlite3_status64SQLite3::status 
sqlite3_stepStatement::step 
sqlite3_stmt_busyStatement::isBusy 
sqlite3_stmt_readonlyStatement::isReadonly 
sqlite3_stmt_scanstatusN/Aadvanced/optional
sqlite3_stmt_scanstatus_resetN/Aadvanced/optional
sqlite3_stmt_statusStatement::getCounter, 1032 | Statement::resetCounter 
sqlite3_strglobN/Ainternal use only
sqlite3_stricmpN/Ainternal use only
sqlite3_strlikeN/Ainternal use only
sqlite3_strnicmpN/Ainternal use only
sqlite3_system_errnoN/Ainternal use only
sqlite3_table_column_metadataSQLite3::getDbColumnMetadata 
sqlite3_test_controlN/Ainternal use only
sqlite3_thread_cleanupN/Adeprecated
sqlite3_threadsafeSQLite3::isThreadSafe 
sqlite3_total_changesSQLite3::getTotalChanges 
sqlite3_traceN/Adeprecated
sqlite3_trace_v2N/Aneed callback
sqlite3_transfer_bindingsN/Adeprecated
sqlite3_unlock_notifyN/Aneed callback
sqlite3_update_hookN/Aneed callback
sqlite3_uri_booleanN/Afor extension
sqlite3_uri_int64N/Afor extension
sqlite3_uri_parameterN/Afor extension
sqlite3_user_dataN/Afor extension
sqlite3_value_blobN/Afor extension
sqlite3_value_bytesN/Afor extension
sqlite3_value_bytes16N/Afor extension
sqlite3_value_doubleN/Afor extension
sqlite3_value_dupN/Afor extension
sqlite3_value_freeN/Afor extension
sqlite3_value_intN/Afor extension
sqlite3_value_int64N/Afor extension
sqlite3_value_numeric_typeN/Afor extension
sqlite3_value_subtypeN/Afor extension
sqlite3_value_textN/Afor extension
sqlite3_value_text16N/Afor extension
sqlite3_value_text16beN/Afor extension
sqlite3_value_text16leN/Afor extension
sqlite3_value_typeN/Afor extension
sqlite3_versionN/Anot a function
sqlite3_vfs_findVfs::find, Vfs::getDefault 
sqlite3_vfs_registerVfs::register, 1218 | Vfs::registerDefault 
sqlite3_vfs_unregisterVfs::unregister 
sqlite3_vmprintfN/Ainternal use only
sqlite3_vsnprintfN/Ainternal use only
sqlite3_vtab_configN/Afor extension
sqlite3_vtab_on_conflictN/Afor extension
sqlite3_wal_autocheckpointSQLite3::setAutoCheckpoint 
sqlite3_wal_checkpointN/Ause v2
sqlite3_wal_checkpoint_v2SQLite3::checkpoint 
sqlite3_wal_hookN/Aneed callback
1268 | 1269 | ## About string encoding and API choices 1270 | 1271 | MQL strings are Win32 UNICODE strings (basically 2-byte UTF-16). While for 1272 | several APIs, SQLite3 provides both UTF-8 and UTF-16 versions, only UTF-8 1273 | strings are accepted in other APIs. For unification and maximum 1274 | interoperabibility, we only use the UTF-8 version of the APIs. In this binding 1275 | all strings are converted to utf-8 strings before sending to the dll layer. And 1276 | strings in the database are also UTF-8 encoded. 1277 | 1278 | ## About callbacks, internal APIs, and convenience wrappers 1279 | 1280 | A lot of SQLite3 APIs are for internal use only, or for extension development, 1281 | or need a C function as callback. These are not included in this binding. This 1282 | will not affect the general applicability of this binding for most application 1283 | level usage. 1284 | 1285 | APIs like `sqlite3_exec`, `sqlite3_get_table`, etc. are convenience wrappers, 1286 | which are not included. Maybe I will implement them in MQL. 1287 | 1288 | ## Usage 1289 | 1290 | You'd better read the official documentation or some books about SQLite before 1291 | using this library. Documentation about this binding will be added when I have 1292 | the time to finish them. You can find a simple test script in `Scripts/Test`. 1293 | Here is a sample from `TestSQLite3.mq4`: 1294 | 1295 | ```MQL5 1296 | //+------------------------------------------------------------------+ 1297 | //| TestSQLite3.mq4 | 1298 | //| Copyright 2017, Li Ding | 1299 | //| dingmaotu@hotmail.com | 1300 | //+------------------------------------------------------------------+ 1301 | #property copyright "Copyright 2017, Li Ding" 1302 | #property link "dingmaotu@hotmail.com" 1303 | #property version "1.00" 1304 | #property strict 1305 | 1306 | #include 1307 | //+------------------------------------------------------------------+ 1308 | //| Script program start function | 1309 | //+------------------------------------------------------------------+ 1310 | void OnStart() 1311 | { 1312 | //--- optional but recommended 1313 | SQLite3::initialize(); 1314 | 1315 | //--- ensure the dll and the lib is of the same version 1316 | Print(SQLite3::getVersionNumber(), " = ", SQLITE_VERSION_NUMBER); 1317 | Print(SQLite3::getVersion(), " = ", SQLITE_VERSION); 1318 | Print(SQLite3::getSourceId(), " = ", SQLITE_SOURCE_ID); 1319 | 1320 | //--- create an empty db 1321 | #ifdef __MQL5__ 1322 | string filesPath=TerminalInfoString(TERMINAL_DATA_PATH)+"\\MQL5\\Files"; 1323 | #else 1324 | string filesPath=TerminalInfoString(TERMINAL_DATA_PATH)+"\\MQL4\\Files"; 1325 | #endif 1326 | string dbPath=filesPath+"\\test.db"; 1327 | Print(dbPath); 1328 | 1329 | SQLite3 db(dbPath,SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); 1330 | if(!db.isValid()) return; 1331 | 1332 | Print("DB created."); 1333 | string sql="create table buy_orders" 1334 | "(a int, b text);"; 1335 | if(Statement::isComplete(sql)) 1336 | Print(">>> SQL is complete"); 1337 | else 1338 | Print(">>> SQL not complete"); 1339 | 1340 | Statement s(db,sql); 1341 | 1342 | if(!s.isValid()) 1343 | { 1344 | Print(db.getErrorMsg()); 1345 | return; 1346 | } 1347 | 1348 | int r=s.step(); 1349 | if(r == SQLITE_OK) 1350 | Print(">>> Step finished."); 1351 | else if(r==SQLITE_DONE) 1352 | Print(">>> Successfully created table."); 1353 | else 1354 | Print(">>> Error executing statement: ",db.getErrorMsg()); 1355 | 1356 | //--- optional but recommended 1357 | SQLite3::shutdown(); 1358 | } 1359 | ``` 1360 | 1361 | Here is a sample for select: 1362 | 1363 | ```MQL5 1364 | //+------------------------------------------------------------------+ 1365 | //| TestSQLite3Select.mq4 | 1366 | //| Copyright 2018, Li Ding | 1367 | //| dingmaotu@hotmail.com | 1368 | //+------------------------------------------------------------------+ 1369 | #property copyright "Copyright 2018, Li Ding" 1370 | #property link "dingmaotu@hotmail.com" 1371 | #property version "1.00" 1372 | #property strict 1373 | 1374 | #include 1375 | //+------------------------------------------------------------------+ 1376 | //| Script program start function | 1377 | //+------------------------------------------------------------------+ 1378 | void OnStart() 1379 | { 1380 | //--- optional but recommended 1381 | SQLite3::initialize(); 1382 | 1383 | //--- open database 1384 | #ifdef __MQL5__ 1385 | string filesPath=TerminalInfoString(TERMINAL_DATA_PATH)+"\\MQL5\\Files"; 1386 | #else 1387 | string filesPath=TerminalInfoString(TERMINAL_DATA_PATH)+"\\MQL4\\Files"; 1388 | #endif 1389 | string dbPath=filesPath+"\\test.db"; 1390 | Print(dbPath); 1391 | 1392 | SQLite3 db(dbPath,SQLITE_OPEN_READWRITE); 1393 | if(!db.isValid()) return; 1394 | 1395 | string sql="select a, b from buy_orders;"; 1396 | 1397 | Statement s(db,sql); 1398 | 1399 | if(!s.isValid()) 1400 | { 1401 | Print(db.getErrorMsg()); 1402 | return; 1403 | } 1404 | 1405 | int r=s.step(); 1406 | do 1407 | { 1408 | if(r==SQLITE_ROW) 1409 | { 1410 | Print(">>> New row!"); 1411 | int c=s.getColumnCount(); 1412 | for(int i=0; i 12 | //+------------------------------------------------------------------+ 13 | //| Script program start function | 14 | //+------------------------------------------------------------------+ 15 | void OnStart() 16 | { 17 | //--- optional but recommended 18 | SQLite3::initialize(); 19 | 20 | //--- ensure the dll and the lib is of the same version 21 | Print(SQLite3::getVersionNumber(), " = ", SQLITE_VERSION_NUMBER); 22 | Print(SQLite3::getVersion(), " = ", SQLITE_VERSION); 23 | Print(SQLite3::getSourceId(), " = ", SQLITE_SOURCE_ID); 24 | 25 | //--- create an empty db 26 | #ifdef __MQL5__ 27 | string filesPath=TerminalInfoString(TERMINAL_DATA_PATH)+"\\MQL5\\Files"; 28 | #else 29 | string filesPath=TerminalInfoString(TERMINAL_DATA_PATH)+"\\MQL4\\Files"; 30 | #endif 31 | string dbPath=filesPath+"\\test.db"; 32 | Print(dbPath); 33 | 34 | SQLite3 db(dbPath,SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); 35 | if(!db.isValid()) return; 36 | 37 | Print("DB created."); 38 | string sql="create table buy_orders" 39 | "(a int, b text);"; 40 | if(Statement::isComplete(sql)) 41 | Print(">>> SQL is complete"); 42 | else 43 | Print(">>> SQL not complete"); 44 | 45 | Statement s(db,sql); 46 | 47 | if(!s.isValid()) 48 | { 49 | Print(db.getErrorMsg()); 50 | return; 51 | } 52 | 53 | int r=s.step(); 54 | if(r == SQLITE_OK) 55 | Print(">>> Step finished."); 56 | else if(r==SQLITE_DONE) 57 | Print(">>> Successfully created table."); 58 | else 59 | Print(">>> Error executing statement: ",db.getErrorMsg()); 60 | 61 | //--- optional but recommended 62 | SQLite3::shutdown(); 63 | } 64 | //+------------------------------------------------------------------+ 65 | --------------------------------------------------------------------------------