-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
243 lines (191 loc) · 5.26 KB
/
index.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
<?php
/**
* all happen from here
*/
@date_default_timezone_set("Asia/Shanghai");
// composer autoload
require_once 'vendor/autoload.php';
// echo
function _e($msg)
{
echo ">>>>>";
echo "\n";
echo $msg;
echo "\n";
echo "<<<<<";
echo "\n";
die;
}
// debug
function aa($v)
{
echo "\n";
echo "<pre>";
var_dump($v);
echo "<pre>";
echo "\n";
die;
}
function bb($v)
{
echo "\n";
echo "<pre>";
var_dump($v);
echo "<pre>";
echo "\n";
}
if ($argc < 2) {
die('Usage: php index.php <PROJECT_NAME> [<TABLE_NAME>]' . "\n");
} elseif (!file_exists(dirname(__FILE__) . '/projects/' . $argv[1] . '.xlsx')) {
die('Excel 文件 ' . $argv[1] . " 不存在!\n");
}
$projectName = $argv[1];
// excel absolute filepath
$excelFilePath = './projects/' . $projectName . '.xlsx';
// sql filepath
$sqlFilePath = './projects/' . $projectName . '.sql';
// templates dir
$tplDir = "./tpl";
// tables
$tables = [];
// --------------------------------------------------
// Process excel
// load PHPExcel object
try {
$phpexcel = PHPExcel_IOFactory::load($excelFilePath);
} catch (PHPExcel_Reader_Exception $e) {
_e($e->getMessage());
}
// get first spreadsheet
$sheet = $phpexcel->getSheet(0);
// count rows
$cntRows = $sheet->getHighestDataRow();
// loop rows
for ($ri = 1; $ri <= $cntRows; $ri++) {
// table start
if ($sheet->getCell('B' . $ri)->getValue() == 'Table') {
$table = [];
$table['tableName'] = trim($sheet->getCell('C' . $ri)->getValue());
$table['tableNameCn'] = trim($sheet->getCell('D' . $ri)->getValue());
// skip the header rows
$ri += 2;
// loop attr rows
for (; $ri <= $cntRows; $ri++) {
// table end
if ($sheet->getCell('B' . $ri)->getValue() == '') {
$tables[] = $table;
break;
}
$tbCol = [
'col' => trim($sheet->getCell('B' . $ri)->getValue()),
'colCn' => trim($sheet->getCell('C' . $ri)->getValue()),
'type' => trim($sheet->getCell('D' . $ri)->getValue()),
'len' => trim($sheet->getCell('E' . $ri)->getValue()),
'key' => trim($sheet->getCell('F' . $ri)->getValue()),
];
$table['cols'][] = $tbCol;
}
}
}
// finish processing excel
// --------------------------------------------------
// PDO
try {
$conn = new PDO("mysql:dbname={$projectName};host=localhost", 'root', 'root');
} catch (PDOException $e) {
// _e($e->getMessage());
}
// --------------------------------------------------
// process sql file
// sql str
$sql = '';
// loop tables
foreach ($tables as $tb) {
$singleTableSql = '';
$singleTableSql .= globalSql1($tb['tableName'], $tb['tableNameCn']);
// loop cols
foreach ($tb['cols'] as $col) {
$singleTableSql .= colSql($col);
}
$singleTableSql .= globalSql2($tb['tableNameCn']);
if (in_array($tb['tableName'], $argv)) {
$stmt = $conn->prepare($singleTableSql);
$stmt->execute();
// $sqls = explode(';', $singleTableSql);
// foreach ($sqls as $s) {
// $stmt = $conn->prepare($s);
// $stmt->execute();
// }
}
$sql .= $singleTableSql;
}
file_put_contents($sqlFilePath, $sql);
/*
* Create sql1
*/
function globalSql1($tb, $tbCn)
{
$sql = '';
// COMMENT
$sql .= "# {$tbCn}" . "\n";
// DROP TABLE
$sql .= "DROP TABLE IF EXISTS `{$tb}`;" . "\n";
// CREATE TABLE START
$sql .= "CREATE TABLE `{$tb}` (" . "\n";
$sql .= "\t" . "`id` INT(10) NOT NULL AUTO_INCREMENT," . "\n";
$sql .= "\t" . "`add_time` DATETIME NOT NULL DEFAULT NOW()," . "\n";
$sql .= "\t" . "`update_time` DATETIME NOT NULL," . "\n";
$sql .= "\t" . "`status` TINYINT(1) NOT NULL DEFAULT 1," . "\n";
$sql .= "\n";
return $sql;
}
/*
* Create sql col
*/
function colSql($col)
{
$sql = '';
// tab
$sql .= "\t";
// column name
$sql .= str_pad("`{$col['col']}`", 20);
// type and length
if ($col['len']) {
$sql .= ' ' . str_pad("{$col['type']}({$col['len']})", 13);
} else {
// DATETIME etc.
$sql .= ' ' . str_pad("{$col['type']}", 13);
}
// NOT NULL
$sql .= " NOT NULL";
// DEFAULT
// if there is INT, regardless INT/TINYINT/MEDIUMINT/BIGINT,
// then it should be default 0
if (strpos($col['type'], 'INT') !== false) {
$sql .= " DEFAULT 0 ";
} elseif ($col['type'] == 'VARCHAR' || $col['type'] == 'TEXT') {
$sql .= " DEFAULT '' ";
} elseif ($col['type'] == 'DATETIME') {
$sql .= " ";
} elseif ($col['type'] == 'DECIMAL') {
$sql .= " DEFAULT 0 ";
}
// COMMENT
$sql .= " COMMENT '{$col['colCn']}'";
$sql .= "," . "\n";
return $sql;
}
/*
* Create sql2
*/
function globalSql2($tbCn)
{
$sql = "\n";
// PRIMARY KEY
$sql .= "\t" . "PRIMARY KEY(`id`)" . "\n";
// ENGINE=InnoDB MyISAM
$sql .= ") ENGINE=InnoDb CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci COMMENT '{$tbCn}';" . "\n\n\n";
return $sql;
}
// finish processing sql file
// --------------------------------------------------