/[drupal]/contributions/sandbox/crell/pdo/database.mysql.inc
ViewVC logotype

Contents of /contributions/sandbox/crell/pdo/database.mysql.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.13 - (show annotations) (download) (as text)
Fri Feb 15 05:46:13 2008 UTC (21 months, 1 week ago) by crell
Branch: MAIN
CVS Tags: HEAD
Changes since 1.12: +1 -1 lines
File MIME type: text/x-php
- Table creation now works.
1 <?php
2
3 class DatabaseConnection_mysql extends DatabaseConnection {
4
5 public static $rand = 'RAND()';
6 public static $timestamp = 'Y-m-d h:i:s';
7
8 protected $transactionSupport;
9
10 public function __construct(Array $connection_options = array()) {
11
12 $connection_options += array(
13 'transactions' => FALSE,
14 'port' => 3306,
15 );
16 $this->transactionSupport = $connection_options['transactions'];
17
18 $dsn = "mysql:host={$connection_options['host']};dbname={$connection_options['database']}";
19
20 parent::__construct($dsn, $connection_options['username'], $connection_options['password'], array(
21 PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => TRUE , // So we don't have to mess around with cursors.
22 PDO::ATTR_EMULATE_PREPARES => TRUE)// Because MySQL's prepared statements skip the query cache, because it's dumb.
23 );
24 }
25
26 public function queryRange($query, Array $args, $from, $count, Array $options) {
27 // Backward compatibility hack, temporary.
28 $query = str_replace(array('%d' , '%f' , '%b' , "'%s'"), '?', $query);
29
30 return $this->runQuery($query . ' LIMIT ' . $from . ', ' . $count, $args, $options);
31 }
32
33 public function queryTemporary($query, Array $args, $tablename) {
34 $query = preg_replace('/^SELECT/i', 'CREATE TEMPORARY TABLE ' . $tablename . ' Engine=HEAP SELECT', $this->prefixTables($query));
35
36 return $this->runQuery($query, $args, $options);
37 }
38
39 public function replace($query_id, $table, Array $fields, Array $where) {
40
41 $insert_list = $fields + $where;
42
43 $insert_fields = array_keys($insert_list);
44 $insert_values = array_values($insert_list);
45
46 $placeholders = array_fill(0, count($insert_values), '?');
47
48 $update_values = array();
49 $flat_fields = array();
50
51 foreach ($fields as $field => $value) {
52 $insert_values[] = $value;
53 $flat_fields[] = $field . '=?';
54 }
55
56 return $this->runQuery('INSERT INTO {' . $table . '} (' . implode(',', $insert_fields) . ') VALUES (' . implode(',', $placeholders) . ') ON DUPLICATE KEY UPDATE ' . implode(', ', $flat_fields), $insert_values, array(), TRUE);
57 }
58
59 public function driver() {
60 return 'mysql';
61 }
62
63 public function databaseType() {
64 return 'mysql';
65 }
66
67 public function supportsTransactions() {
68 return $this->transactionSupport;
69 }
70
71 public function escapeTable($table) {
72 return preg_replace('/[^A-Za-z0-9_]+/', '', $table);
73 }
74
75 }
76
77 class DatabaseTransaction_mysql extends DatabaseTransaction { }
78
79 class SelectQuery_mysql extends SelectQuery { }
80
81 class InsertQuery_mysql extends InsertQuery {
82
83 public function execute() {
84 //drupal_alter('query', $this->queryId, $this);
85
86 $max_placeholder = 0;
87 $insert_values = array();
88 foreach ($this->insertValues as $insert_values) {
89 foreach ($insert_values as $value) {
90 $values[':db_insert_placeholder_'. $max_placeholder++] = $value;
91 }
92 }
93
94 $num_affected = $this->connection->runQuery((string)$this, $values, $this->queryOptions);
95
96 return $this->connection->lastInsertId();
97 }
98
99 public function __toString() {
100
101 $delay = $this->queryOptions['delay'] ? ' DELAYED ' : '';
102 $query = "INSERT $delay INTO {" . $this->table . '} (' . implode(', ', $this->insertFields) . ') VALUES ';
103
104 $max_placeholder = 0;
105 $values = array();
106 foreach ($this->insertValues as $insert_values) {
107 $placeholders = array();
108 $new_placeholder = $max_placeholder + count($insert_values);
109 for ($i = $max_placeholder; $i < $new_placeholder; ++$i) {
110 $placeholders[] = ':db_insert_placeholder_'. $i;
111 }
112 $max_placeholder = $new_placeholder;
113 $values[] = '('. implode(', ', $placeholders) .')';
114 }
115
116 $query .= implode(', ', $values);
117
118 return $query;
119
120 return "INSERT $delay INTO {" . $this->table . '} (' . implode(', ', $this->insertFields) . ') VALUES (' . implode(', ', $placeholders) . ')';
121 }
122 }
123
124 class UpdateQuery_mysql extends UpdateQuery { }
125
126 class DeleteQuery_mysql extends DeleteQuery { }
127

  ViewVC Help
Powered by ViewVC 1.1.2