-
Notifications
You must be signed in to change notification settings - Fork 35
/
AppUtility.php
137 lines (117 loc) · 5.12 KB
/
AppUtility.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
<?php
/**
* Created by PhpStorm.
* User: user
* Date: 7/20/14
* Time: 8:28 PM
*/
namespace c006\utility\migration\assets;
use Yii;
class AppUtility
{
public $string = '';
private $Tab = "\t";
private $Nw = "\n";
private $array = [];
private $dbType = '';
private $schema;
function __construct($array, $databaseType)
{
$this->array = self::objectToArray($array);
switch (strtolower($databaseType)) {
case "mssql":
self::runMsSql();
break;
case "mysql":
self::runMySql();
break;
case "sqlite":
self::runSqlite();
break;
case "pgsql":
self::runPgSql();
break;
}
}
private function objectToArray($array_in)
{
$array = array();
if (is_object($array_in)) {
return self::objectToArray(get_object_vars($array_in));
} else {
if (is_array($array_in)) {
foreach ($array_in as $key => $value) {
if (is_object($value)) {
$array[$key] = self::objectToArray($value);
} elseif (is_array($value)) {
$array[$key] = self::objectToArray($value);
} else {
$array[$key] = $value;
}
}
}
}
return $array;
}
private function runMsSql()
{
if (isset($this->array['dbType']))
$this->string .= $this->Tab . "'{$this->array['name']}' => '" . strtoupper($this->array['dbType']) . "";
if (isset($this->array['autoIncrement']))
$this->string .= ($this->array['autoIncrement']) ? ' IDENTITY' : '';
if (isset($this->array['allowNull']))
$this->string .= ($this->array['allowNull']) ? ' NULL' : ' NOT NULL';
if (isset($this->array['defaultValue']))
$this->string .= (empty($this->array['defaultValue'])) ? '' : " DEFAULT \'{$this->array['defaultValue']}\'";
}
private function runMySql()
{
if (isset($this->array['dbType'])) {
if (strpos($this->array['dbType'], 'enum') !== FALSE) {
$this->array['dbType'] = str_replace('enum', 'ENUM', $this->array['dbType']);
$this->array['dbType'] = str_replace("'", "\\'", $this->array['dbType']);
$this->string .= $this->Tab . "'{$this->array['name']}' => '" . $this->array['dbType'] . "";
} elseif (strpos($this->array['dbType'], 'set') !== FALSE) {
$this->array['dbType'] = str_replace('set', 'SET', $this->array['dbType']);
$this->array['dbType'] = str_replace("'", "\\'", $this->array['dbType']);
$this->string .= $this->Tab . "'{$this->array['name']}' => '" . $this->array['dbType'] . "";
} else {
$this->string .= $this->Tab . "'{$this->array['name']}' => '" . strtoupper($this->array['dbType']) . "";
}
}
if (isset($this->array['allowNull']))
$this->string .= ($this->array['allowNull']) ? ' NULL' : ' NOT NULL';
if (isset($this->array['autoIncrement']))
$this->string .= ($this->array['autoIncrement']) ? ' AUTO_INCREMENT' : '';
if (isset($this->array['defaultValue']))
if (!is_array($this->array['defaultValue'])) {
$this->string .= (strlen($this->array['defaultValue']) == 0) ? '' : " DEFAULT \'{$this->array['defaultValue']}\'";
} else {
$this->string .= (strlen($this->array['defaultValue']) == 0) ? '' : " DEFAULT " . $this->array['defaultValue']['expression'] . " ";
}
if (isset($this->array['comment']))
$this->string .= (empty($this->array['comment'])) ? '' : " COMMENT \'{$this->array['comment']}\'";
}
private function runSqlite()
{
if (isset($this->array['dbType']))
$this->string .= $this->Tab . "'{$this->array['name']}' => '" . strtoupper($this->array['dbType']) . "";
if (isset($this->array['allowNull']))
$this->string .= ($this->array['allowNull']) ? ' NULL' : ' NOT NULL';
if (isset($this->array['autoIncrement']))
$this->string .= ($this->array['autoIncrement']) ? ' AUTOINCREMENT' : '';
if (isset($this->array['defaultValue']))
$this->string .= (empty($this->array['defaultValue'])) ? '' : " DEFAULT \'{$this->array['defaultValue']}\'";
}
private function runPgSql()
{
if (isset($this->array['dbType']))
$this->string .= $this->Tab . "'{$this->array['name']}' => '" . strtoupper($this->array['dbType']) . "";
if (isset($this->array['autoIncrement']))
$this->string .= ($this->array['autoIncrement']) ? ' SERIAL' : '';
if (isset($this->array['allowNull']))
$this->string .= ($this->array['allowNull']) ? ' NULL' : ' NOT NULL';
if (isset($this->array['defaultValue']))
$this->string .= (empty($this->array['defaultValue'])) ? '' : " DEFAULT \'{$this->array['defaultValue']}\'";
}
}