This repository was archived by the owner on Apr 30, 2025. It is now read-only.
forked from FriendsOfSymfony1/doctrine1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnection.php
More file actions
1716 lines (1507 loc) · 53.8 KB
/
Connection.php
File metadata and controls
1716 lines (1507 loc) · 53.8 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
/**
* Doctrine_Connection
*
* A wrapper layer on top of PDO / Doctrine_Adapter
*
* Doctrine_Connection is the heart of any Doctrine based application.
*
* 1. Event listeners
* An easy to use, pluggable eventlistener architecture. Aspects such as
* logging, query profiling and caching can be easily implemented through
* the use of these listeners
*
* 2. Lazy-connecting
* Creating an instance of Doctrine_Connection does not connect
* to database. Connecting to database is only invoked when actually needed
* (for example when query() is being called)
*
* 3. Convenience methods
* Doctrine_Connection provides many convenience methods such as fetchAll(), fetchOne() etc.
*
* 4. Modular structure
* Higher level functionality such as schema importing, exporting, sequence handling etc.
* is divided into modules. For a full list of connection modules see
* Doctrine_Connection::$_modules
*
* @package Doctrine
* @subpackage Connection
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org
* @since 1.0
* @version $Revision$
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @author Lukas Smith <smith@pooteeweet.org> (MDB2 library)
*/
abstract class Doctrine_Connection extends Doctrine_Configurable implements Countable, IteratorAggregate, Serializable
{
/**
* @var $dbh the database handler
*/
protected $dbh;
/**
* @var array $tables an array containing all the initialized Doctrine_Table objects
* keys representing Doctrine_Table component names and values as Doctrine_Table objects
*/
protected $tables = array();
/**
* $_name
*
* Name of the connection
*
* @var string $_name
*/
protected $_name;
/**
* The name of this connection driver.
*
* @var string $driverName
*/
protected $driverName;
/**
* @var boolean $isConnected whether or not a connection has been established
*/
protected $isConnected = false;
/**
* @var array $supported an array containing all features this driver supports,
* keys representing feature names and values as
* one of the following (true, false, 'emulated')
*/
protected $supported = array();
/**
* @var array $pendingAttributes An array of pending attributes. When setting attributes
* no connection is needed. When connected all the pending
* attributes are passed to the underlying adapter (usually PDO) instance.
*/
protected $pendingAttributes = array();
/**
* @var array $modules an array containing all modules
* transaction Doctrine_Transaction driver, handles savepoint and transaction isolation abstraction
*
* expression Doctrine_Expression_Driver, handles expression abstraction
*
* dataDict Doctrine_DataDict driver, handles datatype abstraction
*
* export Doctrine_Export driver, handles db structure modification abstraction (contains
* methods such as alterTable, createConstraint etc.)
* import Doctrine_Import driver, handles db schema reading
*
* sequence Doctrine_Sequence driver, handles sequential id generation and retrieval
*
* unitOfWork Doctrine_Connection_UnitOfWork handles many orm functionalities such as object
* deletion and saving
*
* formatter Doctrine_Formatter handles data formatting, quoting and escaping
*
* @see Doctrine_Connection::__get()
* @see Doctrine_DataDict
* @see Doctrine_Expression_Driver
* @see Doctrine_Export
* @see Doctrine_Transaction
* @see Doctrine_Sequence
* @see Doctrine_Connection_UnitOfWork
* @see Doctrine_Formatter
*/
private $modules = array('transaction' => false,
'expression' => false,
'dataDict' => false,
'export' => false,
'import' => false,
'sequence' => false,
'unitOfWork' => false,
'formatter' => false,
'util' => false,
);
/**
* @var array $properties an array of connection properties
*/
protected $properties = array('sql_comments' => array(array('start' => '--', 'end' => "\n", 'escape' => false),
array('start' => '/*', 'end' => '*/', 'escape' => false)),
'identifier_quoting' => array('start' => '"', 'end' => '"','escape' => '"'),
'string_quoting' => array('start' => "'",
'end' => "'",
'escape' => false,
'escape_pattern' => false),
'wildcards' => array('%', '_'),
'varchar_max_length' => 255,
'sql_file_delimiter' => ";\n",
'max_identifier_length' => 64,
);
/**
* @var array $serverInfo
*/
protected $serverInfo = array();
protected $options = array();
/**
* @var array $supportedDrivers an array containing all supported drivers
*/
private static $supportedDrivers = array(
'Mysql',
'Pgsql',
'Oracle',
'Mssql',
'Sqlite',
);
protected $_count = 0;
/**
* @var array $_userFkNames array of foreign key names that have been used
*/
protected $_usedNames = array(
'foreign_keys' => array(),
'indexes' => array()
);
/**
* @var Doctrine_Cache_Interface The cache driver used for caching tables.
*/
protected $_tableCache;
protected $_tableCacheTTL;
/**
* the constructor
*
* @param Doctrine_Manager $manager the manager object
* @param PDO|Doctrine_Adapter_Interface $adapter database driver
*/
public function __construct(Doctrine_Manager $manager, $adapter, $user = null, $pass = null)
{
if (is_object($adapter)) {
if ( ! ($adapter instanceof PDO) && ! in_array('Doctrine_Adapter_Interface', class_implements($adapter))) {
throw new Doctrine_Connection_Exception('First argument should be an instance of PDO or implement Doctrine_Adapter_Interface');
}
$this->dbh = $adapter;
$this->isConnected = true;
} else if (is_array($adapter)) {
$this->pendingAttributes[Doctrine_Core::ATTR_DRIVER_NAME] = $adapter['scheme'];
$this->options['dsn'] = $adapter['dsn'];
$this->options['username'] = $adapter['user'];
$this->options['password'] = $adapter['pass'];
$this->options['other'] = array();
if (isset($adapter['other'])) {
$this->options['other'] = array(Doctrine_Core::ATTR_PERSISTENT => $adapter['persistent']);
}
}
$this->setParent($manager);
$this->setAttribute(Doctrine_Core::ATTR_CASE, Doctrine_Core::CASE_NATURAL);
$this->setAttribute(Doctrine_Core::ATTR_ERRMODE, Doctrine_Core::ERRMODE_EXCEPTION);
$this->getAttribute(Doctrine_Core::ATTR_LISTENER)->onOpen($this);
$this->_tableCacheTTL = $this->getAttribute(Doctrine_Core::ATTR_TABLE_CACHE_LIFESPAN);
}
/**
* Check wherther the connection to the database has been made yet
*
* @return boolean
*/
public function isConnected()
{
return $this->isConnected;
}
/**
* getOptions
*
* Get array of all options
*
* @return void
*/
public function getOptions()
{
return $this->options;
}
/**
* getOption
*
* Retrieves option
*
* @param string $option
* @return void
*/
public function getOption($option)
{
if (isset($this->options[$option])) {
return $this->options[$option];
}
}
/**
* setOption
*
* Set option value
*
* @param string $option
* @return void
*/
public function setOption($option, $value)
{
return $this->options[$option] = $value;
}
/**
* getAttribute
* retrieves a database connection attribute
*
* @param integer $attribute
* @return mixed
*/
public function getAttribute($attribute)
{
if ($attribute >= 100 && $attribute < 1000) {
if ( ! isset($this->attributes[$attribute])) {
return parent::getAttribute($attribute);
}
return $this->attributes[$attribute];
}
if ($this->isConnected) {
try {
return $this->dbh->getAttribute($attribute);
} catch (Exception $e) {
throw new Doctrine_Connection_Exception('Attribute ' . $attribute . ' not found.');
}
} else {
if ( ! isset($this->pendingAttributes[$attribute])) {
$this->connect();
$this->getAttribute($attribute);
}
return $this->pendingAttributes[$attribute];
}
}
/**
* returns an array of available PDO drivers
*/
public static function getAvailableDrivers()
{
return PDO::getAvailableDrivers();
}
/**
* Returns an array of supported drivers by Doctrine
*
* @return array $supportedDrivers
*/
public static function getSupportedDrivers()
{
return self::$supportedDrivers;
}
/**
* setAttribute
* sets an attribute
*
* @todo why check for >= 100? has this any special meaning when creating
* attributes?
*
* @param integer $attribute
* @param mixed $value
* @return boolean
*/
public function setAttribute($attribute, $value)
{
if ($attribute >= 100 && $attribute < 1000) {
parent::setAttribute($attribute, $value);
} else {
if ($this->isConnected) {
$this->dbh->setAttribute($attribute, $value);
} else {
$this->pendingAttributes[$attribute] = $value;
}
}
return $this;
}
/**
* getName
* returns the name of this driver
*
* @return string the name of this driver
*/
public function getName()
{
return $this->_name;
}
/**
* setName
*
* Sets the name of the connection
*
* @param string $name
* @return void
*/
public function setName($name)
{
$this->_name = $name;
}
/**
* getDriverName
*
* Gets the name of the instance driver
*
* @return void
*/
public function getDriverName()
{
return $this->driverName;
}
/**
* __get
* lazy loads given module and returns it
*
* @see Doctrine_DataDict
* @see Doctrine_Expression_Driver
* @see Doctrine_Export
* @see Doctrine_Transaction
* @see Doctrine_Connection::$modules all availible modules
* @param string $name the name of the module to get
* @throws Doctrine_Connection_Exception if trying to get an unknown module
* @return Doctrine_Connection_Module connection module
*/
public function __get($name)
{
if (isset($this->properties[$name])) {
return $this->properties[$name];
}
if ( ! isset($this->modules[$name])) {
throw new Doctrine_Connection_Exception('Unknown module / property ' . $name);
}
if ($this->modules[$name] === false) {
switch ($name) {
case 'unitOfWork':
$this->modules[$name] = new Doctrine_Connection_UnitOfWork($this);
break;
case 'formatter':
$this->modules[$name] = new Doctrine_Formatter($this);
break;
default:
$class = 'Doctrine_' . ucwords($name) . '_' . $this->getDriverName();
$this->modules[$name] = new $class($this);
}
}
return $this->modules[$name];
}
/**
* returns the manager that created this connection
*
* @return Doctrine_Manager
*/
public function getManager()
{
return $this->getParent();
}
/**
* returns the database handler of which this connection uses
*
* @return PDO the database handler
*/
public function getDbh()
{
$this->connect();
return $this->dbh;
}
/**
* connect
* connects into database
*
* @return boolean
*/
public function connect()
{
if ($this->isConnected) {
return false;
}
$event = new Doctrine_Event($this, Doctrine_Event::CONN_CONNECT);
$this->getListener()->preConnect($event);
$e = explode(':', $this->options['dsn']);
$found = false;
if (extension_loaded('pdo')) {
if (in_array($e[0], self::getAvailableDrivers())) {
try {
$this->dbh = new PDO($this->options['dsn'], $this->options['username'],
(!$this->options['password'] ? '':$this->options['password']), $this->options['other']);
$this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
throw new Doctrine_Connection_Exception('PDO Connection Error: ' . $e->getMessage());
}
$found = true;
}
}
if ( ! $found) {
$class = 'Doctrine_Adapter_' . ucwords($e[0]);
if (class_exists($class)) {
$this->dbh = new $class($this->options['dsn'], $this->options['username'], $this->options['password'], $this->options);
} else {
throw new Doctrine_Connection_Exception("Couldn't locate driver named " . $e[0]);
}
}
// attach the pending attributes to adapter
foreach($this->pendingAttributes as $attr => $value) {
// some drivers don't support setting this so we just skip it
if ($attr == Doctrine_Core::ATTR_DRIVER_NAME) {
continue;
}
$this->dbh->setAttribute($attr, $value);
}
$this->isConnected = true;
$this->getListener()->postConnect($event);
return true;
}
public function incrementQueryCount()
{
$this->_count++;
}
/**
* converts given driver name
*
* @param
*/
public function driverName($name)
{
}
/**
* supports
*
* @param string $feature the name of the feature
* @return boolean whether or not this drivers supports given feature
*/
public function supports($feature)
{
return (isset($this->supported[$feature])
&& ($this->supported[$feature] === 'emulated'
|| $this->supported[$feature]));
}
/**
* Execute a SQL REPLACE query. A REPLACE query is identical to a INSERT
* query, except that if there is already a row in the table with the same
* key field values, the REPLACE query just updates its values instead of
* inserting a new row.
*
* The REPLACE type of query does not make part of the SQL standards. Since
* practically only MySQL and SQLIte implement it natively, this type of
* query isemulated through this method for other DBMS using standard types
* of queries inside a transaction to assure the atomicity of the operation.
*
* @param string name of the table on which the REPLACE query will
* be executed.
*
* @param array an associative array that describes the fields and the
* values that will be inserted or updated in the specified table. The
* indexes of the array are the names of all the fields of the table.
*
* The values of the array are values to be assigned to the specified field.
*
* @param array $keys an array containing all key fields (primary key fields
* or unique index fields) for this table
*
* the uniqueness of a row will be determined according to
* the provided key fields
*
* this method will fail if no key fields are specified
*
* @throws Doctrine_Connection_Exception if this driver doesn't support replace
* @throws Doctrine_Connection_Exception if some of the key values was null
* @throws Doctrine_Connection_Exception if there were no key fields
* @throws PDOException if something fails at PDO level
* @ return integer number of rows affected
*/
public function replace(Doctrine_Table $table, array $fields, array $keys)
{
if (empty($keys)) {
throw new Doctrine_Connection_Exception('Not specified which fields are keys');
}
$identifier = (array) $table->getIdentifier();
$condition = array();
foreach ($fields as $fieldName => $value) {
if (in_array($fieldName, $keys)) {
if ($value !== null) {
$condition[] = $table->getColumnName($fieldName) . ' = ?';
$conditionValues[] = $value;
}
}
}
$affectedRows = 0;
if ( ! empty($condition) && ! empty($conditionValues)) {
$query = 'DELETE FROM ' . $this->quoteIdentifier($table->getTableName())
. ' WHERE ' . implode(' AND ', $condition);
$affectedRows = $this->exec($query, $conditionValues);
}
$this->insert($table, $fields);
$affectedRows++;
return $affectedRows;
}
/**
* deletes table row(s) matching the specified identifier
*
* @throws Doctrine_Connection_Exception if something went wrong at the database level
* @param string $table The table to delete data from
* @param array $identifier An associateve array containing identifier column-value pairs.
* @return integer The number of affected rows
*/
public function delete(Doctrine_Table $table, array $identifier)
{
$tmp = array();
foreach (array_keys($identifier) as $id) {
$tmp[] = $this->quoteIdentifier($table->getColumnName($id)) . ' = ?';
}
$query = 'DELETE FROM '
. $this->quoteIdentifier($table->getTableName())
. ' WHERE ' . implode(' AND ', $tmp);
return $this->exec($query, array_values($identifier));
}
/**
* Updates table row(s) with specified data.
*
* @throws Doctrine_Connection_Exception if something went wrong at the database level
* @param Doctrine_Table $table The table to insert data into
* @param array $values An associative array containing column-value pairs.
* Values can be strings or Doctrine_Expression instances.
* @return integer the number of affected rows. Boolean false if empty value array was given,
*/
public function update(Doctrine_Table $table, array $fields, array $identifier)
{
if (empty($fields)) {
return false;
}
$set = array();
foreach ($fields as $fieldName => $value) {
if ($value instanceof Doctrine_Expression) {
$set[] = $this->quoteIdentifier($table->getColumnName($fieldName)) . ' = ' . $value->getSql();
unset($fields[$fieldName]);
} else {
$set[] = $this->quoteIdentifier($table->getColumnName($fieldName)) . ' = ?';
}
}
$params = array_merge(array_values($fields), array_values($identifier));
$sql = 'UPDATE ' . $this->quoteIdentifier($table->getTableName())
. ' SET ' . implode(', ', $set)
. ' WHERE ' . implode(' = ? AND ', $this->quoteMultipleIdentifier($table->getIdentifierColumnNames()))
. ' = ?';
return $this->exec($sql, $params);
}
/**
* Inserts a table row with specified data.
*
* @param Doctrine_Table $table The table to insert data into.
* @param array $values An associative array containing column-value pairs.
* Values can be strings or Doctrine_Expression instances.
* @return integer the number of affected rows. Boolean false if empty value array was given,
*/
public function insert(Doctrine_Table $table, array $fields)
{
$tableName = $table->getTableName();
// column names are specified as array keys
$cols = array();
// the query VALUES will contain either expresions (eg 'NOW()') or ?
$a = array();
foreach ($fields as $fieldName => $value) {
$cols[] = $this->quoteIdentifier($table->getColumnName($fieldName));
if ($value instanceof Doctrine_Expression) {
$a[] = $value->getSql();
unset($fields[$fieldName]);
} else {
$a[] = '?';
}
}
// build the statement
$query = 'INSERT INTO ' . $this->quoteIdentifier($tableName)
. ' (' . implode(', ', $cols) . ')'
. ' VALUES (' . implode(', ', $a) . ')';
return $this->exec($query, array_values($fields));
}
/**
* Quote a string so it can be safely used as a table or column name
*
* Delimiting style depends on which database driver is being used.
*
* NOTE: just because you CAN use delimited identifiers doesn't mean
* you SHOULD use them. In general, they end up causing way more
* problems than they solve.
*
* Portability is broken by using the following characters inside
* delimited identifiers:
* + backtick (<kbd>`</kbd>) -- due to MySQL
* + double quote (<kbd>"</kbd>) -- due to Oracle
* + brackets (<kbd>[</kbd> or <kbd>]</kbd>) -- due to Access
*
* Delimited identifiers are known to generally work correctly under
* the following drivers:
* + mssql
* + mysql
* + mysqli
* + oci8
* + pgsql
* + sqlite
*
* InterBase doesn't seem to be able to use delimited identifiers
* via PHP 4. They work fine under PHP 5.
*
* @param string $str identifier name to be quoted
* @param bool $checkOption check the 'quote_identifier' option
*
* @return string quoted identifier string
*/
public function quoteIdentifier($str, $checkOption = true)
{
// quick fix for the identifiers that contain a dot
if (strpos($str, '.')) {
$e = explode('.', $str);
return $this->formatter->quoteIdentifier($e[0], $checkOption) . '.'
. $this->formatter->quoteIdentifier($e[1], $checkOption);
}
return $this->formatter->quoteIdentifier($str, $checkOption);
}
/**
* quoteMultipleIdentifier
* Quotes multiple identifier strings
*
* @param array $arr identifiers array to be quoted
* @param bool $checkOption check the 'quote_identifier' option
*
* @return string quoted identifier string
*/
public function quoteMultipleIdentifier($arr, $checkOption = true)
{
foreach ($arr as $k => $v) {
$arr[$k] = $this->quoteIdentifier($v, $checkOption);
}
return $arr;
}
/**
* convertBooleans
* some drivers need the boolean values to be converted into integers
* when using DQL API
*
* This method takes care of that conversion
*
* @param array $item
* @return void
*/
public function convertBooleans($item)
{
return $this->formatter->convertBooleans($item);
}
/**
* quote
* quotes given input parameter
*
* @param mixed $input parameter to be quoted
* @param string $type
* @return string
*/
public function quote($input, $type = null)
{
return $this->formatter->quote($input, $type);
}
/**
* Set the date/time format for the current connection
*
* @param string time format
*
* @return void
*/
public function setDateFormat($format = null)
{
}
/**
* fetchAll
*
* @param string $statement sql query to be executed
* @param array $params prepared statement params
* @return array
*/
public function fetchAll($statement, array $params = array())
{
return $this->execute($statement, $params)->fetchAll(Doctrine_Core::FETCH_ASSOC);
}
/**
* fetchOne
*
* @param string $statement sql query to be executed
* @param array $params prepared statement params
* @param int $colnum 0-indexed column number to retrieve
* @return mixed
*/
public function fetchOne($statement, array $params = array(), $colnum = 0)
{
return $this->execute($statement, $params)->fetchColumn($colnum);
}
/**
* fetchRow
*
* @param string $statement sql query to be executed
* @param array $params prepared statement params
* @return array
*/
public function fetchRow($statement, array $params = array())
{
return $this->execute($statement, $params)->fetch(Doctrine_Core::FETCH_ASSOC);
}
/**
* fetchArray
*
* @param string $statement sql query to be executed
* @param array $params prepared statement params
* @return array
*/
public function fetchArray($statement, array $params = array())
{
return $this->execute($statement, $params)->fetch(Doctrine_Core::FETCH_NUM);
}
/**
* fetchColumn
*
* @param string $statement sql query to be executed
* @param array $params prepared statement params
* @param int $colnum 0-indexed column number to retrieve
* @return array
*/
public function fetchColumn($statement, array $params = array(), $colnum = 0)
{
return $this->execute($statement, $params)->fetchAll(Doctrine_Core::FETCH_COLUMN, $colnum);
}
/**
* fetchAssoc
*
* @param string $statement sql query to be executed
* @param array $params prepared statement params
* @return array
*/
public function fetchAssoc($statement, array $params = array())
{
return $this->execute($statement, $params)->fetchAll(Doctrine_Core::FETCH_ASSOC);
}
/**
* fetchBoth
*
* @param string $statement sql query to be executed
* @param array $params prepared statement params
* @return array
*/
public function fetchBoth($statement, array $params = array())
{
return $this->execute($statement, $params)->fetchAll(Doctrine_Core::FETCH_BOTH);
}
/**
* query
* queries the database using Doctrine Query Language
* returns a collection of Doctrine_Record objects
*
* <code>
* $users = $conn->query('SELECT u.* FROM User u');
*
* $users = $conn->query('SELECT u.* FROM User u WHERE u.name LIKE ?', array('someone'));
* </code>
*
* @param string $query DQL query
* @param array $params query parameters
* @param int $hydrationMode Doctrine_Core::HYDRATE_ARRAY or Doctrine_Core::HYDRATE_RECORD
* @see Doctrine_Query
* @return Doctrine_Collection Collection of Doctrine_Record objects
*/
public function query($query, array $params = array(), $hydrationMode = null)
{
$parser = Doctrine_Query::create($this);
$res = $parser->query($query, $params, $hydrationMode);
$parser->free();
return $res;
}
/**
* prepare
*
* @param string $statement
*/
public function prepare($statement)
{
$this->connect();
try {
$event = new Doctrine_Event($this, Doctrine_Event::CONN_PREPARE, $statement);
$this->getAttribute(Doctrine_Core::ATTR_LISTENER)->prePrepare($event);
$stmt = false;
if ( ! $event->skipOperation) {
$stmt = $this->dbh->prepare($statement);
}
$this->getAttribute(Doctrine_Core::ATTR_LISTENER)->postPrepare($event);
return new Doctrine_Connection_Statement($this, $stmt);
} catch(Doctrine_Adapter_Exception $e) {
} catch(PDOException $e) { }
$this->rethrowException($e, $this, $statement);
}
/**
* query
* queries the database using Doctrine Query Language and returns
* the first record found
*
* <code>
* $user = $conn->queryOne('SELECT u.* FROM User u WHERE u.id = ?', array(1));
*
* $user = $conn->queryOne('SELECT u.* FROM User u WHERE u.name LIKE ? AND u.password = ?',
* array('someone', 'password')
* );
* </code>
*
* @param string $query DQL query
* @param array $params query parameters
* @see Doctrine_Query
* @return Doctrine_Record|false Doctrine_Record object on success,
* boolean false on failure
*/
public function queryOne($query, array $params = array())
{
$parser = Doctrine_Query::create();
$coll = $parser->query($query, $params);
if ( ! $coll->contains(0)) {
return false;
}
return $coll[0];
}
/**
* queries the database with limit and offset
* added to the query and returns a Doctrine_Connection_Statement object
*
* @param string $query
* @param integer $limit
* @param integer $offset
* @return Doctrine_Connection_Statement
*/
public function select($query, $limit = 0, $offset = 0)
{
if ($limit > 0 || $offset > 0) {
$query = $this->modifyLimitQuery($query, $limit, $offset);
}
return $this->execute($query);
}
/**
* standaloneQuery
*
* @param string $query sql query
* @param array $params query parameters
*
* @return PDOStatement|Doctrine_Adapter_Statement
*/
public function standaloneQuery($query, $params = array())
{
return $this->execute($query, $params);
}
/**