Pluf Framework

Pluf Framework Git Source Tree


Root/src/Pluf/DB/Schema/SQLite.php

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
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# ***** END LICENSE BLOCK ***** */
 
/**
 * Generator of the schemas corresponding to a given model.
 *
 * This class is for SQLite, you can create a class on the same
 * model for another database engine.
 */
class Pluf_DB_Schema_SQLite
 
{
    /**
     * Mapping of the fields.
     */
    public $mappings = array(
                             'varchar' => 'varchar(%s)',
                             'sequence' => 'integer primary key autoincrement',
                             'boolean' => 'bool',
                             'date' => 'date',
                             'datetime' => 'datetime',
                             'file' => 'varchar(150)',
                             'manytomany' => null,
                             'foreignkey' => 'integer',
                             'text' => 'text',
                             'html' => 'text',
                             'time' => 'time',
                             'integer' => 'integer',
                             'email' => 'varchar(150)',
                             'password' => 'varchar(150)',
                             'float' => 'real',
                             'blob' => 'blob',
                             );
 
    public $defaults = array(
                             'varchar' => "''",
                             'sequence' => null,
                             'boolean' => 1,
                             'date' => 0,
                             'datetime' => 0,
                             'file' => "''",
                             'manytomany' => null,
                             'foreignkey' => 0,
                             'text' => "''",
                             'html' => "''",
                             'time' => 0,
                             'integer' => 0,
                             'email' => "''",
                             'password' => "''",
                             'float' => 0.0,
                             'blob' => "''",
                             );
    private $con = null;
 
    function __construct($con)
    {
        $this->con = $con;
    }
 
 
 
    /**
     * Get the SQL to generate the tables of the given model.
     *
     * @param Object Model
     * @return array Array of SQL strings ready to execute.
     */
    function getSqlCreate($model)
    {
        $tables = array();
        $cols = $model->_a['cols'];
        $manytomany = array();
        $query = 'CREATE TABLE '.$this->con->pfx.$model->_a['table'].' (';
        $sql_col = array();
        foreach ($cols as $col => $val) {
            $field = new $val['type']();
            if ($field->type != 'manytomany') {
                $sql = $this->con->qn($col).' ';
                $_tmp = $this->mappings[$field->type];
                if ($field->type == 'varchar') {
                    if (isset($val['size'])) {
                        $_tmp = sprintf($this->mappings['varchar'], $val['size']);
                    } else {
                        $_tmp = sprintf($this->mappings['varchar'], '150');
                    }
                }
                if ($field->type == 'float') {
                    if (!isset($val['max_digits'])) {
                        $val['max_digits'] = 32;
                    }
                    if (!isset($val['decimal_places'])) {
                        $val['decimal_places'] = 8;
                    }
                    $_tmp = sprintf($this->mappings['float'], $val['max_digits'], $val['decimal_places']);
                }
                $sql .= $_tmp;
                if (empty($val['is_null'])) {
                    $sql .= ' not null';
                }
                if (isset($val['default'])) {
                    $sql .= ' default '.$model->_toDb($val['default'], $col);
                } elseif ($field->type != 'sequence') {
                    $sql .= ' default '.$this->defaults[$field->type];
                }
                $sql_col[] = $sql;
            } else {
                $manytomany[] = $col;
            }
        }
        $query = $query."\n".implode(",\n", $sql_col)."\n".');';
        $tables[$this->con->pfx.$model->_a['table']] = $query;
 
        //Now for the many to many
        foreach ($manytomany as $many) {
            $omodel = new $cols[$many]['model']();
            $hay = array(strtolower($model->_a['model']), strtolower($omodel->_a['model']));
            sort($hay);
            $table = $hay[0].'_'.$hay[1].'_assoc';
            $sql = 'CREATE TABLE '.$this->con->pfx.$table.' (';
            $sql .= "\n".strtolower($model->_a['model']).'_id '.$this->mappings['foreignkey'].' default 0,';
            $sql .= "\n".strtolower($omodel->_a['model']).'_id '.$this->mappings['foreignkey'].' default 0,';
            $sql .= "\n".'primary key ('.strtolower($model->_a['model']).'_id, '.strtolower($omodel->_a['model']).'_id)';
            $sql .= "\n".');';
            $tables[$this->con->pfx.$table] = $sql;
        }
        return $tables;
    }
 
    /**
     * SQLite cannot add foreign key constraints to already existing tables,
     * so we skip their creation completely.
     *
     * @param Object Model
     * @return array
     */
    function getSqlCreateConstraints($model)
    {
        return array();
    }
 
    /**
     * Get the SQL to generate the indexes of the given model.
     *
     * @param Object Model
     * @return array Array of SQL strings ready to execute.
     */
    function getSqlIndexes($model)
    {
        $index = array();
        foreach ($model->_a['idx'] as $idx => $val) {
            if (!isset($val['col'])) {
                $val['col'] = $idx;
            }
            $unique =  (isset($val['type']) && ($val['type'] == 'unique')) ? 'UNIQUE ' : '';
            $index[$this->con->pfx.$model->_a['table'].'_'.$idx] =
                sprintf('CREATE %sINDEX %s ON %s (%s);',
                        $unique,
                        $this->con->pfx.$model->_a['table'].'_'.$idx,
                        $this->con->pfx.$model->_a['table'],
                        Pluf_DB_Schema::quoteColumn($val['col'], $this->con)
                        );
        }
        foreach ($model->_a['cols'] as $col => $val) {
            $field = new $val['type']();
            if ($field->type == 'foreignkey') {
                $index[$this->con->pfx.$model->_a['table'].'_'.$col.'_foreignkey'] =
                    sprintf('CREATE INDEX %s ON %s (%s);',
                            $this->con->pfx.$model->_a['table'].'_'.$col.'_foreignkey_idx',
                            $this->con->pfx.$model->_a['table'],
                            Pluf_DB_Schema::quoteColumn($col, $this->con));
            }
            if (isset($val['unique']) and $val['unique'] == true) {
                $index[$this->con->pfx.$model->_a['table'].'_'.$col.'_unique'] =
                    sprintf('CREATE UNIQUE INDEX %s ON %s (%s);',
                            $this->con->pfx.$model->_a['table'].'_'.$col.'_unique_idx',
                            $this->con->pfx.$model->_a['table'],
                            Pluf_DB_Schema::quoteColumn($col, $this->con)
                            );
            }
        }
        return $index;
    }
 
    /**
     * Get the SQL to drop the tables corresponding to the model.
     *
     * @param Object Model
     * @return string SQL string ready to execute.
     */
    function getSqlDelete($model)
    {
        $cols = $model->_a['cols'];
        $manytomany = array();
        $sql = array();
        $sql[] = 'DROP TABLE IF EXISTS '.$this->con->pfx.$model->_a['table'];
        foreach ($cols as $col => $val) {
            $field = new $val['type']();
            if ($field->type == 'manytomany') {
                $manytomany[] = $col;
            }
        }
 
        //Now for the many to many
        foreach ($manytomany as $many) {
            $omodel = new $cols[$many]['model']();
            $hay = array(strtolower($model->_a['model']), strtolower($omodel->_a['model']));
            sort($hay);
            $table = $hay[0].'_'.$hay[1].'_assoc';
            $sql[] = 'DROP TABLE IF EXISTS '.$this->con->pfx.$table;
        }
        return $sql;
    }
 
    /**
     * SQLite cannot drop foreign keys from existing tables,
     * so we skip their deletion completely.
     *
     * @param Object Model
     * @return array
     */
    function getSqlDeleteConstraints($model)
    {
        return array();
    }
}

Archive Download this file

Branches

Tags

Number of commits:
Page rendered in 0.09230s using 11 queries.