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
246
247
248
249
250
251
|
<?php
/*
@version v5.20.14 06-Jan-2019
@copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
@copyright (c) 2014 Damien Regad, Mark Newnham and the ADOdb community
Released under both BSD license and Lesser GPL library license.
Whenever there is any discrepancy between the two licenses,
the BSD license will take precedence.
Set tabs to 4 for best viewing.
*/
error_reporting(E_ALL);
include_once('../adodb.inc.php');
foreach(array('sapdb','sybase','mysql','access','oci8po','odbc_mssql','odbc','db2','firebird','postgres','informix') as $dbType) {
echo "<h3>$dbType</h3><p>";
$db = NewADOConnection($dbType);
$dict = NewDataDictionary($db);
if (!$dict) continue;
$dict->debug = 1;
$opts = array('REPLACE','mysql' => 'ENGINE=INNODB', 'oci8' => 'TABLESPACE USERS');
/* $flds = array(
array('id', 'I',
'AUTO','KEY'),
array('name' => 'firstname', 'type' => 'varchar','size' => 30,
'DEFAULT'=>'Joan'),
array('lastname','varchar',28,
'DEFAULT'=>'Chen','key'),
array('averylonglongfieldname','X',1024,
'NOTNULL','default' => 'test'),
array('price','N','7.2',
'NOTNULL','default' => '0.00'),
array('MYDATE', 'D',
'DEFDATE'),
array('TS','T',
'DEFTIMESTAMP')
);*/
$flds = "
ID I AUTO KEY,
FIRSTNAME VARCHAR(30) DEFAULT 'Joan' INDEX idx_name,
LASTNAME VARCHAR(28) DEFAULT 'Chen' key INDEX idx_name INDEX idx_lastname,
averylonglongfieldname X(1024) DEFAULT 'test',
price N(7.2) DEFAULT '0.00',
MYDATE D DEFDATE INDEX idx_date,
BIGFELLOW X NOTNULL,
TS_SECS T DEFTIMESTAMP,
TS_SUBSEC TS DEFTIMESTAMP
";
$sqla = $dict->CreateDatabase('KUTU',array('postgres'=>"LOCATION='/u01/postdata'"));
$dict->SetSchema('KUTU');
$sqli = ($dict->CreateTableSQL('testtable',$flds, $opts));
$sqla = array_merge($sqla,$sqli);
$sqli = $dict->CreateIndexSQL('idx','testtable','price,firstname,lastname',array('BITMAP','FULLTEXT','CLUSTERED','HASH'));
$sqla = array_merge($sqla,$sqli);
$sqli = $dict->CreateIndexSQL('idx2','testtable','price,lastname');//,array('BITMAP','FULLTEXT','CLUSTERED'));
$sqla = array_merge($sqla,$sqli);
$addflds = array(array('height', 'F'),array('weight','F'));
$sqli = $dict->AddColumnSQL('testtable',$addflds);
$sqla = array_merge($sqla,$sqli);
$addflds = array(array('height', 'F','NOTNULL'),array('weight','F','NOTNULL'));
$sqli = $dict->AlterColumnSQL('testtable',$addflds);
$sqla = array_merge($sqla,$sqli);
printsqla($dbType,$sqla);
if (file_exists('d:\inetpub\wwwroot\php\phplens\adodb\adodb.inc.php'))
if ($dbType == 'mysqlt') {
$db->Connect('localhost', "root", "", "test");
$dict->SetSchema('');
$sqla2 = $dict->ChangeTableSQL('adoxyz',$flds);
if ($sqla2) printsqla($dbType,$sqla2);
}
if ($dbType == 'postgres') {
if (@$db->Connect('localhost', "tester", "test", "test"));
$dict->SetSchema('');
$sqla2 = $dict->ChangeTableSQL('adoxyz',$flds);
if ($sqla2) printsqla($dbType,$sqla2);
}
if ($dbType == 'odbc_mssql') {
$dsn = $dsn = "PROVIDER=MSDASQL;Driver={SQL Server};Server=localhost;Database=northwind;";
if (@$db->Connect($dsn, "sa", "natsoft", "test"));
$dict->SetSchema('');
$sqla2 = $dict->ChangeTableSQL('adoxyz',$flds);
if ($sqla2) printsqla($dbType,$sqla2);
}
adodb_pr($dict->databaseType);
printsqla($dbType, $dict->DropColumnSQL('table',array('my col','`col2_with_Quotes`','A_col3','col3(10)')));
printsqla($dbType, $dict->ChangeTableSQL('adoxyz','LASTNAME varchar(32)'));
}
function printsqla($dbType,$sqla)
{
print "<pre>";
//print_r($dict->MetaTables());
foreach($sqla as $s) {
$s = htmlspecialchars($s);
print "$s;\n";
if ($dbType == 'oci8') print "/\n";
}
print "</pre><hr />";
}
/***
Generated SQL:
mysql
CREATE DATABASE KUTU;
DROP TABLE KUTU.testtable;
CREATE TABLE KUTU.testtable (
id INTEGER NOT NULL AUTO_INCREMENT,
firstname VARCHAR(30) DEFAULT 'Joan',
lastname VARCHAR(28) NOT NULL DEFAULT 'Chen',
averylonglongfieldname LONGTEXT NOT NULL,
price NUMERIC(7,2) NOT NULL DEFAULT 0.00,
MYDATE DATE DEFAULT CURDATE(),
PRIMARY KEY (id, lastname)
)TYPE=ISAM;
CREATE FULLTEXT INDEX idx ON KUTU.testtable (firstname,lastname);
CREATE INDEX idx2 ON KUTU.testtable (price,lastname);
ALTER TABLE KUTU.testtable ADD height DOUBLE;
ALTER TABLE KUTU.testtable ADD weight DOUBLE;
ALTER TABLE KUTU.testtable MODIFY COLUMN height DOUBLE NOT NULL;
ALTER TABLE KUTU.testtable MODIFY COLUMN weight DOUBLE NOT NULL;
--------------------------------------------------------------------------------
oci8
CREATE USER KUTU IDENTIFIED BY tiger;
/
GRANT CREATE SESSION, CREATE TABLE,UNLIMITED TABLESPACE,CREATE SEQUENCE TO KUTU;
/
DROP TABLE KUTU.testtable CASCADE CONSTRAINTS;
/
CREATE TABLE KUTU.testtable (
id NUMBER(16) NOT NULL,
firstname VARCHAR(30) DEFAULT 'Joan',
lastname VARCHAR(28) DEFAULT 'Chen' NOT NULL,
averylonglongfieldname CLOB NOT NULL,
price NUMBER(7,2) DEFAULT 0.00 NOT NULL,
MYDATE DATE DEFAULT TRUNC(SYSDATE),
PRIMARY KEY (id, lastname)
)TABLESPACE USERS;
/
DROP SEQUENCE KUTU.SEQ_testtable;
/
CREATE SEQUENCE KUTU.SEQ_testtable;
/
CREATE OR REPLACE TRIGGER KUTU.TRIG_SEQ_testtable BEFORE insert ON KUTU.testtable
FOR EACH ROW
BEGIN
select KUTU.SEQ_testtable.nextval into :new.id from dual;
END;
/
CREATE BITMAP INDEX idx ON KUTU.testtable (firstname,lastname);
/
CREATE INDEX idx2 ON KUTU.testtable (price,lastname);
/
ALTER TABLE testtable ADD (
height NUMBER,
weight NUMBER);
/
ALTER TABLE testtable MODIFY(
height NUMBER NOT NULL,
weight NUMBER NOT NULL);
/
--------------------------------------------------------------------------------
postgres
AlterColumnSQL not supported for PostgreSQL
CREATE DATABASE KUTU LOCATION='/u01/postdata';
DROP TABLE KUTU.testtable;
CREATE TABLE KUTU.testtable (
id SERIAL,
firstname VARCHAR(30) DEFAULT 'Joan',
lastname VARCHAR(28) DEFAULT 'Chen' NOT NULL,
averylonglongfieldname TEXT NOT NULL,
price NUMERIC(7,2) DEFAULT 0.00 NOT NULL,
MYDATE DATE DEFAULT CURRENT_DATE,
PRIMARY KEY (id, lastname)
);
CREATE INDEX idx ON KUTU.testtable USING HASH (firstname,lastname);
CREATE INDEX idx2 ON KUTU.testtable (price,lastname);
ALTER TABLE KUTU.testtable ADD height FLOAT8;
ALTER TABLE KUTU.testtable ADD weight FLOAT8;
--------------------------------------------------------------------------------
odbc_mssql
CREATE DATABASE KUTU;
DROP TABLE KUTU.testtable;
CREATE TABLE KUTU.testtable (
id INT IDENTITY(1,1) NOT NULL,
firstname VARCHAR(30) DEFAULT 'Joan',
lastname VARCHAR(28) DEFAULT 'Chen' NOT NULL,
averylonglongfieldname TEXT NOT NULL,
price NUMERIC(7,2) DEFAULT 0.00 NOT NULL,
MYDATE DATETIME DEFAULT GetDate(),
PRIMARY KEY (id, lastname)
);
CREATE CLUSTERED INDEX idx ON KUTU.testtable (firstname,lastname);
CREATE INDEX idx2 ON KUTU.testtable (price,lastname);
ALTER TABLE KUTU.testtable ADD
height REAL,
weight REAL;
ALTER TABLE KUTU.testtable ALTER COLUMN height REAL NOT NULL;
ALTER TABLE KUTU.testtable ALTER COLUMN weight REAL NOT NULL;
--------------------------------------------------------------------------------
*/
echo "<h1>Test XML Schema</h1>";
$ff = file('xmlschema.xml');
echo "<pre>";
foreach($ff as $xml) echo htmlspecialchars($xml);
echo "</pre>";
include_once('test-xmlschema.php');
|