-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSqlAnaly.php
More file actions
251 lines (222 loc) · 8.14 KB
/
SqlAnaly.php
File metadata and controls
251 lines (222 loc) · 8.14 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
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
<?php
/**
* sql分析类
* User: duxing
* Email: duxing@taobao.com
* Date: 2013-1-22
*/
if(!class_exists('SqlAnaly')){
Class SqlAnaly {
public static function analyColumnLine($createTableSql) {
preg_match("/create\s*table\s*(?:if\s*not\s*exists)*\s*[^\(]*\s*\((.*)\)\s*[^\s\)]*\s*(?:ENGINE.*)/isU", $createTableSql, $matches);
if ($matches != "" && count($matches) == 2) {
return $matches;
} else {
return "";
}
}
public static function analyTableName($createTableSql) {
preg_match("/create\s*table.*([^\(\s]*)\s*\(/isU", $createTableSql, $matches);
if ($matches != "" && count($matches) == 2) {
return trim($matches[1], " `'\"");
} else {
return "";
}
}
public static function analyPrimaryKey($createTableSql) {
preg_match("/primary\s+key\s+\(([^\)]*)\)/isU", $createTableSql, $matches);
if ($matches != "" && count($matches) == 2) {
return trim($matches[1], " `'\"");
} else {
return "";
}
}
//某一行是否是索引行
public static function isKeyLine($line) {
preg_match("/\s*(primary|unique)*\s*key/isU", $line, $matchTmp);
if ($matchTmp != "" && count($matchTmp) > 0 && substr(trim($line), 0, 1) != '`') {
return true;
} else {
return false;
}
}
public static function analyColumnInfo($line) {
$typeMap = array(
/**
* mysql
*/
"TINYINT" => "Integer",
"SMALLINT" => "Integer",
"MEDIUMINT" => "Integer",
"INT" => "Long",
"BIGINT" => "Long",
"DECIMAL" => "BigDecimal",
"FLOAT" => "Float",
"DOUBLE" => "Double",
"REAL" => "Float",
"BIT" => "Boolean",
"BOOLEAN" => "Boolean",
"SERIAL" => "Long",
"DATE" => "Date",
"DATETIME" => "Date",
"TIMESTAMP" => "Long",
"TIME" => "String",
"YEAR" => "Integer",
"CHAR" => "String",
"VARCHAR" => "String",
"TINYTEXT" => "String",
"TEXT" => "String",
"MEDIUMTEXT" => "String",
"LONGTEXT" => "String",
"BINARY" => "String",
"VARBINARY" => "String",
//有待考验,blob里面一般存储字节流
"TINYBLOB" => "Object",
"MEDIUMBLOB" => "Object",
"BLOB" => "Object",
"LONGBLOB" => "Object",
"ENUM" => "String",
"SET" => "String",
/**
* oracle
*/
"NUMBER" => "Long",
"RAW" => "String"
);
$line.=" ";
$column = array();
// preg_match("/\s*[`]*([^\s]*)[`]*\s+([^\s]*)\s+(not|null|\s)*(comment)*.*/isU",$line,$matchTmp);
preg_match("/[`]*([^\s`]*)[`]*\s+([^\s]*)\s+[unsigned\s]*[not\s]*[null\s]*[default\s]*.*[auto_increament\s]*\s*(?:comment\s*'([^']*)')*/isU", $line, $matchTmp);
preg_match("/comment\s*'([^']*)'/isU", $line, $matchCommentTmp);
if ($matchTmp != "" && count($matchTmp) == 3) {
$column['name'] = strtolower($matchTmp[1]);
$column['typeStr'] = $matchTmp[2];
$sqlType = strtoupper(preg_replace("/\([^\)]*\)/isU", "", $column['typeStr']));
$column['type'] = isset($typeMap[$sqlType]) ? $typeMap[$sqlType] : "String";
if (count($matchCommentTmp) == 2) {
$column['comment'] = $matchCommentTmp[1];
}
}
return $column;
}
/**
* 删除注释中的逗号标记,换为全角
* @param $comment
* @return string
*/
public static function replaceComma($comment) {
return stripslashes(str_replace(",", ",", $comment));
}
/**
* @param $name
* 符合明明规范(驼峰式或者下划线分割)的名称去除下划线,使用空格分割
*/
public static function toWords($name) {
return str_replace("_", " ", $name);
}
public static function formatName($name) {
return str_replace(" ", "", ucwords(SqlAnaly::toWords(strtolower($name))));
}
public static function getClassName($name) {
return SqlAnaly::formatName($name)."DO";
}
public static function getClassDAOName($name) {
return SqlAnaly::formatName($name)."DAO";
}
public static function getClassDAOImplName($name) {
return SqlAnaly::formatName($name)."DAOImpl";
}
public static function getPropertyName($name) {
return lcfirst(SqlAnaly::formatName($name));
}
public static function getSqlMapName($name) {
return strtolower($name) . "_sqlmap";
}
/**
* 修正sql的注释,去除英文逗号影响
* @param $sql
* @return mixed
*/
public static function fixComment($sql) {
return preg_replace("/comment\s+'([^']*),([^']*)'/ies", 'SqlAnaly::replaceComma("\\0")', $sql);
}
public static function analy($createTableSql) {
$matches = SqlAnaly::analyColumnLine($createTableSql);
$matches_table = SqlAnaly::analyTableName($createTableSql);
$primaryKey = SqlAnaly::analyPrimaryKey($createTableSql);
$result = array();
$result[0] = false;
$result['table'] = $matches_table;
$result['primaryKey'] = $primaryKey;
if ($matches != "" && count($matches) == 2) {
$columnsPre = $matches[1];
$columnsPre = preg_replace("/\s+\n/isU", "\n", $columnsPre);
$columnAry = explode("\n", trim($columnsPre));
for ($i = 0; $i < count($columnAry); $i++) {
$line = trim($columnAry[$i]);
if (!SqlAnaly::isKeyLine($line) && $line != "") {
$columnInfo = SqlAnaly::analyColumnInfo($line);
//字段,数据库类型,java类型
if (count($columnInfo) >= 3) {
$result[0] = true;
if (!isset($result['columns'])) {
$result['columns'] = array();
}
$result['columns'][] = $columnInfo;
}
}
}
} else {
$result[1] = "程序不支持的sql!";
}
return $result;
}
public static function main() {
$sql = <<<EOF
-- phpMyAdmin SQL Dump
-- version 3.5.2
-- http://www.phpmyadmin.net
--
-- 主机: localhost
-- 生成日期: 2013 年 01 月 21 日 03:53
-- 服务器版本: 5.5.25a
-- PHP 版本: 5.4.4
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
--
-- 数据库: `web_transaction`
--
-- --------------------------------------------------------
--
-- 表的结构 `transaction2`
--
CREATE TABLE IF NOT EXISTS `transaction2` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '序号',
`app` varchar(20) NOT NULL COMMENT '应用',
`model` varchar(20) NOT NULL COMMENT '应用内的模块',
`key` varchar(50) NOT NULL COMMENT '事务自定义标记',
`resume_bean_name` varchar(50) NOT NULL COMMENT '事务补偿当前对象',
`resume_method` varchar(255) NOT NULL COMMENT '事务补偿方法',
`resume_parameter` longtext NOT NULL COMMENT '事务方法参数',
`exception` varchar(50) NOT NULL COMMENT '异常标记串',
`exception_info` varchar(255) NOT NULL COMMENT '异常信息',
`exception_stack` longtext NOT NULL COMMENT '异常堆栈',
`step` tinyint(4) NOT NULL COMMENT '事务进度',
`status` tinyint(4) NOT NULL COMMENT '事务状态',
`mark` varchar(32) NOT NULL COMMENT '当前事务标记,为各种信息md5结果',
`gmt_create` datetime NOT NULL COMMENT '创建时间',
`gmt_modified` datetime NOT NULL COMMENT '修改时间',
`is_deleted` tinyint(4) NOT NULL COMMENT '记录是否逻辑删除',
PRIMARY KEY (`id`),
UNIQUE KEY `mark_2` (`mark`),
KEY `mark` (`mark`),
KEY `app` (`app`,`model`,`key`,`mark`),
KEY `key` (`key`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='事务记录表' AUTO_INCREMENT=2 ;
EOF;
$matches = SqlAnaly::analy($sql);
var_dump($matches);
}
}
}
//SqlAnaly::main();