| 1 |
lars |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
* $Id: DBAdapter.php 1262 2009-10-26 20:54:39Z francois $
|
|
|
4 |
*
|
|
|
5 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
6 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
7 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
8 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
9 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
10 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
11 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
12 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
13 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
14 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
15 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
16 |
*
|
|
|
17 |
* This software consists of voluntary contributions made by many individuals
|
|
|
18 |
* and is licensed under the LGPL. For more information please see
|
|
|
19 |
* <http://propel.phpdb.org>.
|
|
|
20 |
*/
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* DBAdapter</code> defines the interface for a Propel database adapter.
|
|
|
24 |
*
|
|
|
25 |
* <p>Support for new databases is added by subclassing
|
|
|
26 |
* <code>DBAdapter</code> and implementing its abstract interface, and by
|
|
|
27 |
* registering the new database adapter and corresponding Creole
|
|
|
28 |
* driver in the private adapters map (array) in this class.</p>
|
|
|
29 |
*
|
|
|
30 |
* <p>The Propel database adapters exist to present a uniform
|
|
|
31 |
* interface to database access across all available databases. Once
|
|
|
32 |
* the necessary adapters have been written and configured,
|
|
|
33 |
* transparent swapping of databases is theoretically supported with
|
|
|
34 |
* <i>zero code change</i> and minimal configuration file
|
|
|
35 |
* modifications.</p>
|
|
|
36 |
*
|
|
|
37 |
* @author Hans Lellelid <hans@xmpl.org> (Propel)
|
|
|
38 |
* @author Jon S. Stevens <jon@latchkey.com> (Torque)
|
|
|
39 |
* @author Brett McLaughlin <bmclaugh@algx.net> (Torque)
|
|
|
40 |
* @author Daniel Rall <dlr@finemaltcoding.com> (Torque)
|
|
|
41 |
* @version $Revision: 1262 $
|
|
|
42 |
* @package propel.adapter
|
|
|
43 |
*/
|
|
|
44 |
abstract class DBAdapter {
|
|
|
45 |
|
|
|
46 |
const ID_METHOD_NONE = 0;
|
|
|
47 |
const ID_METHOD_AUTOINCREMENT = 1;
|
|
|
48 |
const ID_METHOD_SEQUENCE = 2;
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Creole driver to Propel adapter map.
|
|
|
52 |
* @var array
|
|
|
53 |
*/
|
|
|
54 |
private static $adapters = array(
|
|
|
55 |
'mysql' => 'DBMySQL',
|
|
|
56 |
'mysqli' => 'DBMySQLi',
|
|
|
57 |
'mssql' => 'DBMSSQL',
|
|
|
58 |
'dblib' => 'DBMSSQL',
|
|
|
59 |
'sybase' => 'DBSybase',
|
|
|
60 |
'oracle' => 'DBOracle',
|
|
|
61 |
'pgsql' => 'DBPostgres',
|
|
|
62 |
'sqlite' => 'DBSQLite',
|
|
|
63 |
'' => 'DBNone',
|
|
|
64 |
);
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* Creates a new instance of the database adapter associated
|
|
|
68 |
* with the specified Creole driver.
|
|
|
69 |
*
|
|
|
70 |
* @param string $driver The name of the Propel/Creole driver to
|
|
|
71 |
* create a new adapter instance for or a shorter form adapter key.
|
|
|
72 |
* @return DBAdapter An instance of a Propel database adapter.
|
|
|
73 |
* @throws PropelException if the adapter could not be instantiated.
|
|
|
74 |
*/
|
|
|
75 |
public static function factory($driver) {
|
|
|
76 |
$adapterClass = isset(self::$adapters[$driver]) ? self::$adapters[$driver] : null;
|
|
|
77 |
if ($adapterClass !== null) {
|
|
|
78 |
$a = new $adapterClass();
|
|
|
79 |
return $a;
|
|
|
80 |
} else {
|
|
|
81 |
throw new PropelException("Unsupported Propel driver: " . $driver . ": Check your configuration file");
|
|
|
82 |
}
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* This method is called after a connection was created to run necessary
|
|
|
87 |
* post-initialization queries or code.
|
|
|
88 |
*
|
|
|
89 |
* If a charset was specified, this will be set before any other queries
|
|
|
90 |
* are executed.
|
|
|
91 |
*
|
|
|
92 |
* This base method runs queries specified using the "query" setting.
|
|
|
93 |
*
|
|
|
94 |
* @param PDO A PDO connection instance.
|
|
|
95 |
* @param array An array of settings.
|
|
|
96 |
* @see setCharset()
|
|
|
97 |
*/
|
|
|
98 |
public function initConnection(PDO $con, array $settings)
|
|
|
99 |
{
|
|
|
100 |
if (isset($settings['charset']['value'])) {
|
|
|
101 |
$this->setCharset($con, $settings['charset']['value']);
|
|
|
102 |
}
|
|
|
103 |
if (isset($settings['queries']) && is_array($settings['queries'])) {
|
|
|
104 |
foreach ($settings['queries'] as $queries) {
|
|
|
105 |
foreach ((array)$queries as $query) {
|
|
|
106 |
$con->exec($query);
|
|
|
107 |
}
|
|
|
108 |
}
|
|
|
109 |
}
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
/**
|
|
|
113 |
* Sets the character encoding using SQL standard SET NAMES statement.
|
|
|
114 |
*
|
|
|
115 |
* This method is invoked from the default initConnection() method and must
|
|
|
116 |
* be overridden for an RDMBS which does _not_ support this SQL standard.
|
|
|
117 |
*
|
|
|
118 |
* @param PDO A PDO connection instance.
|
|
|
119 |
* @param string The charset encoding.
|
|
|
120 |
* @see initConnection()
|
|
|
121 |
*/
|
|
|
122 |
public function setCharset(PDO $con, $charset)
|
|
|
123 |
{
|
|
|
124 |
$con->exec("SET NAMES '" . $charset . "'");
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
/**
|
|
|
128 |
* This method is used to ignore case.
|
|
|
129 |
*
|
|
|
130 |
* @param string The string to transform to upper case.
|
|
|
131 |
* @return string The upper case string.
|
|
|
132 |
*/
|
|
|
133 |
public abstract function toUpperCase($in);
|
|
|
134 |
|
|
|
135 |
/**
|
|
|
136 |
* Returns the character used to indicate the beginning and end of
|
|
|
137 |
* a piece of text used in a SQL statement (generally a single
|
|
|
138 |
* quote).
|
|
|
139 |
*
|
|
|
140 |
* @return string The text delimeter.
|
|
|
141 |
*/
|
|
|
142 |
public function getStringDelimiter()
|
|
|
143 |
{
|
|
|
144 |
return '\'';
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
/**
|
|
|
148 |
* This method is used to ignore case.
|
|
|
149 |
*
|
|
|
150 |
* @param string $in The string whose case to ignore.
|
|
|
151 |
* @return string The string in a case that can be ignored.
|
|
|
152 |
*/
|
|
|
153 |
public abstract function ignoreCase($in);
|
|
|
154 |
|
|
|
155 |
/**
|
|
|
156 |
* This method is used to ignore case in an ORDER BY clause.
|
|
|
157 |
* Usually it is the same as ignoreCase, but some databases
|
|
|
158 |
* (Interbase for example) does not use the same SQL in ORDER BY
|
|
|
159 |
* and other clauses.
|
|
|
160 |
*
|
|
|
161 |
* @param string $in The string whose case to ignore.
|
|
|
162 |
* @return string The string in a case that can be ignored.
|
|
|
163 |
*/
|
|
|
164 |
public function ignoreCaseInOrderBy($in)
|
|
|
165 |
{
|
|
|
166 |
return $this->ignoreCase($in);
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
/**
|
|
|
170 |
* Returns SQL which concatenates the second string to the first.
|
|
|
171 |
*
|
|
|
172 |
* @param string String to concatenate.
|
|
|
173 |
* @param string String to append.
|
|
|
174 |
* @return string
|
|
|
175 |
*/
|
|
|
176 |
public abstract function concatString($s1, $s2);
|
|
|
177 |
|
|
|
178 |
/**
|
|
|
179 |
* Returns SQL which extracts a substring.
|
|
|
180 |
*
|
|
|
181 |
* @param string String to extract from.
|
|
|
182 |
* @param int Offset to start from.
|
|
|
183 |
* @param int Number of characters to extract.
|
|
|
184 |
* @return string
|
|
|
185 |
*/
|
|
|
186 |
public abstract function subString($s, $pos, $len);
|
|
|
187 |
|
|
|
188 |
/**
|
|
|
189 |
* Returns SQL which calculates the length (in chars) of a string.
|
|
|
190 |
*
|
|
|
191 |
* @param string String to calculate length of.
|
|
|
192 |
* @return string
|
|
|
193 |
*/
|
|
|
194 |
public abstract function strLength($s);
|
|
|
195 |
|
|
|
196 |
|
|
|
197 |
/**
|
|
|
198 |
* Quotes database objec identifiers (table names, col names, sequences, etc.).
|
|
|
199 |
* @param string $text The identifier to quote.
|
|
|
200 |
* @return string The quoted identifier.
|
|
|
201 |
*/
|
|
|
202 |
public function quoteIdentifier($text)
|
|
|
203 |
{
|
|
|
204 |
return '"' . $text . '"';
|
|
|
205 |
}
|
|
|
206 |
|
|
|
207 |
/**
|
|
|
208 |
* Quotes a database table which could have space seperating it from an alias, both should be identified seperately
|
|
|
209 |
* @param string $table The table name to quo
|
|
|
210 |
* @return string The quoted table name
|
|
|
211 |
**/
|
|
|
212 |
public function quoteIdentifierTable($table) {
|
|
|
213 |
return implode(" ", array_map(array($this, "quoteIdentifier"), explode(" ", $table) ) );
|
|
|
214 |
}
|
|
|
215 |
|
|
|
216 |
/**
|
|
|
217 |
* Returns the native ID method for this RDBMS.
|
|
|
218 |
* @return int one of DBAdapter:ID_METHOD_SEQUENCE, DBAdapter::ID_METHOD_AUTOINCREMENT.
|
|
|
219 |
*/
|
|
|
220 |
protected function getIdMethod()
|
|
|
221 |
{
|
|
|
222 |
return DBAdapter::ID_METHOD_AUTOINCREMENT;
|
|
|
223 |
}
|
|
|
224 |
|
|
|
225 |
/**
|
|
|
226 |
* Whether this adapter uses an ID generation system that requires getting ID _before_ performing INSERT.
|
|
|
227 |
* @return boolean
|
|
|
228 |
*/
|
|
|
229 |
public function isGetIdBeforeInsert()
|
|
|
230 |
{
|
|
|
231 |
return ($this->getIdMethod() === DBAdapter::ID_METHOD_SEQUENCE);
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
/**
|
|
|
235 |
* Whether this adapter uses an ID generation system that requires getting ID _before_ performing INSERT.
|
|
|
236 |
* @return boolean
|
|
|
237 |
*/
|
|
|
238 |
public function isGetIdAfterInsert()
|
|
|
239 |
{
|
|
|
240 |
return ($this->getIdMethod() === DBAdapter::ID_METHOD_AUTOINCREMENT);
|
|
|
241 |
}
|
|
|
242 |
|
|
|
243 |
/**
|
|
|
244 |
* Gets the generated ID (either last ID for autoincrement or next sequence ID).
|
|
|
245 |
* @return mixed
|
|
|
246 |
*/
|
|
|
247 |
public function getId(PDO $con, $name = null)
|
|
|
248 |
{
|
|
|
249 |
return $con->lastInsertId($name);
|
|
|
250 |
}
|
|
|
251 |
|
|
|
252 |
/**
|
|
|
253 |
* Returns timestamp formatter string for use in date() function.
|
|
|
254 |
* @return string
|
|
|
255 |
*/
|
|
|
256 |
public function getTimestampFormatter()
|
|
|
257 |
{
|
|
|
258 |
return "Y-m-d H:i:s";
|
|
|
259 |
}
|
|
|
260 |
|
|
|
261 |
/**
|
|
|
262 |
* Returns date formatter string for use in date() function.
|
|
|
263 |
* @return string
|
|
|
264 |
*/
|
|
|
265 |
public function getDateFormatter()
|
|
|
266 |
{
|
|
|
267 |
return "Y-m-d";
|
|
|
268 |
}
|
|
|
269 |
|
|
|
270 |
/**
|
|
|
271 |
* Returns time formatter string for use in date() function.
|
|
|
272 |
* @return string
|
|
|
273 |
*/
|
|
|
274 |
public function getTimeFormatter()
|
|
|
275 |
{
|
|
|
276 |
return "H:i:s";
|
|
|
277 |
}
|
|
|
278 |
|
|
|
279 |
/**
|
|
|
280 |
* Should Column-Names get identifiers for inserts or updates.
|
|
|
281 |
* By default false is returned -> backwards compability.
|
|
|
282 |
*
|
|
|
283 |
* it`s a workaround...!!!
|
|
|
284 |
*
|
|
|
285 |
* @todo should be abstract
|
|
|
286 |
* @return boolean
|
|
|
287 |
* @deprecated
|
|
|
288 |
*/
|
|
|
289 |
public function useQuoteIdentifier()
|
|
|
290 |
{
|
|
|
291 |
return false;
|
|
|
292 |
}
|
|
|
293 |
|
|
|
294 |
/**
|
|
|
295 |
* Modifies the passed-in SQL to add LIMIT and/or OFFSET.
|
|
|
296 |
*/
|
|
|
297 |
public abstract function applyLimit(&$sql, $offset, $limit);
|
|
|
298 |
|
|
|
299 |
/**
|
|
|
300 |
* Gets the SQL string that this adapter uses for getting a random number.
|
|
|
301 |
*
|
|
|
302 |
* @param mixed $seed (optional) seed value for databases that support this
|
|
|
303 |
*/
|
|
|
304 |
public abstract function random($seed = null);
|
|
|
305 |
|
|
|
306 |
}
|