forked from onPHP/onphp-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateTableQuery.class.php
More file actions
90 lines (71 loc) · 2.25 KB
/
CreateTableQuery.class.php
File metadata and controls
90 lines (71 loc) · 2.25 KB
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
<?php
/***************************************************************************
* Copyright (C) 2006-2007 by Konstantin V. Arkhipov *
* *
* This program 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 3 of the *
* License, or (at your option) any later version. *
* *
***************************************************************************/
/**
* @ingroup OSQL
**/
final class CreateTableQuery extends QueryIdentification
{
private $table = null;
public function __construct(DBTable $table)
{
$this->table = $table;
}
public function toDialectString(Dialect $dialect)
{
$name = $this->table->getName();
$middle = "CREATE TABLE {$dialect->quoteTable($name)} (\n ";
$prepend = array();
$columns = array();
$primary = array();
$order = $this->table->getOrder();
foreach ($order as $column) {
if ($column->isAutoincrement()) {
if ($pre = $dialect->preAutoincrement($column))
$prepend[] = $pre;
$columns[] = implode(' ',
array(
$column->toDialectString($dialect),
$dialect->postAutoincrement($column)
)
);
} else
$columns[] = $column->toDialectString($dialect);
$name = $column->getName();
if ($column->isPrimaryKey())
$primary[] = $dialect->quoteField($name);
}
$out =
(
$prepend
? implode("\n", $prepend)."\n"
: null
)
.$middle
.implode(",\n ", $columns);
if ($primary)
$out .= ",\n PRIMARY KEY(".implode(', ', $primary).')';
if ($uniques = $this->table->getUniques()) {
$names = array();
foreach ($uniques as $row) {
foreach ($row as $name) {
$names[] = $dialect->quoteField($name);
}
$out .= ",\n UNIQUE(".implode(', ', $names).')';
}
}
$out .= "\n)";
if ($dialect instanceof MyDialect) {
$out .= ' TYPE=innodb ';
}
return $out.";\n";
}
}
?>