Pluf Framework

Pluf Framework Commit Details


Date:2008-11-07 03:12:37 (16 years 1 month ago)
Author:Loic d'Anterroches
Branch:develop, master
Commit:69938949b2ac73b7f63978ddf9f71741c5bf77ab
Parents: 38d90e17142e1f1a20ba75ee2c1b0d41d1c1cf83
Message:Fixed schema generation and indexes with correct quoting of the columns.

Changes:

File differences

src/Pluf/DB/Schema.php
9696
9797
9898
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
99122
}
return true;
}
/**
* Given a column name or a string with column names in the format
* "column1, column2, column3", returns the escaped correctly
* quoted column names. This is good for index creation.
*
* @param string Column
* @param Pluf_DB DB handler
* @return string Quoted for the DB column(s)
*/
public static function quoteColumn($col, $db)
{
if (false !== strpos($col, ',')) {
$cols = explode(',', $col);
} else {
$cols = array($col);
}
$res = array();
foreach ($cols as $col) {
$res[] = $db->qn(trim($col));
}
return implode(', ', $res);
}
}
src/Pluf/DB/Schema/PostgreSQL.php
158158
159159
160160
161
161162
162163
163164
164165
165
166
167
166168
167169
168170
......
171173
172174
173175
174
176
177
175178
176179
177180
} else {
$unique = '';
}
$index[$this->con->pfx.$model->_a['table'].'_'.$idx] =
sprintf('CREATE '.$unique.'INDEX %s ON %s (%s);',
$this->con->pfx.$model->_a['table'].'_'.$idx,
$this->con->pfx.$model->_a['table'],
$val['col']);
Pluf_DB_Schema::quoteColumn($val['col'], $this->con)
);
}
foreach ($model->_a['cols'] as $col => $val) {
$field = new $val['type']();
sprintf('CREATE UNIQUE INDEX %s ON %s (%s);',
$this->con->pfx.$model->_a['table'].'_'.$col.'_unique_idx',
$this->con->pfx.$model->_a['table'],
$col);
Pluf_DB_Schema::quoteColumn($col, $this->con)
);
}
}
return $index;
src/Pluf/DB/Schema/SQLite.php
158158
159159
160160
161
162
163
164
165
166
167
168
169
161
170162
171
163
164
172165
173166
174
167
168
175169
176170
177171
......
180174
181175
182176
183
177
184178
185179
186180
187181
188182
189183
190
184
185
191186
192187
193188
if (!isset($val['col'])) {
$val['col'] = $idx;
}
if (false !== strpos($val['col'], ',')) {
$out = array();
foreach (explode(',', $val['col']) as $col) {
$out[] = $this->con->qn(trim($col));
}
$val['col'] = implode(', ', $out);
} else {
$val['col'] = $this->con->qn($val['col']);
}
$unique = (isset($val['type']) && ($val['type'] == 'unique')) ? 'UNIQUE ' : '';
$index[$this->con->pfx.$model->_a['table'].'_'.$idx] =
sprintf('CREATE INDEX %s ON %s (%s);',
sprintf('CREATE %sINDEX %s ON %s (%s);',
$unique,
$this->con->pfx.$model->_a['table'].'_'.$idx,
$this->con->pfx.$model->_a['table'],
$val['col']);
Pluf_DB_Schema::quoteColumn($val['col'], $this->con)
);
}
foreach ($model->_a['cols'] as $col => $val) {
$field = new $val['type']();
sprintf('CREATE INDEX %s ON %s (%s);',
$this->con->pfx.$model->_a['table'].'_'.$col.'_foreignkey_idx',
$this->con->pfx.$model->_a['table'],
$this->con->qn($col));
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'],
$this->con->qn($col));
Pluf_DB_Schema::quoteColumn($col, $this->con)
);
}
}
return $index;
src/Pluf/Tests/Model/Schema.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
<?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 ***** */
class Pluf_Tests_Model_Schema_Model extends Pluf_Model
{
public $_model = __CLASS__;
function init()
{
$this->_a['verbose'] = 'compressed';
$this->_a['table'] = 'compressed';
$this->_a['model'] = __CLASS__;
$this->_a['cols'] = array(
// It is mandatory to have an "id" column.
'id' =>
array(
'type' => 'Pluf_DB_Field_Sequence',
//It is automatically added.
'blank' => true,
),
'column1' =>
array(
'type' => 'Pluf_DB_Field_Varchar',
),
'column2' =>
array(
'type' => 'Pluf_DB_Field_Varchar',
),
'column3' =>
array(
'type' => 'Pluf_DB_Field_Varchar',
),
);
$this->_a['idx'] = array(
'test_idx' =>
array(
'col' => 'column1, column2, column3',
'type' => 'unique',
),
);
}
}
class Pluf_Tests_Model_Schema extends UnitTestCase {
function __construct()
{
parent::__construct('Test the compressed field.');
}
function testCreate()
{
$db = Pluf::db();
$schema = new Pluf_DB_Schema($db);
$m = new Pluf_Tests_Model_Schema_Model();
$schema->model = $m;
$schema->createTables();
$m->column1 = 'Youplaboum';
$m->column2 = 'Youplaboum';
$m->column3 = 'Youplaboum';
$m->create();
$this->assertEqual(1, $m->id);
$m = new Pluf_Tests_Model_Schema_Model();
$m->column1 = 'Youplaboum';
$m->column2 = 'Youplaboum';
$m->column3 = 'Youplaboum';
try {
$m->create();
$this->assertNotEqual(2, $m->id, 'Should not be able to create.');
} catch (Exception $e) {
// do nothing
}
$schema->dropTables();
}
}
src/Pluf/conf/pluf.test.php
5353
5454
5555
56
56
5757
5858
5959
$cfg['db_login'] = 'testpluf';
$cfg['db_password'] = 'testpluf';
$cfg['db_server'] = 'localhost';
$cfg['db_database'] = $cfg['tmp_folder'].'/pluf.tmp.sqlite.db';
$cfg['db_database'] = ':memory:';
$cfg['app_base'] = '/testapp';
$cfg['url_format'] = 'simple';

Archive Download the corresponding diff file

Branches

Tags

Number of commits:
Page rendered in 0.07459s using 13 queries.