EDA:SQLite:SQLiteCipher: moved code common to both drivers into EDASQLiteCommon.ec...
[sdk] / eda / drivers / sqlite / EDASQLite.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 "EDASQLiteCommon"
12
13 static class SQLiteDataSource : DataSourceDriver
14 {
15    class_property(name) = "SQLite";
16    String path;
17    OldList listDatabases;
18    uint databasesCount;
19
20    String BuildLocator(DataSource ds)
21    {
22       return CopyString(ds.host);
23    }
24
25    uint GetDatabasesCount()
26    {
27       return databasesCount;
28    }
29
30    ~SQLiteDataSource()
31    {
32       delete path;
33    }
34
35    bool Connect(const String locator)
36    {
37       delete path;
38       path = CopyString(locator);
39       // TODO, use user name and password for local security?
40       // TODO, open ds in read or write mode
41       if(FileExists(path))
42       {
43          int n = 0;
44          FileListing listing { path, "sqlite" };
45          databasesCount = 0;
46          while(listing.Find())
47             databasesCount++;
48          return true;
49       }
50       return false;
51    }
52
53    bool RenameDatabase(const String name, const String rename)
54    {
55       if(name && rename && path && FileExists(path))
56       {
57          String path;
58          path = MakeDatabasePath(name);
59          if(FileExists(path))
60          {
61             bool renamed;
62             String repath;
63             repath = MakeDatabasePath(rename);
64             renamed = RenameFile(path, repath);
65             delete path;
66             delete repath;
67             return renamed;
68          }
69          delete path;
70       }
71       return false;
72    }
73
74    bool DeleteDatabase(const String name)
75    {
76       if(path && FileExists(path))
77       {
78          bool deleted;
79          String path = MakeDatabasePath(name);
80          deleted = DeleteFile(path);  // delete file seems to return true even if the file does not exist
81          databasesCount--;
82          delete path;
83          return deleted;
84       }
85       return false;
86    }
87
88    String MakeDatabasePath(const String name)
89    {
90       if(name)
91       {
92          char build[MAX_LOCATION];
93          strcpy(build, path ? path : "");
94          PathCat(build, name);
95          ChangeExtension(build, "sqlite", build);
96          return CopyString(build);
97       }
98       return null;
99    }
100
101    Database OpenDatabase(const String name, CreateOptions createOptions, DataSource ds)
102    {
103       Database result = null;
104       if(name && name[0])
105       {
106          String path = MakeDatabasePath(name);
107          sqlite3 * db;
108
109          // sqlite3_open(path, &db);
110          // sqlite3_open_v2(path, &db, SQLITE_OPEN_READONLY /*SQLITE_OPEN_READWRITE*/ /*SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE*/, null );
111          
112          if(sqlite3_open_v2(path, &db, (createOptions == readOnly) ? SQLITE_OPEN_READONLY :
113             (SQLITE_OPEN_READWRITE | ((createOptions == create) ? SQLITE_OPEN_CREATE : 0)), null))
114          {
115             // fprintf(stderr, "%s\n", s); // interesting
116             printf("EDASQLite: Can't open database (%s): %s\n", path, sqlite3_errmsg(db));
117             sqlite3_close(db);
118          }
119          else
120          {
121             char command[1024];
122             sprintf(command, "CREATE TABLE eda_table_fields(Table_Name TEXT, Name TEXT, Type TEXT, Length INT);");
123             sqlite3_exec(db, command, null, null, null);
124
125             result = SQLiteDatabase { db = db };
126          }            
127          delete path;
128       }
129       return result;
130    }
131 }