File: SQL.cpp

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (190 lines) | stat: -rw-r--r-- 4,689 bytes parent folder | download | duplicates (2)
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
#include "stdafx.h"
#include "SQL.h"
#include "Exception.h"

namespace sql {

	/**
	 * Bind helpers.
	 */

	void bind(Statement *to, Nat pos, MAYBE(Str *) str) {
		if (str)
			to->bind(pos, str);
		else
			to->bindNull(pos);
	}

	void bind(Statement *to, Nat pos, Maybe<Bool> v) {
		if (v.any())
			to->bind(pos, v.value());
		else
			to->bindNull(pos);
	}

	void bind(Statement *to, Nat pos, Maybe<Int> v) {
		if (v.any())
			to->bind(pos, v.value());
		else
			to->bindNull(pos);
	}

	void bind(Statement *to, Nat pos, Maybe<Long> v) {
		if (v.any())
			to->bind(pos, v.value());
		else
			to->bindNull(pos);
	}

	void bind(Statement *to, Nat pos, Maybe<Float> v) {
		if (v.any())
			to->bind(pos, v.value());
		else
			to->bindNull(pos);
	}

	void bind(Statement *to, Nat pos, Maybe<Double> v) {
		if (v.any())
			to->bind(pos, v.value());
		else
			to->bindNull(pos);
	}


	/**
	 * Statement.
	 */

	Statement::Statement(DBConnection *connection) : resultAliveCount(0), resultSequence(0) {
		connection->statements->putRaw(reinterpret_cast<TObject *>(this));
	}

	void Statement::invalidateResult() {
		atomicIncrement(resultSequence);
		atomicWrite(resultAliveCount, 0);
	}

	void Statement::deregister(DBConnection *c) {
		c->statements->removeRaw(reinterpret_cast<TObject *>(this));
	}

	Statement::Statement(const Statement &o) {
		throw new (this) SQLError(new (this) Str(S("Can not copy a prepared statement.")));
	}

	void Statement::deepCopy(CloneEnv *env) {
		throw new (this) SQLError(new (this) Str(S("Can not move a prepared statement across thread boundaries.")));
	}


	/**
	 * Statement result.
	 *
	 * Note: We use atomics to update the shared data in Statement. This is mostly to be able to be
	 * somewhat robust against the case where the API is misused from multiple threads. The
	 * implementation is not thread-safe (there are cases where we will fail), but as this is mostly
	 * a safeguard to be able to detect such cases, it is fine.
	 */

	Statement::Result::Result(Statement *stmt) : owner(stmt), sequence(atomicRead(stmt->resultSequence)) {
		atomicIncrement(owner->resultAliveCount);
	}

	Statement::Result::~Result() {
		if (atomicRead(owner->resultSequence) == sequence) {
			if (atomicDecrement(owner->resultAliveCount) == 0) {
				owner->disposeResult();
			}
		}
	}

	Statement::Result::Result(const Result &other) : owner(other.owner), sequence(other.sequence) {
		if (atomicRead(owner->resultSequence) == sequence) {
			atomicIncrement(owner->resultAliveCount);
		}
	}

	Statement::Result &Statement::Result::operator =(const Result &other) {
		if (this == &other)
			return *this;

		Result tmp(other);
		std::swap(owner, tmp.owner);
		std::swap(sequence, tmp.sequence);
		return *this;
	}

	Maybe<Row> Statement::Result::next() {
		if (atomicRead(owner->resultSequence) != sequence)
			return Maybe<Row>();

		return owner->nextRow();
	}

	Int Statement::Result::lastRowId() const {
		if (atomicRead(owner->resultSequence) != sequence)
			return -1;

		return owner->lastRowId();
	}

	Nat Statement::Result::changes() const {
		if (atomicRead(owner->resultSequence) != sequence)
			return 0;

		return owner->changes();
	}

	void Statement::Result::finalize() {
		if (atomicRead(owner->resultSequence) == sequence)
			owner->disposeResult();
	}

	void Statement::Result::deepCopy(CloneEnv *env) {
		throw new (env) SQLError(new (env) Str(S("Deep copies of database iterators are not supported.")));
	}


	/**
	 * Connection.
	 */

	DBConnection::DBConnection() {
		statements = new (this) WeakSetBase();
		cachedStatements = new (this) RefMap<CachedQuery *, Statement *>();
	}

	DBConnection::DBConnection(const DBConnection &o) {
		throw new (this) SQLError(new (this) Str(S("Can not copy a database connection.")));
	}

	void DBConnection::deepCopy(CloneEnv *env) {
		throw new (this) SQLError(new (this) Str(S("Can not move a database connection across thread boundaries.")));
	}

	Statement *DBConnection::prepare(QueryStr *query) {
		return prepare(query->generate(visitor()));
	}

	Statement *DBConnection::prepare(CachedQuery *cached) {
		Statement *out = cachedStatements->get(cached, null);
		if (!out) {
			out = cached->query(this);
			cachedStatements->put(cached, out);
		}
		return out;
	}

	void DBConnection::close() {
		// Note: We could close all 'cachedStatements' also, but those should all be in 'statements'
		// anyway, so we can just clear it here.
		cachedStatements->clear();

		WeakSetBase::Iter iter(statements);
		// Note: We know that everything that comes to us from the set will be alive and not finalized.
		while (RootObject *stmt = iter.nextRaw()) {
			reinterpret_cast<Statement *>(stmt)->finalize();
		}
	}

}