Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 *  $Id: Sqlite.php 7490 2010-03-29 19:53:27Z jwage $
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, see
19
 * <http://www.doctrine-project.org>.
20
 */
21
 
22
/**
23
 * @package     Doctrine
24
 * @subpackage  DataDict
25
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
26
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
27
 * @author      Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
28
 * @version     $Revision: 7490 $
29
 * @link        www.doctrine-project.org
30
 * @since       1.0
31
 */
32
class Doctrine_DataDict_Sqlite extends Doctrine_DataDict
33
{
34
    /**
35
     * Obtain DBMS specific SQL code portion needed to declare an text type
36
     * field to be used in statements like CREATE TABLE.
37
     *
38
     * @param array $field  associative array with the name of the properties
39
     *      of the field being declared as array indexes. Currently, the types
40
     *      of supported field properties are as follows:
41
     *
42
     *      length
43
     *          Integer value that determines the maximum length of the text
44
     *          field. If this argument is missing the field should be
45
     *          declared to have the longest length allowed by the DBMS.
46
     *
47
     *      default
48
     *          Text value to be used as default for this field.
49
     *
50
     *      notnull
51
     *          Boolean flag that indicates whether this field is constrained
52
     *          to not be set to null.
53
     * @author Lukas Smith (PEAR MDB2 library)
54
     * @return string  DBMS specific SQL code portion that should be used to
55
     *      declare the specified field.
56
     */
57
    public function getNativeDeclaration(array $field)
58
    {
59
        if ( ! isset($field['type'])) {
60
            throw new Doctrine_DataDict_Exception('Missing column type.');
61
        }
62
        switch ($field['type']) {
63
            case 'enum':
64
                $field['length'] = isset($field['length']) && $field['length'] ? $field['length']:255;
65
            case 'text':
66
            case 'object':
67
            case 'array':
68
            case 'string':
69
            case 'char':
70
            case 'gzip':
71
            case 'varchar':
72
                $length = (isset($field['length']) && $field['length']) ? $field['length'] : null;
73
 
74
                $fixed  = ((isset($field['fixed']) && $field['fixed']) || $field['type'] == 'char') ? true : false;
75
 
76
                return $fixed ? ($length ? 'CHAR('.$length.')' : 'CHAR('.$this->conn->varchar_max_length.')')
77
                    : ($length ? 'VARCHAR('.$length.')' : 'TEXT');
78
            case 'clob':
79
                if ( ! empty($field['length'])) {
80
                    $length = $field['length'];
81
                    if ($length <= 255) {
82
                        return 'TINYTEXT';
83
                    } elseif ($length <= 65535) {
84
                        return 'TEXT';
85
                    } elseif ($length <= 16777215) {
86
                        return 'MEDIUMTEXT';
87
                    }
88
                }
89
                return 'LONGTEXT';
90
            case 'blob':
91
                if ( ! empty($field['length'])) {
92
                    $length = $field['length'];
93
                    if ($length <= 255) {
94
                        return 'TINYBLOB';
95
                    } elseif ($length <= 65535) {
96
                        return 'BLOB';
97
                    } elseif ($length <= 16777215) {
98
                        return 'MEDIUMBLOB';
99
                    }
100
                }
101
                return 'LONGBLOB';
102
            case 'integer':
103
            case 'boolean':
104
            case 'int':
105
                return 'INTEGER';
106
            case 'date':
107
                return 'DATE';
108
            case 'time':
109
                return 'TIME';
110
            case 'timestamp':
111
                return 'DATETIME';
112
            case 'float':
113
            case 'double':
114
                return 'DOUBLE';//($this->conn->options['fixed_float'] ? '('.
115
                    //($this->conn->options['fixed_float']+2).','.$this->conn->options['fixed_float'].')' : '');
116
            case 'decimal':
117
                $length = !empty($field['length']) ? $field['length'] : 18;
118
                $scale = !empty($field['scale']) ? $field['scale'] : $this->conn->getAttribute(Doctrine_Core::ATTR_DECIMAL_PLACES);
119
                return 'DECIMAL('.$length.','.$scale.')';
120
        }
121
        return $field['type'] . (isset($field['length']) ? '('.$field['length'].')':null);
122
    }
123
 
124
    /**
125
     * Maps a native array description of a field to Doctrine datatype and length
126
     *
127
     * @param array  $field native field description
128
     * @return array containing the various possible types, length, sign, fixed
129
     */
130
    public function getPortableDeclaration(array $field)
131
    {
132
        $e = explode('(', $field['type']);
133
        $field['type'] = $e[0];
134
        if (isset($e[1])) {
135
            $length = trim($e[1], ')');
136
            $field['length'] = $length;
137
        }
138
 
139
        $dbType = strtolower($field['type']);
140
 
141
        if ( ! $dbType) {
142
            throw new Doctrine_DataDict_Exception('Missing "type" from field definition');
143
        }
144
 
145
        $length = (isset($field['length'])) ? $field['length'] : null;
146
        $unsigned = (isset($field['unsigned'])) ? $field['unsigned'] : null;
147
        $fixed = null;
148
        $type = array();
149
 
150
        if ( ! isset($field['name'])) {
151
            $field['name'] = '';
152
        }
153
 
154
        switch ($dbType) {
155
            case 'boolean':
156
                $type[] = 'boolean';
157
                break;
158
            case 'tinyint':
159
                $type[] = 'integer';
160
                $type[] = 'boolean';
161
                if (preg_match('/^(is|has)/', $field['name'])) {
162
                    $type = array_reverse($type);
163
                }
164
                $unsigned = preg_match('/ unsigned/i', $field['type']);
165
                $length = 1;
166
                break;
167
            case 'smallint':
168
                $type[] = 'integer';
169
                $unsigned = preg_match('/ unsigned/i', $field['type']);
170
                $length = 2;
171
                break;
172
            case 'mediumint':
173
                $type[] = 'integer';
174
                $unsigned = preg_match('/ unsigned/i', $field['type']);
175
                $length = 3;
176
                break;
177
            case 'int':
178
            case 'integer':
179
            case 'serial':
180
                $type[] = 'integer';
181
                $unsigned = preg_match('/ unsigned/i', $field['type']);
182
                $length = 4;
183
                break;
184
            case 'bigint':
185
            case 'bigserial':
186
                $type[] = 'integer';
187
                $unsigned = preg_match('/ unsigned/i', $field['type']);
188
                $length = 8;
189
                break;
190
            case 'clob':
191
            case 'tinytext':
192
            case 'mediumtext':
193
            case 'longtext':
194
            case 'text':
195
            case 'varchar':
196
            case 'varchar2':
197
            case 'nvarchar':
198
            case 'ntext':
199
            case 'image':
200
            case 'nchar':
201
                $fixed = false;
202
            case 'char':
203
                $type[] = 'text';
204
                if ($length == '1') {
205
                    $type[] = 'boolean';
206
                    if (preg_match('/^(is|has)/', $field['name'])) {
207
                        $type = array_reverse($type);
208
                    }
209
                } elseif (strstr($dbType, 'text')) {
210
                    $type[] = 'clob';
211
                }
212
                if ($fixed !== false) {
213
                    $fixed = true;
214
                }
215
                break;
216
            case 'date':
217
                $type[] = 'date';
218
                $length = null;
219
                break;
220
            case 'datetime':
221
            case 'timestamp':
222
                $type[] = 'timestamp';
223
                $length = null;
224
                break;
225
            case 'time':
226
                $type[] = 'time';
227
                $length = null;
228
                break;
229
            case 'float':
230
            case 'double':
231
            case 'real':
232
                $type[] = 'float';
233
                $length = null;
234
                break;
235
            case 'decimal':
236
            case 'numeric':
237
                $type[] = 'decimal';
238
                $length = null;
239
                break;
240
            case 'tinyblob':
241
            case 'mediumblob':
242
            case 'longblob':
243
            case 'blob':
244
                $type[] = 'blob';
245
                $length = null;
246
                break;
247
            case 'year':
248
                $type[] = 'integer';
249
                $type[] = 'date';
250
                $length = null;
251
                break;
252
            default:
253
                $type[] = $field['type'];
254
                $length = isset($field['length']) ? $field['length']:null;
255
        }
256
 
257
        return array('type'     => $type,
258
                     'length'   => $length,
259
                     'unsigned' => $unsigned,
260
                     'fixed'    => $fixed);
261
    }
262
 
263
    /**
264
     * Obtain DBMS specific SQL code portion needed to declare an integer type
265
     * field to be used in statements like CREATE TABLE.
266
     *
267
     * @param string  $name   name the field to be declared.
268
     * @param array  $field   associative array with the name of the properties
269
     *                        of the field being declared as array indexes.
270
     *                        Currently, the types of supported field
271
     *                        properties are as follows:
272
     *
273
     *                       unsigned
274
     *                        Boolean flag that indicates whether the field
275
     *                        should be declared as unsigned integer if
276
     *                        possible.
277
     *
278
     *                       default
279
     *                        Integer value to be used as default for this
280
     *                        field.
281
     *
282
     *                       notnull
283
     *                        Boolean flag that indicates whether this field is
284
     *                        constrained to not be set to null.
285
     * @return string  DBMS specific SQL code portion that should be used to
286
     *                 declare the specified field.
287
     * @access protected
288
     */
289
    public function getIntegerDeclaration($name, array $field)
290
    {
291
        $default = $autoinc = '';
292
        $type    = $this->getNativeDeclaration($field);
293
 
294
        $autoincrement = isset($field['autoincrement']) && $field['autoincrement'];
295
 
296
        if ($autoincrement) {
297
            $autoinc = ' PRIMARY KEY AUTOINCREMENT';
298
            $type    = 'INTEGER';
299
        } elseif (array_key_exists('default', $field)) {
300
            if ($field['default'] === '') {
301
                $field['default'] = empty($field['notnull']) ? null : 0;
302
            }
303
 
304
            $default = ' DEFAULT ' . (is_null($field['default'])
305
                ? 'NULL'
306
                : $this->conn->quote($field['default'], $field['type']));
307
        }/**
308
        elseif (empty($field['notnull'])) {
309
            $default = ' DEFAULT NULL';
310
        }
311
        */
312
 
313
        $notnull  = (isset($field['notnull']) && $field['notnull']) ? ' NOT NULL' : '';
314
 
315
        // sqlite does not support unsigned attribute for autoinremented fields
316
        $unsigned = (isset($field['unsigned']) && $field['unsigned'] && !$autoincrement) ? ' UNSIGNED' : '';
317
 
318
        $name = $this->conn->quoteIdentifier($name, true);
319
        return $name . ' ' . $type . $unsigned . $default . $notnull . $autoinc;
320
    }
321
}