ecere/gui/Window: Prevent uninitialized values if base Window methods not overridden...
[sdk] / eda / drivers / sqliteCipher / EDASQLiteCipher.ec
1 #ifdef ECERE_STATIC
2 public import static "ecere"
3 public import static "EDA"
4 #else
5 public import "ecere"
6 public import "EDA"
7 #endif
8
9 #include "sqlite3.h"
10
11 import "EDASQLite.ec"
12
13 static class SQLiteCipherDataSource : SQLiteDataSource
14 {
15    class_property(name) = "SQLiteCipher";
16    class_property(databaseFileExtension) = "sqlcipher";
17
18    Database OpenDatabase(const String name, CreateOptions createOptions, DataSource ds)
19    {
20       Database result = null;
21       if(name && name[0])
22       {
23          String path = MakeDatabasePath(name);
24          sqlite3 * db;
25
26          // sqlite3_open(path, &db);
27          // sqlite3_open_v2(path, &db, SQLITE_OPEN_READONLY /*SQLITE_OPEN_READWRITE*/ /*SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE*/, null );
28
29          if(sqlite3_open_v2(path, &db, (createOptions == readOnly) ? SQLITE_OPEN_READONLY :
30             (SQLITE_OPEN_READWRITE | ((createOptions == create) ? SQLITE_OPEN_CREATE : 0)), null))
31             // fprintf(stderr, "%s\n", s); // interesting
32             printf($"Can't open database (%s): %s\n", path, sqlite3_errmsg(db));
33          else
34          {
35             int rc = SQLITE_ERROR;
36             char command[1024];
37             if(ds.pass && ds.pass[0])
38             {
39                sprintf(command, "PRAGMA key = '%s';", ds.pass);
40                sqlite3_exec(db, command, null, null, null);
41                rc = sqlite3_exec(db, "SELECT count(*) FROM sqlite_master;", null, null, null);
42                if(rc == SQLITE_NOTADB)
43                {
44                   printf($"EDASQLiteCipher: database (%s) format not recognized, disabling cipher_use_hmac to support version 1.1.x databases\n", path);
45                   strcpy(command, "PRAGMA cipher_use_hmac = OFF;");
46                   sqlite3_exec(db, command, null, null, null);
47                   rc = sqlite3_exec(db, "SELECT count(*) FROM sqlite_master;", null, null, null);
48                }
49                if(rc == SQLITE_OK)
50                {
51                   sprintf(command, "CREATE TABLE eda_table_fields(Table_Name TEXT, Name TEXT, Type TEXT, Length INT);");
52                   sqlite3_exec(db, command, null, null, null);
53                   result = SQLiteDatabase { db = db };
54                }
55                else
56                   printf($"Can't open database (%s): %s -- password may be incorrect\n", path, sqlite3_errstr(rc));
57             }
58          }
59          if(!result)
60             sqlite3_close(db);
61          delete path;
62       }
63       return result;
64    }
65 }