summaryrefslogtreecommitdiffstats
path: root/krecipes/src/backends/SQLite/libqsqlite/krecqsqlitedb.cpp
blob: e6175d3b9be20d295635c3dbd0d9267e97cd8a9c (plain)
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
/***************************************************************************
*                                                                         *
*                           Copyright (C) 2003                            *
*                by Unai Garro (ugarro@users.sourceforge.net)             *
*                       Martin Imobersteg <imm@gmx.ch>                    *
*                          and opie project                               *
*                                                                         *
*                                                                         *
*    This code was originally developed by the opie project, on which     *
*                    Martin Imobersteg based his work.                    *
* This file is adds a small extension, necessary to perform some minimum  * 
* SQL actions                                                             *
*                                                                         *
*         (this project is different from that in qsqlite.sf.net)         *
*   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 of the License, or     *
*   (at your option) any later version.                                   *
***************************************************************************/


#include "krecqsqlitedb.h"
#include "krecqsqliteresult.h"

#include <qvaluelist.h>

#include <kdebug.h>

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#if HAVE_SQLITE
#include <sqlite.h>
#elif HAVE_SQLITE3
#include <sqlite3.h>
#endif
#include <stdlib.h>

QSQLiteDB::QSQLiteDB( QObject *, const char * )
{}

bool QSQLiteDB::open( const QString &dbname )
{
	char * errmsg = 0;

#if HAVE_SQLITE

	m_db = sqlite_open( dbname.latin1(), 0, &errmsg );
#elif HAVE_SQLITE3

	int res = sqlite3_open( dbname.latin1(), &m_db );

	if ( res != SQLITE_OK )
		return false;
#endif

	if ( m_db == 0L ) {
#if HAVE_SQLITE
		sqlite_freemem( errmsg );
#elif HAVE_SQLITE3
		sqlite3_free( errmsg );
#endif
		return false;
	}

	int func_res;
	#if HAVE_SQLITE
	func_res = sqlite_create_function(m_db,"lastInsertID",0,&lastInsertID,m_db );
	#elif HAVE_SQLITE3
	func_res = sqlite3_create_function(m_db,"lastInsertID",0,SQLITE_ANY,m_db,
	   &lastInsertID, 0, 0 );
	#endif

	return func_res == 0;
}

void QSQLiteDB::close()
{
	if ( m_db ) {
#if HAVE_SQLITE
		sqlite_close( m_db );
#elif HAVE_SQLITE3

		sqlite3_close( m_db );
#endif

		m_db = 0L;
	}
}

QSQLiteResult QSQLiteDB::executeQuery( const QString &query, int *lastID )
{
	QSQLiteResult res;
	if ( !m_db ) {
		return res;
	}

	char *errmsg = 0;
#if HAVE_SQLITE

	if ( sqlite_exec( m_db, query.latin1(), &call_back, &res, &errmsg ) > 0 )
#elif HAVE_SQLITE3

	if ( sqlite3_exec( m_db, query.latin1(), &call_back, &res, &errmsg ) > 0 )
#endif

	{
		kdDebug() << "SQLite error: " << errmsg << endl <<
		"\t (Query: " << query << ")" << endl;
		res.setError( errmsg );
		res.setStatus( QSQLiteResult::Failure );
#if HAVE_SQLITE
		sqlite_freemem( errmsg );
#elif HAVE_SQLITE3
		sqlite3_free( errmsg );
#endif
	}

	if ( lastID ) {
#if HAVE_SQLITE
		* lastID = sqlite_last_insert_rowid( m_db );
#elif HAVE_SQLITE3

		*lastID = sqlite3_last_insert_rowid( m_db );
#endif

	}

	res.setStatus( QSQLiteResult::Success );

	return res;
}

int QSQLiteDB::call_back( void* result, int argc, char** argv, char** columns )
{
	QSQLiteResult * res = ( QSQLiteResult* ) result;

	QMap<QString, QCString> tableString;
	QMap<int, QCString> tableInt;

	for ( int i = 0; i < argc; i++ ) {
		tableInt.insert( i, argv[ i ] );
		tableString.insert( columns[ i ],
		                    argv[ i ] );
	}

	QSQLiteResultRow row( tableString, tableInt );
	res->addRow( row );

	return 0;
}

#if HAVE_SQLITE
void QSQLiteDB::lastInsertID(sqlite_func *context,int argc,const char**)
{
	Q_ASSERT( argc==0 );

	void *db = sqlite_user_data(context);
	sqlite_set_result_int(context, sqlite_last_insert_rowid( (sqlite*)db ) );
}
#elif HAVE_SQLITE3
void QSQLiteDB::lastInsertID( sqlite3_context *context, int argc, sqlite3_value ** )
{
	Q_ASSERT( argc==0 );

	void *db = sqlite3_user_data(context);
	sqlite3_result_int(context, sqlite3_last_insert_rowid( (sqlite3*)db ) );
}
#endif