Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
7fda2b32
提交
7fda2b32
authored
8月 16, 2008
作者:
Thomas Mueller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
JaQu is now using prepared statements and supports Date, Time, Timestamp.
上级
fe741914
显示空白字符变更
内嵌
并排
正在显示
19 个修改的文件
包含
297 行增加
和
186 行删除
+297
-186
ComplexObject.java
h2/src/test/org/h2/test/jaqu/ComplexObject.java
+20
-18
Customer.java
h2/src/test/org/h2/test/jaqu/Customer.java
+1
-1
Order.java
h2/src/test/org/h2/test/jaqu/Order.java
+1
-1
Product.java
h2/src/test/org/h2/test/jaqu/Product.java
+1
-1
SamplesTest.java
h2/src/test/org/h2/test/jaqu/SamplesTest.java
+24
-9
Condition.java
h2/src/tools/org/h2/jaqu/Condition.java
+5
-4
ConditionAndOr.java
h2/src/tools/org/h2/jaqu/ConditionAndOr.java
+2
-2
Db.java
h2/src/tools/org/h2/jaqu/Db.java
+0
-8
Function.java
h2/src/tools/org/h2/jaqu/Function.java
+27
-25
OrderExpression.java
h2/src/tools/org/h2/jaqu/OrderExpression.java
+5
-7
Query.java
h2/src/tools/org/h2/jaqu/Query.java
+52
-41
QueryWhere.java
h2/src/tools/org/h2/jaqu/QueryWhere.java
+7
-1
SelectColumn.java
h2/src/tools/org/h2/jaqu/SelectColumn.java
+4
-3
SelectTable.java
h2/src/tools/org/h2/jaqu/SelectTable.java
+11
-12
SqlStatement.java
h2/src/tools/org/h2/jaqu/SqlStatement.java
+79
-0
TableDefinition.java
h2/src/tools/org/h2/jaqu/TableDefinition.java
+13
-28
TestCondition.java
h2/src/tools/org/h2/jaqu/TestCondition.java
+36
-12
Token.java
h2/src/tools/org/h2/jaqu/Token.java
+1
-1
Utils.java
h2/src/tools/org/h2/jaqu/util/Utils.java
+8
-12
没有找到文件。
h2/src/test/org/h2/test/jaqu/ComplexObject.java
浏览文件 @
7fda2b32
...
...
@@ -23,30 +23,32 @@ import org.h2.jaqu.Table;
*/
//## Java 1.5 begin ##
public
class
ComplexObject
implements
Table
{
Integer
id
;
Long
amount
;
String
name
;
BigDecimal
value
;
Date
birthday
;
Time
time
;
Timestamp
created
;
public
Integer
id
;
public
Long
amount
;
public
String
name
;
public
BigDecimal
value
;
public
Date
birthday
;
public
Time
time
;
public
Timestamp
created
;
static
ComplexObject
build
(
Integer
id
,
boolean
isNull
)
{
ComplexObject
obj
=
new
ComplexObject
();
obj
.
id
=
id
;
obj
.
amount
=
isNull
?
null
:
new
Long
(
1
);
obj
.
name
=
isNull
?
null
:
"hello"
;
obj
.
value
=
isNull
?
null
:
new
BigDecimal
(
"1"
);
obj
.
birthday
=
isNull
?
null
:
java
.
sql
.
Date
.
valueOf
(
"2001-01-01"
);
obj
.
time
=
isNull
?
null
:
Time
.
valueOf
(
"10:20:30"
);
obj
.
created
=
isNull
?
null
:
Timestamp
.
valueOf
(
"2002-02-02 02:02:02"
);
return
obj
;
}
public
void
define
()
{
primaryKey
(
id
);
}
public
static
List
<
ComplexObject
>
getList
()
{
ComplexObject
[]
list
=
new
ComplexObject
[]
{
// new Order("ALFKI", 10702, "330.00", "2007-01-02"),
// new Order("ALFKI", 10952, "471.20", "2007-02-03"),
// new Order("ANATR", 10308, "88.80", "2007-01-03"),
// new Order("ANATR", 10625, "479.75", "2007-03-03"),
// new Order("ANATR", 10759, "320.00", "2007-04-01"),
// new Order("ANTON", 10365, "403.20", "2007-02-13"),
// new Order("ANTON", 10682, "375.50", "2007-03-13"),
// new Order("ANTON", 10355, "480.00", "2007-04-11")
};
return
Arrays
.
asList
(
list
);
return
Arrays
.
asList
(
new
ComplexObject
[]
{
build
(
0
,
true
),
build
(
1
,
false
)
});
}
}
...
...
h2/src/test/org/h2/test/jaqu/Customer.java
浏览文件 @
7fda2b32
...
...
@@ -33,7 +33,7 @@ public class Customer {
}
//## Java 1.5 begin ##
public
static
List
<
Customer
>
get
Customer
List
()
{
public
static
List
<
Customer
>
getList
()
{
Customer
[]
list
=
new
Customer
[]
{
new
Customer
(
"ALFKI"
,
"WA"
),
new
Customer
(
"ANATR"
,
"WA"
),
...
...
h2/src/test/org/h2/test/jaqu/Order.java
浏览文件 @
7fda2b32
...
...
@@ -45,7 +45,7 @@ public class Order implements Table {
primaryKey
(
customerId
,
orderId
);
}
public
static
List
<
Order
>
get
Order
List
()
{
public
static
List
<
Order
>
getList
()
{
Order
[]
list
=
new
Order
[]
{
new
Order
(
"ALFKI"
,
10702
,
"330.00"
,
"2007-01-02"
),
new
Order
(
"ALFKI"
,
10952
,
"471.20"
,
"2007-02-03"
),
...
...
h2/src/test/org/h2/test/jaqu/Product.java
浏览文件 @
7fda2b32
...
...
@@ -52,7 +52,7 @@ public class Product implements Table {
unitPrice
,
unitsInStock
);
}
public
static
List
<
Product
>
get
Product
List
()
{
public
static
List
<
Product
>
getList
()
{
Product
[]
list
=
new
Product
[]
{
create
(
1
,
"Chai"
,
"Beverages"
,
18
,
39
),
create
(
2
,
"Chang"
,
"Beverages"
,
19.0
,
17
),
...
...
h2/src/test/org/h2/test/jaqu/SamplesTest.java
浏览文件 @
7fda2b32
...
...
@@ -46,12 +46,12 @@ public class SamplesTest extends TestBase {
public
void
test
()
throws
Exception
{
//## Java 1.5 begin ##
db
=
Db
.
open
(
"jdbc:h2:mem:"
,
"sa"
,
"sa"
);
db
.
insertAll
(
Product
.
get
Product
List
());
db
.
insertAll
(
Customer
.
get
Customer
List
());
db
.
insertAll
(
Order
.
get
Order
List
());
db
.
insertAll
(
Product
.
getList
());
db
.
insertAll
(
Customer
.
getList
());
db
.
insertAll
(
Order
.
getList
());
db
.
insertAll
(
ComplexObject
.
getList
());
// TODO
use prepared statements
// TODO
test all relevant data types (Date
,...)
// TODO
support JavaBeans specification
// TODO
support all relevant data types (byte[]
,...)
// TODO nested AND/OR, >, <,...
// TODO NOT
// TODO +, -, *, /, ||, nested operations
...
...
@@ -59,6 +59,7 @@ public class SamplesTest extends TestBase {
// TODO UPDATE: FROM ... UPDATE?
// TODO SELECT UNION
// TODO DatabaseAdapter
testComplexObject
();
testOrAndNot
();
testDelete
();
testIsNull
();
...
...
@@ -142,7 +143,7 @@ public class SamplesTest extends TestBase {
db
.
from
(
p
).
orderBy
(
p
.
productId
).
select
(
p
.
productName
);
List
<
Product
>
products
=
Product
.
get
Product
List
();
List
<
Product
>
products
=
Product
.
getList
();
for
(
int
i
=
0
;
i
<
products
.
size
();
i
++)
{
assertEquals
(
products
.
get
(
i
).
productName
,
productNames
.
get
(
i
));
}
...
...
@@ -178,7 +179,7 @@ public class SamplesTest extends TestBase {
price
=
p
.
unitPrice
;
}});
List
<
Product
>
products
=
Product
.
get
Product
List
();
List
<
Product
>
products
=
Product
.
getList
();
assertEquals
(
products
.
size
(),
productInfos
.
size
());
for
(
int
i
=
0
;
i
<
products
.
size
();
i
++)
{
ProductPrice
pr
=
productInfos
.
get
(
i
);
...
...
@@ -243,7 +244,7 @@ public class SamplesTest extends TestBase {
assertEquals
(
1
,
deleted
);
deleted
=
db
.
from
(
p
).
delete
();
assertEquals
(
9
,
deleted
);
db
.
insertAll
(
Product
.
get
Product
List
());
db
.
insertAll
(
Product
.
getList
());
}
private
void
testOrAndNot
()
throws
Exception
{
...
...
@@ -253,7 +254,7 @@ public class SamplesTest extends TestBase {
sql
=
db
.
from
(
p
).
whereTrue
(
not
(
isNull
(
p
.
productName
))).
getSQL
();
assertEquals
(
"SELECT * FROM Product WHERE (NOT productName IS NULL)"
,
sql
);
sql
=
db
.
from
(
p
).
whereTrue
(
db
.
test
(
p
.
productId
).
is
(
1
)).
getSQL
();
assertEquals
(
"SELECT * FROM Product WHERE ((productId =
1
))"
,
sql
);
assertEquals
(
"SELECT * FROM Product WHERE ((productId =
?
))"
,
sql
);
}
private
void
testLength
()
throws
Exception
{
...
...
@@ -296,6 +297,20 @@ public class SamplesTest extends TestBase {
assertEquals
(
10
,
count
);
}
private
void
testComplexObject
()
throws
Exception
{
ComplexObject
co
=
new
ComplexObject
();
long
count
=
db
.
from
(
co
).
where
(
co
.
id
).
is
(
1
).
and
(
co
.
amount
).
is
(
1L
).
and
(
co
.
birthday
).
smaller
(
new
java
.
util
.
Date
()).
and
(
co
.
created
).
smaller
(
java
.
sql
.
Timestamp
.
valueOf
(
"2005-05-05 05:05:05"
)).
and
(
co
.
name
).
is
(
"hello"
).
and
(
co
.
time
).
smaller
(
java
.
sql
.
Time
.
valueOf
(
"23:23:23"
)).
and
(
co
.
value
).
is
(
new
BigDecimal
(
"1"
)).
selectCount
();
assertEquals
(
1
,
count
);
}
//## Java 1.5 end ##
/**
...
...
h2/src/tools/org/h2/jaqu/Condition.java
浏览文件 @
7fda2b32
...
...
@@ -24,12 +24,13 @@ class Condition<A> implements Token {
this
.
y
=
y
;
}
public
String
getString
(
Query
query
)
{
public
void
appendSQL
(
SqlStatement
stat
,
Query
query
)
{
query
.
appendSQL
(
stat
,
x
);
stat
.
appendSQL
(
" "
);
stat
.
appendSQL
(
compareType
.
getString
());
if
(
compareType
.
hasRightExpression
())
{
return
query
.
getString
(
x
)
+
" "
+
compareType
.
getString
()
+
" "
+
query
.
getString
(
y
);
query
.
appendSQL
(
stat
,
y
);
}
return
query
.
getString
(
x
)
+
" "
+
compareType
.
getString
();
}
}
//## Java 1.5 end ##
h2/src/tools/org/h2/jaqu/ConditionAndOr.java
浏览文件 @
7fda2b32
...
...
@@ -20,8 +20,8 @@ enum ConditionAndOr implements Token {
this
.
text
=
text
;
}
public
String
getString
(
Query
query
)
{
return
text
;
public
void
appendSQL
(
SqlStatement
stat
,
Query
query
)
{
stat
.
appendSQL
(
text
)
;
}
}
...
...
h2/src/tools/org/h2/jaqu/Db.java
浏览文件 @
7fda2b32
...
...
@@ -124,14 +124,6 @@ public class Db {
}
}
void
execute
(
String
sql
)
{
try
{
conn
.
createStatement
().
execute
(
sql
);
}
catch
(
SQLException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
PreparedStatement
prepare
(
String
sql
)
{
try
{
return
conn
.
prepareStatement
(
sql
);
...
...
h2/src/tools/org/h2/jaqu/Function.java
浏览文件 @
7fda2b32
...
...
@@ -26,17 +26,16 @@ public class Function implements Token {
this
.
x
=
x
;
}
public
String
getString
(
Query
query
)
{
StringBuilder
buff
=
new
StringBuilder
(
);
buff
.
append
(
name
).
append
(
'('
);
public
void
appendSQL
(
SqlStatement
stat
,
Query
query
)
{
stat
.
appendSQL
(
name
);
stat
.
appendSQL
(
"("
);
for
(
int
i
=
0
;
i
<
x
.
length
;
i
++)
{
if
(
i
>
0
)
{
buff
.
append
(
','
);
stat
.
appendSQL
(
","
);
}
buff
.
append
(
query
.
getString
(
x
[
i
])
);
query
.
appendSQL
(
stat
,
x
[
i
]
);
}
buff
.
append
(
')'
);
return
buff
.
toString
();
stat
.
appendSQL
(
")"
);
}
public
static
Long
count
()
{
...
...
@@ -61,8 +60,9 @@ public class Function implements Token {
public
static
Boolean
isNull
(
Object
x
)
{
return
Db
.
registerToken
(
Utils
.
newObject
(
Boolean
.
class
),
new
Function
(
""
,
x
)
{
public
String
getString
(
Query
query
)
{
return
query
.
getString
(
x
[
0
])
+
" IS NULL"
;
public
void
appendSQL
(
SqlStatement
stat
,
Query
query
)
{
query
.
appendSQL
(
stat
,
x
[
0
]);
stat
.
appendSQL
(
" IS NULL"
);
}
});
}
...
...
@@ -70,8 +70,9 @@ public class Function implements Token {
public
static
Boolean
isNotNull
(
Object
x
)
{
return
Db
.
registerToken
(
Utils
.
newObject
(
Boolean
.
class
),
new
Function
(
""
,
x
)
{
public
String
getString
(
Query
query
)
{
return
query
.
getString
(
x
[
0
])
+
" IS NOT NULL"
;
public
void
appendSQL
(
SqlStatement
stat
,
Query
query
)
{
query
.
appendSQL
(
stat
,
x
[
0
]);
stat
.
appendSQL
(
" IS NOT NULL"
);
}
});
}
...
...
@@ -79,8 +80,9 @@ public class Function implements Token {
public
static
Boolean
not
(
Boolean
x
)
{
return
Db
.
registerToken
(
Utils
.
newObject
(
Boolean
.
class
),
new
Function
(
""
,
x
)
{
public
String
getString
(
Query
query
)
{
return
"NOT "
+
query
.
getString
(
x
[
0
]);
public
void
appendSQL
(
SqlStatement
stat
,
Query
query
)
{
stat
.
appendSQL
(
"NOT "
);
query
.
appendSQL
(
stat
,
x
[
0
]);
}
});
}
...
...
@@ -89,15 +91,13 @@ public class Function implements Token {
return
Db
.
registerToken
(
Utils
.
newObject
(
Boolean
.
class
),
new
Function
(
""
,
(
Object
[])
x
)
{
public
String
getString
(
Query
query
)
{
StringBuilder
buff
=
new
StringBuilder
();
public
void
appendSQL
(
SqlStatement
stat
,
Query
query
)
{
for
(
int
i
=
0
;
i
<
x
.
length
;
i
++)
{
if
(
i
>
0
)
{
buff
.
append
(
" OR "
);
stat
.
appendSQL
(
" OR "
);
}
buff
.
append
(
query
.
getString
(
x
[
i
])
);
query
.
appendSQL
(
stat
,
x
[
i
]
);
}
return
buff
.
toString
();
}
});
}
...
...
@@ -106,15 +106,13 @@ public class Function implements Token {
return
Db
.
registerToken
(
Utils
.
newObject
(
Boolean
.
class
),
new
Function
(
""
,
(
Object
[])
x
)
{
public
String
getString
(
Query
query
)
{
StringBuilder
buff
=
new
StringBuilder
();
public
void
appendSQL
(
SqlStatement
stat
,
Query
query
)
{
for
(
int
i
=
0
;
i
<
x
.
length
;
i
++)
{
if
(
i
>
0
)
{
buff
.
append
(
" AND "
);
stat
.
appendSQL
(
" AND "
);
}
buff
.
append
(
query
.
getString
(
x
[
i
])
);
query
.
appendSQL
(
stat
,
x
[
i
]
);
}
return
buff
.
toString
();
}
});
}
...
...
@@ -134,8 +132,12 @@ public class Function implements Token {
public
static
Boolean
like
(
String
x
,
String
pattern
)
{
Boolean
o
=
Utils
.
newObject
(
Boolean
.
class
);
return
Db
.
registerToken
(
o
,
new
Function
(
"LIKE"
,
x
,
pattern
)
{
public
String
getString
(
Query
query
)
{
return
"("
+
query
.
getString
(
x
[
0
])
+
" LIKE "
+
query
.
getString
(
x
[
1
])
+
")"
;
public
void
appendSQL
(
SqlStatement
stat
,
Query
query
)
{
stat
.
appendSQL
(
"("
);
query
.
appendSQL
(
stat
,
x
[
0
]);
stat
.
appendSQL
(
" LIKE "
);
query
.
appendSQL
(
stat
,
x
[
1
]);
stat
.
appendSQL
(
")"
);
}
});
}
...
...
h2/src/tools/org/h2/jaqu/OrderExpression.java
浏览文件 @
7fda2b32
...
...
@@ -28,19 +28,17 @@ class OrderExpression<T> {
this
.
nullsLast
=
nullsLast
;
}
String
getString
()
{
StringBuilder
buff
=
new
StringBuilder
();
buff
.
append
(
query
.
getString
(
expression
));
void
appendSQL
(
SqlStatement
stat
)
{
query
.
appendSQL
(
stat
,
expression
);
if
(
desc
)
{
buff
.
append
(
" DESC"
);
stat
.
appendSQL
(
" DESC"
);
}
if
(
nullsLast
)
{
buff
.
append
(
" NULLS LAST"
);
stat
.
appendSQL
(
" NULLS LAST"
);
}
if
(
nullsFirst
)
{
buff
.
append
(
" NULLS FIRST"
);
stat
.
appendSQL
(
" NULLS FIRST"
);
}
return
buff
.
toString
();
}
}
...
...
h2/src/tools/org/h2/jaqu/Query.java
浏览文件 @
7fda2b32
...
...
@@ -45,7 +45,9 @@ public class Query<T> {
}
public
long
selectCount
()
{
ResultSet
rs
=
db
.
executeQuery
(
getSQL
(
"COUNT(*)"
,
false
));
SqlStatement
selectList
=
new
SqlStatement
(
db
);
selectList
.
setSQL
(
"COUNT(*)"
);
ResultSet
rs
=
prepare
(
selectList
,
false
).
executeQuery
();
try
{
rs
.
next
();
long
value
=
rs
.
getLong
(
1
);
...
...
@@ -69,12 +71,16 @@ public class Query<T> {
}
public
String
getSQL
()
{
return
getSQL
(
"*"
,
false
).
trim
();
SqlStatement
selectList
=
new
SqlStatement
(
db
);
selectList
.
setSQL
(
"*"
);
return
prepare
(
selectList
,
false
).
getSQL
().
trim
();
}
private
List
<
T
>
select
(
boolean
distinct
)
{
List
<
T
>
result
=
Utils
.
newArrayList
();
ResultSet
rs
=
db
.
executeQuery
(
getSQL
(
"*"
,
distinct
));
SqlStatement
selectList
=
new
SqlStatement
(
db
);
selectList
.
setSQL
(
"*"
);
ResultSet
rs
=
prepare
(
selectList
,
distinct
).
executeQuery
();
try
{
while
(
rs
.
next
())
{
T
item
=
from
.
newObject
();
...
...
@@ -88,12 +94,11 @@ public class Query<T> {
}
public
int
delete
()
{
StringBuilder
buff
=
new
StringBuilder
();
buff
.
append
(
"DELETE FROM "
);
buff
.
append
(
from
.
getString
());
buff
.
append
(
getSQLWhere
());
String
sql
=
buff
.
toString
();
return
db
.
executeUpdate
(
sql
);
SqlStatement
stat
=
new
SqlStatement
(
db
);
stat
.
appendSQL
(
"DELETE FROM "
);
from
.
appendSQL
(
stat
);
appendWhere
(
stat
);
return
stat
.
executeUpdate
();
}
public
<
X
,
Z
>
List
<
X
>
selectDistinct
(
Z
x
)
{
...
...
@@ -115,8 +120,8 @@ public class Query<T> {
private
<
X
>
List
<
X
>
select
(
Class
<
X
>
clazz
,
X
x
,
boolean
distinct
)
{
TableDefinition
<
X
>
def
=
db
.
define
(
clazz
);
S
tring
selectList
=
def
.
getSelectList
(
this
,
x
);
ResultSet
rs
=
db
.
executeQuery
(
getSQL
(
selectList
,
distinct
)
);
S
qlStatement
selectList
=
def
.
getSelectList
(
this
,
x
);
ResultSet
rs
=
prepare
(
selectList
,
distinct
).
executeQuery
(
);
List
<
X
>
result
=
Utils
.
newArrayList
();
try
{
while
(
rs
.
next
())
{
...
...
@@ -131,8 +136,9 @@ public class Query<T> {
}
private
<
X
>
List
<
X
>
getSimple
(
X
x
,
boolean
distinct
)
{
String
selectList
=
getString
(
x
);
ResultSet
rs
=
db
.
executeQuery
(
getSQL
(
selectList
,
distinct
));
SqlStatement
selectList
=
new
SqlStatement
(
db
);
appendSQL
(
selectList
,
x
);
ResultSet
rs
=
prepare
(
selectList
,
distinct
).
executeQuery
();
List
<
X
>
result
=
Utils
.
newArrayList
();
try
{
while
(
rs
.
next
())
{
...
...
@@ -188,72 +194,77 @@ public class Query<T> {
return
this
;
}
String
getString
(
Object
x
)
{
void
appendSQL
(
SqlStatement
stat
,
Object
x
)
{
if
(
x
==
Function
.
count
())
{
return
"COUNT(*)"
;
stat
.
appendSQL
(
"COUNT(*)"
);
return
;
}
Token
token
=
Db
.
getToken
(
x
);
if
(
token
!=
null
)
{
return
token
.
getString
(
this
);
token
.
appendSQL
(
stat
,
this
);
return
;
}
SelectColumn
col
=
aliasMap
.
get
(
x
);
if
(
col
!=
null
)
{
return
col
.
getString
();
col
.
appendSQL
(
stat
);
return
;
}
return
Utils
.
quoteSQL
(
x
);
stat
.
appendSQL
(
"?"
);
stat
.
addParameter
(
x
);
}
void
addConditionToken
(
Token
condition
)
{
conditions
.
add
(
condition
);
}
String
getSQLWhere
()
{
StringBuilder
buff
=
new
StringBuilder
(
""
);
void
appendWhere
(
SqlStatement
stat
)
{
if
(!
conditions
.
isEmpty
())
{
buff
.
append
(
" WHERE "
);
stat
.
appendSQL
(
" WHERE "
);
for
(
Token
token
:
conditions
)
{
buff
.
append
(
token
.
getString
(
this
)
);
buff
.
append
(
' '
);
token
.
appendSQL
(
stat
,
this
);
stat
.
appendSQL
(
" "
);
}
}
return
buff
.
toString
();
}
String
getSQL
(
String
selectList
,
boolean
distinct
)
{
StringBuilder
buff
=
new
StringBuilder
(
"SELECT "
);
SqlStatement
prepare
(
SqlStatement
selectList
,
boolean
distinct
)
{
SqlStatement
stat
=
selectList
;
String
selectSQL
=
stat
.
getSQL
();
stat
.
setSQL
(
""
);
stat
.
appendSQL
(
"SELECT "
);
if
(
distinct
)
{
buff
.
append
(
"DISTINCT "
);
stat
.
appendSQL
(
"DISTINCT "
);
}
buff
.
append
(
selectList
);
buff
.
append
(
" FROM "
);
buff
.
append
(
from
.
getString
()
);
stat
.
appendSQL
(
selectSQL
);
stat
.
appendSQL
(
" FROM "
);
from
.
appendSQL
(
stat
);
for
(
SelectTable
join
:
joins
)
{
buff
.
append
(
join
.
getStringAsJoin
(
this
)
);
join
.
appendSQLAsJoin
(
stat
,
this
);
}
buff
.
append
(
getSQLWhere
()
);
appendWhere
(
stat
);
if
(
groupByExpressions
!=
null
)
{
buff
.
append
(
" GROUP BY "
);
stat
.
appendSQL
(
" GROUP BY "
);
for
(
int
i
=
0
;
i
<
groupByExpressions
.
length
;
i
++)
{
if
(
i
>
0
)
{
buff
.
append
(
", "
);
stat
.
appendSQL
(
", "
);
}
Object
obj
=
groupByExpressions
[
i
];
buff
.
append
(
getString
(
obj
)
);
buff
.
append
(
' '
);
appendSQL
(
stat
,
obj
);
stat
.
appendSQL
(
" "
);
}
}
if
(!
orderByList
.
isEmpty
())
{
buff
.
append
(
" ORDER BY "
);
stat
.
appendSQL
(
" ORDER BY "
);
for
(
int
i
=
0
;
i
<
orderByList
.
size
();
i
++)
{
if
(
i
>
0
)
{
buff
.
append
(
", "
);
stat
.
appendSQL
(
", "
);
}
OrderExpression
o
=
orderByList
.
get
(
i
);
buff
.
append
(
o
.
getString
()
);
buff
.
append
(
' '
);
o
.
appendSQL
(
stat
);
stat
.
appendSQL
(
" "
);
}
}
return
buff
.
toString
()
;
return
stat
;
}
//## Java 1.5 end ##
...
...
h2/src/tools/org/h2/jaqu/QueryWhere.java
浏览文件 @
7fda2b32
...
...
@@ -39,7 +39,9 @@ public class QueryWhere<T> {
}
public
String
getSQL
()
{
return
query
.
getSQL
(
"*"
,
false
).
trim
();
SqlStatement
selectList
=
new
SqlStatement
(
query
.
getDb
());
selectList
.
appendSQL
(
"*"
);
return
query
.
prepare
(
selectList
,
false
).
getSQL
().
trim
();
}
public
<
X
,
Z
>
List
<
X
>
selectDistinct
(
Z
x
)
{
...
...
@@ -116,5 +118,9 @@ public class QueryWhere<T> {
return
query
.
delete
();
}
public
long
selectCount
()
{
return
query
.
selectCount
();
}
}
//## Java 1.5 end ##
h2/src/tools/org/h2/jaqu/SelectColumn.java
浏览文件 @
7fda2b32
...
...
@@ -25,11 +25,12 @@ class SelectColumn<X> {
this
.
fieldDef
=
fieldDef
;
}
String
getString
(
)
{
void
appendSQL
(
SqlStatement
stat
)
{
if
(
selectTable
.
getQuery
().
isJoin
())
{
return
selectTable
.
getAs
()
+
"."
+
fieldDef
.
columnName
;
stat
.
appendSQL
(
selectTable
.
getAs
()
+
"."
+
fieldDef
.
columnName
);
}
else
{
stat
.
appendSQL
(
fieldDef
.
columnName
);
}
return
fieldDef
.
columnName
;
}
FieldDefinition
<
X
>
getFieldDefinition
()
{
...
...
h2/src/tools/org/h2/jaqu/SelectTable.java
浏览文件 @
7fda2b32
...
...
@@ -46,29 +46,28 @@ class SelectTable <T> {
return
aliasDef
;
}
String
getString
(
)
{
void
appendSQL
(
SqlStatement
stat
)
{
if
(
query
.
isJoin
())
{
return
aliasDef
.
tableName
+
" AS "
+
as
;
stat
.
appendSQL
(
aliasDef
.
tableName
+
" AS "
+
as
);
}
else
{
stat
.
appendSQL
(
aliasDef
.
tableName
);
}
return
aliasDef
.
tableName
;
}
String
getStringAsJoin
(
Query
query
)
{
StringBuilder
buff
=
new
StringBuilder
();
void
appendSQLAsJoin
(
SqlStatement
stat
,
Query
query
)
{
if
(
outerJoin
)
{
buff
.
append
(
" LEFT OUTER JOIN "
);
stat
.
appendSQL
(
" LEFT OUTER JOIN "
);
}
else
{
buff
.
append
(
" INNER JOIN "
);
stat
.
appendSQL
(
" INNER JOIN "
);
}
buff
.
append
(
getString
()
);
appendSQL
(
stat
);
if
(!
joinConditions
.
isEmpty
())
{
buff
.
append
(
" ON "
);
stat
.
appendSQL
(
" ON "
);
for
(
Token
token
:
joinConditions
)
{
buff
.
append
(
token
.
getString
(
query
)
);
buff
.
append
(
' '
);
token
.
appendSQL
(
stat
,
query
);
stat
.
appendSQL
(
" "
);
}
}
return
buff
.
toString
();
}
boolean
getOuterJoin
()
{
...
...
h2/src/tools/org/h2/jaqu/SqlStatement.java
0 → 100644
浏览文件 @
7fda2b32
/*
* Copyright 2004-2008 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package
org
.
h2
.
jaqu
;
//## Java 1.5 begin ##
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.util.ArrayList
;
//## Java 1.5 end ##
/**
* This class represents a parameterized SQL statement.
*/
//## Java 1.5 begin ##
public
class
SqlStatement
{
private
Db
db
;
private
String
sql
=
""
;
private
ArrayList
params
=
new
ArrayList
();
SqlStatement
(
Db
db
)
{
this
.
db
=
db
;
}
void
setSQL
(
String
sql
)
{
this
.
sql
=
sql
;
}
void
appendSQL
(
String
s
)
{
sql
+=
s
;
}
String
getSQL
()
{
return
sql
;
}
void
addParameter
(
Object
o
)
{
params
.
add
(
o
);
}
ResultSet
executeQuery
()
{
try
{
return
prepare
().
executeQuery
();
}
catch
(
SQLException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
int
executeUpdate
()
{
try
{
return
prepare
().
executeUpdate
();
}
catch
(
SQLException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
private
void
setValue
(
PreparedStatement
prep
,
int
parameterIndex
,
Object
x
)
{
try
{
prep
.
setObject
(
parameterIndex
,
x
);
}
catch
(
SQLException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
private
PreparedStatement
prepare
()
{
PreparedStatement
prep
=
db
.
prepare
(
sql
);
for
(
int
i
=
0
;
i
<
params
.
size
();
i
++)
{
Object
o
=
params
.
get
(
i
);
setValue
(
prep
,
i
+
1
,
o
);
}
return
prep
;
}
}
//## Java 1.5 end ##
h2/src/tools/org/h2/jaqu/TableDefinition.java
浏览文件 @
7fda2b32
...
...
@@ -8,7 +8,6 @@ package org.h2.jaqu;
//## Java 1.5 begin ##
import
java.lang.reflect.Field
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.util.ArrayList
;
...
...
@@ -16,7 +15,6 @@ import java.util.IdentityHashMap;
import
java.util.Map
;
import
org.h2.jaqu.util.Utils
;
//## Java 1.5 end ##
/**
* A table definition contains the index definitions of a table, the field
...
...
@@ -157,6 +155,7 @@ class TableDefinition<T> {
}
void
insert
(
Db
db
,
Object
obj
)
{
SqlStatement
stat
=
new
SqlStatement
(
db
);
StringBuilder
buff
=
new
StringBuilder
(
"INSERT INTO "
);
buff
.
append
(
tableName
);
buff
.
append
(
" VALUES("
);
...
...
@@ -165,31 +164,17 @@ class TableDefinition<T> {
buff
.
append
(
", "
);
}
buff
.
append
(
'?'
);
}
buff
.
append
(
')'
);
String
sql
=
buff
.
toString
();
PreparedStatement
prep
=
db
.
prepare
(
sql
);
for
(
int
i
=
0
;
i
<
fields
.
size
();
i
++)
{
FieldDefinition
field
=
fields
.
get
(
i
);
Object
value
=
field
.
getValue
(
obj
);
setValue
(
prep
,
i
+
1
,
value
);
}
try
{
prep
.
execute
();
}
catch
(
SQLException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
private
void
setValue
(
PreparedStatement
prep
,
int
parameterIndex
,
Object
x
)
{
try
{
prep
.
setObject
(
parameterIndex
,
x
);
}
catch
(
SQLException
e
)
{
throw
new
RuntimeException
(
e
);
stat
.
addParameter
(
value
);
}
buff
.
append
(
')'
);
stat
.
setSQL
(
buff
.
toString
());
stat
.
executeUpdate
();
}
TableDefinition
createTableIfRequired
(
Db
db
)
{
SqlStatement
stat
=
new
SqlStatement
(
db
);
StringBuilder
buff
=
new
StringBuilder
(
"CREATE TABLE IF NOT EXISTS "
);
buff
.
append
(
tableName
);
buff
.
append
(
'('
);
...
...
@@ -213,7 +198,8 @@ class TableDefinition<T> {
buff
.
append
(
')'
);
}
buff
.
append
(
')'
);
db
.
execute
(
buff
.
toString
());
stat
.
setSQL
(
buff
.
toString
());
stat
.
executeUpdate
();
// TODO create indexes
return
this
;
}
...
...
@@ -247,18 +233,17 @@ class TableDefinition<T> {
}
}
<
X
>
S
tring
getSelectList
(
Query
query
,
X
x
)
{
S
tringBuilder
buff
=
new
StringBuilder
(
);
<
X
>
S
qlStatement
getSelectList
(
Query
query
,
X
x
)
{
S
qlStatement
selectList
=
new
SqlStatement
(
query
.
getDb
()
);
for
(
int
i
=
0
;
i
<
fields
.
size
();
i
++)
{
if
(
i
>
0
)
{
buff
.
append
(
", "
);
selectList
.
appendSQL
(
", "
);
}
FieldDefinition
def
=
fields
.
get
(
i
);
Object
obj
=
def
.
getValue
(
x
);
String
s
=
query
.
getString
(
obj
);
buff
.
append
(
s
);
query
.
appendSQL
(
selectList
,
obj
);
}
return
buff
.
toString
()
;
return
selectList
;
}
<
U
,
X
>
void
copyAttributeValues
(
Query
query
,
X
to
,
X
map
)
{
...
...
h2/src/tools/org/h2/jaqu/TestCondition.java
浏览文件 @
7fda2b32
...
...
@@ -25,8 +25,12 @@ public class TestCondition<A> {
public
Boolean
is
(
A
y
)
{
Boolean
o
=
Utils
.
newObject
(
Boolean
.
class
);
return
Db
.
registerToken
(
o
,
new
Function
(
"="
,
x
,
y
)
{
public
String
getString
(
Query
query
)
{
return
"("
+
query
.
getString
(
x
[
0
])
+
" = "
+
query
.
getString
(
x
[
1
])
+
")"
;
public
void
appendSQL
(
SqlStatement
stat
,
Query
query
)
{
stat
.
appendSQL
(
"("
);
query
.
appendSQL
(
stat
,
x
[
0
]);
stat
.
appendSQL
(
" = "
);
query
.
appendSQL
(
stat
,
x
[
1
]);
stat
.
appendSQL
(
")"
);
}
});
}
...
...
@@ -34,8 +38,12 @@ public class TestCondition<A> {
public
Boolean
bigger
(
A
y
)
{
Boolean
o
=
Utils
.
newObject
(
Boolean
.
class
);
return
Db
.
registerToken
(
o
,
new
Function
(
">"
,
x
,
y
)
{
public
String
getString
(
Query
query
)
{
return
"("
+
query
.
getString
(
x
[
0
])
+
" > "
+
query
.
getString
(
x
[
1
])
+
")"
;
public
void
appendSQL
(
SqlStatement
stat
,
Query
query
)
{
stat
.
appendSQL
(
"("
);
query
.
appendSQL
(
stat
,
x
[
0
]);
stat
.
appendSQL
(
" > "
);
query
.
appendSQL
(
stat
,
x
[
1
]);
stat
.
appendSQL
(
")"
);
}
});
}
...
...
@@ -43,8 +51,12 @@ public class TestCondition<A> {
public
Boolean
biggerEqual
(
A
y
)
{
Boolean
o
=
Utils
.
newObject
(
Boolean
.
class
);
return
Db
.
registerToken
(
o
,
new
Function
(
">="
,
x
,
y
)
{
public
String
getString
(
Query
query
)
{
return
"("
+
query
.
getString
(
x
[
0
])
+
" >= "
+
query
.
getString
(
x
[
1
])
+
")"
;
public
void
appendSQL
(
SqlStatement
stat
,
Query
query
)
{
stat
.
appendSQL
(
"("
);
query
.
appendSQL
(
stat
,
x
[
0
]);
stat
.
appendSQL
(
" >= "
);
query
.
appendSQL
(
stat
,
x
[
1
]);
stat
.
appendSQL
(
")"
);
}
});
}
...
...
@@ -52,8 +64,12 @@ public class TestCondition<A> {
public
Boolean
smaller
(
A
y
)
{
Boolean
o
=
Utils
.
newObject
(
Boolean
.
class
);
return
Db
.
registerToken
(
o
,
new
Function
(
"<"
,
x
,
y
)
{
public
String
getString
(
Query
query
)
{
return
"("
+
query
.
getString
(
x
[
0
])
+
" < "
+
query
.
getString
(
x
[
1
])
+
")"
;
public
void
appendSQL
(
SqlStatement
stat
,
Query
query
)
{
stat
.
appendSQL
(
"("
);
query
.
appendSQL
(
stat
,
x
[
0
]);
stat
.
appendSQL
(
" < "
);
query
.
appendSQL
(
stat
,
x
[
1
]);
stat
.
appendSQL
(
")"
);
}
});
}
...
...
@@ -61,8 +77,12 @@ public class TestCondition<A> {
public
Boolean
smallerEqual
(
A
y
)
{
Boolean
o
=
Utils
.
newObject
(
Boolean
.
class
);
return
Db
.
registerToken
(
o
,
new
Function
(
"<="
,
x
,
y
)
{
public
String
getString
(
Query
query
)
{
return
"("
+
query
.
getString
(
x
[
0
])
+
" <= "
+
query
.
getString
(
x
[
1
])
+
")"
;
public
void
appendSQL
(
SqlStatement
stat
,
Query
query
)
{
stat
.
appendSQL
(
"("
);
query
.
appendSQL
(
stat
,
x
[
0
]);
stat
.
appendSQL
(
" <= "
);
query
.
appendSQL
(
stat
,
x
[
1
]);
stat
.
appendSQL
(
")"
);
}
});
}
...
...
@@ -70,8 +90,12 @@ public class TestCondition<A> {
public
Boolean
like
(
A
pattern
)
{
Boolean
o
=
Utils
.
newObject
(
Boolean
.
class
);
return
Db
.
registerToken
(
o
,
new
Function
(
"LIKE"
,
x
,
pattern
)
{
public
String
getString
(
Query
query
)
{
return
"("
+
query
.
getString
(
x
[
0
])
+
" LIKE "
+
query
.
getString
(
x
[
1
])
+
")"
;
public
void
appendSQL
(
SqlStatement
stat
,
Query
query
)
{
stat
.
appendSQL
(
"("
);
query
.
appendSQL
(
stat
,
x
[
0
]);
stat
.
appendSQL
(
" LIKE "
);
query
.
appendSQL
(
stat
,
x
[
1
]);
stat
.
appendSQL
(
")"
);
}
});
}
...
...
h2/src/tools/org/h2/jaqu/Token.java
浏览文件 @
7fda2b32
...
...
@@ -11,6 +11,6 @@ package org.h2.jaqu;
*/
interface
Token
{
//## Java 1.5 begin ##
String
getString
(
Query
query
);
void
appendSQL
(
SqlStatement
stat
,
Query
query
);
//## Java 1.5 end ##
}
h2/src/tools/org/h2/jaqu/util/Utils.java
浏览文件 @
7fda2b32
...
...
@@ -16,8 +16,6 @@ import java.util.HashMap;
import
java.util.IdentityHashMap
;
import
java.util.List
;
import
java.util.Map
;
import
org.h2.util.StringUtils
;
//## Java 1.5 end ##
/**
...
...
@@ -73,6 +71,14 @@ public class Utils {
return
(
T
)
new
BigDecimal
(
counter
++);
}
else
if
(
clazz
==
BigInteger
.
class
)
{
return
(
T
)
new
BigInteger
(
""
+
counter
++);
}
else
if
(
clazz
==
java
.
sql
.
Date
.
class
)
{
return
(
T
)
new
java
.
sql
.
Date
(
counter
++);
}
else
if
(
clazz
==
java
.
sql
.
Time
.
class
)
{
return
(
T
)
new
java
.
sql
.
Time
(
counter
++);
}
else
if
(
clazz
==
java
.
sql
.
Timestamp
.
class
)
{
return
(
T
)
new
java
.
sql
.
Timestamp
(
counter
++);
}
else
if
(
clazz
==
java
.
util
.
Date
.
class
)
{
return
(
T
)
new
java
.
util
.
Date
(
counter
++);
}
else
if
(
clazz
==
List
.
class
)
{
return
(
T
)
new
ArrayList
();
}
...
...
@@ -109,16 +115,6 @@ public class Utils {
}
}
public
static
String
quoteSQL
(
Object
x
)
{
if
(
x
==
null
)
{
return
"NULL"
;
}
if
(
x
instanceof
String
)
{
return
StringUtils
.
quoteStringSQL
((
String
)
x
);
}
return
x
.
toString
();
}
public
static
<
T
>
boolean
isSimpleType
(
Class
<
T
>
clazz
)
{
if
(
Number
.
class
.
isAssignableFrom
(
clazz
))
{
return
true
;
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论