/
qgsspatialiteconnection.h
187 lines (151 loc) · 5.51 KB
/
qgsspatialiteconnection.h
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
/***************************************************************************
qgsspatialiteconnection.h
---------------------
begin : October 2011
copyright : (C) 2011 by Martin Dobias
email : wonder dot sk at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSSPATIALITECONNECTION_H
#define QGSSPATIALITECONNECTION_H
#include <QStringList>
#include <QObject>
extern "C"
{
#include <sqlite3.h>
#include <spatialite/gaiageo.h>
#include <spatialite.h>
}
class QgsSpatiaLiteConnection : public QObject
{
Q_OBJECT
public:
/** Construct a connection. Name can be either stored connection name or a path to the database file */
explicit QgsSpatiaLiteConnection( const QString& name );
QString path() { return mPath; }
static QStringList connectionList();
static void deleteConnection( const QString& name );
static QString connectionPath( const QString& name );
typedef struct TableEntry
{
TableEntry( const QString& _tableName, const QString& _column, const QString& _type )
: tableName( _tableName ), column( _column ), type( _type ) {}
QString tableName;
QString column;
QString type;
} TableEntry;
enum Error
{
NoError,
NotExists,
FailedToOpen,
FailedToCheckMetadata,
FailedToGetTables,
};
enum DbLayoutVersion
{
LayoutUnknown,
LayoutLegacy,
LayoutCurrent,
};
Error fetchTables( bool loadGeometrylessTables );
/** Return list of tables. fetchTables() function has to be called before */
QList<TableEntry> tables() { return mTables; }
/** Return additional error message (if an error occurred before) */
QString errorMessage() { return mErrorMsg; }
/** Updates the Internal Statistics*/
bool updateStatistics();
protected:
// SpatiaLite DB open / close
sqlite3 *openSpatiaLiteDb( const QString& path );
void closeSpatiaLiteDb( sqlite3 * handle );
/** Checks if geometry_columns and spatial_ref_sys exist and have expected layout*/
int checkHasMetadataTables( sqlite3* handle );
/** Inserts information about the spatial tables into mTables
@return true if querying of tables was successful, false on error */
bool getTableInfo( sqlite3 * handle, bool loadGeometrylessTables );
#ifdef SPATIALITE_VERSION_GE_4_0_0
// only if libspatialite version is >= 4.0.0
/**
* Inserts information about the spatial tables into mTables
* please note: this method is fully based on the Abstract Interface
* implemented in libspatialite starting since v.4.0
*
* using the Abstract Interface is highly reccommended, because all
* version-dependent implementation details become completly transparent,
* thus completely freeing the client application to take care of them.
*/
bool getTableInfoAbstractInterface( sqlite3 * handle, bool loadGeometrylessTables );
#endif
/** Cleaning well-formatted SQL strings*/
QString quotedValue( QString value ) const;
/** Checks if geometry_columns_auth table exists*/
bool checkGeometryColumnsAuth( sqlite3 * handle );
/** Checks if views_geometry_columns table exists*/
bool checkViewsGeometryColumns( sqlite3 * handle );
/** Checks if virts_geometry_columns table exists*/
bool checkVirtsGeometryColumns( sqlite3 * handle );
/** Checks if this layer has been declared HIDDEN*/
bool isDeclaredHidden( sqlite3 * handle, const QString& table, const QString& geom );
/** Checks if this layer is a RasterLite-1 datasource*/
bool isRasterlite1Datasource( sqlite3 * handle, const char * table );
QString mErrorMsg;
QString mPath; // full path to the database
QList<TableEntry> mTables;
};
class QgsSqliteHandle
{
//
// a class allowing to reuse the same sqlite handle for more layers
//
public:
QgsSqliteHandle( sqlite3 * handle, const QString& dbPath, bool shared )
: ref( shared ? 1 : -1 )
, sqlite_handle( handle )
, mDbPath( dbPath )
, mIsValid( true )
{
}
sqlite3 *handle()
{
return sqlite_handle;
}
QString dbPath() const
{
return mDbPath;
}
bool isValid() const
{
return mIsValid;
}
void invalidate()
{
mIsValid = false;
}
//
// libsqlite3 wrapper
//
void sqliteClose();
static QgsSqliteHandle *openDb( const QString & dbPath, bool shared = true );
static bool checkMetadata( sqlite3 * handle );
static void closeDb( QgsSqliteHandle * &handle );
/**
* Will close any cached connection
* To be called on application exit
*/
static void closeAll();
//static void closeDb( QMap < QString, QgsSqliteHandle * >&handlesRO, QgsSqliteHandle * &handle );
private:
int ref;
sqlite3 *sqlite_handle;
QString mDbPath;
bool mIsValid;
static QMap < QString, QgsSqliteHandle * > handles;
};
#endif // QGSSPATIALITECONNECTION_H