~chroot64bit/zivios/gentoo-experimental

« back to all changes in this revision

Viewing changes to application/library/Zend/Db/Adapter/Pdo/Mysql.php

  • Committer: Mustafa A. Hashmi
  • Date: 2008-12-04 13:32:21 UTC
  • Revision ID: mhashmi@zivios.org-20081204133221-0nd1trunwevijj38
Inclusion of new installation framework with ties to zend layout and dojo layout

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Zend Framework
 
4
 *
 
5
 * LICENSE
 
6
 *
 
7
 * This source file is subject to the new BSD license that is bundled
 
8
 * with this package in the file LICENSE.txt.
 
9
 * It is also available through the world-wide-web at this URL:
 
10
 * http://framework.zend.com/license/new-bsd
 
11
 * If you did not receive a copy of the license and are unable to
 
12
 * obtain it through the world-wide-web, please send an email
 
13
 * to license@zend.com so we can send you a copy immediately.
 
14
 *
 
15
 * @category   Zend
 
16
 * @package    Zend_Db
 
17
 * @subpackage Adapter
 
18
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
19
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 
20
 * @version    $Id: Mysql.php 12842 2008-11-25 22:07:48Z mikaelkael $
 
21
 */
 
22
 
 
23
 
 
24
/**
 
25
 * @see Zend_Db_Adapter_Pdo_Abstract
 
26
 */
 
27
require_once 'Zend/Db/Adapter/Pdo/Abstract.php';
 
28
 
 
29
 
 
30
/**
 
31
 * Class for connecting to MySQL databases and performing common operations.
 
32
 *
 
33
 * @category   Zend
 
34
 * @package    Zend_Db
 
35
 * @subpackage Adapter
 
36
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
37
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 
38
 */
 
39
class Zend_Db_Adapter_Pdo_Mysql extends Zend_Db_Adapter_Pdo_Abstract
 
40
{
 
41
 
 
42
    /**
 
43
     * PDO type.
 
44
     *
 
45
     * @var string
 
46
     */
 
47
    protected $_pdoType = 'mysql';
 
48
 
 
49
    /**
 
50
     * Keys are UPPERCASE SQL datatypes or the constants
 
51
     * Zend_Db::INT_TYPE, Zend_Db::BIGINT_TYPE, or Zend_Db::FLOAT_TYPE.
 
52
     *
 
53
     * Values are:
 
54
     * 0 = 32-bit integer
 
55
     * 1 = 64-bit integer
 
56
     * 2 = float or decimal
 
57
     *
 
58
     * @var array Associative array of datatypes to values 0, 1, or 2.
 
59
     */
 
60
    protected $_numericDataTypes = array(
 
61
        Zend_Db::INT_TYPE    => Zend_Db::INT_TYPE,
 
62
        Zend_Db::BIGINT_TYPE => Zend_Db::BIGINT_TYPE,
 
63
        Zend_Db::FLOAT_TYPE  => Zend_Db::FLOAT_TYPE,
 
64
        'INT'                => Zend_Db::INT_TYPE,
 
65
        'INTEGER'            => Zend_Db::INT_TYPE,
 
66
        'MEDIUMINT'          => Zend_Db::INT_TYPE,
 
67
        'SMALLINT'           => Zend_Db::INT_TYPE,
 
68
        'TINYINT'            => Zend_Db::INT_TYPE,
 
69
        'BIGINT'             => Zend_Db::BIGINT_TYPE,
 
70
        'SERIAL'             => Zend_Db::BIGINT_TYPE,
 
71
        'DEC'                => Zend_Db::FLOAT_TYPE,
 
72
        'DECIMAL'            => Zend_Db::FLOAT_TYPE,
 
73
        'DOUBLE'             => Zend_Db::FLOAT_TYPE,
 
74
        'DOUBLE PRECISION'   => Zend_Db::FLOAT_TYPE,
 
75
        'FIXED'              => Zend_Db::FLOAT_TYPE,
 
76
        'FLOAT'              => Zend_Db::FLOAT_TYPE
 
77
    );
 
78
 
 
79
    /**
 
80
     * @return string
 
81
     */
 
82
    public function getQuoteIdentifierSymbol()
 
83
    {
 
84
        return "`";
 
85
    }
 
86
 
 
87
    /**
 
88
     * Returns a list of the tables in the database.
 
89
     *
 
90
     * @return array
 
91
     */
 
92
    public function listTables()
 
93
    {
 
94
        return $this->fetchCol('SHOW TABLES');
 
95
    }
 
96
 
 
97
    /**
 
98
     * Returns the column descriptions for a table.
 
99
     *
 
100
     * The return value is an associative array keyed by the column name,
 
101
     * as returned by the RDBMS.
 
102
     *
 
103
     * The value of each array element is an associative array
 
104
     * with the following keys:
 
105
     *
 
106
     * SCHEMA_NAME      => string; name of database or schema
 
107
     * TABLE_NAME       => string;
 
108
     * COLUMN_NAME      => string; column name
 
109
     * COLUMN_POSITION  => number; ordinal position of column in table
 
110
     * DATA_TYPE        => string; SQL datatype name of column
 
111
     * DEFAULT          => string; default expression of column, null if none
 
112
     * NULLABLE         => boolean; true if column can have nulls
 
113
     * LENGTH           => number; length of CHAR/VARCHAR
 
114
     * SCALE            => number; scale of NUMERIC/DECIMAL
 
115
     * PRECISION        => number; precision of NUMERIC/DECIMAL
 
116
     * UNSIGNED         => boolean; unsigned property of an integer type
 
117
     * PRIMARY          => boolean; true if column is part of the primary key
 
118
     * PRIMARY_POSITION => integer; position of column in primary key
 
119
     * IDENTITY         => integer; true if column is auto-generated with unique values
 
120
     *
 
121
     * @param string $tableName
 
122
     * @param string $schemaName OPTIONAL
 
123
     * @return array
 
124
     */
 
125
    public function describeTable($tableName, $schemaName = null)
 
126
    {
 
127
        // @todo  use INFORMATION_SCHEMA someday when MySQL's
 
128
        // implementation has reasonably good performance and
 
129
        // the version with this improvement is in wide use.
 
130
 
 
131
        if ($schemaName) {
 
132
            $sql = 'DESCRIBE ' . $this->quoteIdentifier("$schemaName.$tableName", true);
 
133
        } else {
 
134
            $sql = 'DESCRIBE ' . $this->quoteIdentifier($tableName, true);
 
135
        }
 
136
        $stmt = $this->query($sql);
 
137
 
 
138
        // Use FETCH_NUM so we are not dependent on the CASE attribute of the PDO connection
 
139
        $result = $stmt->fetchAll(Zend_Db::FETCH_NUM);
 
140
 
 
141
        $field   = 0;
 
142
        $type    = 1;
 
143
        $null    = 2;
 
144
        $key     = 3;
 
145
        $default = 4;
 
146
        $extra   = 5;
 
147
 
 
148
        $desc = array();
 
149
        $i = 1;
 
150
        $p = 1;
 
151
        foreach ($result as $row) {
 
152
            list($length, $scale, $precision, $unsigned, $primary, $primaryPosition, $identity)
 
153
                = array(null, null, null, null, false, null, false);
 
154
            if (preg_match('/unsigned/', $row[$type])) {
 
155
                $unsigned = true;
 
156
            }
 
157
            if (preg_match('/^((?:var)?char)\((\d+)\)/', $row[$type], $matches)) {
 
158
                $row[$type] = $matches[1];
 
159
                $length = $matches[2];
 
160
            } else if (preg_match('/^decimal\((\d+),(\d+)\)/', $row[$type], $matches)) {
 
161
                $row[$type] = 'decimal';
 
162
                $precision = $matches[1];
 
163
                $scale = $matches[2];
 
164
            } else if (preg_match('/^float\((\d+),(\d+)\)/', $row[$type], $matches)) {
 
165
                $row[$type] = 'float';
 
166
                $precision = $matches[1];
 
167
                $scale = $matches[2];
 
168
            } else if (preg_match('/^((?:big|medium|small|tiny)?int)\((\d+)\)/', $row[$type], $matches)) {
 
169
                $row[$type] = $matches[1];
 
170
                // The optional argument of a MySQL int type is not precision
 
171
                // or length; it is only a hint for display width.
 
172
            }
 
173
            if (strtoupper($row[$key]) == 'PRI') {
 
174
                $primary = true;
 
175
                $primaryPosition = $p;
 
176
                if ($row[$extra] == 'auto_increment') {
 
177
                    $identity = true;
 
178
                } else {
 
179
                    $identity = false;
 
180
                }
 
181
                ++$p;
 
182
            }
 
183
            $desc[$this->foldCase($row[$field])] = array(
 
184
                'SCHEMA_NAME'      => null, // @todo
 
185
                'TABLE_NAME'       => $this->foldCase($tableName),
 
186
                'COLUMN_NAME'      => $this->foldCase($row[$field]),
 
187
                'COLUMN_POSITION'  => $i,
 
188
                'DATA_TYPE'        => $row[$type],
 
189
                'DEFAULT'          => $row[$default],
 
190
                'NULLABLE'         => (bool) ($row[$null] == 'YES'),
 
191
                'LENGTH'           => $length,
 
192
                'SCALE'            => $scale,
 
193
                'PRECISION'        => $precision,
 
194
                'UNSIGNED'         => $unsigned,
 
195
                'PRIMARY'          => $primary,
 
196
                'PRIMARY_POSITION' => $primaryPosition,
 
197
                'IDENTITY'         => $identity
 
198
            );
 
199
            ++$i;
 
200
        }
 
201
        return $desc;
 
202
    }
 
203
 
 
204
    /**
 
205
     * Adds an adapter-specific LIMIT clause to the SELECT statement.
 
206
     *
 
207
     * @param  string $sql
 
208
     * @param  integer $count
 
209
     * @param  integer $offset OPTIONAL
 
210
     * @throws Zend_Db_Adapter_Exception
 
211
     * @return string
 
212
     */
 
213
     public function limit($sql, $count, $offset = 0)
 
214
     {
 
215
        $count = intval($count);
 
216
        if ($count <= 0) {
 
217
            /** @see Zend_Db_Adapter_Exception */
 
218
            require_once 'Zend/Db/Adapter/Exception.php';
 
219
            throw new Zend_Db_Adapter_Exception("LIMIT argument count=$count is not valid");
 
220
        }
 
221
 
 
222
        $offset = intval($offset);
 
223
        if ($offset < 0) {
 
224
            /** @see Zend_Db_Adapter_Exception */
 
225
            require_once 'Zend/Db/Adapter/Exception.php';
 
226
            throw new Zend_Db_Adapter_Exception("LIMIT argument offset=$offset is not valid");
 
227
        }
 
228
 
 
229
        $sql .= " LIMIT $count";
 
230
        if ($offset > 0) {
 
231
            $sql .= " OFFSET $offset";
 
232
        }
 
233
 
 
234
        return $sql;
 
235
    }
 
236
 
 
237
}