1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
/* ============================================================
*
* This file is a part of digiKam project
* http://www.digikam.org
*
* Date : 2005-06-05
* Description : TQSlite DB interface.
*
* Copyright (C) 2005 by Renchi Raju <[email protected]>
*
* This program is free software; you can redistribute it
* and/or modify it under the terms of the GNU General
* Public License as published by the Free Software Foundation;
* either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* ============================================================ */
#ifdef HAVE_CONFIG_H
#include "config.h" // Needed for NFS_HACK
#endif
// TQt includes.
#include <tqstringlist.h>
#include <tqdir.h>
#include <tqfile.h>
// KDE includes.
#include <tdeio/global.h>
#include <kdebug.h>
// SQlite includes.
#include "sqlite3.h"
// Local includes.
#include "sqlitedb.h"
SqliteDB::SqliteDB()
{
m_db = 0;
}
SqliteDB::~SqliteDB()
{
closeDB();
}
void SqliteDB::openDB(const TQString& directory)
{
if (m_db)
{
closeDB();
}
TQString dbPath = directory + "/digikam3.db";
#ifdef NFS_HACK
dbPath = TQDir::homeDirPath() + "/.trinity/share/apps/digikam/" +
TDEIO::encodeFileName(TQDir::cleanDirPath(dbPath));
#endif
sqlite3_open(TQFile::encodeName(dbPath), &m_db);
if (m_db == 0)
{
kdWarning() << "Cannot open database: "
<< sqlite3_errmsg(m_db)
<< endl;
}
}
void SqliteDB::closeDB()
{
if (m_db)
{
sqlite3_close(m_db);
m_db = 0;
}
}
bool SqliteDB::execSql(const TQString& sql, TQStringList* const values,
TQString* errMsg, bool debug ) const
{
if ( debug )
kdDebug() << "SQL-query: " << sql << endl;
if ( !m_db )
{
kdWarning() << k_funcinfo << "SQLite pointer == NULL"
<< endl;
if (errMsg)
{
*errMsg = TQString::fromLatin1("SQLite database not open");
}
return false;
}
const char* tail;
sqlite3_stmt* stmt;
int error;
//compile SQL program to virtual machine
error = sqlite3_prepare(m_db, sql.utf8(), -1, &stmt, &tail);
if ( error != SQLITE_OK )
{
kdWarning() << k_funcinfo
<< "sqlite_compile error: "
<< sqlite3_errmsg(m_db)
<< " on query: "
<< sql << endl;
if (errMsg)
{
*errMsg = TQString::fromLatin1("sqlite_compile error: ") +
TQString::fromLatin1(sqlite3_errmsg(m_db)) +
TQString::fromLatin1(" on query: ") +
sql;
}
return false;
}
int cols = sqlite3_column_count(stmt);
while ( true )
{
error = sqlite3_step( stmt );
if ( error == SQLITE_DONE || error == SQLITE_ERROR )
break;
//iterate over columns
for ( int i = 0; values && i < cols; i++ )
{
*values << TQString::fromUtf8( (const char*)sqlite3_column_text( stmt, i ) );
}
}
sqlite3_finalize( stmt );
if ( error != SQLITE_DONE )
{
kdWarning() << "sqlite_step error: "
<< sqlite3_errmsg( m_db )
<< " on query: "
<< sql << endl;
if (errMsg)
{
*errMsg = TQString::fromLatin1("sqlite_step error: ") +
TQString::fromLatin1(sqlite3_errmsg(m_db)) +
TQString::fromLatin1(" on query: ") +
sql;
}
return false;
}
return true;
}
void SqliteDB::setSetting( const TQString& keyword, const TQString& value )
{
execSql( TQString("REPLACE into Settings VALUES ('%1','%2');")
.arg( escapeString(keyword) )
.arg( escapeString(value) ));
}
TQString SqliteDB::getSetting( const TQString& keyword )
{
TQStringList values;
execSql( TQString("SELECT value FROM Settings "
"WHERE keyword='%1';")
.arg(escapeString(keyword)),
&values );
if (values.isEmpty())
return TQString();
else
return values[0];
}
extern TQString escapeString(const TQString& str)
{
TQString st(str);
st.replace( "'", "''" );
return st;
}
TQ_LLONG SqliteDB::lastInsertedRow() const
{
return sqlite3_last_insert_rowid(m_db);
}
|