39 if (strncmp(db,
"sqlite://", 9)) {
41 Error(
"TSQLiteServer",
"protocol in db argument should be sqlite it is %s",
42 givenProtocol.
Data());
47 const char *dbase = db + 9;
49#ifndef SQLITE_OPEN_URI
50#define SQLITE_OPEN_URI 0x00000000
52#if SQLITE_VERSION_NUMBER >= 3005000
66 Error(
"TSQLiteServer",
"opening of %s failed with error: %d %s", dbase, sqlite3_errcode(
fSQLite), sqlite3_errmsg(
fSQLite));
106 return Exec(
"BEGIN TRANSACTION");
117 return sqlite3_get_autocommit(
fSQLite) == 0;
127 return Exec(
"COMMIT TRANSACTION");
136 return Exec(
"ROLLBACK TRANSACTION");
147 Error(
"Query",
"not connected");
151 sqlite3_stmt *preparedStmt =
nullptr;
152 const char *tail =
nullptr;
155#if SQLITE_VERSION_NUMBER >= 3005000
156 int retVal = sqlite3_prepare_v2(
fSQLite, sql, -1, &preparedStmt, &tail);
158 int retVal = sqlite3_prepare(
fSQLite, sql, -1, &preparedStmt, &tail);
160 if (retVal != SQLITE_OK) {
161 Error(
"Query",
"SQL Error: %d %s", retVal, sqlite3_errmsg(
fSQLite));
164 if (tail && tail[0] !=
'\0')
165 Warning(
"Query",
"Don't use multiple queries, '%s' query was ignored", tail);
177 Error(
"Exec",
"not connected");
181 char *sqlite_err_msg;
182 int ret = sqlite3_exec(
fSQLite, sql, NULL, NULL, &sqlite_err_msg);
183 if (ret != SQLITE_OK) {
184 Error(
"Exec",
"SQL Error: %d %s", ret, sqlite_err_msg);
185 sqlite3_free(sqlite_err_msg);
198 Error(
"SelectDataBase",
"SelectDataBase command makes no sense for SQLite!");
208 Error(
"GetDataBases",
"GetDataBases command makes no sense for SQLite!");
221 Error(
"GetTables",
"not connected");
225 TString sql =
"SELECT name FROM sqlite_master where type='table'";
227 sql +=
Form(
" AND name LIKE '%s'", wild);
245 Error(
"GetColumns",
"not connected");
250 Error(
"GetColumns",
"Not implementable for SQLite as a query with wildcard, use GetFieldNames() after SELECT instead!");
253 TString sql =
Form(
"PRAGMA table_info('%s')", table);
265 Error(
"GetTableInfo",
"not connected");
269 if (!tablename || (*tablename==0))
return nullptr;
273 if (columnRes ==
nullptr) {
274 Error(
"GetTableInfo",
"could not query columns");
278 TList* lst =
nullptr;
282 while ((columnRow = columnRes->
Next()) !=
nullptr) {
315 Error(
"CreateDataBase",
"CreateDataBase command makes no sense for SQLite!");
325 Error(
"DropDataBase",
"DropDataBase command makes no sense for SQLite!");
336 Error(
"Reload",
"not connected");
340 Error(
"Reload",
"not implemented");
351 Error(
"Shutdown",
"not connected");
355 Error(
"Shutdown",
"not implemented");
376 SetError(-1,
"no query string specified",
"Statement");
381 Error(
"Statement",
"not connected");
385 sqlite3_stmt *preparedStmt =
nullptr;
386 const char *tail =
nullptr;
389#if SQLITE_VERSION_NUMBER >= 3005000
390 int retVal = sqlite3_prepare_v2(
fSQLite, sql, -1, &preparedStmt, &tail);
392 int retVal = sqlite3_prepare(
fSQLite, sql, -1, &preparedStmt, &tail);
394 if (retVal != SQLITE_OK) {
395 Error(
"Statement",
"SQL Error: %d %s", retVal, sqlite3_errmsg(
fSQLite));
398 if (tail && tail[0] !=
'\0')
399 Warning(
"Statement",
"Don't use multiple statements, '%s' statement was ignored", tail);
403 stmt->
fRes = preparedStmt;
414 Error(
"ServerInfo",
"not connected");
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
void Add(TObject *obj) override
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
virtual TSQLRow * Next()=0
virtual const char * GetField(Int_t field)=0
void SetError(Int_t code, const char *msg, const char *method=nullptr)
set new values for error fields if method is specified, displays error message
virtual Bool_t IsConnected() const
Int_t SelectDataBase(const char *dbname) final
Select a database.
TSQLStatement * Statement(const char *sql, Int_t=100) final
Produce TSQLiteStatement.
Bool_t HasStatement() const final
We assume prepared statements work for all SQLite-versions.
Int_t DropDataBase(const char *dbname) final
Drop (i.e.
TSQLResult * GetTables(const char *dbname, const char *wild=nullptr) final
List all tables in the specified database.
Bool_t Commit() final
submit "COMMIT TRANSACTION" query to database return kTRUE, if successful
TSQLResult * Query(const char *sql) final
Execute SQL command.
Bool_t HasTransactionInFlight() final
returns kTRUE when transaction is running
const char * ServerInfo() final
Return server info, must be deleted by user.
Bool_t Rollback() final
submit "ROLLBACK TRANSACTION" query to database return kTRUE, if successful
TSQLResult * GetColumns(const char *dbname, const char *table, const char *wild=nullptr) final
List all columns in specified table (database argument is ignored).
Int_t Shutdown() final
Shutdown the database server.
Bool_t Exec(const char *sql) final
Execute SQL command which does not produce any result sets.
Int_t Reload() final
Reload permission tables.
TSQLResult * GetDataBases(const char *wild=nullptr) final
List all available databases.
Int_t CreateDataBase(const char *dbname) final
Create a database.
~TSQLiteServer()
Close SQLite DB.
void Close(Option_t *opt="") final
Close connection to SQLite DB.
TSQLiteServer(const char *db, const char *uid=nullptr, const char *pw=nullptr)
Open a connection to an SQLite DB server.
Bool_t StartTransaction() final
submit "BEGIN TRANSACTION" query to database return kTRUE, if successful
TSQLTableInfo * GetTableInfo(const char *tablename) final
Produces SQL table info.
const char * Data() const