summaryrefslogtreecommitdiff
path: root/libsqlitepp/sqlite-connection.cpp
blob: 369931d8481abb974ae6f0c9d5f9fc370c101f4d (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
#include "sqlite-connection.h"
#include "sqlite-error.h"
#include "sqlite-selectcommand.h"
#include "sqlite-modifycommand.h"

NAMEDFACTORY("sqlite", SQLite::Connection, DB::ConnectionFactory);

SQLite::ConnectionError::ConnectionError(sqlite3 * db) :
	SQLite::Error(db)
{
}

SQLite::Connection::Connection(const std::string & str)
{
	if (sqlite3_open(str.c_str(), &db) != SQLITE_OK) {
		ConnectionError err(db);
		sqlite3_close(db);
		throw err;
	}
}

SQLite::Connection::~Connection()
{
	sqlite3_close(db);
}

void
SQLite::Connection::beginTxInt()
{
	if (sqlite3_exec(db, "BEGIN TRANSACTION", NULL, NULL, NULL) != SQLITE_OK) {
		throw Error(db);
	}
}

void
SQLite::Connection::commitTxInt()
{
	if (sqlite3_exec(db, "COMMIT TRANSACTION", NULL, NULL, NULL) != SQLITE_OK) {
		throw Error(db);
	}
}

void
SQLite::Connection::rollbackTxInt()
{
	if (sqlite3_exec(db, "ROLLBACK TRANSACTION", NULL, NULL, NULL) != SQLITE_OK) {
		throw Error(db);
	}
}

DB::BulkDeleteStyle
SQLite::Connection::bulkDeleteStyle() const
{
	return DB::BulkDeleteUsingUsingAlias;
}

DB::BulkUpdateStyle
SQLite::Connection::bulkUpdateStyle() const
{
	return DB::BulkUpdateUsingJoin;
}

void
SQLite::Connection::ping() const
{
	// Can this fail?
}


DB::SelectCommand *
SQLite::Connection::newSelectCommand(const std::string & sql, const DB::CommandOptions *)
{
	return new SelectCommand(this, sql);
}

DB::ModifyCommand *
SQLite::Connection::newModifyCommand(const std::string & sql, const DB::CommandOptions *)
{
	return new ModifyCommand(this, sql);
}

int64_t
SQLite::Connection::insertId()
{
	return sqlite3_last_insert_rowid(db);
}