xlang v4.0 Release
程序设计语言基础库文档
载入中...
搜索中...
未找到
sqlcipher.xcsm
浏览该文件的文档.
1//xlang Source, Name:SQLCipher.xcsm
2//Date: Wed Mar 00:26:43 2019
3
4
5class SQLCipher : Sql.Connection{
6 static const String DRIVERNAME = "sqlcipher";
7
8 public static bool registry(){
9 if (Native.init()){
10 Sql.Database.reigstry(DRIVERNAME, new SQLCipherRegister());
11 return true;
12 }
13 return false;
14 }
15
16
17 static class SQLCipherRegister : Sql.ConnectionRegister{
19 if (drivername.equals(DRIVERNAME)){
20 return new SQLCipher();
21 }
22 return nilptr;
23 }
24 };
25
26 static class Native : Library{
27 static bool bloaded = false;
28
29 public static bool init(){
30 if (bloaded == false){
31 if (Sqlite.CSTDNative.load()){
32 try{
33 loadLibrary("sqlite3");
34 bloaded = true;
35 }catch(Exception e){
37 }
38 }
39 }
40 return bloaded;
41 }
42
43 public import {
44 int cdecl sqlite3_open(
45 String filename, /* Database filename (UTF-8) */
46 ObjectPtr ppDb /* OUT: SQLite db handle */
47 );
48
49 /*int cdecl sqlite3_key(
50 Pointer db, /* Database to be rekeyed * /
51 String pKey, int nKey /* The key * /
52 );*/
53
54 int cdecl sqlite3_close(
55 Pointer db);
56
57 int cdecl sqlite3_key(Pointer db, ObjectPtr pKey, int len);
58 int cdecl sqlite3_key_v2(Pointer db, String zDbName, ObjectPtr pKey, int len);
59 int cdecl sqlite3_rekey(Pointer db, ObjectPtr pKey, int nKey);
60 int cdecl sqlite3_rekey_v2(Pointer db, String zDbName, ObjectPtr pKey, int nKey);
61
62 int cdecl sqlite3_step(long pStmt);
63 int cdecl sqlite3_exec(
64 Pointer db, /* An open database */
65 String sql, /* SQL to be evaluated */
66 Pointer , /* Callback function */
67 Pointer , /* 1st argument to callback */
68 ObjectPtr msg /* Error msg written here */
69 );
70 int cdecl sqlite3_prepare(
71 Pointer db, /* Database handle. */
72 String zSql, /* UTF-8 encoded SQL statement. */
73 int nBytes, /* Length of zSql in bytes. */
74 ObjectPtr ppStmt, /* OUT: A pointer to the prepared statement */
75 ObjectPtr pzTail /* OUT: End of parsed string */
76 );
78 Pointer db, /* Database handle. */
79 String zSql, /* UTF-8 encoded SQL statement. */
80 int nBytes, /* Length of zSql in bytes. */
81 ObjectPtr ppStmt, /* OUT: A pointer to the prepared statement */
82 ObjectPtr pzTail /* OUT: End of parsed string */
83 );
84 int cdecl sqlite3_reset(Pointer pStmt);
85 int cdecl sqlite3_bind_int(Pointer pStmt, int, int);
86 int cdecl sqlite3_bind_int64(Pointer pStmt, int, long);
87 int cdecl sqlite3_bind_double(Pointer pStmt, int, double);
88 int cdecl sqlite3_bind_null(Pointer pStmt, int);
89 int cdecl sqlite3_bind_text(Pointer pStmt, int, String, int , Pointer);
90 int cdecl sqlite3_bind_blob(Pointer pStmt, int ,ObjectPtr, int , Pointer);
91 String cdecl sqlite3_errmsg(Pointer pStmt);
92 int cdecl sqlite3_errcode(Pointer db);
93 int cdecl sqlite3_column_int(Pointer pStmt, int i);
94 int cdecl sqlite3_changes(Pointer pStmt);
95 double cdecl sqlite3_column_double(Pointer pStmt, int i);
96 long cdecl sqlite3_column_int64(Pointer pStmt, int i);
97 String cdecl sqlite3_column_text(Pointer pStmt, int i);
98 int cdecl sqlite3_column_count(Pointer pStmt);
99 String cdecl sqlite3_column_name(Pointer pStmt, int N);
100 String cdecl sqlite3_column_decltype(Pointer pStmt, int N);
101 int cdecl sqlite3_finalize(Pointer pStmt);
102 long cdecl sqlite3_last_insert_rowid(Pointer pStmt);
103 };
104 };
105
106 static bool registried = registry();
107 long hdb = 0;
108
109 public void create(String uri, String username, String pwd)override{
110 int err = Native.sqlite3_open(uri, hdb);
111 if (err != 0){
112 throw new Sql.SqlException(err, "can not open database:" + uri);
113 }
114 /*if (pwd != nilptr){
115 err = Native.sqlite3_key(hdb, pwd, pwd.length());
116 if (err != 0){
117 throw new Sql.SqlException(err, "password incorrect");
118 }
119 }*/
120 }
121
126 public void setOption(int opt, Object option)override{
127 throw new Sql.DatabaseNotSupportException("setOption");
128 }
129
130 public void throw_sqlite_error(){
131 throw new Sql.SqlException(getErrorCode(),__file__ + ":" + __line__ + ", SQLITE: " + getError() );
132 }
133 public int getErrorCode()override{
134 return Native.sqlite3_errcode(hdb);
135 }
136
137 public void key(byte [] pKey){
138 int err = Native.sqlite3_key(hdb, pKey, pKey.length);
139 if (err != 0){
141 }
142 }
143
144 public void key(String dbname, byte [] pKey){
145 int err = Native.sqlite3_key_v2(hdb, dbname, pKey, pKey.length);
146 if (err != 0){
148 }
149 }
150
151 public void rekey(byte [] pKey){
152 int err = Native.sqlite3_rekey(hdb, pKey, pKey.length);
153 if (err != 0){
155 }
156 }
157
158 public void rekey(String dbname, byte [] pKey){
159 int err = Native.sqlite3_rekey_v2(hdb, dbname, pKey, pKey.length);
160 if (err != 0){
162 }
163 }
164
165 public String getError()override{
166 return Native.sqlite3_errmsg(hdb);
167 }
168
169 public Object getOption(int opt)override{
170 throw new Sql.DatabaseNotSupportException("getOption");
171 return nilptr;
172 }
173
175 return new SQLCipherPreparedStatement(this, sql);
176 }
177
179 return new SQLCipherPreparedStatement(this);
180 }
181
182 public bool isClosed()override{
183 return hdb != 0;
184 }
185
186 public long getLastInsertedRowId(){
187 return Native.sqlite3_last_insert_rowid(hdb);
188 }
189
190 public void close()override{
191 if (hdb != 0){
192 Native.sqlite3_close(hdb);
193 hdb = 0;
194 }
195 }
196
197 void finalize(){
198 close();
199 }
200
201
202 public int enableSycnhronous(int n){
203 if (n < 0 || n > 2){
204 return -1;
205 }
206 String [] cof = {"FULL", "NORMAL", "OFF"};
207 return execute("PRAGMA synchronous = " + cof[n] + ";");
208 }
209
210 public int workBegin(){
211 return execute("BEGIN;");
212 }
213
214 public int commit(){
215 return execute("COMMIT;");
216 }
217
218 public int rollback(){
219 return execute("ROLLBACK;");
220 }
221
222 public String generateErrorText(long herr, String fallback){
223 if (herr != 0){
224 long len = Sqlite.CSTDNative.strlen(herr);
225 byte [] data = new byte[len + 1];
226 Sqlite.CSTDNative.memcpy(data, herr, len);
227 return new String(data, 0, len);
228 }
229 return fallback;
230 }
231
232 public int execute(String stmt){
233 if (hdb != 0){
234 long msg = 0;
235 int err = Native.sqlite3_exec(hdb, stmt, (long)0, (long)0, msg);
236 if (err != 0){
238 }
239 return err;
240 }
241 throw new Sql.SqlException(-1, "database not opened");
242 return -1;
243 }
244
245 public int get_changes(){
246 return Native.sqlite3_changes(hdb);
247 }
248
249 public long prepare(String sql){
250 long nstmt = 0;
251 if (hdb != 0){
252 int err = Native.sqlite3_prepare_v2(hdb, sql, sql.length(), nstmt, nilptr);
253 if (err != 0 || nstmt == 0){
255 }
256 return nstmt;
257 }
258 throw new Sql.SqlException(-1, "database not opened");
259 return nstmt;
260 }
261
262 public long getDb(){
263 return hdb;
264 }
265
266
267 public static class SQLCipherPreparedStatement : Sql.PreparedStatement{
270 long nstmt = 0;
271 int param_pos = 0;
272
273 public long getHandle(){
274 return nstmt;
275 }
276
278 sqlite = db;
279 sql_text = sql;
280 nstmt = sqlite.prepare(sql);
281 }
282
284 sqlite = db;
285 }
286
287 public int execute(String sql)override{
288 return sqlite.execute(sql);
289 }
290
291 public bool reset(){
292 return Native.sqlite3_reset(nstmt) == 0;
293 }
294 public int executeUpdate()override{
295 int r = Native.sqlite3_step(nstmt);
296 if (101 == r){
297 return 0;
298 }
299 return r;
300 }
301
302 public int execute()override{
303 int r = Native.sqlite3_step(nstmt);
304 if (101 == r){
305 return 0;
306 }
307 return r;
308 }
309
310 public Sql.ResultSet executeQuery()override{
311 return new SQLCipherResultSet(sqlite, this);
312 }
313
314 public Sql.ResultSet executeQuery(String sql)override{
315 nstmt = sqlite.prepare(sql);
316 return new SQLCipherResultSet(sqlite, this);
317 }
318
319 public int executeUpdate(String sql)override{
320 return sqlite.execute(sql);
321 }
322
323 public int get_changes()override{
324 return sqlite.get_changes();
325 }
326
327 public Sql.ResultSet getResult()override{
328 throw new Sql.DatabaseNotSupportException("getResult");
329 return nilptr;
330 }
331
332 public void close()override{
333 if (nstmt != 0){
334 Native.sqlite3_finalize(nstmt);
335 nstmt = 0;
336 }
337 sqlite = nilptr;
338 }
339
340
341 public void setValue(String value)override{
342 int res = 0;
343 if (value != nilptr){
344 res = Native.sqlite3_bind_text(nstmt, ++param_pos, value, value.length(), nilptr);
345 if (0 != res){
346 sqlite.throw_sqlite_error();
347 }
348 }else{
349 res = Native.sqlite3_bind_null(nstmt, ++param_pos);
350 if (0 != res){
351 sqlite.throw_sqlite_error();
352 }
353 }
354 }
355
356 public void setValue(int value)override{
357 int res = Native.sqlite3_bind_int(nstmt, ++param_pos, value);
358 if (0 != res){
359 sqlite.throw_sqlite_error();
360 }
361 }
362
363 public void setValue(long value)override{
364 int res = Native.sqlite3_bind_int64(nstmt, ++param_pos, value);
365 if (0 != res){
366 sqlite.throw_sqlite_error();
367 }
368 }
369
370 public void setValue(byte value)override{
371 int res = Native.sqlite3_bind_int(nstmt, ++param_pos, value);
372 if (0 != res){
373 sqlite.throw_sqlite_error();
374 }
375 }
376
377 public void setValue(double value)override{
378 int res = Native.sqlite3_bind_double(nstmt, ++param_pos, value);
379 if (0 != res){
380 sqlite.throw_sqlite_error();
381 }
382 }
383
384
385 public void setValue(int id, String value)override{
386 int res = 0;
387 if (value != nilptr){
388 res = Native.sqlite3_bind_text(nstmt, id, value, value.length(), nilptr);
389 if (0 != res){
390 sqlite.throw_sqlite_error();
391 }
392 }else{
393 res = Native.sqlite3_bind_null(nstmt, id);
394 if (0 != res){
395 sqlite.throw_sqlite_error();
396 }
397 }
398 }
399
400 public void setValue(int id, int value)override{
401 int res = Native.sqlite3_bind_int(nstmt, id, value);
402 if (0 != res){
403 sqlite.throw_sqlite_error();
404 }
405 }
406
407 public void setValue(int id, long value)override{
408 int res = Native.sqlite3_bind_int64(nstmt, id, value);
409 if (0 != res){
410 sqlite.throw_sqlite_error();
411 }
412 }
413
414 public void setValue(int id, byte value)override{
415 int res = Native.sqlite3_bind_int(nstmt, id, value);
416 if (0 != res){
417 sqlite.throw_sqlite_error();
418 }
419 }
420
421 public void setValue(int id, double value)override{
422 int res = Native.sqlite3_bind_double(nstmt, id, value);
423 if (0 != res){
424 sqlite.throw_sqlite_error();
425 }
426 }
427
428 public void setValues(Object [] args)override{
429 for (int i = 0; i < args.length; i++){
430 if (args[i].instanceOf(String)){
431 setValue((String)args[i]);
432 }else
433 if (args[i].instanceOf(int)){
434 setValue((int)args[i]);
435 }else
436 if (args[i].instanceOf(long)){
437 setValue((long)args[i]);
438 }else
439 if (args[i].instanceOf(byte)){
440 setValue((byte)args[i]);
441 }else
442 if (args[i].instanceOf(double)){
443 setValue((double)args[i]);
444 }else{
445 throw new Sql.SqlException(-1, "sql " + sql_text + " dont accept type:" + i);
446 }
447 }
448 }
449 };
450
451 public static class SQLCipherResultSet : Sql.ResultSet{
453 SQLCipherPreparedStatement sps;
455
456 public SQLCipherResultSet(SQLCipher db, SQLCipherPreparedStatement _sps){
457 sqlite = db;
458 sps = _sps;
459 parseResult();
460 }
461
462 public int getColumnCount()override{
463 return Native.sqlite3_column_count(sps.getHandle());
464 }
465
466 public String getColumnName(int n){
467 return Native.sqlite3_column_name(sps.getHandle(), n);
468 }
469
470 public void parseResult(){
471 int ncolumn = Native.sqlite3_column_count(sps.getHandle());
472 for (int i = 0; i < ncolumn; i++){
473 String colutext = Native.sqlite3_column_name(sps.getHandle(), i);
474 __fields_Map.put(colutext, i);
475 }
476 }
477
478 public int label2idx(String label){
479 return __fields_Map.get(label);
480 }
481
482 public bool first()override{
483 throw new Sql.DatabaseNotSupportException("first");
484 return false;
485 }
486
487 public bool last()override{
488 throw new Sql.DatabaseNotSupportException("last");
489 return false;
490 }
491
492 public bool next()override{
493 int code = Native.sqlite3_step(sps.getHandle());
494 return 100 == code;
495 }
496
497 public bool previous()override{
498 throw new Sql.DatabaseNotSupportException("previous");
499 return false;
500 }
501
502 public bool isFirst()override{
503 throw new Sql.DatabaseNotSupportException("isFirst");
504 return false;
505 }
506
507 public bool isLast()override{
508 throw new Sql.DatabaseNotSupportException("isLast");
509 return false;
510 }
511
512 public bool isValid()override{
513 return sps != nilptr && (sps.getHandle() != 0);
514 }
515
516 public bool isEof()override{
517 throw new Sql.DatabaseNotSupportException("isEof");
518 return false;
519 }
520
521 public int getInt(String columnlabel)override{
522 return Native.sqlite3_column_int(sps.getHandle(), label2idx(columnlabel));
523 }
524
525 public long getLong(String columnlabel)override{
526 return Native.sqlite3_column_int64(sps.getHandle(), label2idx(columnlabel));
527 }
528
529 public double getDouble(String columnlabel)override{
530 return Native.sqlite3_column_double(sps.getHandle(), label2idx(columnlabel));
531 }
532
533 public byte getByte(String columnlabel)override{
534 return Native.sqlite3_column_int(sps.getHandle(), label2idx(columnlabel));
535 }
536
537 public bool getBoolean(String columnlabel)override{
538 return Native.sqlite3_column_int(sps.getHandle(), label2idx(columnlabel)) != 0;
539 }
540
541 public String getString(String columnlabel)override{
542 return Native.sqlite3_column_text(sps.getHandle(), label2idx(columnlabel));
543 }
544
545 public int getInt(int columnIndex)override{
546 return Native.sqlite3_column_int(sps.getHandle(), columnIndex);
547 }
548
549 public long getLong(int columnIndex)override{
550 return Native.sqlite3_column_int64(sps.getHandle(), columnIndex);
551 }
552
553 public double getDouble(int columnIndex)override{
554 return Native.sqlite3_column_double(sps.getHandle(), columnIndex);
555 }
556
557 public byte getByte(int columnIndex)override{
558 return Native.sqlite3_column_int(sps.getHandle(), columnIndex);
559 }
560
561 public bool getBoolean(int columnIndex)override{
562 return Native.sqlite3_column_int(sps.getHandle(), columnIndex)!= 0;
563 }
564
565 public String getString(int columnIndex)override{
566 return Native.sqlite3_column_text(sps.getHandle(), columnIndex);
567 }
568
569
570
571 public int findColumn(String label)override{
572 return label2idx(label);
573 }
574
575 public long getRowCount()override{
576 throw new Sql.DatabaseNotSupportException("getRow");
577 return 0;
578 }
579
580 public int getRow()override{
581 throw new Sql.DatabaseNotSupportException("getRow");
582 return 0;
583 }
584
585 public void close()override{
586
587 }
588
589 };
590};
系统和IO相关
static final void output(String)
异常类
final String getMessage()
static final void loadLibrary(String)
bool get(K)
Iterator put(K, V)
int cdecl sqlite3_bind_text(Pointer pStmt, int, String, int , Pointer)
int cdecl sqlite3_finalize(Pointer pStmt)
int cdecl sqlite3_rekey(Pointer db, ObjectPtr pKey, int nKey)
int cdecl sqlite3_bind_int(Pointer pStmt, int, int)
int cdecl sqlite3_close( Pointer db)
long cdecl sqlite3_column_int64(Pointer pStmt, int i)
int cdecl sqlite3_rekey_v2(Pointer db, String zDbName, ObjectPtr pKey, int nKey)
double cdecl sqlite3_column_double(Pointer pStmt, int i)
int cdecl sqlite3_errcode(Pointer db)
String cdecl sqlite3_column_text(Pointer pStmt, int i)
int cdecl sqlite3_key_v2(Pointer db, String zDbName, ObjectPtr pKey, int len)
int cdecl sqlite3_bind_double(Pointer pStmt, int, double)
int cdecl sqlite3_bind_null(Pointer pStmt, int)
int cdecl sqlite3_bind_blob(Pointer pStmt, int ,ObjectPtr, int , Pointer)
int cdecl sqlite3_prepare( Pointer db, String zSql, int nBytes, ObjectPtr ppStmt, ObjectPtr pzTail )
int cdecl sqlite3_exec( Pointer db, String sql, Pointer , Pointer , ObjectPtr msg )
int cdecl sqlite3_step(long pStmt)
String cdecl sqlite3_errmsg(Pointer pStmt)
int cdecl sqlite3_column_int(Pointer pStmt, int i)
String cdecl sqlite3_column_decltype(Pointer pStmt, int N)
int cdecl sqlite3_column_count(Pointer pStmt)
long cdecl sqlite3_last_insert_rowid(Pointer pStmt)
int cdecl sqlite3_reset(Pointer pStmt)
static bool bloaded
int cdecl sqlite3_bind_int64(Pointer pStmt, int, long)
int cdecl sqlite3_prepare_v2( Pointer db, String zSql, int nBytes, ObjectPtr ppStmt, ObjectPtr pzTail )
int cdecl sqlite3_key(Pointer db, ObjectPtr pKey, int len)
String cdecl sqlite3_column_name(Pointer pStmt, int N)
int cdecl sqlite3_changes(Pointer pStmt)
static bool init()
void setValue(int id, byte value) override
void setValue(int value) override
void setValue(int id, long value) override
void setValue(int id, int value) override
int execute(String sql) override
void setValue(long value) override
Sql.ResultSet executeQuery() override
void setValue(int id, String value) override
void setValue(int id, double value) override
Sql.ResultSet executeQuery(String sql) override
int executeUpdate(String sql) override
void setValue(String value) override
void setValues(Object [] args) override
Sql.ResultSet getResult() override
void setValue(byte value) override
void setValue(double value) override
SQLCipherPreparedStatement(SQLCipher db, String sql)
Sql.Connection allocConnection(String drivername) override
String getString(int columnIndex) override
String getString(String columnlabel) override
bool getBoolean(int columnIndex) override
SQLCipherPreparedStatement sps
int getColumnCount() override
double getDouble(String columnlabel) override
int label2idx(String label)
long getRowCount() override
Map<String, int> __fields_Map
int getInt(String columnlabel) override
long getLong(int columnIndex) override
double getDouble(int columnIndex) override
SQLCipherResultSet(SQLCipher db, SQLCipherPreparedStatement _sps)
byte getByte(String columnlabel) override
long getLong(String columnlabel) override
bool getBoolean(String columnlabel) override
byte getByte(int columnIndex) override
int getInt(int columnIndex) override
int findColumn(String label) override
int getErrorCode() override
void throw_sqlite_error()
void rekey(String dbname, byte [] pKey)
long getDb()
void close() override
void create(String uri, String username, String pwd) override
Object getOption(int opt) override
void finalize()
int execute(String stmt)
int workBegin()
bool isClosed() override
static const String DRIVERNAME
Definition sqlcipher.xcsm:6
static bool registry()
Definition sqlcipher.xcsm:8
int commit()
void key(String dbname, byte [] pKey)
long getLastInsertedRowId()
void key(byte [] pKey)
void rekey(byte [] pKey)
long prepare(String sql)
int enableSycnhronous(int n)
Sql.Statement createStatement() override
Sql.PreparedStatement prepareStatement(String sql) override
void setOption(int opt, Object option) override
String getError() override
int rollback()
static bool registried
String generateErrorText(long herr, String fallback)
int get_changes()
static bool reigstry(String name, ConnectionRegister reg)
Definition xsql.xcs:210
static bool load()
Definition Sqlite.xcsm:29
字符串类
int length()
bool equals(String)
Definition xsql.xcs:3