Added 'length' to area.
[UnsignedByte.git] / src / DAL / Database.cpp
blob0c5f238adacd68f397861a26ab3a5921159f21c5
1 /*
2 ** Database.cpp
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.
24 #include <stdio.h>
25 #ifdef _WIN32
26 #ifndef __MINGW32__
27 #pragma warning(disable:4786)
28 #endif
29 #endif
31 #include <map>
32 #include <stdexcept>
33 #include <string>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sqlite3.h>
39 #include <stdarg.h>
41 #include "Assert.h"
42 #include "Database.h"
45 #ifdef SQLITEW_NAMESPACE
46 namespace SQLITEW_NAMESPACE {
47 #endif
50 Database::Database(const std::string& d)
51 :database(d)
55 Database::~Database()
57 for (opendb_v::iterator it = m_opendbs.begin(); it != m_opendbs.end(); it++)
59 OPENDB *p = *it;
60 sqlite3_close(p -> db);
62 while (m_opendbs.size())
64 opendb_v::iterator it = m_opendbs.begin();
65 OPENDB *p = *it;
66 /* If we're busy, it's YOUR fault you didn't delete the
67 * query object. Therefore, you take the consequences too. */
68 Assert(!p->busy);
69 delete p;
70 m_opendbs.erase(it);
74 Database::OPENDB *Database::grabdb()
76 OPENDB *odb = NULL;
78 for (opendb_v::iterator it = m_opendbs.begin(); it != m_opendbs.end(); it++)
80 odb = *it;
81 if (!odb -> busy)
83 break;
85 else
87 odb = NULL;
90 if (!odb)
92 odb = new OPENDB;
93 if (!odb)
94 throw std::runtime_error("grabdb: OPENDB struct "
95 "couldn't be created");
97 int rc = sqlite3_open(database.c_str(), &odb -> db);
98 if (rc) {
99 std::string msg(sqlite3_errmsg(odb->db));
100 sqlite3_close(odb -> db);
101 delete odb;
102 throw std::runtime_error("Can't open database: " + msg);
104 odb -> busy = true;
105 m_opendbs.push_back(odb);
107 else
109 odb -> busy = true;
111 return odb;
115 void Database::freedb(Database::OPENDB *odb)
117 if (odb)
119 odb -> busy = false;
123 bool Database::Connected()
125 OPENDB *odb = grabdb();
126 if (!odb)
128 return false;
130 freedb(odb);
131 return true;
134 std::string Database::safestr(const std::string& str)
136 std::string str2;
137 for (size_t i = 0; i < str.size(); i++)
139 switch (str[i])
141 case '\'':
142 case '\\':
143 case 34:
144 str2 += '\'';
145 default:
146 str2 += str[i];
149 return str2;
153 std::string Database::xmlsafestr(const std::string& str)
155 std::string str2;
156 for (size_t i = 0; i < str.size(); i++)
158 switch (str[i])
160 case '&':
161 str2 += "&amp;";
162 break;
163 case '<':
164 str2 += "&lt;";
165 break;
166 case '>':
167 str2 += "&gt;";
168 break;
169 case '"':
170 str2 += "&quot;";
171 break;
172 case '\'':
173 str2 += "&apos;";
174 break;
175 default:
176 str2 += str[i];
179 return str2;
183 int64_t Database::a2bigint(const std::string& str)
185 int64_t val = 0;
186 bool sign = false;
187 size_t i = 0;
188 if (str[i] == '-')
190 sign = true;
191 i++;
193 for (; i < str.size(); i++)
195 val = val * 10 + (str[i] - 48);
197 return sign ? -val : val;
201 uint64_t Database::a2ubigint(const std::string& str)
203 uint64_t val = 0;
204 for (size_t i = 0; i < str.size(); i++)
206 val = val * 10 + (str[i] - 48);
208 return val;
212 #ifdef SQLITEW_NAMESPACE
213 } // namespace SQLITEW_NAMESPACE {
214 #endif