Added 'length' to area.
[UnsignedByte.git] / src / DAL / Query.h
blobbc9dfd042e5d2a85f2a90627670fd5dd30b4bf81
1 #ifdef _WIN32
2 #ifndef __MINGW32__
3 #pragma warning(disable:4786)
4 #endif
5 #endif
6 /*
7 ** Query.h
8 **
9 ** Published / author: 2005-08-12 / grymse@alhem.net
10 **/
13 Copyright (C) 2001-2006 Anders Hedstrom
15 This program is free software; you can redistribute it and/or
16 modify it under the terms of the GNU General Public License
17 as published by the Free Software Foundation; either version 2
18 of the License, or (at your option) any later version.
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 #ifndef _QUERY_H_SQLITE
31 #define _QUERY_H_SQLITE
33 /**
34 * @file Query.h
35 * @brief This file contains the Query class.
37 * @see Query
38 */
40 #include <string>
41 #include <map>
42 #ifdef WIN32
43 typedef unsigned __int64 uint64_t;
44 typedef __int64 int64_t;
45 #else
46 #include <stdint.h>
47 #endif
50 #ifdef SQLITEW_NAMESPACE
51 namespace SQLITEW_NAMESPACE {
52 #endif
55 /** SQL Statement execute / result. */
56 class Query
58 public:
59 /** Constructor accepting reference to database object. */
60 Query(Database& dbin);
61 /** Constructor accepting reference to database object
62 and query string to execute. */
63 Query(Database& dbin,const std::string& sql);
64 /** Destructor. */
65 ~Query();
67 /** Check if database object is connectable. */
68 bool Connected();
69 /** Return reference to database object. */
70 Database& GetDatabase() const;
71 /** Return string containing last query executed. */
72 const std::string& GetLastQuery();
74 /** execute() returns true if query is successful,
75 does not store result. */
76 bool execute(const std::string& sql);
77 /** Execute query and store result. */
78 sqlite3_stmt *get_result(const std::string& sql);
79 /** Free stored result, must be called after get_result() before calling
80 execute()/get_result() again. */
81 void free_result();
82 /** Fetch next result row.
83 \return false if there was no row to fetch (end of rows) */
84 bool fetch_row();
85 /** Get id of last insert. */
86 sqlite_int64 insert_id();
87 /** Returns 0 if there are no rows to fetch. */
88 long num_rows();
89 /** Last error string. */
90 std::string GetError();
91 /** Last error code. */
92 int GetErrno();
94 /** Execute query and return first result as a string. */
95 const char *get_string(const std::string& sql);
96 /** Execute query and return first result as a long integer. */
97 long get_count(const std::string& sql);
98 /** Execute query and return first result as a double. */
99 double get_num(const std::string& sql);
101 /** Check if column x in current row is null. */
102 bool is_null(int x);
104 /** Return column named x as a string value. */
105 const char *getstr(const std::string& x);
106 /** Return column x as a string value. */
107 const char *getstr(int x);
108 /** Return next column as a string value - see rowcount. */
109 const char *getstr();
111 /** Return column named x as a long integer. */
112 long getval(const std::string& x);
113 /** Return column x as a long integer. */
114 long getval(int x);
115 /** Return next column as a long integer - see rowcount. */
116 long getval();
118 /** Return column named x as an unsigned long integer. */
119 unsigned long getuval(const std::string& x);
120 /** Return column x as an unsigned long integer. */
121 unsigned long getuval(int x);
122 /** Return next column as an unsigned long integer. */
123 unsigned long getuval();
125 /** Return column named x as a 64-bit integer value. */
126 int64_t getbigint(const std::string& x);
127 /** Return column x as a 64-bit integer value. */
128 int64_t getbigint(int x);
129 /** Return next column as a 64-bit integer value. */
130 int64_t getbigint();
132 /** Return column named x as an unsigned 64-bit integer value. */
133 uint64_t getubigint(const std::string& x);
134 /** Return column x as an unsigned 64-bit integer value. */
135 uint64_t getubigint(int x);
136 /** Return next column as an unsigned 64-bit integer value. */
137 uint64_t getubigint();
139 /** Return column named x as a double. */
140 double getnum(const std::string& x);
141 /** Return column x as a double. */
142 double getnum(int x);
143 /** Return next column as a double. */
144 double getnum();
146 private:
147 /** Hide the copy constructor. */
148 Query(const Query& q) : m_db(q.GetDatabase()) {}
149 /** Hide the assignment operator. */
150 Query& operator=(const Query& ) { return *this; }
151 /** Print current result to stdout. */
152 void ViewRes();
153 /** Print error to debug class. */
154 void error(const std::string& );
155 Database& m_db; ///< Reference to database object
156 Database::OPENDB *odb; ///< Connection pool handle
157 sqlite3_stmt *res; ///< Stored result
158 bool row; ///< true if fetch_row succeeded
159 short rowcount; ///< Current column pointer in result
160 std::string m_tmpstr; ///< Used to store result in get_string() call
161 std::string m_last_query; ///< Last query executed
162 int cache_rc; ///< Cached result after call to get_result()
163 bool cache_rc_valid; ///< Indicates cache_rc is valid
164 int m_row_count; ///< 0 if get_result() returned no rows
166 std::map<std::string,int> m_nmap; ///< map translating column names to index
167 int m_num_cols; ///< number of columns in result
171 #ifdef SQLITEW_NAMESPACE
172 } // namespace SQLITEW_NAMESPACE {
173 #endif
175 #endif // _QUERY_H