Skip to content

Commit e527afd

Browse files
committed
TimeZone may not be set.
1 parent 30e717a commit e527afd

File tree

9 files changed

+155
-55
lines changed

9 files changed

+155
-55
lines changed

examples/UnitTest/source/test/MySqlSelectTest_1.d

Lines changed: 122 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,102 @@ import std.traits;
1010
import test.base;
1111
import std.string;
1212

13+
public class MySqlSelectTest_1 {
14+
15+
void test() {
16+
testLike();
17+
// testLeftJoinQuery();
18+
// testInSubQuery();
19+
// test_0();
20+
// test_1();
21+
}
22+
23+
void testLike() {
24+
string sql = "select a from UserInfo a where id::text like '12%' ";
25+
26+
PGSQLStatementParser parser = new PGSQLStatementParser(sql);
27+
List!SQLStatement statementList = parser.parseStatementList();
28+
SQLStatement statemen = statementList.get(0);
29+
30+
PGSchemaStatVisitor visitor = new PGSchemaStatVisitor();
31+
statemen.accept(visitor);
1332

14-
public class MySqlSelectTest_1 {
33+
// trace("Tables : " ~ visitor.getTables().toString());
34+
// trace("fields : " ~ visitor.getColumns().to!string());
35+
// trace("coditions : " ~ visitor.getConditions().toString());
36+
37+
SQLSelectStatement selStmt = cast(SQLSelectStatement) statemen;
38+
SQLSelectQueryBlock queryBlock = selStmt.getSelect().getQueryBlock();
39+
SQLExpr sqlExpr = queryBlock.getWhere();
40+
string w = SQLUtils.toSQLString(queryBlock);
41+
42+
trace(w);
43+
44+
}
45+
46+
private void testLeftJoinQuery() {
1547

16-
public void test_0() {
48+
mixin(DO_TEST);
49+
50+
string sql = "select a, b from UserInfo a left join Car b on a.id = b.uid " ~
51+
" where a.age = 5";
52+
53+
MySqlStatementParser parser = new MySqlStatementParser(sql);
54+
List!SQLStatement statementList = parser.parseStatementList();
55+
SQLStatement statemen = statementList.get(0);
56+
// print(statementList);
57+
SQLSelectStatement stmt = cast(SQLSelectStatement) statemen;
58+
SQLSelectQueryBlock queryBlock = stmt.getSelect().getQueryBlock();
59+
foreach (selectItem; queryBlock.getSelectList()) {
60+
logDebug(" select : ( %s , %s ) ".format(SQLUtils.toSQLString(selectItem.getExpr()), selectItem.getAlias()));
61+
}
62+
63+
assert(1 == statementList.size());
64+
65+
SQLJoinTableSource tableSource = cast(SQLJoinTableSource) queryBlock.getFrom();
66+
SQLExprTableSource exprTableSource = cast(SQLExprTableSource) tableSource.getLeft();
67+
string name = exprTableSource.getAlias();
68+
warning("alias: ", name);
69+
assert(name == "a");
70+
71+
SQLExpr sqlExpr = queryBlock.getWhere();
72+
string tableAlias = tableSource.getAlias();
73+
}
74+
75+
private void testInSubQuery() {
76+
77+
mixin(DO_TEST);
78+
79+
string sql = "select columnName from table1 where id in (select id from table3 where name = ?)";
80+
// string sql = "SELECT t1.name as tname, t2.salary FROM employee t1, info t2 WHERE t1.name = t2.name order by name desc;";
81+
82+
MySqlStatementParser parser = new MySqlStatementParser(sql);
83+
List!SQLStatement statementList = parser.parseStatementList();
84+
SQLStatement statemen = statementList.get(0);
85+
// print(statementList);
86+
SQLSelectStatement stmt = cast(SQLSelectStatement) statemen;
87+
SQLSelectQueryBlock queryBlock = stmt.getSelect().getQueryBlock();
88+
foreach (selectItem; queryBlock.getSelectList()) {
89+
logDebug(" select : ( %s , %s ) ".format(SQLUtils.toSQLString(selectItem.getExpr()), selectItem.getAlias()));
90+
}
91+
92+
assert(1 == statementList.size());
93+
94+
SQLInSubQueryExpr subQueryExpr = cast(SQLInSubQueryExpr) queryBlock.getWhere();
95+
assert(subQueryExpr !is null);
96+
string w = SQLUtils.toSQLString(subQueryExpr);
97+
tracef("where: %s", w);
98+
99+
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
100+
statemen.accept(visitor);
101+
102+
logDebug("Tables : ", visitor.getTables());
103+
logDebug("fields : ", visitor.getColumns());
104+
logDebug("coditions : ", visitor.getConditions());
105+
logDebug("orderBy : ", visitor.getOrderByColumns());
106+
}
107+
108+
public void test_0() {
17109
mixin(DO_TEST);
18110

19111
string sql = "SELECT t1.name as tname, t2.salary FROM employee t1, info t2 WHERE t1.name = t2.name order by name desc;";
@@ -22,21 +114,21 @@ public class MySqlSelectTest_1 {
22114
List!SQLStatement statementList = parser.parseStatementList();
23115
SQLStatement statemen = statementList.get(0);
24116
// print(statementList);
25-
SQLSelectStatement stmt = cast(SQLSelectStatement)statemen ;
117+
SQLSelectStatement stmt = cast(SQLSelectStatement) statemen;
26118
SQLSelectQueryBlock queryBlock = stmt.getSelect().getQueryBlock();
27-
foreach(selectItem; queryBlock.getSelectList()) {
28-
logDebug(" selcet : ( %s , %s ) ".format(SQLUtils.toSQLString(selectItem.getExpr()),selectItem.getAlias()));
119+
foreach (selectItem; queryBlock.getSelectList()) {
120+
logDebug(" select : ( %s , %s ) ".format(SQLUtils.toSQLString(selectItem.getExpr()), selectItem.getAlias()));
29121
}
30122

31123
assert(1 == statementList.size());
32124

33125
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
34126
statemen.accept(visitor);
35127

36-
logDebug("Tables : " , visitor.getTables());
37-
logDebug("fields : " , visitor.getColumns());
38-
logDebug("coditions : " , visitor.getConditions());
39-
logDebug("orderBy : " , visitor.getOrderByColumns());
128+
logDebug("Tables : ", visitor.getTables());
129+
logDebug("fields : ", visitor.getColumns());
130+
logDebug("coditions : ", visitor.getConditions());
131+
logDebug("orderBy : ", visitor.getOrderByColumns());
40132

41133
assert(2 == visitor.getTables().size());
42134
assert(3 == visitor.getColumns().length);
@@ -45,12 +137,12 @@ public class MySqlSelectTest_1 {
45137
assert(visitor.getTables().containsKey(new TableStat.Name("employee")));
46138
assert(visitor.getTables().containsKey(new TableStat.Name("info")));
47139

48-
assert(search(visitor.getColumns(),new TableStat.Column("employee", "name")) != -1);
49-
assert(search(visitor.getColumns(),new TableStat.Column("info", "name")) != -1);
140+
assert(search(visitor.getColumns(), new TableStat.Column("employee", "name")) != -1);
141+
assert(search(visitor.getColumns(), new TableStat.Column("info", "name")) != -1);
50142
assert(search(visitor.getColumns(), new TableStat.Column("info", "salary")) != -1);
51143
}
52144

53-
public void test_1() {
145+
public void test_1() {
54146
mixin(DO_TEST);
55147

56148
string sql = "SELECT t1, t2.salary FROM employee t1 left join t1.add t2 where t1.id > 0 and t1.name = 'gxc' order by t1.id desc;"; //on t1.name = t2.name
@@ -60,46 +152,44 @@ public class MySqlSelectTest_1 {
60152
List!SQLStatement statementList = parser.parseStatementList();
61153
SQLStatement statemen = statementList.get(0);
62154
// print(statementList);
63-
SQLSelectStatement stmt = cast(SQLSelectStatement)statemen ;
155+
SQLSelectStatement stmt = cast(SQLSelectStatement) statemen;
64156
SQLSelectQueryBlock queryBlock = stmt.getSelect().getQueryBlock();
65-
66-
foreach(selectItem; queryBlock.getSelectList()) {
67-
logDebug(" selcet : ( %s , %s ) ".format(SQLUtils.toSQLString(selectItem.getExpr()),selectItem.getAlias()));
157+
158+
foreach (selectItem; queryBlock.getSelectList()) {
159+
logDebug(" select : ( %s , %s ) ".format(SQLUtils.toSQLString(selectItem.getExpr()), selectItem.getAlias()));
68160
}
69-
161+
70162
auto whe = SQLUtils.toSQLString(queryBlock.getWhere());
71-
logDebug("where : ",whe);
72-
logDebug("order : ",SQLUtils.toSQLString(queryBlock.getOrderBy()));
73-
foreach(item ; queryBlock.getOrderBy().getItems)
74-
{
163+
logDebug("where : ", whe);
164+
logDebug("order : ", SQLUtils.toSQLString(queryBlock.getOrderBy()));
165+
foreach (item; queryBlock.getOrderBy().getItems) {
75166
logDebug("order item : %s".format(SQLUtils.toSQLString(item.getExpr())));
76-
item.replace(item.getExpr(),SQLUtils.toSQLExpr("t1.name"));
167+
item.replace(item.getExpr(), SQLUtils.toSQLExpr("t1.name"));
77168
}
78-
79169

80170
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
81171
statemen.accept(visitor);
82172

83-
logDebug("Tables : " , visitor.getTables());
84-
logDebug("fields : " , visitor.getColumns());
85-
logDebug("coditions : " , visitor.getConditions());
86-
logDebug("orderBy : " , visitor.getOrderByColumns());
173+
logDebug("Tables : ", visitor.getTables());
174+
logDebug("fields : ", visitor.getColumns());
175+
logDebug("coditions : ", visitor.getConditions());
176+
logDebug("orderBy : ", visitor.getOrderByColumns());
87177

88178
auto select_copy = queryBlock.clone();
89-
foreach(selectItem; select_copy.getSelectList()) {
90-
logDebug("clone selcet : ( %s , %s ) ".format(SQLUtils.toSQLString(selectItem.getExpr()),selectItem.computeAlias()));
179+
foreach (selectItem; select_copy.getSelectList()) {
180+
logDebug("clone select: ( %s , %s ) ".format(SQLUtils.toSQLString(selectItem.getExpr()), selectItem
181+
.computeAlias()));
91182
selectItem.setAlias("_as_b_");
92183
auto expr = selectItem.getExpr();
93-
if(cast(SQLIdentifierExpr)expr !is null)
184+
if (cast(SQLIdentifierExpr) expr !is null)
94185
selectItem.setAlias("_as_idnt_");
95-
if(cast(SQLPropertyExpr)expr !is null)
186+
if (cast(SQLPropertyExpr) expr !is null)
96187
selectItem.setAlias("_as_proper_");
97188

98189
}
99190

100191
logDebug("clone : %s".format(SQLUtils.toSQLString(select_copy)));
101192

102-
103193
// SQLSelectBuilder builder = SQLBuilderFactory.createSelectSQLBuilder(DBType.MYSQL.name);
104194

105195
// builder.from("mytable");

source/hunt/sql/SQLTransformUtils.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ public class SQLTransformUtils {
594594
x.setName(SQLDataType.Constants.TIMESTAMP);
595595
if (x.isWithLocalTimeZone()) {
596596
x.setWithLocalTimeZone(false);
597-
x.setWithTimeZone(false);
597+
x.setWithTimeZone(null);
598598
}
599599
dataType = x;
600600
} else if (nameHash == FnvHash.Constants.DATETIME) {

source/hunt/sql/ast/SQLArrayDataType.d

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import hunt.sql.ast.SQLExpr;
66
import hunt.sql.ast.SQLDataType;
77
import hunt.sql.visitor.SQLASTVisitor;
88
import hunt.sql.util.FnvHash;
9+
10+
import hunt.Boolean;
911
import hunt.collection;
1012

1113

@@ -38,11 +40,11 @@ public class SQLArrayDataType : SQLObjectImpl , SQLDataType {
3840
return new ArrayList!SQLExpr();
3941
}
4042

41-
public bool getWithTimeZone() {
42-
return false;
43+
public Boolean getWithTimeZone() {
44+
return Boolean.FALSE;
4345
}
4446

45-
public void setWithTimeZone(bool value) {
47+
public void setWithTimeZone(Boolean value) {
4648
throw new Exception("UnsupportedOperation");
4749
}
4850

source/hunt/sql/ast/SQLDataType.d

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module hunt.sql.ast.SQLDataType;
33
import hunt.sql.ast.SQLObject;
44
import hunt.sql.ast.SQLExpr;
55

6+
import hunt.Boolean;
67
import hunt.collection;
78

89

@@ -16,8 +17,8 @@ public interface SQLDataType : SQLObject {
1617

1718
List!SQLExpr getArguments();
1819

19-
bool getWithTimeZone();
20-
void setWithTimeZone(bool value);
20+
Boolean getWithTimeZone();
21+
void setWithTimeZone(Boolean value);
2122

2223
bool isWithLocalTimeZone();
2324
void setWithLocalTimeZone(bool value);

source/hunt/sql/ast/SQLDataTypeImpl.d

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
module hunt.sql.ast.SQLDataTypeImpl;
22

3-
import hunt.collection;
43
import hunt.sql.ast.SQLObjectImpl;
54
import hunt.sql.ast.SQLDataType;
65
import hunt.sql.ast.SQLExpr;
@@ -9,14 +8,17 @@ import hunt.sql.ast.SQLObject;
98
import hunt.sql.util.FnvHash;
109
import hunt.sql.SQLUtils;
1110

11+
import hunt.Boolean;
12+
import hunt.collection;
13+
1214

1315

1416
public class SQLDataTypeImpl : SQLObjectImpl , SQLDataType {
1517

1618
private string name;
1719
private long _nameHashCode64;
1820
protected List!SQLExpr arguments;
19-
private bool withTimeZone;
21+
private Boolean withTimeZone;
2022
private bool withLocalTimeZone = false;
2123
private string dbType;
2224

@@ -91,19 +93,20 @@ public class SQLDataTypeImpl : SQLObjectImpl , SQLDataType {
9193

9294
if (name !is null ? !(name == dataType.name) : dataType.name !is null) return false;
9395
if (arguments !is null ? !(arguments == dataType.arguments) : dataType.arguments !is null) return false;
94-
return withTimeZone !is false ? withTimeZone == (dataType.withTimeZone) : dataType.withTimeZone == false;
96+
return withTimeZone !is null ?
97+
(withTimeZone.value() == dataType.withTimeZone.value) : (dataType.withTimeZone is null);
9598
}
9699

97100
override public size_t toHash() @trusted nothrow {
98101
long value = nameHashCode64();
99102
return cast(size_t)(value ^ (value >>> 32));
100103
}
101104

102-
public override bool getWithTimeZone() {
105+
public override Boolean getWithTimeZone() {
103106
return withTimeZone;
104107
}
105108

106-
public void setWithTimeZone(bool withTimeZone) {
109+
public void setWithTimeZone(Boolean withTimeZone) {
107110
this.withTimeZone = withTimeZone;
108111
}
109112

source/hunt/sql/ast/SQLMapDataType.d

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import hunt.sql.ast.SQLDataType;
44
import hunt.sql.ast.SQLObjectImpl;
55
import hunt.sql.ast.SQLExpr;
66
import hunt.sql.visitor.SQLASTVisitor;
7-
import hunt.collection;
87
import hunt.sql.util.FnvHash;
98
import hunt.sql.ast.SQLObject;
109

10+
import hunt.Boolean;
11+
import hunt.collection;
12+
1113
public class SQLMapDataType : SQLObjectImpl , SQLDataType {
1214
private string dbType;
1315
private SQLDataType keyType;
@@ -45,11 +47,11 @@ public class SQLMapDataType : SQLObjectImpl , SQLDataType {
4547
}
4648

4749

48-
public override bool getWithTimeZone() {
49-
return false;
50+
public override Boolean getWithTimeZone() {
51+
return Boolean.FALSE;
5052
}
5153

52-
public override void setWithTimeZone(bool value) {
54+
public override void setWithTimeZone(Boolean value) {
5355
throw new Exception("UnsupportedOperation");
5456
}
5557

source/hunt/sql/ast/SQLStructDataType.d

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import hunt.sql.ast.SQLExpr;
88
import hunt.sql.util.FnvHash;
99
import hunt.sql.visitor.SQLASTVisitor;
1010

11+
import hunt.Boolean;
1112
import hunt.collection;
1213

1314

@@ -40,11 +41,11 @@ public class SQLStructDataType : SQLObjectImpl , SQLDataType {
4041
return new ArrayList!SQLExpr();
4142
}
4243

43-
public override bool getWithTimeZone() {
44-
return false;
44+
public override Boolean getWithTimeZone() {
45+
return Boolean.FALSE;
4546
}
4647

47-
public void setWithTimeZone(bool value) {
48+
public void setWithTimeZone(Boolean value) {
4849
throw new Exception("UnsupportedOperation");
4950
}
5051

source/hunt/sql/parser/SQLExprParser.d

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2638,12 +2638,12 @@ public class SQLExprParser : SQLParser {
26382638
lexer.nextToken();
26392639
acceptIdentifier("TIME");
26402640
acceptIdentifier("ZONE");
2641-
dataType.setWithTimeZone(false);
2641+
dataType.setWithTimeZone(Boolean.FALSE);
26422642
} else if (lexer.token == Token.WITH) {
26432643
lexer.nextToken();
26442644
acceptIdentifier("TIME");
26452645
acceptIdentifier("ZONE");
2646-
dataType.setWithTimeZone(true);
2646+
dataType.setWithTimeZone(Boolean.TRUE);
26472647
}
26482648
}
26492649

0 commit comments

Comments
 (0)