This repository has been archived by the owner on Dec 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresqliteun-manager.cc
144 lines (126 loc) · 4.58 KB
/
resqliteun-manager.cc
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
/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/**
* @file resqliteun-manager.cc
* @brief Definitions for ReSqliteUnManager class.
* @author Nicu Tofan <nicu.tofan@gmail.com>
* @copyright Copyright 2014 piles contributors. All rights reserved.
* This file is released under the
* [MIT License](http://opensource.org/licenses/mit-license.html)
*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/* INCLUDES ------------------------------------------------------------ */
#include <sqlite/sqlite3.h>
#include "resqliteun.h"
#include "resqliteun-private.h"
#include <assert.h>
#include <QStringBuilder>
/* INCLUDES ============================================================ */
//
//
//
//
/* DEFINITIONS --------------------------------------------------------- */
// the list of instances
QList<ReSqliteUn *> ReSqliteUnManager::instances_;
/* DEFINITIONS ========================================================= */
//
//
//
//
/* CLASS --------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
/**
*
* @param interface_version Internal use (checks that the version is teh right one).
* @return NULL if there is no database.
*/
ReSqliteUn *ReSqliteUnManager::instance (int interface_version)
{
if (interface_version != RESQLITEUN_VERSION) {
fprintf(stderr, "Version missmatch; library version is %#010x, "
"interface version is %#010x",
RESQLITEUN_VERSION, interface_version);
return NULL;
}
int last = instances_.count () - 1;
if (last < 0) {
return NULL;
} else {
return instances_.at (last);
}
}
/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/**
* The index of a database changes when a database created befoore this one
* is closed (the destructor removes instances from the list).
*
* @param i The index in internal list where valid range is 0 .. count()-1
* @param interface_version Internal use (checks that the version is teh right one).
* @return NULL if index is invalid.
*/
ReSqliteUn *ReSqliteUnManager::instanceForIndex (
int i, int interface_version)
{
if (interface_version != RESQLITEUN_VERSION) {
fprintf(stderr, "Version missmatch; library version is %#010x, "
"interface version is %#010x",
RESQLITEUN_VERSION, interface_version);
return NULL;
}
if ((i < 0) || (i >= instances_.count ())) {
return NULL;
} else {
return instances_.at (i);
}
}
/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/**
* The method involves checking all instances so it may be worth caching
* the result if this is called often.
*
* @param sqlite_database The database to look for.
* @param interface_version Internal use (checks that the version is teh right one).
* @return NULL if no such database is inside.
*/
ReSqliteUn *ReSqliteUnManager::instanceForDatabase (
void *sqlite_database, int interface_version)
{
if (interface_version != RESQLITEUN_VERSION) {
fprintf(stderr, "Version missmatch; library version is %#010x, "
"interface version is %#010x",
RESQLITEUN_VERSION, interface_version);
return NULL;
}
sqlite3 * dtb = static_cast<sqlite3 *>(sqlite_database);
foreach(ReSqliteUn * iter, instances_) {
if (dtb == static_cast<sqlite3 *>(iter->db_)) {
return iter;
}
}
return NULL;
}
/* ========================================================================= */
/* ------------------------------------------------------------------------- */
bool ReSqliteUnManager::autoregister ()
{
RESQLITEUN_TRACE_ENTRY;
int rc = sqlite3_auto_extension (getEntryPoint ());
RESQLITEUN_TRACE_EXIT;
return (rc == SQLITE_OK);
}
/* ========================================================================= */
/* CLASS =============================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */