-rw-r--r-- | libsql/osqlitedriver.cpp | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/libsql/osqlitedriver.cpp b/libsql/osqlitedriver.cpp new file mode 100644 index 0000000..9214ad3 --- a/dev/null +++ b/libsql/osqlitedriver.cpp | |||
@@ -0,0 +1,126 @@ | |||
1 | #include <stdlib.h> | ||
2 | |||
3 | #include "osqlresult.h" | ||
4 | #include "osqlquery.h" | ||
5 | #include "osqlitedriver.h" | ||
6 | |||
7 | |||
8 | namespace { | ||
9 | struct Query { | ||
10 | OSQLError::ValueList errors; | ||
11 | OSQLResultItem::ValueList items; | ||
12 | OSQLiteDriver *driver; | ||
13 | }; | ||
14 | } | ||
15 | |||
16 | OSQLiteDriver::OSQLiteDriver( QLibrary *lib ) | ||
17 | : OSQLDriver( lib ) | ||
18 | { | ||
19 | m_sqlite = 0l; | ||
20 | } | ||
21 | OSQLiteDriver::~OSQLiteDriver() { | ||
22 | close(); | ||
23 | } | ||
24 | |||
25 | QString OSQLiteDriver::id()const { | ||
26 | return QString::fromLatin1("SQLite"); | ||
27 | } | ||
28 | |||
29 | void OSQLiteDriver::setUserName( const QString& ) {} | ||
30 | void OSQLiteDriver::setPassword( const QString& ) {} | ||
31 | |||
32 | void OSQLiteDriver::setUrl( const QString& url ) { | ||
33 | m_url = url; | ||
34 | } | ||
35 | void OSQLiteDriver::setOptions( const QStringList& ) { | ||
36 | } | ||
37 | /* | ||
38 | * try to open a db specified via setUrl | ||
39 | * and options | ||
40 | */ | ||
41 | bool OSQLiteDriver::open() { | ||
42 | char *error; | ||
43 | qWarning("about to open"); | ||
44 | m_sqlite = sqlite_open(m_url.local8Bit(), | ||
45 | 0, | ||
46 | &error ); | ||
47 | |||
48 | /* failed to open */ | ||
49 | if (m_sqlite == 0l ) { | ||
50 | // FIXME set the last error | ||
51 | qWarning("error:%s", error ); | ||
52 | free( error ); | ||
53 | return false; | ||
54 | } | ||
55 | return true; | ||
56 | } | ||
57 | /* close the db | ||
58 | * sqlite closes them without | ||
59 | * telling failure or success | ||
60 | */ | ||
61 | bool OSQLiteDriver::close() { | ||
62 | if (m_sqlite ) | ||
63 | sqlite_close( m_sqlite ), m_sqlite=0l; | ||
64 | |||
65 | return true; | ||
66 | } | ||
67 | /* Query */ | ||
68 | OSQLResult OSQLiteDriver::query( OSQLQuery* qu) { | ||
69 | if ( !m_sqlite ) { | ||
70 | // FIXME set error code | ||
71 | OSQLResult result( OSQLResult::Failure ); | ||
72 | return result; | ||
73 | } | ||
74 | Query query; | ||
75 | query.driver = this; | ||
76 | char *err; | ||
77 | /* SQLITE_OK 0 if return code > 0 == failure */ | ||
78 | if ( sqlite_exec(m_sqlite, qu->query(),&call_back, &query, &err) > 0 ) { | ||
79 | qWarning("Error while executing"); | ||
80 | free(err ); | ||
81 | // FixMe Errors | ||
82 | } | ||
83 | qWarning("Item count is %d", query.items.count() ); | ||
84 | OSQLResult result(OSQLResult::Success, | ||
85 | query.items, | ||
86 | query.errors ); | ||
87 | return result; | ||
88 | } | ||
89 | OSQLTable::ValueList OSQLiteDriver::tables() const { | ||
90 | |||
91 | } | ||
92 | OSQLError OSQLiteDriver::lastError() { | ||
93 | OSQLError error; | ||
94 | return error; | ||
95 | }; | ||
96 | /* handle a callback add the row to the global | ||
97 | * OSQLResultItem | ||
98 | */ | ||
99 | int OSQLiteDriver::handleCallBack( int, char**, char** ) { | ||
100 | return 0; | ||
101 | } | ||
102 | /* callback_handler add the values to the list*/ | ||
103 | int OSQLiteDriver::call_back( void* voi, int argc, | ||
104 | char** argv, char** columns) { | ||
105 | qWarning("Callback with %d items", argc ); | ||
106 | Query* qu = (Query*)voi; | ||
107 | |||
108 | //copy them over to a OSQLResultItem | ||
109 | QMap<QString, QString> tableString; | ||
110 | QMap<int, QString> tableInt; | ||
111 | for (int i = 0; i < argc; i++ ) { | ||
112 | qWarning("%s|%s", columns[i], argv[i] ); | ||
113 | tableInt.insert( i, QString::fromLocal8Bit(argv[i] ) ); | ||
114 | tableString.insert( QString::fromLocal8Bit( columns[i]), | ||
115 | QString::fromLocal8Bit( argv[i] ) ); | ||
116 | |||
117 | } | ||
118 | OSQLResultItem item( tableString, tableInt ); | ||
119 | qu->items.append( item ); | ||
120 | |||
121 | return ((Query*)voi)->driver->handleCallBack( argc, | ||
122 | argv, | ||
123 | columns ); | ||
124 | |||
125 | |||
126 | } | ||