forked from sqlitebrowser/sqlitebrowser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlitedb.h
129 lines (105 loc) · 4.31 KB
/
sqlitedb.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
#ifndef SQLITEDB_H
#define SQLITEDB_H
#include "sqlitetypes.h"
#include <QStringList>
#include <QMultiMap>
#include <QByteArray>
class sqlite3;
enum
{
kLogMsg_User,
kLogMsg_App
};
typedef QMultiMap<QString, class DBBrowserObject> objectMap;
class DBBrowserObject
{
public:
DBBrowserObject() : name( "" ) { }
DBBrowserObject( const QString& wname,const QString& wsql, const QString& wtype, const QString& tbl_name )
: name( wname), sql( wsql ), type(wtype), table_name(tbl_name)
{ }
void addField(sqlb::FieldPtr field) { fldmap.push_back(field); }
QString getname() const { return name; }
QString getsql() const { return sql; }
QString gettype() const { return type; }
QString getTableName() const { return table_name; }
sqlb::FieldPtr getField(const QString& name) const
{
for(int i=0;i<fldmap.size();i++)
if(fldmap.at(i)->name() == name)
return fldmap.at(i);
return sqlb::FieldPtr();
}
sqlb::FieldVector fldmap;
private:
QString name;
QString sql;
QString type;
QString table_name; // The name of the table this object references, interesting for views, triggers and indices
};
class DBBrowserDB : public QObject
{
Q_OBJECT
public:
explicit DBBrowserDB () : _db( 0 ) {}
virtual ~DBBrowserDB (){}
bool open ( const QString & db);
bool create ( const QString & db);
bool close();
bool setRestorePoint(const QString& pointname = "RESTOREPOINT");
bool save (const QString& pointname = "RESTOREPOINT");
bool revert (const QString& pointname = "RESTOREPOINT");
bool saveAll();
bool revertAll();
bool dump( const QString & filename);
bool executeSQL ( const QString & statement, bool dirtyDB=true, bool logsql=true);
bool executeMultiSQL(const QString& statement, bool dirty = true, bool log = false);
/**
* @brief getRow Executes a sqlite statement to get the rowdata(columns)
* for the given rowid.
* @param sTableName Table to query.
* @param rowid The rowid to fetch.
* @param rowdata A list of QByteArray containing the row data.
* @return true if statement execution was ok, else false.
*/
bool getRow(const QString& sTableName, int rowid, QList<QByteArray>& rowdata);
void updateSchema();
int addRecord(const QString& sTableName);
bool deleteRecord(const QString& table, int rowid);
bool updateRecord(const QString& table, const QString& column, int row, const QByteArray& value);
bool createTable(const QString& name, const sqlb::FieldVector& structure);
bool renameTable(const QString& from_table, const QString& to_table);
bool addColumn(const QString& table, const sqlb::FieldPtr& field);
/**
* @brief renameColumn Can be used to rename, modify or drop an existing column of a given table
* @param tablename Specifies the table name
* @param name Name of the column to edit
* @param to The new field definition with changed name, type or the like. If Null-Pointer is given the column is dropped.
* @param move Set this to a value != 0 to move the new column to a different position
* @return true if renaming was successfull, false if not. In the latter case also lastErrorMessage is set
*/
bool renameColumn(const QString& tablename, const QString& name, sqlb::FieldPtr to, int move = 0);
QStringList getTableFields(const QString & tablename) const;
QStringList getBrowsableObjectNames() const;
objectMap getBrowsableObjects() const;
DBBrowserObject getObjectByName(const QString& name) const;
bool isOpen() const;
bool getDirty() const;
void logSQL(QString statement, int msgtype);
QString getPragma(const QString& pragma);
bool setPragma(const QString& pragma, const QString& value);
bool setPragma(const QString& pragma, const QString& value, QString& originalvalue);
bool setPragma(const QString& pragma, int value, int& originalvalue);
bool loadExtension(const QString& filename);
sqlite3 * _db;
QStringList decodeCSV(const QString & csvfilename, char sep, char quote, int maxrecords, int * numfields);
objectMap objMap;
QString lastErrorMessage;
QString curDBFilename;
signals:
void sqlExecuted(QString sql, int msgtype);
void dbChanged(bool dirty);
private:
QStringList savepointList;
};
#endif