forked from ssscrape/ssscrape
/
backend.lib.php
209 lines (185 loc) · 4.41 KB
/
backend.lib.php
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/*
* Anewt, Almost No Effort Web Toolkit, database module
*
* Copyright (C) 2004-2006 Wouter Bolsterlee <uws@xs4all.nl>
* Copyright (C) 2004-2005 Jasper Looije <jasper@jamu.nl>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA+
*/
/**
* Base class for database backends. Most methods should be overridden in
* subclasses that implement a specific backend.
*/
abstract class DatabaseBackend
{
var $db; /**< \private Database object instance */
/**
* Returns the type of the database (must be overridden).
*/
abstract function get_type();
/* Connection handling */
/**
* Sets up the settings when using a deferred connection. Can be overridden.
*/
function setup($settings) { }
/**
* Connects to the database.
*
* \param $settings
* The database settings as an associative array.
*/
abstract function connect(array $settings);
/**
* Disconnects from the database.
*/
abstract function disconnect();
/**
* Selects the database to use.
*
* \param $name
* The name of teh database to use.
*/
abstract function select_db($name);
/* Escaping */
/**
* Escapes a boolean for use in SQL queries. Override this method if the
* specific type of database has better escaping functions available.
*
* \param $bool
* The boolean to escape.
*
* \return
* The escaped value.
*/
function escape_boolean($bool)
{
assert('is_bool($bool)');
return $bool ? '1' : '0';
}
/**
* Escapes a string for use in SQL queries. Override this method if the
* specific type of database has better escaping functions available.
*
* \param $str
* The string to escape.
*
* \return
* The escaped string.
*/
function escape_string($str)
{
assert('is_string($str)');
return sprintf("'%s'", addslashes($str));
}
/**
* Escapes a table name for use in SQL queries. Override this method if the
* specific type of database has better escaping functions available.
*
* \param $str
* The string to escape.
*
* \return
* The escaped string.
*/
function escape_table_name($str)
{
assert('is_string($str)');
return $str;
}
/**
* Escapes a column name for use in SQL queries. Override this method if the
* specific type of database has better escaping functions available.
*
* \param $str
* The string to escape.
*
* \return
* The escaped string.
*/
function escape_column_name($str)
{
assert('is_string($str)');
return $str;
}
/**
* Escapes a date. This method only adds quotes.
*
* \param $date
* The value to escape.
*
* \return
* The escaped value.
*/
function escape_date($date)
{
assert('is_string($date)');
return sprintf("'%s'", $date);
}
/**
* Escapes a time. This method only adds quotes.
*
* \param $time
* The value to escape.
*
* \return
* The escaped value.
*/
function escape_time($time)
{
assert('is_string($time)');
return sprintf("'%s'", $time);
}
/**
* Escapes a datetime. This method only adds quotes.
*
* \param $datetime
* The value to escape.
*
* \return
* The escaped value.
*/
function escape_datetime($datetime)
{
assert('is_string($datetime)');
return sprintf("'%s'", $datetime);
}
/* Transaction-related methods */
/**
* Starts a transaction. Override this method for backends that need
* customization.
*/
function transaction_begin()
{
$this->db->prepare_execute('BEGIN');
}
/**
* Commits a transaction. Override this method for backends that need
* customization.
*/
function transaction_commit()
{
$this->db->prepare_execute('COMMIT');
}
/**
* Rolls back a transaction. Override this method for backends that need
* customization.
*/
function transaction_rollback()
{
$this->db->prepare_execute('ROLLBACK');
}
}
?>