File: SQLite.h

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 (155 lines) | stat: -rw-r--r-- 3,762 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
#pragma once
#include "SQLite/sqlite3.h"
#include "Core/Io/Url.h"
#include "Core/Array.h"
#include "Core/Timing.h"
#include "SQL.h"

namespace sql {

	/**
	 * Implementation of a connection to SQLite databases.
	 */
	class SQLite : public DBConnection {
		STORM_CLASS;
	public:
		// Create a connection to an SQLite database in a file. The Url has to refer to a file that
		// resides in the filesystem.
		STORM_CTOR SQLite(Url *str);

		// Create a connection to a temporary, in-memory database.
		STORM_CTOR SQLite();

		// Destroy. Calls 'close'.
		virtual ~SQLite();

		// Prepare a statement.
		using DBConnection::prepare;
		Statement *STORM_FN prepare(Str *query) override;

		// Close the connection.
		void STORM_FN close() override;

		// Returns all names of tables in SQLite connection in an Array of strings.
		Array<Str*> *STORM_FN tables() override;

		// Returns a Schema for SQLite connection.
		MAYBE(Schema *) STORM_FN schema(Str *table) override;

		// Migrate.
		void STORM_FN migrate(Migration *migration) override;

		// Get features.
		virtual features::DBFeatures STORM_FN features() const override;

		// Set the timeout when accessing a locked database.
		void STORM_ASSIGN timeout(Duration timeout);

		// Get the timeout.
		Duration STORM_FN timeout() { return dbTimeout; }

	protected:
		// Visitor.
		QueryStr::Visitor *STORM_FN visitor() const override;

		// Transaction management.
		void STORM_FN beginTransaction() override;
		void STORM_FN endTransaction(Transaction::End end) override;

	private:
		// The SQLite3 object.
		UNKNOWN(PTR_NOGC) sqlite3 *db;

		// Timeout set for the database.
		Duration dbTimeout;

		// Query execution functions.
		void query(const char *query);
		void query(Str *query);
		void query(QueryStr *query);

		// Migrate a single table.
		void migrateTable(Migration::Table *migration);

	public:

		/**
		 * Prepared statements in SQLite.
		 */
		class Stmt : public Statement {
			STORM_CLASS;
		public:
			// Create. Called by SQLite class.
			Stmt(SQLite *owner, Str *statement);

			// Destroy.
			virtual ~Stmt();

			// Bind parameters.
			virtual void STORM_FN bind(Nat pos, Str *str) override;
			virtual void STORM_FN bind(Nat pos, Bool b) override;
			virtual void STORM_FN bind(Nat pos, Int i) override;
			virtual void STORM_FN bind(Nat pos, Long i) override;
			virtual void STORM_FN bind(Nat pos, Float f) override;
			virtual void STORM_FN bind(Nat pos, Double d) override;
			virtual void STORM_FN bindNull(Nat pos) override;

			// Execute.
			Statement::Result STORM_FN execute() override;

			// Finalize the statement.
			void STORM_FN finalize() override;

		protected:
			// Dispose results, re-create the statement.
			void STORM_FN disposeResult() override;

			// Get the next row.
			Maybe<Row> STORM_FN nextRow() override;

			// Get the last row id.
			Int STORM_FN lastRowId() override { return lastId; }

			// Get the number of changed rows.
			Nat STORM_FN changes() override { return lastChanges; }

		private:
			// Owner.
			SQLite *owner;

			// Prepared statement.
			UNKNOWN(PTR_NOGC) sqlite3_stmt *stmt;

			// Last row id.
			Int lastId;

			// Last number of changed rows.
			Nat lastChanges;

			// Is the statement in a clean state?
			Bool isClean;

			// Do we have a row of results ready?
			Bool hasRow;

			// Do we have more results to read using step?
			Bool moreRows;

			// Ensure that the statement is ready for manipulation.
			void reset();
		};


		/**
		 * Visitor to transform query strings into proper queries.
		 */
		class Visitor : public QueryStr::Visitor {
			STORM_CLASS;
		public:
			STORM_CTOR Visitor();
			void STORM_FN lastRowId(StrBuf *to) override;
			void STORM_FN type(StrBuf *to, QueryType type) override;
		};
	};

}