cleaned all trailing white space from source files.
[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
17    String MakeDatabasePath(const String name)
18    {
19       if(name)
20       {
21          char build[MAX_LOCATION];
22          strcpy(build, path ? path : "");
23          PathCat(build, name);
24          ChangeExtension(build, "sqlcipher", build);
25          return CopyString(build);
26       }
27       return null;
28    }
29
30    Database OpenDatabase(const String name, CreateOptions createOptions, DataSource ds)
31    {
32       Database result = null;
33       if(name && name[0])
34       {
35          String path = MakeDatabasePath(name);
36          sqlite3 * db;
37
38          // sqlite3_open(path, &db);
39          // sqlite3_open_v2(path, &db, SQLITE_OPEN_READONLY /*SQLITE_OPEN_READWRITE*/ /*SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE*/, null );
40
41          if(sqlite3_open_v2(path, &db, (createOptions == readOnly) ? SQLITE_OPEN_READONLY :
42             (SQLITE_OPEN_READWRITE | ((createOptions == create) ? SQLITE_OPEN_CREATE : 0)), null))
43             // fprintf(stderr, "%s\n", s); // interesting
44             printf($"Can't open database (%s): %s\n", path, sqlite3_errmsg(db));
45          else
46          {
47             int rc = SQLITE_ERROR;
48             char command[1024];
49             if(ds.pass && ds.pass[0])
50             {
51                sprintf(command, "PRAGMA key = '%s';", ds.pass);
52                sqlite3_exec(db, command, null, null, null);
53                rc = sqlite3_exec(db, "SELECT count(*) FROM sqlite_master;", null, null, null);
54                if(rc == SQLITE_NOTADB)
55                {
56                   printf($"EDASQLiteCipher: database (%s) format not recognized, disabling cipher_use_hmac to support version 1.1.x databases\n", path);
57                   strcpy(command, "PRAGMA cipher_use_hmac = OFF;");
58                   sqlite3_exec(db, command, null, null, null);
59                   rc = sqlite3_exec(db, "SELECT count(*) FROM sqlite_master;", null, null, null);
60                }
61                if(rc == SQLITE_OK)
62                {
63                   sprintf(command, "CREATE TABLE eda_table_fields(Table_Name TEXT, Name TEXT, Type TEXT, Length INT);");
64                   sqlite3_exec(db, command, null, null, null);
65                   result = SQLiteDatabase { db = db };
66                }
67                else
68                   printf($"Can't open database (%s): %s -- password may be incorrect\n", path, sqlite3_errstr(rc));
69             }
70          }
71          if(!result)
72             sqlite3_close(db);
73          delete path;
74       }
75       return result;
76    }
77 }