Sorted Socket.
[UnsignedByte.git] / src / DAL / Database.h
blobb90978d1737e005d54b163c5861c96c055a82f15
1 /*
2 ** Database.h
3 **
4 ** Published / author: 2005-08-12 / grymse@alhem.net
5 **/
7 /*
8 Copyright (C) 2001-2006 Anders Hedstrom
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License
12 as published by the Free Software Foundation; either version 2
13 of the License, or (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #ifndef _DATABASE_H_SQLITE
26 #define _DATABASE_H_SQLITE
28 /**
29 * @file Database.h
30 * This file contains the Database class.
32 * @see Database
33 */
35 #include <string>
36 #include <list>
38 #include <stdint.h>
39 #include <sqlite3.h>
41 #ifdef SQLITEW_NAMESPACE
42 namespace SQLITEW_NAMESPACE {
43 #endif
46 /** Connection information and pool. */
47 class Database
49 public:
50 /** Connection pool struct. */
51 struct OPENDB {
52 /** Constructor. */
53 OPENDB() : busy(false) {}
54 sqlite3 *db; ///< The database connection
55 bool busy; ///< Is the database connection used
57 typedef std::list<OPENDB *> opendb_v; ///< The pool of open database connections
59 public:
60 /** Construc a Database that uses the specified filename. */
61 Database(const std::string& database);
63 /** Destructor. */
64 virtual ~Database();
66 /** try to establish connection with given host */
67 bool Connected();
69 /** Request a database connection.
70 The "grabdb" method is used by the Query class, so that each object instance of Query gets a unique
71 database connection. I will reimplement your connection check logic in the Query class, as that's where
72 the database connection is really used.
73 It should be used something like this.
75 Query q(db);
76 if (!q.Connected())
77 return false;
78 q.execute("delete * from user"); // well maybe not
81 When the Query object is deleted, then "freedb" is called - the database connection stays open in the
82 m_opendbs vector. New Query objects can then reuse old connections.
84 OPENDB *grabdb();
86 /** Free the database connection. */
87 void freedb(OPENDB *odb);
89 /** Escape string - change all ' to ''. */
90 std::string safestr(const std::string& );
91 /** Make string xml safe. */
92 std::string xmlsafestr(const std::string& );
94 /** Convert string to 64-bit integer. */
95 int64_t a2bigint(const std::string& );
96 /** Convert string to unsigned 64-bit integer. */
97 uint64_t a2ubigint(const std::string& );
99 private:
100 /** Hide the copy constructor. */
101 Database(const Database& );
102 /** Hide the assignment operator. */
103 Database& operator=(const Database& );
105 /** Log an error. */
106 void error(const char *format, ...);
108 std::string database; ///< The name of the database
109 opendb_v m_opendbs; ///< The open database connection
113 #ifdef SQLITEW_NAMESPACE
114 } // namespace SQLITEW_NAMESPACE {
115 #endif
117 #endif // _DATABASE_H