-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.h
176 lines (157 loc) · 5.49 KB
/
database.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
/**
* @(#)database.h 1.0.0
*
* Copyright 2004 Vijay Mathew Pandyalakal. All rights reserved.
*
* Change history:
* Created on July 26, 2004
* Last Modified on January 01, 2007
*
*/
#ifndef DSQLXPREZ_2_H
#define DSQLXPREZ_2_H
#include <string>
#include <vector>
#include <exception>
#include <sqlite3.h>
class Statement;
class DatabaseMetaData;
class ResultSet;
class ResultSetMetaData;
/**
* class SQLException
* Thrown by DsqlXprez classes accesing the database
* @since 1.0.0
*/
class SQLException : std::exception
{
public:
SQLException(int code); /// Creates an exception with the given code
SQLException(std::string msg); /// Creates an exception with the given message
~SQLException() throw();
int getCode(); /// Returns the error code
std::string getMessage(); /// Returns the error message
const char* what() const throw();
private:
void setMessageFromCode(); /// Creates a message from the current error code
private:
int m_nCode; /// Error code
std::string m_strMsg; /// Error message
};
/**
* class Connection
* Interface with the low-level database
* @since 1.0.0
*/
class Connection
{
public:
Connection(); /// Constructor
~Connection(); /// Destructor
void open(const char* db_name) throw (SQLException); /// opens a database
Statement* createStatement(); /// Creates and returns a statement object
DatabaseMetaData* getDatabaseMetaData(); /// Returns meta data of the entire database
void refreshMetaData() throw (SQLException); /// Refreshes the meta data
void close(); /// closes the database and releases all resources
private:
sqlite3 *m_sqlite; /// Handle to the database engine
Statement* m_stmt; /// Handle to the object to execute sql commands
DatabaseMetaData* m_dbMtdt; /// Database metadata
};
/**
* class Statement
* Class to execute SQL commands
* @since 1.0.0
*/
class Statement
{
public:
Statement(sqlite3 *pSqlite); /// Constructor
~Statement(); /// Destructor
int execute(const char* sql) throw (SQLException); /// Executes an sql and returns the number of rows affected
ResultSet* executeQuery(const char* sql) throw (SQLException); /// Executes an sql and returns a pointer to the resultset
private:
sqlite3 *m_sqlite;
ResultSet* m_rslt;
};
/**
* class ResultSet
* Wraps up the result of a SELECT
* @since 1.0.0
*/
class ResultSet
{
public:
ResultSet(sqlite3_stmt *ps)throw (SQLException); /// Constructor
~ResultSet(); /// Destructor
bool next() throw (SQLException); /// Moves to the next row of the resultset
bool isNull(int colNum) throw (SQLException); /// Returns true if the given column has a null value
std::string getString(int colNum) throw (SQLException); /// Returns data of column as a string
int getInt(int colNum) throw (SQLException); /// Returns data of column as an int
long getLong(int colNum) throw (SQLException); /// Returns data of column as a Long
unsigned int getUInt(int colNum) throw (SQLException); /// Returns data of column as an unsiged int
unsigned long getULong(int colNum) throw (SQLException); /// Returns data of column as a unsigned long
float getFloat(int colNum) throw (SQLException); /// Returns data of column as a float
double getDouble(int colNum) throw (SQLException); /// Returns data of column as a double
bool getBoolean(int colNum) throw (SQLException); /// Returns data of column as a bool
ResultSetMetaData* getMetaData() throw (SQLException); /// Returns meta data of the resultset
private:
sqlite3_stmt *m_stmt;
ResultSetMetaData *m_rsltMtdt;
int m_nRes;
bool m_bSecondStep;
};
/**
* struct Column
* Represents a single column in a resultset
* @since 1.0.0
*/
struct Column
{
std::string name_; /// Column name
std::string type_; /// Data type
};
/**
* class ResultSetMetaData
* reprrsents meta data of a resultset
* @since 1.0.0
*/
class ResultSetMetaData
{
public:
ResultSetMetaData(std::vector<Column> vct); /// Constructor
int getColumnCount(); /// Returns number of columns in the resultset
std::string getColumnName(int colNum) throw (SQLException); /// Returns the name of the column
std::string getColumnType(int colNum) throw (SQLException); /// Returns the datatype of the column
private:
std::vector<Column> m_vctColumns;
};
/**
* struct DatabaseObject
* Represents an object (like a table,index..) in the database
* @since 1.0.0
*/
struct DatabaseObject
{
std::string name_; /// Object name
std::string type_; /// Object type
std::string sql_; /// Object sql
};
/**
* class DatabaseMetaData
* Wraps up the meta data of the entire database
* @since 1.0.0
*/
class DatabaseMetaData
{
public:
DatabaseMetaData(std::vector<DatabaseObject> vct); /// Constructor
int getNumObjects(); /// Returns the number of objects in the database
std::string getName(int colNum) throw (SQLException); /// Returns the name of the object at the given position
std::string getType(int colNum) throw (SQLException); /// Returns the type of the object at the given position
std::string getSql(int colNum) throw (SQLException); /// Returns the sql that created the object at the given position
bool doesObjectExist(const char* name_,const char* type); /// Checks if the given object exists in the database
private:
std::vector<DatabaseObject> m_vctObjects;
};
#endif