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

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

// NOLINTNEXTLINE(bugprone-throw-keyword-missing)
SQLite::ConnectionError::ConnectionError(sqlite3 * db) : SQLite::Error(db) { }

SQLite::Connection::Connection(const std::string & str) : db(nullptr)
{
	if (sqlite3_open(str.c_str(), &db) != SQLITE_OK) {
		auto dbp = std::unique_ptr<sqlite3, decltype(&sqlite3_close)>(db, &sqlite3_close);
		throw ConnectionError(dbp.get());
	}
}

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

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

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

void
SQLite::Connection::rollbackTxInt()
{
	if (sqlite3_exec(db, "ROLLBACK TRANSACTION", nullptr, nullptr, nullptr) != 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::SelectCommandPtr
SQLite::Connection::select(const std::string & sql, const DB::CommandOptionsCPtr &)
{
	return std::make_shared<SelectCommand>(this, sql);
}

DB::ModifyCommandPtr
SQLite::Connection::modify(const std::string & sql, const DB::CommandOptionsCPtr &)
{
	return std::make_shared<ModifyCommand>(this, sql);
}

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