-
Notifications
You must be signed in to change notification settings - Fork 0
/
doctrine1.php
462 lines (388 loc) · 12.5 KB
/
doctrine1.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
<?php
/**
* Set of functions used to build Doctrine 1.2 YAML dumps of tables
*
* @package phpMyAdmin-Export-Doctrine1 PHP
*
* @author jerome Erasmus 2011 www.developermill.com
*/
if (! defined('PHPMYADMIN')) {
exit;
}
/**
* This gets executed twice so avoid a notice
*/
if (! defined('D1_FORMAT_PHP')) {
define("D1_FORMAT_PHP", "Doctrine 1.2 YAML");
define("D1_HANDLER_PHP_BODY", "handleDoctrineBody");
}
$PARSE_FORMATS = array(D1_FORMAT_PHP);
$PARSE_HANDLERS = array(D1_HANDLER_PHP_BODY);
$YAML_dataTypes;
/**
* Export Options
*/
if (isset($plugin_list)) {
$plugin_list['doctrine1'] = array(
'text' => 'Doctrine 1.2',
'extension' => '.php.txt',
'mime_type' => 'text/php',
'options' => array(
array('type' => 'hidden', 'name' => 'data'),
array('type' => 'select', 'name' => 'format', 'text' => 'strFormat', 'values' => $PARSE_FORMATS),
),
'options_text' => 'strOptions',
);
} else {
/**
* Set of functions used to build exports of tables
*/
/**
* Outputs comment
*
* @param string Text of comment
*
* @return bool Whether it suceeded
*/
function PMA_exportComment($text)
{
return TRUE;
}
/**
* Outputs export footer
*
* @return bool Whether it suceeded
*
* @access public
*/
function PMA_exportFooter()
{
return TRUE;
}
/**
* Outputs export header
*
* @return bool Whether it suceeded
*
* @access public
*
abstract Whether or not to make the generated class abstract. Defaults to false. When a class is abstract it is not exported to the database.
className Name of the class to generate
tableName Name of the table in your DBMS to use.
connection Name of the Doctrine_Connection instance to bind the model to.
columns Column definitions.
relations Relationship definitions.
indexes Index definitions.
attributes Attribute definitions.
actAs ActAs definitions.
options Option definitions.
inheritance Array for inheritance definition
listeners Array defining listeners to attach
checks Checks to run at application level as well as exporting to your DBMS
*/
function PMA_exportHeader()
{
$str = "";
return PMA_exportOutputHandler($str);
}
/**
* Outputs database header
*
* @param string Database name
*
* @return bool Whether it suceeded
*
* @access public
*/
function PMA_exportDBHeader($db)
{
return TRUE;
}
/**
* Outputs database footer
*
* @param string Database name
*
* @return bool Whether it suceeded
*
* @access public
*/
function PMA_exportDBFooter($db)
{
return TRUE;
}
/**
* Outputs create database database
*
* @param string Database name
*
* @return bool Whether it suceeded
*
* @access public
*/
function PMA_exportDBCreate($db)
{
return TRUE;
}
/**
* Outputs the content of a table in Doctrine 2 format
*
* @param string the database name
* @param string the table name
* @param string the end of line sequence
* @param string the url to go back in case of error
* @param string SQL query for obtaining data
*
* @return bool Whether it suceeded
*
* @access public
*/
function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
{
global $PARSE_FORMATS, $PARSE_HANDLERS;
$format = cgGetOption("format");
$index = array_search($format, $PARSE_FORMATS);
if ($index >= 0)
{
$str = '';
$str .= PMA_exportOutputHandler($PARSE_HANDLERS[$index]($db, $table, $crlf));
}
return $str;
return PMA_exportOutputHandler(sprintf("%s is not supported.", $format));
}
/*
* ---- YAML COLUMN SCHEMA ----
name Name of the column.
fixed Whether or not the column is fixed.
primary Whether or not the column is a part of the primary key.
autoincrement Whether or not the column is an autoincrement column.
type Doctrine data type of the column
length Length of the column
default Default value of the column
scale Scale of the column. Used for the decimal type.
values List of values for the enum type.
comment Comment for the column.
sequence Sequence definition for column.
zerofill Whether or not to make the column fill empty characters with zeros
extra Array of extra information to store with the column definition
unsigned Unsigned modifiers for some field definitions, although not all DBMS's support this modifier for integer field types.
*/
/*
* ---- YAML DataType SCHEMA ----
* Below creates the available column types that can is used for the YAML data Type conversion
* as well as the type it is translated to when using the MySQL
*
*/
function createYAML_dataTypeSchema()
{
$type = array();
$type['integer'] = 'integer';
$type['tinyint'] = 'integer';
$type['smallint'] = 'integer';
$type['mediumint'] = 'integer';
$type['int'] = 'integer';
$type['bigint'] = 'integer';
$type['double'] = 'float'; // ? correct schema?
// $type['double'] = 'double'; // ? correct schema?
$type['decimal'] = 'decimal';
$type['char'] = 'char';
// $type['varchar'] = 'varchar'; // ? correct schema?
$type['varchar'] = 'string'; // ? correct schema?
$type['text'] = 'array'; // ? correct schema?
// $type['text'] = 'object'; // ? correct schema?
$type['longblob'] = 'blob';
$type['tinyblob'] = 'blob';
$type['blob'] = 'blob';
$type['mediumblob'] = 'blob';
$type['longtext'] = 'clob';
$type['tinytext'] = 'clob';
$type['text'] = 'clob';
$type['mediumtext'] = 'clob';
$type['datetime'] = 'timestamp';
$type['time'] = 'time';
$type['date'] = 'date';
// $type['text'] = 'gzip'; // ? correct schema?
// $type['tinyint(1)'] = 'boolean'; // ? correct schema?
$type['bit'] = 'bit';
//varbit ?
//inet ?
//enum ?
return $type;
}
/**
*
* @package phpMyAdmin-Export-Doctrine
*/
class TableProperty
{
public $fields;
function __construct($rowObj)
{
$this->fields = new stdClass();
$this->fields->name = $this->mapEntity('COLUMN_NAME', 'name', $rowObj, false);
$this->fields->fixed = $this->mapEntity('', 'fixed', $rowObj, false);
$this->fields->primary = $this->mapEntity('COLUMN_KEY', 'primary', $rowObj);
$this->fields->autoincrement = $this->mapEntity('EXTRA', 'autoincrement', $rowObj);
$this->fields->type = $this->mapEntity('DATA_TYPE', 'type', $rowObj);
$this->fields->length = $this->mapEntity('COLUMN_TYPE', 'length', $rowObj, false);
$this->fields->default = $this->mapEntity('COLUMN_DEFAULT', 'default', $rowObj);
$this->fields->scale = $this->mapEntity('NUMERIC_SCALE', 'scale', $rowObj);
$this->fields->values = $this->mapEntity('', 'enum', $rowObj, false); // ??? needs identification
$this->fields->comment = $this->mapEntity('COLUMN_COMMENT', 'comment', $rowObj);
$this->fields->sequence = $this->mapEntity('', 'sequence', $rowObj, false); // ??? needs identification
$this->fields->zerofill = $this->mapEntity('', 'zerofill', $rowObj, false); // ??? needs identification
$this->fields->extra = $this->mapEntity('EXTRA', 'extra', $rowObj);
$this->fields->unsigned = $this->mapEntity('', 'unsigned', $rowObj, false); // ??? needs identification
$this->fields->nullable = $this->mapEntity('IS_NULLABLE', 'nullable', $rowObj);
}
function mapEntity($columnName, $columnSchemaName, $entityObj, $include=true)
{
global $YAML_dataTypes;
$prop = new stdClass();
$prop->schemaName = $columnSchemaName;
$prop->include = $include;
switch($columnSchemaName)
{
case "type":
$prop->schemaVal = $YAML_dataTypes[$entityObj[$columnName]];
break;
case "length":
$prop->schemaVal = $this->getPureLength($entityObj[$columnName]);
break;
case "autoincrement":
$prop->schemaVal = $entityObj[$columnName] == "auto_increment" ? "true" : "";
break;
case "primary":
$prop->schemaVal = $entityObj[$columnName] == "PRI" ? "true" : "";
break;
case "nullable":
$prop->schemaVal = $entityObj[$columnName] == "YES" ? "true" : "false";
break;
case "extra":
if($entityObj[$columnName] == "auto_increment")
$prop->include = false;
break;
default:
$prop->schemaVal = $entityObj[$columnName];
break;
}
return $prop;
}
function getPureLength($string)
{
$sS = strrpos($string , '(');
$sE = strrpos($string , ')');
if($sS && $sE)
return substr($string , $sS+1, ($sE-$sS)-1);
return '';
}
/*
* INSERT COLUMN
*
* @param boolean use verbose syntax
*/
function insertColumn($useVerboseSyntax=true)
{
$lines = array();
$lines[] = $this->fields->name->schemaVal.":";
foreach($this->fields as $field => $val)
{
if(!empty($val->schemaVal) && $val->include == true)
{
$str = " ".$val->schemaName.": ".$val->schemaVal;
// append verbose length to type field e.g. varchar(255)
if($useVerboseSyntax && $val->schemaName == 'type')
{
if(!empty($this->fields->length->schemaVal))
$str .= '('.$this->fields->length->schemaVal.')';
}
$lines[] = $str;
}
}
return implode("\n", $lines);
}
}
/*
* ============================================================================
*/
function handleDoctrineBody($db, $table, $crlf)
{
global $YAML_dataTypes;
$lines=array();
/*
* Doctrine offers the ability to specify schema in an abbreviated syntax.
*
* If verbose is set to false, a lot of the schema parameters have values they default to,
* this allows us to abbreviate the syntax and let Doctrine just use its defaults.
*
* If verbose is set to true ALL schema parameters will be included. This is recomended!
*/
$useVerboseSyntax = true;
// create schema
$YAML_dataTypes = createYAML_dataTypeSchema();
// build header
if(!$useVerboseSyntax)
{
// $lines[] = "detect_relations: true\n";
}
// build body
$sqlQuery = "SELECT * FROM information_schema.columns WHERE TABLE_SCHEMA = '$db' AND TABLE_NAME = '$table'";
$result=PMA_DBI_query($sqlQuery);
if ($result)
{
$tableProperties=array();
while ($rowObj = PMA_DBI_fetch_assoc($result))
{
$tableProperties[] = new TableProperty($rowObj);// $lines[] = print_r($rowObj);
}
// insert table Class name
$tableClass = toCamelCase($table, true);
$lines[] = $tableClass.":";
// insert table name
$lines[] = " tableName: ".$table;
//insert columns
$lines[] = " options:\n type: INNODB\n";
// insert columns
$lines[] = " columns:";
foreach ($tableProperties as $tablePropertie)
{
// $lines[] = print_r($tablePropertie);
$lines[] = " ".$tablePropertie->insertColumn($useVerboseSyntax);
}
$lines[] = "\n";
if($useVerboseSyntax)
{
// DO relations here
}
PMA_DBI_free_result($result);
}
return implode("\n", $lines);
}
/**
* Translates a camel case string into a string with underscores
*/
function fromCamelCase($str)
{
$str[0] = strtolower($str[0]);
$func = create_function('$c', 'return "_" . strtolower($c[1]);');
return preg_replace_callback('/([A-Z])/', $func, $str);
}
/**
* Translates a string with underscores into camel case
*/
function toCamelCase($str, $capFirstChar = false)
{
$str = strtolower($str);
if($capFirstChar) {
$str[0] = strtoupper($str[0]);
}
$func = create_function('$c', 'return strtoupper($c[1]);');
return preg_replace_callback('/_([a-z])/', $func, $str);
}
function cgGetOption($optionName)
{
global $what;
return $GLOBALS[$what . "_" . $optionName];
}
}
?>