root/branches/1.3/runtime/classes/propel/adapter/DBAdapter.php

Revision 1011, 8.8 kB (checked in by hans, 9 months ago)

Minor style / phpdoc cleanup in adapter classes

  • Property svn:keywords set to Id Rev Date Author HeadURL Revision
Line 
1 <?php
2 /*
3  *  $Id$
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$
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         'sybase' => 'DBSybase',
59         'oracle' => 'DBOracle',
60         'pgsql' => 'DBPostgres',
61         'sqlite' => 'DBSQLite',
62         '' => 'DBNone',
63     );
64
65     /**
66      * Creates a new instance of the database adapter associated
67      * with the specified Creole driver.
68      *
69      * @param      string $driver The name of the Propel/Creole driver to
70      * create a new adapter instance for or a shorter form adapter key.
71      * @return     DBAdapter An instance of a Propel database adapter.
72      * @throws     PropelException if the adapter could not be instantiated.
73      */
74     public static function factory($driver) {
75         $adapterClass = isset(self::$adapters[$driver]) ? self::$adapters[$driver] : null;
76         if ($adapterClass !== null) {
77             $a = new $adapterClass();
78             return $a;
79         } else {
80             throw new PropelException("Unsupported Propel driver: " . $driver . ": Check your configuration file");
81         }
82     }
83
84     /**
85      * This method is called after a connection was created to run necessary
86      * post-initialization queries or code.
87      *
88      * If a charset was specified, this will be set before any other queries
89      * are executed.
90      *
91      * This base method runs queries specified using the "query" setting.
92      *
93      * @param      PDO   A PDO connection instance.
94      * @param      array An array of settings.
95      * @see        setCharset()
96      */
97     public function initConnection(PDO $con, array $settings)
98     {
99         if (isset($settings['charset']['value'])) {
100             $this->setCharset($con, $settings['charset']['value']);
101         }
102         if (isset($settings['queries']) && is_array($settings['queries'])) {
103             foreach ($settings['queries'] as $queries) {
104                 foreach ((array)$queries as $query) {
105                     $con->exec($query);
106                 }
107             }
108         }
109     }
110
111     /**
112      * Sets the character encoding using SQL standard SET NAMES statement.
113      *
114      * This method is invoked from the default initConnection() method and must
115      * be overridden for an RDMBS which does _not_ support this SQL standard.
116      *
117      * @param      PDO   A PDO connection instance.
118      * @param      string The charset encoding.
119      * @see        initConnection()
120      */
121     public function setCharset(PDO $con, $charset)
122     {
123         $con->exec("SET NAMES '" . $charset . "'");
124     }
125
126     /**
127      * This method is used to ignore case.
128      *
129      * @param      string The string to transform to upper case.
130      * @return     string The upper case string.
131      */
132     public abstract function toUpperCase($in);
133
134     /**
135      * Returns the character used to indicate the beginning and end of
136      * a piece of text used in a SQL statement (generally a single
137      * quote).
138      *
139      * @return     string The text delimeter.
140      */
141     public function getStringDelimiter()
142     {
143         return '\'';
144     }
145
146     /**
147      * This method is used to ignore case.
148      *
149      * @param      string $in The string whose case to ignore.
150      * @return     string The string in a case that can be ignored.
151      */
152     public abstract function ignoreCase($in);
153
154     /**
155      * This method is used to ignore case in an ORDER BY clause.
156      * Usually it is the same as ignoreCase, but some databases
157      * (Interbase for example) does not use the same SQL in ORDER BY
158      * and other clauses.
159      *
160      * @param      string $in The string whose case to ignore.
161      * @return     string The string in a case that can be ignored.
162      */
163     public function ignoreCaseInOrderBy($in)
164     {
165         return $this->ignoreCase($in);
166     }
167
168     /**
169      * Returns SQL which concatenates the second string to the first.
170      *
171      * @param      string String to concatenate.
172      * @param      string String to append.
173      * @return     string
174      */
175     public abstract function concatString($s1, $s2);
176
177     /**
178      * Returns SQL which extracts a substring.
179      *
180      * @param      string String to extract from.
181      * @param      int Offset to start from.
182      * @param      int Number of characters to extract.
183      * @return     string
184      */
185     public abstract function subString($s, $pos, $len);
186
187     /**
188      * Returns SQL which calculates the length (in chars) of a string.
189      *
190      * @param      string String to calculate length of.
191      * @return     string
192      */
193     public abstract function strLength($s);
194
195
196     /**
197      * Quotes database objec identifiers (table names, col names, sequences, etc.).
198      * @param      string $text The identifier to quote.
199      * @return     string The quoted identifier.
200      */
201     public function quoteIdentifier($text)
202     {
203         return '"' . $text . '"';
204     }
205
206     /**
207      * Quotes a database table which could have space seperating it from an alias, both should be identified seperately
208      * @param      string $table The table name to quo
209      * @return     string The quoted table name
210      **/
211     public function quoteIdentifierTable($table) {
212         return implode(" ", array_map(array($this, "quoteIdentifier"), explode(" ", $table) ) );
213     }
214
215     /**
216      * Returns the native ID method for this RDBMS.
217      * @return     int one of DBAdapter:ID_METHOD_SEQUENCE, DBAdapter::ID_METHOD_AUTOINCREMENT.
218      */
219     protected function getIdMethod()
220     {
221         return DBAdapter::ID_METHOD_AUTOINCREMENT;
222     }
223
224     /**
225      * Whether this adapter uses an ID generation system that requires getting ID _before_ performing INSERT.
226      * @return     boolean
227      */
228     public function isGetIdBeforeInsert()
229     {
230         return ($this->getIdMethod() === DBAdapter::ID_METHOD_SEQUENCE);
231     }
232
233     /**
234      * Whether this adapter uses an ID generation system that requires getting ID _before_ performing INSERT.
235      * @return     boolean
236      */
237     public function isGetIdAfterInsert()
238     {
239         return ($this->getIdMethod() === DBAdapter::ID_METHOD_AUTOINCREMENT);
240     }
241
242     /**
243      * Gets the generated ID (either last ID for autoincrement or next sequence ID).
244      * @return     mixed
245      */
246     public function getId(PDO $con, $name = null)
247     {
248         return $con->lastInsertId($name);
249     }
250
251     /**
252      * Returns timestamp formatter string for use in date() function.
253      * @return     string
254      */
255     public function getTimestampFormatter()
256     {
257         return "Y-m-d H:i:s";
258     }
259
260     /**
261      * Returns date formatter string for use in date() function.
262      * @return     string
263      */
264     public function getDateFormatter()
265     {
266         return "Y-m-d";
267     }
268
269     /**
270      * Returns time formatter string for use in date() function.
271      * @return     string
272      */
273     public function getTimeFormatter()
274     {
275         return "H:i:s";
276     }
277
278     /**
279      * Should Column-Names get identifiers for inserts or updates.
280      * By default false is returned -> backwards compability.
281      *
282      * it`s a workaround...!!!
283      *
284      * @todo       should be abstract
285      * @return     boolean
286      * @deprecated
287      */
288     public function useQuoteIdentifier()
289     {
290         return false;
291     }
292
293     /**
294      * Modifies the passed-in SQL to add LIMIT and/or OFFSET.
295      */
296     public abstract function applyLimit(&$sql, $offset, $limit);
297     
298     /**
299      * Gets the SQL string that this adapter uses for getting a random number.
300      *
301      * @param      mixed $seed (optional) seed value for databases that support this
302      */
303     public abstract function random($seed = null);
304
305 }
306
Note: See TracBrowser for help on using the browser.