Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
74e99362
提交
74e99362
authored
7月 27, 2011
作者:
Thomas Mueller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Remove duplicate code.
上级
e31a44a4
隐藏空白字符变更
内嵌
并排
正在显示
19 个修改的文件
包含
310 行增加
和
925 行删除
+310
-925
Db.java
h2/src/tools/org/h2/jaqu/Db.java
+4
-5
DbInspector.java
h2/src/tools/org/h2/jaqu/DbInspector.java
+7
-7
Function.java
h2/src/tools/org/h2/jaqu/Function.java
+12
-12
ModelUtils.java
h2/src/tools/org/h2/jaqu/ModelUtils.java
+1
-2
Query.java
h2/src/tools/org/h2/jaqu/Query.java
+32
-18
SQLDialect.java
h2/src/tools/org/h2/jaqu/SQLDialect.java
+2
-2
SQLStatement.java
h2/src/tools/org/h2/jaqu/SQLStatement.java
+1
-1
SelectTable.java
h2/src/tools/org/h2/jaqu/SelectTable.java
+3
-4
TableDefinition.java
h2/src/tools/org/h2/jaqu/TableDefinition.java
+18
-17
TableInspector.java
h2/src/tools/org/h2/jaqu/TableInspector.java
+52
-50
TestCondition.java
h2/src/tools/org/h2/jaqu/TestCondition.java
+7
-7
ValidationRemark.java
h2/src/tools/org/h2/jaqu/ValidationRemark.java
+1
-1
ConstantString.java
h2/src/tools/org/h2/jaqu/bytecode/ConstantString.java
+1
-1
ClassUtils.java
h2/src/tools/org/h2/jaqu/util/ClassUtils.java
+167
-14
GenerateModels.java
h2/src/tools/org/h2/jaqu/util/GenerateModels.java
+2
-0
JdbcUtils.java
h2/src/tools/org/h2/jaqu/util/JdbcUtils.java
+0
-243
StatementBuilder.java
h2/src/tools/org/h2/jaqu/util/StatementBuilder.java
+0
-139
StringUtils.java
h2/src/tools/org/h2/jaqu/util/StringUtils.java
+0
-185
Utils.java
h2/src/tools/org/h2/jaqu/util/Utils.java
+0
-217
没有找到文件。
h2/src/tools/org/h2/jaqu/Db.java
浏览文件 @
74e99362
...
@@ -24,11 +24,11 @@ import org.h2.jaqu.DbUpgrader.DefaultDbUpgrader;
...
@@ -24,11 +24,11 @@ import org.h2.jaqu.DbUpgrader.DefaultDbUpgrader;
import
org.h2.jaqu.SQLDialect.DefaultSQLDialect
;
import
org.h2.jaqu.SQLDialect.DefaultSQLDialect
;
import
org.h2.jaqu.Table.JQDatabase
;
import
org.h2.jaqu.Table.JQDatabase
;
import
org.h2.jaqu.Table.JQTable
;
import
org.h2.jaqu.Table.JQTable
;
import
org.h2.jaqu.util.JdbcUtils
;
import
org.h2.jaqu.util.StringUtils
;
import
org.h2.jaqu.util.Utils
;
import
org.h2.jaqu.util.WeakIdentityHashMap
;
import
org.h2.jaqu.util.WeakIdentityHashMap
;
//## Java 1.5 end ##
//## Java 1.5 end ##
import
org.h2.util.JdbcUtils
;
import
org.h2.util.New
;
import
org.h2.util.StringUtils
;
/**
/**
* This class represents a connection to a database.
* This class represents a connection to a database.
...
@@ -46,8 +46,7 @@ public class Db {
...
@@ -46,8 +46,7 @@ public class Db {
Collections
.
synchronizedMap
(
new
WeakIdentityHashMap
<
Object
,
Token
>());
Collections
.
synchronizedMap
(
new
WeakIdentityHashMap
<
Object
,
Token
>());
private
final
Connection
conn
;
private
final
Connection
conn
;
private
final
Map
<
Class
<?>,
TableDefinition
<?>>
classMap
=
private
final
Map
<
Class
<?>,
TableDefinition
<?>>
classMap
=
New
.
hashMap
();
Utils
.
newHashMap
();
private
final
SQLDialect
dialect
;
private
final
SQLDialect
dialect
;
private
DbUpgrader
dbUpgrader
=
new
DefaultDbUpgrader
();
private
DbUpgrader
dbUpgrader
=
new
DefaultDbUpgrader
();
private
final
Set
<
Class
<?>>
upgradeChecked
=
Collections
.
synchronizedSet
(
new
HashSet
<
Class
<?>>());
private
final
Set
<
Class
<?>>
upgradeChecked
=
Collections
.
synchronizedSet
(
new
HashSet
<
Class
<?>>());
...
...
h2/src/tools/org/h2/jaqu/DbInspector.java
浏览文件 @
74e99362
...
@@ -13,9 +13,9 @@ import java.text.MessageFormat;
...
@@ -13,9 +13,9 @@ import java.text.MessageFormat;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.List
;
import
org.h2.jaqu.Table.JQTable
;
import
org.h2.jaqu.Table.JQTable
;
import
org.h2.
jaqu.
util.JdbcUtils
;
import
org.h2.util.JdbcUtils
;
import
org.h2.
jaqu.util.StringUtils
;
import
org.h2.
util.New
;
import
org.h2.
jaqu.util.
Utils
;
import
org.h2.
util.String
Utils
;
/**
/**
* Class to inspect a model and a database for the purposes of model validation
* Class to inspect a model and a database for the purposes of model validation
...
@@ -58,7 +58,7 @@ public class DbInspector {
...
@@ -58,7 +58,7 @@ public class DbInspector {
public
List
<
String
>
generateModel
(
String
schema
,
String
table
,
public
List
<
String
>
generateModel
(
String
schema
,
String
table
,
String
packageName
,
boolean
annotateSchema
,
boolean
trimStrings
)
{
String
packageName
,
boolean
annotateSchema
,
boolean
trimStrings
)
{
try
{
try
{
List
<
String
>
models
=
Utils
.
newA
rrayList
();
List
<
String
>
models
=
New
.
a
rrayList
();
List
<
TableInspector
>
tables
=
getTables
(
schema
,
table
);
List
<
TableInspector
>
tables
=
getTables
(
schema
,
table
);
for
(
TableInspector
t
:
tables
)
{
for
(
TableInspector
t
:
tables
)
{
t
.
read
(
metaData
);
t
.
read
(
metaData
);
...
@@ -131,7 +131,7 @@ public class DbInspector {
...
@@ -131,7 +131,7 @@ public class DbInspector {
ResultSet
rs
=
null
;
ResultSet
rs
=
null
;
try
{
try
{
rs
=
getMetaData
().
getSchemas
();
rs
=
getMetaData
().
getSchemas
();
ArrayList
<
String
>
schemaList
=
Utils
.
newA
rrayList
();
ArrayList
<
String
>
schemaList
=
New
.
a
rrayList
();
while
(
rs
.
next
())
{
while
(
rs
.
next
())
{
schemaList
.
add
(
rs
.
getString
(
"TABLE_SCHEM"
));
schemaList
.
add
(
rs
.
getString
(
"TABLE_SCHEM"
));
}
}
...
@@ -139,7 +139,7 @@ public class DbInspector {
...
@@ -139,7 +139,7 @@ public class DbInspector {
String
jaquTables
=
DbVersion
.
class
.
getAnnotation
(
JQTable
.
class
).
name
();
String
jaquTables
=
DbVersion
.
class
.
getAnnotation
(
JQTable
.
class
).
name
();
List
<
TableInspector
>
tables
=
Utils
.
newA
rrayList
();
List
<
TableInspector
>
tables
=
New
.
a
rrayList
();
if
(
schemaList
.
size
()
==
0
)
{
if
(
schemaList
.
size
()
==
0
)
{
schemaList
.
add
(
null
);
schemaList
.
add
(
null
);
}
}
...
@@ -159,7 +159,7 @@ public class DbInspector {
...
@@ -159,7 +159,7 @@ public class DbInspector {
return
tables
;
return
tables
;
}
}
// schema subset OR table subset OR exact match
// schema subset OR table subset OR exact match
List
<
TableInspector
>
matches
=
Utils
.
newA
rrayList
();
List
<
TableInspector
>
matches
=
New
.
a
rrayList
();
for
(
TableInspector
t
:
tables
)
{
for
(
TableInspector
t
:
tables
)
{
if
(
t
.
matches
(
schema
,
table
))
{
if
(
t
.
matches
(
schema
,
table
))
{
matches
.
add
(
t
);
matches
.
add
(
t
);
...
...
h2/src/tools/org/h2/jaqu/Function.java
浏览文件 @
74e99362
...
@@ -7,7 +7,7 @@
...
@@ -7,7 +7,7 @@
package
org
.
h2
.
jaqu
;
package
org
.
h2
.
jaqu
;
//## Java 1.5 begin ##
//## Java 1.5 begin ##
import
org.h2.jaqu.util.Utils
;
import
org.h2.jaqu.util.
Class
Utils
;
//## Java 1.5 end ##
//## Java 1.5 end ##
/**
/**
...
@@ -45,23 +45,23 @@ public class Function implements Token {
...
@@ -45,23 +45,23 @@ public class Function implements Token {
public
static
Integer
length
(
Object
x
)
{
public
static
Integer
length
(
Object
x
)
{
return
Db
.
registerToken
(
return
Db
.
registerToken
(
Utils
.
newObject
(
Integer
.
class
),
new
Function
(
"LENGTH"
,
x
));
Class
Utils
.
newObject
(
Integer
.
class
),
new
Function
(
"LENGTH"
,
x
));
}
}
@SuppressWarnings
(
"unchecked"
)
@SuppressWarnings
(
"unchecked"
)
public
static
<
T
extends
Number
>
T
sum
(
T
x
)
{
public
static
<
T
extends
Number
>
T
sum
(
T
x
)
{
return
(
T
)
Db
.
registerToken
(
return
(
T
)
Db
.
registerToken
(
Utils
.
newObject
(
x
.
getClass
()),
new
Function
(
"SUM"
,
x
));
Class
Utils
.
newObject
(
x
.
getClass
()),
new
Function
(
"SUM"
,
x
));
}
}
public
static
Long
count
(
Object
x
)
{
public
static
Long
count
(
Object
x
)
{
return
Db
.
registerToken
(
return
Db
.
registerToken
(
Utils
.
newObject
(
Long
.
class
),
new
Function
(
"COUNT"
,
x
));
Class
Utils
.
newObject
(
Long
.
class
),
new
Function
(
"COUNT"
,
x
));
}
}
public
static
Boolean
isNull
(
Object
x
)
{
public
static
Boolean
isNull
(
Object
x
)
{
return
Db
.
registerToken
(
return
Db
.
registerToken
(
Utils
.
newObject
(
Boolean
.
class
),
new
Function
(
""
,
x
)
{
Class
Utils
.
newObject
(
Boolean
.
class
),
new
Function
(
""
,
x
)
{
public
<
T
>
void
appendSQL
(
SQLStatement
stat
,
Query
<
T
>
query
)
{
public
<
T
>
void
appendSQL
(
SQLStatement
stat
,
Query
<
T
>
query
)
{
query
.
appendSQL
(
stat
,
x
[
0
]);
query
.
appendSQL
(
stat
,
x
[
0
]);
stat
.
appendSQL
(
" IS NULL"
);
stat
.
appendSQL
(
" IS NULL"
);
...
@@ -71,7 +71,7 @@ public class Function implements Token {
...
@@ -71,7 +71,7 @@ public class Function implements Token {
public
static
Boolean
isNotNull
(
Object
x
)
{
public
static
Boolean
isNotNull
(
Object
x
)
{
return
Db
.
registerToken
(
return
Db
.
registerToken
(
Utils
.
newObject
(
Boolean
.
class
),
new
Function
(
""
,
x
)
{
Class
Utils
.
newObject
(
Boolean
.
class
),
new
Function
(
""
,
x
)
{
public
<
T
>
void
appendSQL
(
SQLStatement
stat
,
Query
<
T
>
query
)
{
public
<
T
>
void
appendSQL
(
SQLStatement
stat
,
Query
<
T
>
query
)
{
query
.
appendSQL
(
stat
,
x
[
0
]);
query
.
appendSQL
(
stat
,
x
[
0
]);
stat
.
appendSQL
(
" IS NOT NULL"
);
stat
.
appendSQL
(
" IS NOT NULL"
);
...
@@ -81,7 +81,7 @@ public class Function implements Token {
...
@@ -81,7 +81,7 @@ public class Function implements Token {
public
static
Boolean
not
(
Boolean
x
)
{
public
static
Boolean
not
(
Boolean
x
)
{
return
Db
.
registerToken
(
return
Db
.
registerToken
(
Utils
.
newObject
(
Boolean
.
class
),
new
Function
(
""
,
x
)
{
Class
Utils
.
newObject
(
Boolean
.
class
),
new
Function
(
""
,
x
)
{
public
<
T
>
void
appendSQL
(
SQLStatement
stat
,
Query
<
T
>
query
)
{
public
<
T
>
void
appendSQL
(
SQLStatement
stat
,
Query
<
T
>
query
)
{
stat
.
appendSQL
(
"NOT "
);
stat
.
appendSQL
(
"NOT "
);
query
.
appendSQL
(
stat
,
x
[
0
]);
query
.
appendSQL
(
stat
,
x
[
0
]);
...
@@ -91,7 +91,7 @@ public class Function implements Token {
...
@@ -91,7 +91,7 @@ public class Function implements Token {
public
static
Boolean
or
(
Boolean
...
x
)
{
public
static
Boolean
or
(
Boolean
...
x
)
{
return
Db
.
registerToken
(
return
Db
.
registerToken
(
Utils
.
newObject
(
Boolean
.
class
),
Class
Utils
.
newObject
(
Boolean
.
class
),
new
Function
(
""
,
(
Object
[])
x
)
{
new
Function
(
""
,
(
Object
[])
x
)
{
public
<
T
>
void
appendSQL
(
SQLStatement
stat
,
Query
<
T
>
query
)
{
public
<
T
>
void
appendSQL
(
SQLStatement
stat
,
Query
<
T
>
query
)
{
int
i
=
0
;
int
i
=
0
;
...
@@ -107,7 +107,7 @@ public class Function implements Token {
...
@@ -107,7 +107,7 @@ public class Function implements Token {
public
static
Boolean
and
(
Boolean
...
x
)
{
public
static
Boolean
and
(
Boolean
...
x
)
{
return
Db
.
registerToken
(
return
Db
.
registerToken
(
Utils
.
newObject
(
Boolean
.
class
),
Class
Utils
.
newObject
(
Boolean
.
class
),
new
Function
(
""
,
(
Object
[])
x
)
{
new
Function
(
""
,
(
Object
[])
x
)
{
public
<
T
>
void
appendSQL
(
SQLStatement
stat
,
Query
<
T
>
query
)
{
public
<
T
>
void
appendSQL
(
SQLStatement
stat
,
Query
<
T
>
query
)
{
int
i
=
0
;
int
i
=
0
;
...
@@ -124,19 +124,19 @@ public class Function implements Token {
...
@@ -124,19 +124,19 @@ public class Function implements Token {
@SuppressWarnings
(
"unchecked"
)
@SuppressWarnings
(
"unchecked"
)
public
static
<
X
>
X
min
(
X
x
)
{
public
static
<
X
>
X
min
(
X
x
)
{
Class
<
X
>
clazz
=
(
Class
<
X
>)
x
.
getClass
();
Class
<
X
>
clazz
=
(
Class
<
X
>)
x
.
getClass
();
X
o
=
Utils
.
newObject
(
clazz
);
X
o
=
Class
Utils
.
newObject
(
clazz
);
return
Db
.
registerToken
(
o
,
new
Function
(
"MIN"
,
x
));
return
Db
.
registerToken
(
o
,
new
Function
(
"MIN"
,
x
));
}
}
@SuppressWarnings
(
"unchecked"
)
@SuppressWarnings
(
"unchecked"
)
public
static
<
X
>
X
max
(
X
x
)
{
public
static
<
X
>
X
max
(
X
x
)
{
Class
<
X
>
clazz
=
(
Class
<
X
>)
x
.
getClass
();
Class
<
X
>
clazz
=
(
Class
<
X
>)
x
.
getClass
();
X
o
=
Utils
.
newObject
(
clazz
);
X
o
=
Class
Utils
.
newObject
(
clazz
);
return
Db
.
registerToken
(
o
,
new
Function
(
"MAX"
,
x
));
return
Db
.
registerToken
(
o
,
new
Function
(
"MAX"
,
x
));
}
}
public
static
Boolean
like
(
String
x
,
String
pattern
)
{
public
static
Boolean
like
(
String
x
,
String
pattern
)
{
Boolean
o
=
Utils
.
newObject
(
Boolean
.
class
);
Boolean
o
=
Class
Utils
.
newObject
(
Boolean
.
class
);
return
Db
.
registerToken
(
o
,
new
Function
(
"LIKE"
,
x
,
pattern
)
{
return
Db
.
registerToken
(
o
,
new
Function
(
"LIKE"
,
x
,
pattern
)
{
public
<
T
>
void
appendSQL
(
SQLStatement
stat
,
Query
<
T
>
query
)
{
public
<
T
>
void
appendSQL
(
SQLStatement
stat
,
Query
<
T
>
query
)
{
stat
.
appendSQL
(
"("
);
stat
.
appendSQL
(
"("
);
...
...
h2/src/tools/org/h2/jaqu/ModelUtils.java
浏览文件 @
74e99362
...
@@ -6,7 +6,6 @@
...
@@ -6,7 +6,6 @@
*/
*/
package
org
.
h2
.
jaqu
;
package
org
.
h2
.
jaqu
;
import
static
org
.
h2
.
jaqu
.
util
.
StringUtils
.
isNullOrEmpty
;
import
java.lang.reflect.Method
;
import
java.lang.reflect.Method
;
import
java.math.BigDecimal
;
import
java.math.BigDecimal
;
import
java.util.Arrays
;
import
java.util.Arrays
;
...
@@ -208,7 +207,7 @@ public class ModelUtils {
...
@@ -208,7 +207,7 @@ public class ModelUtils {
* @return true if it is
* @return true if it is
*/
*/
static
boolean
isProperlyFormattedDefaultValue
(
String
defaultValue
)
{
static
boolean
isProperlyFormattedDefaultValue
(
String
defaultValue
)
{
if
(
isNullOrEmpty
(
defaultValue
))
{
if
(
StringUtils
.
isNullOrEmpty
(
defaultValue
))
{
return
true
;
return
true
;
}
}
Pattern
literalDefault
=
Pattern
.
compile
(
"'.*'"
);
Pattern
literalDefault
=
Pattern
.
compile
(
"'.*'"
);
...
...
h2/src/tools/org/h2/jaqu/Query.java
浏览文件 @
74e99362
...
@@ -11,15 +11,17 @@ import java.lang.reflect.Field;
...
@@ -11,15 +11,17 @@ import java.lang.reflect.Field;
import
java.sql.Clob
;
import
java.sql.Clob
;
import
java.sql.ResultSet
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.HashMap
;
import
java.util.IdentityHashMap
;
import
java.util.IdentityHashMap
;
import
java.util.List
;
import
java.util.List
;
import
org.h2.jaqu.bytecode.ClassReader
;
import
org.h2.jaqu.bytecode.ClassReader
;
import
org.h2.jaqu.util.StatementLogger
;
import
org.h2.jaqu.util.StatementLogger
;
import
org.h2.jaqu.util.JdbcUtils
;
import
org.h2.jaqu.util.ClassUtils
;
import
org.h2.jaqu.util.Utils
;
//## Java 1.5 end ##
//## Java 1.5 end ##
import
org.h2.util.JdbcUtils
;
import
org.h2.util.New
;
/**
/**
* This class represents a query.
* This class represents a query.
...
@@ -31,11 +33,11 @@ public class Query<T> {
...
@@ -31,11 +33,11 @@ public class Query<T> {
private
Db
db
;
private
Db
db
;
private
SelectTable
<
T
>
from
;
private
SelectTable
<
T
>
from
;
private
ArrayList
<
Token
>
conditions
=
Utils
.
newA
rrayList
();
private
ArrayList
<
Token
>
conditions
=
New
.
a
rrayList
();
private
ArrayList
<
UpdateColumn
>
updateColumnDeclarations
=
Utils
.
newA
rrayList
();
private
ArrayList
<
UpdateColumn
>
updateColumnDeclarations
=
New
.
a
rrayList
();
private
ArrayList
<
SelectTable
<?>>
joins
=
Utils
.
newA
rrayList
();
private
ArrayList
<
SelectTable
<?>>
joins
=
New
.
a
rrayList
();
private
final
IdentityHashMap
<
Object
,
SelectColumn
<
T
>>
aliasMap
=
Utils
.
newIdentityHashMap
();
private
final
IdentityHashMap
<
Object
,
SelectColumn
<
T
>>
aliasMap
=
Class
Utils
.
newIdentityHashMap
();
private
ArrayList
<
OrderExpression
<
T
>>
orderByList
=
Utils
.
newA
rrayList
();
private
ArrayList
<
OrderExpression
<
T
>>
orderByList
=
New
.
a
rrayList
();
private
Object
[]
groupByExpressions
;
private
Object
[]
groupByExpressions
;
private
long
limit
;
private
long
limit
;
private
long
offset
;
private
long
offset
;
...
@@ -58,14 +60,17 @@ public class Query<T> {
...
@@ -58,14 +60,17 @@ public class Query<T> {
stat
.
appendSQL
(
"COUNT(*) "
);
stat
.
appendSQL
(
"COUNT(*) "
);
appendFromWhere
(
stat
);
appendFromWhere
(
stat
);
ResultSet
rs
=
stat
.
executeQuery
();
ResultSet
rs
=
stat
.
executeQuery
();
Statement
s
=
null
;
try
{
try
{
s
=
rs
.
getStatement
();
rs
.
next
();
rs
.
next
();
long
value
=
rs
.
getLong
(
1
);
long
value
=
rs
.
getLong
(
1
);
return
value
;
return
value
;
}
catch
(
SQLException
e
)
{
}
catch
(
SQLException
e
)
{
throw
new
RuntimeException
(
e
);
throw
new
RuntimeException
(
e
);
}
finally
{
}
finally
{
JdbcUtils
.
closeSilently
(
rs
,
true
);
JdbcUtils
.
closeSilently
(
rs
);
JdbcUtils
.
closeSilently
(
s
);
}
}
}
}
...
@@ -95,13 +100,15 @@ public class Query<T> {
...
@@ -95,13 +100,15 @@ public class Query<T> {
}
}
private
List
<
T
>
select
(
boolean
distinct
)
{
private
List
<
T
>
select
(
boolean
distinct
)
{
List
<
T
>
result
=
Utils
.
newA
rrayList
();
List
<
T
>
result
=
New
.
a
rrayList
();
TableDefinition
<
T
>
def
=
from
.
getAliasDefinition
();
TableDefinition
<
T
>
def
=
from
.
getAliasDefinition
();
SQLStatement
stat
=
getSelectStatement
(
distinct
);
SQLStatement
stat
=
getSelectStatement
(
distinct
);
def
.
appendSelectList
(
stat
);
def
.
appendSelectList
(
stat
);
appendFromWhere
(
stat
);
appendFromWhere
(
stat
);
ResultSet
rs
=
stat
.
executeQuery
();
ResultSet
rs
=
stat
.
executeQuery
();
Statement
s
=
null
;
try
{
try
{
s
=
rs
.
getStatement
();
while
(
rs
.
next
())
{
while
(
rs
.
next
())
{
T
item
=
from
.
newObject
();
T
item
=
from
.
newObject
();
from
.
getAliasDefinition
().
readRow
(
item
,
rs
);
from
.
getAliasDefinition
().
readRow
(
item
,
rs
);
...
@@ -110,7 +117,8 @@ public class Query<T> {
...
@@ -110,7 +117,8 @@ public class Query<T> {
}
catch
(
SQLException
e
)
{
}
catch
(
SQLException
e
)
{
throw
new
RuntimeException
(
e
);
throw
new
RuntimeException
(
e
);
}
finally
{
}
finally
{
JdbcUtils
.
closeSilently
(
rs
,
true
);
JdbcUtils
.
closeSilently
(
rs
);
JdbcUtils
.
closeSilently
(
s
);
}
}
return
result
;
return
result
;
}
}
...
@@ -163,7 +171,7 @@ public class Query<T> {
...
@@ -163,7 +171,7 @@ public class Query<T> {
@SuppressWarnings
(
"unchecked"
)
@SuppressWarnings
(
"unchecked"
)
private
<
X
,
Z
>
List
<
X
>
select
(
Z
x
,
boolean
distinct
)
{
private
<
X
,
Z
>
List
<
X
>
select
(
Z
x
,
boolean
distinct
)
{
Class
<?>
clazz
=
x
.
getClass
();
Class
<?>
clazz
=
x
.
getClass
();
if
(
Utils
.
isSimpleType
(
clazz
))
{
if
(
Class
Utils
.
isSimpleType
(
clazz
))
{
return
selectSimple
((
X
)
x
,
distinct
);
return
selectSimple
((
X
)
x
,
distinct
);
}
}
clazz
=
clazz
.
getSuperclass
();
clazz
=
clazz
.
getSuperclass
();
...
@@ -171,22 +179,25 @@ public class Query<T> {
...
@@ -171,22 +179,25 @@ public class Query<T> {
}
}
private
<
X
>
List
<
X
>
select
(
Class
<
X
>
clazz
,
X
x
,
boolean
distinct
)
{
private
<
X
>
List
<
X
>
select
(
Class
<
X
>
clazz
,
X
x
,
boolean
distinct
)
{
List
<
X
>
result
=
Utils
.
newA
rrayList
();
List
<
X
>
result
=
New
.
a
rrayList
();
TableDefinition
<
X
>
def
=
db
.
define
(
clazz
);
TableDefinition
<
X
>
def
=
db
.
define
(
clazz
);
SQLStatement
stat
=
getSelectStatement
(
distinct
);
SQLStatement
stat
=
getSelectStatement
(
distinct
);
def
.
appendSelectList
(
stat
,
this
,
x
);
def
.
appendSelectList
(
stat
,
this
,
x
);
appendFromWhere
(
stat
);
appendFromWhere
(
stat
);
ResultSet
rs
=
stat
.
executeQuery
();
ResultSet
rs
=
stat
.
executeQuery
();
Statement
s
=
null
;
try
{
try
{
s
=
rs
.
getStatement
();
while
(
rs
.
next
())
{
while
(
rs
.
next
())
{
X
row
=
Utils
.
newObject
(
clazz
);
X
row
=
Class
Utils
.
newObject
(
clazz
);
def
.
readRow
(
row
,
rs
);
def
.
readRow
(
row
,
rs
);
result
.
add
(
row
);
result
.
add
(
row
);
}
}
}
catch
(
SQLException
e
)
{
}
catch
(
SQLException
e
)
{
throw
new
RuntimeException
(
e
);
throw
new
RuntimeException
(
e
);
}
finally
{
}
finally
{
JdbcUtils
.
closeSilently
(
rs
,
true
);
JdbcUtils
.
closeSilently
(
rs
);
JdbcUtils
.
closeSilently
(
s
);
}
}
return
result
;
return
result
;
}
}
...
@@ -197,15 +208,17 @@ public class Query<T> {
...
@@ -197,15 +208,17 @@ public class Query<T> {
appendSQL
(
stat
,
x
);
appendSQL
(
stat
,
x
);
appendFromWhere
(
stat
);
appendFromWhere
(
stat
);
ResultSet
rs
=
stat
.
executeQuery
();
ResultSet
rs
=
stat
.
executeQuery
();
List
<
X
>
result
=
Utils
.
newArrayList
();
List
<
X
>
result
=
New
.
arrayList
();
Statement
s
=
null
;
try
{
try
{
s
=
rs
.
getStatement
();
while
(
rs
.
next
())
{
while
(
rs
.
next
())
{
try
{
try
{
X
value
;
X
value
;
Object
o
=
rs
.
getObject
(
1
);
Object
o
=
rs
.
getObject
(
1
);
int
convertHereIsProbablyWrong
;
int
convertHereIsProbablyWrong
;
if
(
Clob
.
class
.
isAssignableFrom
(
o
.
getClass
()))
{
if
(
Clob
.
class
.
isAssignableFrom
(
o
.
getClass
()))
{
value
=
(
X
)
Utils
.
convert
(
o
,
String
.
class
);
value
=
(
X
)
Class
Utils
.
convert
(
o
,
String
.
class
);
}
else
{
}
else
{
value
=
(
X
)
o
;
value
=
(
X
)
o
;
}
}
...
@@ -217,7 +230,8 @@ public class Query<T> {
...
@@ -217,7 +230,8 @@ public class Query<T> {
}
catch
(
SQLException
e
)
{
}
catch
(
SQLException
e
)
{
throw
new
RuntimeException
(
e
);
throw
new
RuntimeException
(
e
);
}
finally
{
}
finally
{
JdbcUtils
.
closeSilently
(
rs
,
true
);
JdbcUtils
.
closeSilently
(
rs
);
JdbcUtils
.
closeSilently
(
s
);
}
}
return
result
;
return
result
;
}
}
...
@@ -236,7 +250,7 @@ public class Query<T> {
...
@@ -236,7 +250,7 @@ public class Query<T> {
}
}
public
<
A
>
QueryWhere
<
T
>
where
(
Filter
filter
)
{
public
<
A
>
QueryWhere
<
T
>
where
(
Filter
filter
)
{
HashMap
<
String
,
Object
>
fieldMap
=
Utils
.
newH
ashMap
();
HashMap
<
String
,
Object
>
fieldMap
=
New
.
h
ashMap
();
for
(
Field
f
:
filter
.
getClass
().
getDeclaredFields
())
{
for
(
Field
f
:
filter
.
getClass
().
getDeclaredFields
())
{
f
.
setAccessible
(
true
);
f
.
setAccessible
(
true
);
try
{
try
{
...
...
h2/src/tools/org/h2/jaqu/SQLDialect.java
浏览文件 @
74e99362
...
@@ -7,8 +7,8 @@
...
@@ -7,8 +7,8 @@
package
org
.
h2
.
jaqu
;
package
org
.
h2
.
jaqu
;
import
org.h2.jaqu.TableDefinition.IndexDefinition
;
import
org.h2.jaqu.TableDefinition.IndexDefinition
;
import
org.h2.
jaqu.
util.StatementBuilder
;
import
org.h2.util.StatementBuilder
;
import
org.h2.
jaqu.
util.StringUtils
;
import
org.h2.util.StringUtils
;
/**
/**
* This interface defines points where JaQu can build different statements
* This interface defines points where JaQu can build different statements
...
...
h2/src/tools/org/h2/jaqu/SQLStatement.java
浏览文件 @
74e99362
...
@@ -11,8 +11,8 @@ import java.sql.PreparedStatement;
...
@@ -11,8 +11,8 @@ import java.sql.PreparedStatement;
import
java.sql.ResultSet
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.sql.SQLException
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
org.h2.jaqu.util.JdbcUtils
;
//## Java 1.5 end ##
//## Java 1.5 end ##
import
org.h2.util.JdbcUtils
;
/**
/**
* This class represents a parameterized SQL statement.
* This class represents a parameterized SQL statement.
...
...
h2/src/tools/org/h2/jaqu/SelectTable.java
浏览文件 @
74e99362
...
@@ -8,9 +8,8 @@ package org.h2.jaqu;
...
@@ -8,9 +8,8 @@ package org.h2.jaqu;
//## Java 1.5 begin ##
//## Java 1.5 begin ##
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
org.h2.jaqu.util.ClassUtils
;
import
org.h2.jaqu.util.ClassUtils
;
import
org.h2.
jaqu.util.Utils
;
import
org.h2.
util.New
;
//## Java 1.5 end ##
//## Java 1.5 end ##
/**
/**
...
@@ -28,7 +27,7 @@ class SelectTable <T> {
...
@@ -28,7 +27,7 @@ class SelectTable <T> {
private
String
as
;
private
String
as
;
private
TableDefinition
<
T
>
aliasDef
;
private
TableDefinition
<
T
>
aliasDef
;
private
boolean
outerJoin
;
private
boolean
outerJoin
;
private
ArrayList
<
Token
>
joinConditions
=
Utils
.
newA
rrayList
();
private
ArrayList
<
Token
>
joinConditions
=
New
.
a
rrayList
();
private
T
alias
;
private
T
alias
;
@SuppressWarnings
(
"unchecked"
)
@SuppressWarnings
(
"unchecked"
)
...
@@ -46,7 +45,7 @@ class SelectTable <T> {
...
@@ -46,7 +45,7 @@ class SelectTable <T> {
}
}
T
newObject
()
{
T
newObject
()
{
return
Utils
.
newObject
(
clazz
);
return
Class
Utils
.
newObject
(
clazz
);
}
}
TableDefinition
<
T
>
getAliasDefinition
()
{
TableDefinition
<
T
>
getAliasDefinition
()
{
...
...
h2/src/tools/org/h2/jaqu/TableDefinition.java
浏览文件 @
74e99362
...
@@ -22,10 +22,11 @@ import org.h2.jaqu.Table.JQIndex;
...
@@ -22,10 +22,11 @@ import org.h2.jaqu.Table.JQIndex;
import
org.h2.jaqu.Table.JQSchema
;
import
org.h2.jaqu.Table.JQSchema
;
import
org.h2.jaqu.Table.JQTable
;
import
org.h2.jaqu.Table.JQTable
;
import
org.h2.jaqu.util.StatementLogger
;
import
org.h2.jaqu.util.StatementLogger
;
import
org.h2.jaqu.util.StatementBuilder
;
import
org.h2.jaqu.util.ClassUtils
;
import
org.h2.jaqu.util.StringUtils
;
import
org.h2.jaqu.util.Utils
;
//## Java 1.5 end ##
//## Java 1.5 end ##
import
org.h2.util.New
;
import
org.h2.util.StatementBuilder
;
import
org.h2.util.StringUtils
;
/**
/**
* A table definition contains the index definitions of a table, the field
* A table definition contains the index definitions of a table, the field
...
@@ -73,7 +74,7 @@ class TableDefinition<T> {
...
@@ -73,7 +74,7 @@ class TableDefinition<T> {
}
}
void
initWithNewObject
(
Object
obj
)
{
void
initWithNewObject
(
Object
obj
)
{
Object
o
=
Utils
.
newObject
(
field
.
getType
());
Object
o
=
Class
Utils
.
newObject
(
field
.
getType
());
setValue
(
obj
,
o
);
setValue
(
obj
,
o
);
}
}
...
@@ -82,7 +83,7 @@ class TableDefinition<T> {
...
@@ -82,7 +83,7 @@ class TableDefinition<T> {
if
(!
field
.
isAccessible
())
{
if
(!
field
.
isAccessible
())
{
field
.
setAccessible
(
true
);
field
.
setAccessible
(
true
);
}
}
o
=
Utils
.
convert
(
o
,
field
.
getType
());
o
=
Class
Utils
.
convert
(
o
,
field
.
getType
());
field
.
set
(
obj
,
o
);
field
.
set
(
obj
,
o
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
e
);
throw
new
RuntimeException
(
e
);
...
@@ -103,12 +104,12 @@ class TableDefinition<T> {
...
@@ -103,12 +104,12 @@ class TableDefinition<T> {
int
tableVersion
;
int
tableVersion
;
private
boolean
createTableIfRequired
=
true
;
private
boolean
createTableIfRequired
=
true
;
private
Class
<
T
>
clazz
;
private
Class
<
T
>
clazz
;
private
ArrayList
<
FieldDefinition
>
fields
=
Utils
.
newA
rrayList
();
private
ArrayList
<
FieldDefinition
>
fields
=
New
.
a
rrayList
();
private
IdentityHashMap
<
Object
,
FieldDefinition
>
fieldMap
=
private
IdentityHashMap
<
Object
,
FieldDefinition
>
fieldMap
=
Utils
.
newIdentityHashMap
();
Class
Utils
.
newIdentityHashMap
();
private
List
<
String
>
primaryKeyColumnNames
;
private
List
<
String
>
primaryKeyColumnNames
;
private
ArrayList
<
IndexDefinition
>
indexes
=
Utils
.
newA
rrayList
();
private
ArrayList
<
IndexDefinition
>
indexes
=
New
.
a
rrayList
();
private
boolean
memoryTable
;
private
boolean
memoryTable
;
TableDefinition
(
Class
<
T
>
clazz
)
{
TableDefinition
(
Class
<
T
>
clazz
)
{
...
@@ -149,7 +150,7 @@ class TableDefinition<T> {
...
@@ -149,7 +150,7 @@ class TableDefinition<T> {
* @param columnNames the ordered list of column names
* @param columnNames the ordered list of column names
*/
*/
void
setPrimaryKey
(
List
<
String
>
columnNames
)
{
void
setPrimaryKey
(
List
<
String
>
columnNames
)
{
primaryKeyColumnNames
=
Utils
.
newA
rrayList
(
columnNames
);
primaryKeyColumnNames
=
New
.
a
rrayList
(
columnNames
);
// set isPrimaryKey flag for all field definitions
// set isPrimaryKey flag for all field definitions
for
(
FieldDefinition
fieldDefinition
:
fieldMap
.
values
())
{
for
(
FieldDefinition
fieldDefinition
:
fieldMap
.
values
())
{
fieldDefinition
.
isPrimaryKey
=
this
.
primaryKeyColumnNames
fieldDefinition
.
isPrimaryKey
=
this
.
primaryKeyColumnNames
...
@@ -163,7 +164,7 @@ class TableDefinition<T> {
...
@@ -163,7 +164,7 @@ class TableDefinition<T> {
}
}
private
ArrayList
<
String
>
mapColumnNames
(
Object
[]
columns
)
{
private
ArrayList
<
String
>
mapColumnNames
(
Object
[]
columns
)
{
ArrayList
<
String
>
columnNames
=
Utils
.
newA
rrayList
();
ArrayList
<
String
>
columnNames
=
New
.
a
rrayList
();
for
(
Object
column
:
columns
)
{
for
(
Object
column
:
columns
)
{
columnNames
.
add
(
getColumnName
(
column
));
columnNames
.
add
(
getColumnName
(
column
));
}
}
...
@@ -190,7 +191,7 @@ class TableDefinition<T> {
...
@@ -190,7 +191,7 @@ class TableDefinition<T> {
void
addIndex
(
IndexType
type
,
List
<
String
>
columnNames
)
{
void
addIndex
(
IndexType
type
,
List
<
String
>
columnNames
)
{
IndexDefinition
index
=
new
IndexDefinition
();
IndexDefinition
index
=
new
IndexDefinition
();
index
.
indexName
=
tableName
+
"_"
+
indexes
.
size
();
index
.
indexName
=
tableName
+
"_"
+
indexes
.
size
();
index
.
columnNames
=
Utils
.
newA
rrayList
(
columnNames
);
index
.
columnNames
=
New
.
a
rrayList
(
columnNames
);
index
.
type
=
type
;
index
.
type
=
type
;
indexes
.
add
(
index
);
indexes
.
add
(
index
);
}
}
...
@@ -216,7 +217,7 @@ class TableDefinition<T> {
...
@@ -216,7 +217,7 @@ class TableDefinition<T> {
strictTypeMapping
=
tableAnnotation
.
strictTypeMapping
();
strictTypeMapping
=
tableAnnotation
.
strictTypeMapping
();
}
}
List
<
Field
>
classFields
=
Utils
.
newA
rrayList
();
List
<
Field
>
classFields
=
New
.
a
rrayList
();
classFields
.
addAll
(
Arrays
.
asList
(
clazz
.
getDeclaredFields
()));
classFields
.
addAll
(
Arrays
.
asList
(
clazz
.
getDeclaredFields
()));
if
(
inheritColumns
)
{
if
(
inheritColumns
)
{
Class
<?>
superClass
=
clazz
.
getSuperclass
();
Class
<?>
superClass
=
clazz
.
getSuperclass
();
...
@@ -261,7 +262,7 @@ class TableDefinition<T> {
...
@@ -261,7 +262,7 @@ class TableDefinition<T> {
fields
.
add
(
fieldDef
);
fields
.
add
(
fieldDef
);
}
}
}
}
List
<
String
>
primaryKey
=
Utils
.
newA
rrayList
();
List
<
String
>
primaryKey
=
New
.
a
rrayList
();
for
(
FieldDefinition
fieldDef
:
fields
)
{
for
(
FieldDefinition
fieldDef
:
fields
)
{
if
(
fieldDef
.
isPrimaryKey
)
{
if
(
fieldDef
.
isPrimaryKey
)
{
primaryKey
.
add
(
fieldDef
.
columnName
);
primaryKey
.
add
(
fieldDef
.
columnName
);
...
@@ -372,7 +373,7 @@ class TableDefinition<T> {
...
@@ -372,7 +373,7 @@ class TableDefinition<T> {
stat
.
addParameter
(
value
);
stat
.
addParameter
(
value
);
}
}
}
}
Object
alias
=
Utils
.
newObject
(
obj
.
getClass
());
Object
alias
=
Class
Utils
.
newObject
(
obj
.
getClass
());
Query
<
Object
>
query
=
Query
.
from
(
db
,
alias
);
Query
<
Object
>
query
=
Query
.
from
(
db
,
alias
);
boolean
firstCondition
=
true
;
boolean
firstCondition
=
true
;
for
(
FieldDefinition
field
:
fields
)
{
for
(
FieldDefinition
field
:
fields
)
{
...
@@ -403,7 +404,7 @@ class TableDefinition<T> {
...
@@ -403,7 +404,7 @@ class TableDefinition<T> {
StatementBuilder
buff
=
new
StatementBuilder
(
"DELETE FROM "
);
StatementBuilder
buff
=
new
StatementBuilder
(
"DELETE FROM "
);
buff
.
append
(
db
.
getDialect
().
getTableName
(
schemaName
,
tableName
));
buff
.
append
(
db
.
getDialect
().
getTableName
(
schemaName
,
tableName
));
buff
.
resetCount
();
buff
.
resetCount
();
Object
alias
=
Utils
.
newObject
(
obj
.
getClass
());
Object
alias
=
Class
Utils
.
newObject
(
obj
.
getClass
());
Query
<
Object
>
query
=
Query
.
from
(
db
,
alias
);
Query
<
Object
>
query
=
Query
.
from
(
db
,
alias
);
boolean
firstCondition
=
true
;
boolean
firstCondition
=
true
;
for
(
FieldDefinition
field
:
fields
)
{
for
(
FieldDefinition
field
:
fields
)
{
...
@@ -506,7 +507,7 @@ class TableDefinition<T> {
...
@@ -506,7 +507,7 @@ class TableDefinition<T> {
* @return the column list
* @return the column list
*/
*/
private
List
<
String
>
getColumns
(
String
index
)
{
private
List
<
String
>
getColumns
(
String
index
)
{
List
<
String
>
cols
=
Utils
.
newA
rrayList
();
List
<
String
>
cols
=
New
.
a
rrayList
();
if
(
index
==
null
||
index
.
length
()
==
0
)
{
if
(
index
==
null
||
index
.
length
()
==
0
)
{
return
null
;
return
null
;
}
}
...
@@ -579,7 +580,7 @@ class TableDefinition<T> {
...
@@ -579,7 +580,7 @@ class TableDefinition<T> {
}
}
List
<
IndexDefinition
>
getIndexes
(
IndexType
type
)
{
List
<
IndexDefinition
>
getIndexes
(
IndexType
type
)
{
List
<
IndexDefinition
>
list
=
Utils
.
newA
rrayList
();
List
<
IndexDefinition
>
list
=
New
.
a
rrayList
();
for
(
IndexDefinition
def:
indexes
)
{
for
(
IndexDefinition
def:
indexes
)
{
if
(
def
.
type
.
equals
(
type
))
{
if
(
def
.
type
.
equals
(
type
))
{
list
.
add
(
def
);
list
.
add
(
def
);
...
...
h2/src/tools/org/h2/jaqu/TableInspector.java
浏览文件 @
74e99362
...
@@ -6,16 +6,11 @@
...
@@ -6,16 +6,11 @@
*/
*/
package
org
.
h2
.
jaqu
;
package
org
.
h2
.
jaqu
;
import
static
java
.
text
.
MessageFormat
.
format
;
import
static
org
.
h2
.
jaqu
.
ValidationRemark
.
consider
;
import
static
org
.
h2
.
jaqu
.
ValidationRemark
.
error
;
import
static
org
.
h2
.
jaqu
.
ValidationRemark
.
warn
;
import
static
org
.
h2
.
jaqu
.
util
.
JdbcUtils
.
closeSilently
;
import
static
org
.
h2
.
jaqu
.
util
.
StringUtils
.
isNullOrEmpty
;
import
java.lang.reflect.Modifier
;
import
java.lang.reflect.Modifier
;
import
java.sql.DatabaseMetaData
;
import
java.sql.DatabaseMetaData
;
import
java.sql.ResultSet
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.sql.SQLException
;
import
java.text.MessageFormat
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.Collections
;
import
java.util.Date
;
import
java.util.Date
;
...
@@ -29,8 +24,14 @@ import org.h2.jaqu.Table.JQSchema;
...
@@ -29,8 +24,14 @@ import org.h2.jaqu.Table.JQSchema;
import
org.h2.jaqu.Table.JQTable
;
import
org.h2.jaqu.Table.JQTable
;
import
org.h2.jaqu.TableDefinition.FieldDefinition
;
import
org.h2.jaqu.TableDefinition.FieldDefinition
;
import
org.h2.jaqu.TableDefinition.IndexDefinition
;
import
org.h2.jaqu.TableDefinition.IndexDefinition
;
import
org.h2.jaqu.util.StatementBuilder
;
import
org.h2.util.JdbcUtils
;
import
org.h2.jaqu.util.Utils
;
import
org.h2.util.New
;
import
org.h2.util.StatementBuilder
;
import
org.h2.util.StringUtils
;
import
static
org
.
h2
.
jaqu
.
ValidationRemark
.
consider
;
import
static
org
.
h2
.
jaqu
.
ValidationRemark
.
error
;
import
static
org
.
h2
.
jaqu
.
ValidationRemark
.
warn
;
/**
/**
* Class to inspect the contents of a particular table including its indexes.
* Class to inspect the contents of a particular table including its indexes.
...
@@ -45,7 +46,7 @@ public class TableInspector {
...
@@ -45,7 +46,7 @@ public class TableInspector {
private
String
table
;
private
String
table
;
private
boolean
forceUpperCase
;
private
boolean
forceUpperCase
;
private
Class
<?
extends
java
.
util
.
Date
>
dateTimeClass
;
private
Class
<?
extends
java
.
util
.
Date
>
dateTimeClass
;
private
List
<
String
>
primaryKeys
=
Utils
.
newA
rrayList
();
private
List
<
String
>
primaryKeys
=
New
.
a
rrayList
();
private
Map
<
String
,
IndexInspector
>
indexes
;
private
Map
<
String
,
IndexInspector
>
indexes
;
private
Map
<
String
,
ColumnInspector
>
columns
;
private
Map
<
String
,
ColumnInspector
>
columns
;
private
final
String
eol
=
"\n"
;
private
final
String
eol
=
"\n"
;
...
@@ -66,10 +67,10 @@ public class TableInspector {
...
@@ -66,10 +67,10 @@ public class TableInspector {
* @return true if the table matches
* @return true if the table matches
*/
*/
boolean
matches
(
String
schema
,
String
table
)
{
boolean
matches
(
String
schema
,
String
table
)
{
if
(
isNullOrEmpty
(
schema
))
{
if
(
StringUtils
.
isNullOrEmpty
(
schema
))
{
// table name matching
// table name matching
return
this
.
table
.
equalsIgnoreCase
(
table
);
return
this
.
table
.
equalsIgnoreCase
(
table
);
}
else
if
(
isNullOrEmpty
(
table
))
{
}
else
if
(
StringUtils
.
isNullOrEmpty
(
table
))
{
// schema name matching
// schema name matching
return
this
.
schema
.
equalsIgnoreCase
(
schema
);
return
this
.
schema
.
equalsIgnoreCase
(
schema
);
}
else
{
}
else
{
...
@@ -95,11 +96,11 @@ public class TableInspector {
...
@@ -95,11 +96,11 @@ public class TableInspector {
String
c
=
rs
.
getString
(
"COLUMN_NAME"
);
String
c
=
rs
.
getString
(
"COLUMN_NAME"
);
primaryKeys
.
add
(
c
);
primaryKeys
.
add
(
c
);
}
}
closeSilently
(
rs
);
JdbcUtils
.
closeSilently
(
rs
);
// indexes
// indexes
rs
=
metaData
.
getIndexInfo
(
null
,
schema
,
table
,
false
,
true
);
rs
=
metaData
.
getIndexInfo
(
null
,
schema
,
table
,
false
,
true
);
indexes
=
Utils
.
newH
ashMap
();
indexes
=
New
.
h
ashMap
();
while
(
rs
.
next
())
{
while
(
rs
.
next
())
{
IndexInspector
info
=
new
IndexInspector
(
rs
);
IndexInspector
info
=
new
IndexInspector
(
rs
);
if
(
info
.
type
.
equals
(
IndexType
.
UNIQUE
)
if
(
info
.
type
.
equals
(
IndexType
.
UNIQUE
)
...
@@ -113,11 +114,11 @@ public class TableInspector {
...
@@ -113,11 +114,11 @@ public class TableInspector {
indexes
.
put
(
info
.
name
,
info
);
indexes
.
put
(
info
.
name
,
info
);
}
}
}
}
closeSilently
(
rs
);
JdbcUtils
.
closeSilently
(
rs
);
// columns
// columns
rs
=
metaData
.
getColumns
(
null
,
schema
,
table
,
null
);
rs
=
metaData
.
getColumns
(
null
,
schema
,
table
,
null
);
columns
=
Utils
.
newH
ashMap
();
columns
=
New
.
h
ashMap
();
while
(
rs
.
next
())
{
while
(
rs
.
next
())
{
ColumnInspector
col
=
new
ColumnInspector
();
ColumnInspector
col
=
new
ColumnInspector
();
col
.
name
=
rs
.
getString
(
"COLUMN_NAME"
);
col
.
name
=
rs
.
getString
(
"COLUMN_NAME"
);
...
@@ -137,7 +138,7 @@ public class TableInspector {
...
@@ -137,7 +138,7 @@ public class TableInspector {
columns
.
put
(
col
.
name
,
col
);
columns
.
put
(
col
.
name
,
col
);
}
}
}
finally
{
}
finally
{
closeSilently
(
rs
);
JdbcUtils
.
closeSilently
(
rs
);
}
}
}
}
...
@@ -160,7 +161,7 @@ public class TableInspector {
...
@@ -160,7 +161,7 @@ public class TableInspector {
boolean
trimStrings
)
{
boolean
trimStrings
)
{
// import statements
// import statements
Set
<
String
>
imports
=
Utils
.
newH
ashSet
();
Set
<
String
>
imports
=
New
.
h
ashSet
();
imports
.
add
(
JQSchema
.
class
.
getCanonicalName
());
imports
.
add
(
JQSchema
.
class
.
getCanonicalName
());
imports
.
add
(
JQTable
.
class
.
getCanonicalName
());
imports
.
add
(
JQTable
.
class
.
getCanonicalName
());
imports
.
add
(
JQIndex
.
class
.
getCanonicalName
());
imports
.
add
(
JQIndex
.
class
.
getCanonicalName
());
...
@@ -168,7 +169,7 @@ public class TableInspector {
...
@@ -168,7 +169,7 @@ public class TableInspector {
// fields
// fields
StringBuilder
fields
=
new
StringBuilder
();
StringBuilder
fields
=
new
StringBuilder
();
List
<
ColumnInspector
>
sortedColumns
=
Utils
.
newA
rrayList
(
columns
.
values
());
List
<
ColumnInspector
>
sortedColumns
=
New
.
a
rrayList
(
columns
.
values
());
Collections
.
sort
(
sortedColumns
);
Collections
.
sort
(
sortedColumns
);
for
(
ColumnInspector
col
:
sortedColumns
)
{
for
(
ColumnInspector
col
:
sortedColumns
)
{
fields
.
append
(
generateColumn
(
imports
,
col
,
trimStrings
));
fields
.
append
(
generateColumn
(
imports
,
col
,
trimStrings
));
...
@@ -176,7 +177,7 @@ public class TableInspector {
...
@@ -176,7 +177,7 @@ public class TableInspector {
// build complete class definition
// build complete class definition
StringBuilder
model
=
new
StringBuilder
();
StringBuilder
model
=
new
StringBuilder
();
if
(!
isNullOrEmpty
(
packageName
))
{
if
(!
StringUtils
.
isNullOrEmpty
(
packageName
))
{
// package
// package
model
.
append
(
"package "
+
packageName
+
";"
);
model
.
append
(
"package "
+
packageName
+
";"
);
model
.
append
(
eol
).
append
(
eol
);
model
.
append
(
eol
).
append
(
eol
);
...
@@ -191,7 +192,7 @@ public class TableInspector {
...
@@ -191,7 +192,7 @@ public class TableInspector {
model
.
append
(
eol
);
model
.
append
(
eol
);
// @JQSchema
// @JQSchema
if
(
annotateSchema
&&
!
isNullOrEmpty
(
schema
))
{
if
(
annotateSchema
&&
!
StringUtils
.
isNullOrEmpty
(
schema
))
{
model
.
append
(
'@'
).
append
(
JQSchema
.
class
.
getSimpleName
());
model
.
append
(
'@'
).
append
(
JQSchema
.
class
.
getSimpleName
());
model
.
append
(
'('
);
model
.
append
(
'('
);
AnnotationBuilder
ap
=
new
AnnotationBuilder
();
AnnotationBuilder
ap
=
new
AnnotationBuilder
();
...
@@ -236,7 +237,7 @@ public class TableInspector {
...
@@ -236,7 +237,7 @@ public class TableInspector {
// class declaration
// class declaration
String
clazzName
=
ModelUtils
.
convertTableToClassName
(
table
);
String
clazzName
=
ModelUtils
.
convertTableToClassName
(
table
);
model
.
append
(
format
(
"public class {0} '{'"
,
clazzName
)).
append
(
eol
);
model
.
append
(
MessageFormat
.
format
(
"public class {0} '{'"
,
clazzName
)).
append
(
eol
);
model
.
append
(
eol
);
model
.
append
(
eol
);
// field declarations
// field declarations
...
@@ -265,7 +266,7 @@ public class TableInspector {
...
@@ -265,7 +266,7 @@ public class TableInspector {
if
(
list
.
size
()
==
1
)
{
if
(
list
.
size
()
==
1
)
{
ap
.
addParameter
(
parameter
,
list
.
get
(
0
).
getColumnsString
());
ap
.
addParameter
(
parameter
,
list
.
get
(
0
).
getColumnsString
());
}
else
{
}
else
{
List
<
String
>
parameters
=
Utils
.
newA
rrayList
();
List
<
String
>
parameters
=
New
.
a
rrayList
();
for
(
IndexInspector
index:
list
)
{
for
(
IndexInspector
index:
list
)
{
parameters
.
add
(
index
.
getColumnsString
());
parameters
.
add
(
index
.
getColumnsString
());
}
}
...
@@ -275,7 +276,7 @@ public class TableInspector {
...
@@ -275,7 +276,7 @@ public class TableInspector {
}
}
private
List
<
IndexInspector
>
getIndexes
(
IndexType
type
)
{
private
List
<
IndexInspector
>
getIndexes
(
IndexType
type
)
{
List
<
IndexInspector
>
list
=
Utils
.
newA
rrayList
();
List
<
IndexInspector
>
list
=
New
.
a
rrayList
();
for
(
IndexInspector
index:
indexes
.
values
())
{
for
(
IndexInspector
index:
indexes
.
values
())
{
if
(
index
.
type
.
equals
(
type
))
{
if
(
index
.
type
.
equals
(
type
))
{
list
.
add
(
index
);
list
.
add
(
index
);
...
@@ -335,16 +336,17 @@ public class TableInspector {
...
@@ -335,16 +336,17 @@ public class TableInspector {
}
}
// JQColumn.defaultValue
// JQColumn.defaultValue
if
(!
isNullOrEmpty
(
col
.
defaultValue
))
{
if
(!
StringUtils
.
isNullOrEmpty
(
col
.
defaultValue
))
{
ap
.
addParameter
(
"defaultValue=\""
+
col
.
defaultValue
+
"\""
);
ap
.
addParameter
(
"defaultValue=\""
+
col
.
defaultValue
+
"\""
);
}
}
// add leading and trailing ()
// add leading and trailing ()
if
(
ap
.
length
()
>
0
)
{
if
(
ap
.
length
()
>
0
)
{
ap
.
insert
(
0
,
'('
);
AnnotationBuilder
b
=
new
AnnotationBuilder
();
ap
.
append
(
')'
);
b
.
append
(
'('
).
append
(
ap
.
toString
()).
append
(
')'
);
ap
=
b
;
}
}
sb
.
append
(
ap
);
sb
.
append
(
ap
.
toString
()
);
}
}
sb
.
append
(
eol
);
sb
.
append
(
eol
);
...
@@ -373,24 +375,24 @@ public class TableInspector {
...
@@ -373,24 +375,24 @@ public class TableInspector {
*/
*/
<
T
>
List
<
ValidationRemark
>
validate
(
TableDefinition
<
T
>
def
,
<
T
>
List
<
ValidationRemark
>
validate
(
TableDefinition
<
T
>
def
,
boolean
throwError
)
{
boolean
throwError
)
{
List
<
ValidationRemark
>
remarks
=
Utils
.
newA
rrayList
();
List
<
ValidationRemark
>
remarks
=
New
.
a
rrayList
();
// model class definition validation
// model class definition validation
if
(!
Modifier
.
isPublic
(
def
.
getModelClass
().
getModifiers
()))
{
if
(!
Modifier
.
isPublic
(
def
.
getModelClass
().
getModifiers
()))
{
remarks
.
add
(
error
(
table
,
"SCHEMA"
,
remarks
.
add
(
error
(
table
,
"SCHEMA"
,
format
(
"Class {0} MUST BE PUBLIC!"
,
MessageFormat
.
format
(
"Class {0} MUST BE PUBLIC!"
,
def
.
getModelClass
().
getCanonicalName
())).
throwError
(
throwError
));
def
.
getModelClass
().
getCanonicalName
())).
throwError
(
throwError
));
}
}
// Schema Validation
// Schema Validation
if
(!
isNullOrEmpty
(
schema
))
{
if
(!
StringUtils
.
isNullOrEmpty
(
schema
))
{
if
(
isNullOrEmpty
(
def
.
schemaName
))
{
if
(
StringUtils
.
isNullOrEmpty
(
def
.
schemaName
))
{
remarks
.
add
(
consider
(
table
,
"SCHEMA"
,
remarks
.
add
(
consider
(
table
,
"SCHEMA"
,
format
(
"@{0}(name={1})"
,
MessageFormat
.
format
(
"@{0}(name={1})"
,
JQSchema
.
class
.
getSimpleName
(),
schema
)));
JQSchema
.
class
.
getSimpleName
(),
schema
)));
}
else
if
(!
schema
.
equalsIgnoreCase
(
def
.
schemaName
))
{
}
else
if
(!
schema
.
equalsIgnoreCase
(
def
.
schemaName
))
{
remarks
.
add
(
error
(
table
,
"SCHEMA"
,
remarks
.
add
(
error
(
table
,
"SCHEMA"
,
format
(
"@{0}(name={1}) != {2}"
,
MessageFormat
.
format
(
"@{0}(name={1}) != {2}"
,
JQSchema
.
class
.
getSimpleName
(),
def
.
schemaName
,
JQSchema
.
class
.
getSimpleName
(),
def
.
schemaName
,
schema
)).
throwError
(
throwError
));
schema
)).
throwError
(
throwError
));
}
}
...
@@ -457,12 +459,12 @@ public class TableInspector {
...
@@ -457,12 +459,12 @@ public class TableInspector {
if
(!
fieldClass
.
equals
(
jdbcClass
))
{
if
(!
fieldClass
.
equals
(
jdbcClass
))
{
if
(
Number
.
class
.
isAssignableFrom
(
fieldClass
))
{
if
(
Number
.
class
.
isAssignableFrom
(
fieldClass
))
{
remarks
.
add
(
warn
(
table
,
col
,
remarks
.
add
(
warn
(
table
,
col
,
format
(
"Precision mismatch: ModelObject={0}, ColumnObject={1}"
,
MessageFormat
.
format
(
"Precision mismatch: ModelObject={0}, ColumnObject={1}"
,
fieldClass
.
getSimpleName
(),
jdbcClass
.
getSimpleName
())));
fieldClass
.
getSimpleName
(),
jdbcClass
.
getSimpleName
())));
}
else
{
}
else
{
if
(!
Date
.
class
.
isAssignableFrom
(
jdbcClass
))
{
if
(!
Date
.
class
.
isAssignableFrom
(
jdbcClass
))
{
remarks
.
add
(
warn
(
table
,
col
,
remarks
.
add
(
warn
(
table
,
col
,
format
(
"Object Mismatch: ModelObject={0}, ColumnObject={1}"
,
MessageFormat
.
format
(
"Object Mismatch: ModelObject={0}, ColumnObject={1}"
,
fieldClass
.
getSimpleName
(),
jdbcClass
.
getSimpleName
())));
fieldClass
.
getSimpleName
(),
jdbcClass
.
getSimpleName
())));
}
}
}
}
...
@@ -473,13 +475,13 @@ public class TableInspector {
...
@@ -473,13 +475,13 @@ public class TableInspector {
if
((
fieldDef
.
maxLength
!=
col
.
size
)
if
((
fieldDef
.
maxLength
!=
col
.
size
)
&&
(
col
.
size
<
Integer
.
MAX_VALUE
))
{
&&
(
col
.
size
<
Integer
.
MAX_VALUE
))
{
remarks
.
add
(
warn
(
table
,
col
,
remarks
.
add
(
warn
(
table
,
col
,
format
(
"{0}.maxLength={1}, ColumnMaxLength={2}"
,
MessageFormat
.
format
(
"{0}.maxLength={1}, ColumnMaxLength={2}"
,
JQColumn
.
class
.
getSimpleName
(),
JQColumn
.
class
.
getSimpleName
(),
fieldDef
.
maxLength
,
col
.
size
)));
fieldDef
.
maxLength
,
col
.
size
)));
}
}
if
(
fieldDef
.
maxLength
>
0
&&
!
fieldDef
.
trimString
)
{
if
(
fieldDef
.
maxLength
>
0
&&
!
fieldDef
.
trimString
)
{
remarks
.
add
(
consider
(
table
,
col
,
remarks
.
add
(
consider
(
table
,
col
,
format
(
"{0}.truncateToMaxLength=true"
MessageFormat
.
format
(
"{0}.truncateToMaxLength=true"
+
" will prevent RuntimeExceptions on"
+
" will prevent RuntimeExceptions on"
+
" INSERT or UPDATE, but will clip data!"
,
+
" INSERT or UPDATE, but will clip data!"
,
JQColumn
.
class
.
getSimpleName
())));
JQColumn
.
class
.
getSimpleName
())));
...
@@ -488,7 +490,7 @@ public class TableInspector {
...
@@ -488,7 +490,7 @@ public class TableInspector {
// numeric autoIncrement
// numeric autoIncrement
if
(
fieldDef
.
isAutoIncrement
!=
col
.
isAutoIncrement
)
{
if
(
fieldDef
.
isAutoIncrement
!=
col
.
isAutoIncrement
)
{
remarks
.
add
(
warn
(
table
,
col
,
format
(
"{0}.isAutoIncrement={1}"
remarks
.
add
(
warn
(
table
,
col
,
MessageFormat
.
format
(
"{0}.isAutoIncrement={1}"
+
" while Column autoIncrement={2}"
,
+
" while Column autoIncrement={2}"
,
JQColumn
.
class
.
getSimpleName
(),
fieldDef
.
isAutoIncrement
,
JQColumn
.
class
.
getSimpleName
(),
fieldDef
.
isAutoIncrement
,
col
.
isAutoIncrement
)));
col
.
isAutoIncrement
)));
...
@@ -497,7 +499,7 @@ public class TableInspector {
...
@@ -497,7 +499,7 @@ public class TableInspector {
if
(!
col
.
isAutoIncrement
&&
!
col
.
isPrimaryKey
)
{
if
(!
col
.
isAutoIncrement
&&
!
col
.
isPrimaryKey
)
{
// check Model.defaultValue format
// check Model.defaultValue format
if
(!
ModelUtils
.
isProperlyFormattedDefaultValue
(
fieldDef
.
defaultValue
))
{
if
(!
ModelUtils
.
isProperlyFormattedDefaultValue
(
fieldDef
.
defaultValue
))
{
remarks
.
add
(
error
(
table
,
col
,
format
(
"{0}.defaultValue=\"{1}\""
remarks
.
add
(
error
(
table
,
col
,
MessageFormat
.
format
(
"{0}.defaultValue=\"{1}\""
+
" is improperly formatted!"
,
+
" is improperly formatted!"
,
JQColumn
.
class
.
getSimpleName
(),
JQColumn
.
class
.
getSimpleName
(),
fieldDef
.
defaultValue
)).
throwError
(
throwError
));
fieldDef
.
defaultValue
)).
throwError
(
throwError
));
...
@@ -505,23 +507,23 @@ public class TableInspector {
...
@@ -505,23 +507,23 @@ public class TableInspector {
return
;
return
;
}
}
// compare Model.defaultValue to Column.defaultValue
// compare Model.defaultValue to Column.defaultValue
if
(
isNullOrEmpty
(
fieldDef
.
defaultValue
)
if
(
StringUtils
.
isNullOrEmpty
(
fieldDef
.
defaultValue
)
&&
!
isNullOrEmpty
(
col
.
defaultValue
))
{
&&
!
StringUtils
.
isNullOrEmpty
(
col
.
defaultValue
))
{
// Model.defaultValue is NULL, Column.defaultValue is NOT NULL
// Model.defaultValue is NULL, Column.defaultValue is NOT NULL
remarks
.
add
(
warn
(
table
,
col
,
format
(
"{0}.defaultValue=\"\""
remarks
.
add
(
warn
(
table
,
col
,
MessageFormat
.
format
(
"{0}.defaultValue=\"\""
+
" while column default=\"{1}\""
,
+
" while column default=\"{1}\""
,
JQColumn
.
class
.
getSimpleName
(),
col
.
defaultValue
)));
JQColumn
.
class
.
getSimpleName
(),
col
.
defaultValue
)));
}
else
if
(!
isNullOrEmpty
(
fieldDef
.
defaultValue
)
}
else
if
(!
StringUtils
.
isNullOrEmpty
(
fieldDef
.
defaultValue
)
&&
isNullOrEmpty
(
col
.
defaultValue
))
{
&&
StringUtils
.
isNullOrEmpty
(
col
.
defaultValue
))
{
// Column.defaultValue is NULL, Model.defaultValue is NOT NULL
// Column.defaultValue is NULL, Model.defaultValue is NOT NULL
remarks
.
add
(
warn
(
table
,
col
,
format
(
"{0}.defaultValue=\"{1}\""
remarks
.
add
(
warn
(
table
,
col
,
MessageFormat
.
format
(
"{0}.defaultValue=\"{1}\""
+
" while column default=\"\""
,
+
" while column default=\"\""
,
JQColumn
.
class
.
getSimpleName
(),
fieldDef
.
defaultValue
)));
JQColumn
.
class
.
getSimpleName
(),
fieldDef
.
defaultValue
)));
}
else
if
(!
isNullOrEmpty
(
fieldDef
.
defaultValue
)
}
else
if
(!
StringUtils
.
isNullOrEmpty
(
fieldDef
.
defaultValue
)
&&
!
isNullOrEmpty
(
col
.
defaultValue
))
{
&&
!
StringUtils
.
isNullOrEmpty
(
col
.
defaultValue
))
{
if
(!
fieldDef
.
defaultValue
.
equals
(
col
.
defaultValue
))
{
if
(!
fieldDef
.
defaultValue
.
equals
(
col
.
defaultValue
))
{
// Model.defaultValue != Column.defaultValue
// Model.defaultValue != Column.defaultValue
remarks
.
add
(
warn
(
table
,
col
,
format
(
"{0}.defaultValue=\"{1}\""
remarks
.
add
(
warn
(
table
,
col
,
MessageFormat
.
format
(
"{0}.defaultValue=\"{1}\""
+
" while column default=\"{2}\""
,
+
" while column default=\"{2}\""
,
JQColumn
.
class
.
getSimpleName
(),
fieldDef
.
defaultValue
,
JQColumn
.
class
.
getSimpleName
(),
fieldDef
.
defaultValue
,
col
.
defaultValue
)));
col
.
defaultValue
)));
...
@@ -532,7 +534,7 @@ public class TableInspector {
...
@@ -532,7 +534,7 @@ public class TableInspector {
if
(!
ModelUtils
.
isValidDefaultValue
(
fieldDef
.
field
.
getType
(),
if
(!
ModelUtils
.
isValidDefaultValue
(
fieldDef
.
field
.
getType
(),
fieldDef
.
defaultValue
))
{
fieldDef
.
defaultValue
))
{
remarks
.
add
(
error
(
table
,
col
,
remarks
.
add
(
error
(
table
,
col
,
format
(
"{0}.defaultValue=\"{1}\" is invalid!"
,
MessageFormat
.
format
(
"{0}.defaultValue=\"{1}\" is invalid!"
,
JQColumn
.
class
.
getSimpleName
(),
JQColumn
.
class
.
getSimpleName
(),
fieldDef
.
defaultValue
)));
fieldDef
.
defaultValue
)));
}
}
...
@@ -644,7 +646,7 @@ public class TableInspector {
...
@@ -644,7 +646,7 @@ public class TableInspector {
flat
.
append
(
'\"'
);
flat
.
append
(
'\"'
);
}
}
}
}
append
(
flat
);
append
(
flat
.
toString
()
);
append
(
" }"
);
append
(
" }"
);
}
else
{
}
else
{
if
(
value
instanceof
String
)
{
if
(
value
instanceof
String
)
{
...
...
h2/src/tools/org/h2/jaqu/TestCondition.java
浏览文件 @
74e99362
...
@@ -6,7 +6,7 @@
...
@@ -6,7 +6,7 @@
*/
*/
package
org
.
h2
.
jaqu
;
package
org
.
h2
.
jaqu
;
import
org.h2.jaqu.util.Utils
;
import
org.h2.jaqu.util.
Class
Utils
;
/**
/**
* This class represents an incomplete condition.
* This class represents an incomplete condition.
...
@@ -23,7 +23,7 @@ public class TestCondition<A> {
...
@@ -23,7 +23,7 @@ public class TestCondition<A> {
}
}
public
Boolean
is
(
A
y
)
{
public
Boolean
is
(
A
y
)
{
Boolean
o
=
Utils
.
newObject
(
Boolean
.
class
);
Boolean
o
=
Class
Utils
.
newObject
(
Boolean
.
class
);
return
Db
.
registerToken
(
o
,
new
Function
(
"="
,
x
,
y
)
{
return
Db
.
registerToken
(
o
,
new
Function
(
"="
,
x
,
y
)
{
public
<
T
>
void
appendSQL
(
SQLStatement
stat
,
Query
<
T
>
query
)
{
public
<
T
>
void
appendSQL
(
SQLStatement
stat
,
Query
<
T
>
query
)
{
stat
.
appendSQL
(
"("
);
stat
.
appendSQL
(
"("
);
...
@@ -36,7 +36,7 @@ public class TestCondition<A> {
...
@@ -36,7 +36,7 @@ public class TestCondition<A> {
}
}
public
Boolean
bigger
(
A
y
)
{
public
Boolean
bigger
(
A
y
)
{
Boolean
o
=
Utils
.
newObject
(
Boolean
.
class
);
Boolean
o
=
Class
Utils
.
newObject
(
Boolean
.
class
);
return
Db
.
registerToken
(
o
,
new
Function
(
">"
,
x
,
y
)
{
return
Db
.
registerToken
(
o
,
new
Function
(
">"
,
x
,
y
)
{
public
<
T
>
void
appendSQL
(
SQLStatement
stat
,
Query
<
T
>
query
)
{
public
<
T
>
void
appendSQL
(
SQLStatement
stat
,
Query
<
T
>
query
)
{
stat
.
appendSQL
(
"("
);
stat
.
appendSQL
(
"("
);
...
@@ -49,7 +49,7 @@ public class TestCondition<A> {
...
@@ -49,7 +49,7 @@ public class TestCondition<A> {
}
}
public
Boolean
biggerEqual
(
A
y
)
{
public
Boolean
biggerEqual
(
A
y
)
{
Boolean
o
=
Utils
.
newObject
(
Boolean
.
class
);
Boolean
o
=
Class
Utils
.
newObject
(
Boolean
.
class
);
return
Db
.
registerToken
(
o
,
new
Function
(
">="
,
x
,
y
)
{
return
Db
.
registerToken
(
o
,
new
Function
(
">="
,
x
,
y
)
{
public
<
T
>
void
appendSQL
(
SQLStatement
stat
,
Query
<
T
>
query
)
{
public
<
T
>
void
appendSQL
(
SQLStatement
stat
,
Query
<
T
>
query
)
{
stat
.
appendSQL
(
"("
);
stat
.
appendSQL
(
"("
);
...
@@ -62,7 +62,7 @@ public class TestCondition<A> {
...
@@ -62,7 +62,7 @@ public class TestCondition<A> {
}
}
public
Boolean
smaller
(
A
y
)
{
public
Boolean
smaller
(
A
y
)
{
Boolean
o
=
Utils
.
newObject
(
Boolean
.
class
);
Boolean
o
=
Class
Utils
.
newObject
(
Boolean
.
class
);
return
Db
.
registerToken
(
o
,
new
Function
(
"<"
,
x
,
y
)
{
return
Db
.
registerToken
(
o
,
new
Function
(
"<"
,
x
,
y
)
{
public
<
T
>
void
appendSQL
(
SQLStatement
stat
,
Query
<
T
>
query
)
{
public
<
T
>
void
appendSQL
(
SQLStatement
stat
,
Query
<
T
>
query
)
{
stat
.
appendSQL
(
"("
);
stat
.
appendSQL
(
"("
);
...
@@ -75,7 +75,7 @@ public class TestCondition<A> {
...
@@ -75,7 +75,7 @@ public class TestCondition<A> {
}
}
public
Boolean
smallerEqual
(
A
y
)
{
public
Boolean
smallerEqual
(
A
y
)
{
Boolean
o
=
Utils
.
newObject
(
Boolean
.
class
);
Boolean
o
=
Class
Utils
.
newObject
(
Boolean
.
class
);
return
Db
.
registerToken
(
o
,
new
Function
(
"<="
,
x
,
y
)
{
return
Db
.
registerToken
(
o
,
new
Function
(
"<="
,
x
,
y
)
{
public
<
T
>
void
appendSQL
(
SQLStatement
stat
,
Query
<
T
>
query
)
{
public
<
T
>
void
appendSQL
(
SQLStatement
stat
,
Query
<
T
>
query
)
{
stat
.
appendSQL
(
"("
);
stat
.
appendSQL
(
"("
);
...
@@ -88,7 +88,7 @@ public class TestCondition<A> {
...
@@ -88,7 +88,7 @@ public class TestCondition<A> {
}
}
public
Boolean
like
(
A
pattern
)
{
public
Boolean
like
(
A
pattern
)
{
Boolean
o
=
Utils
.
newObject
(
Boolean
.
class
);
Boolean
o
=
Class
Utils
.
newObject
(
Boolean
.
class
);
return
Db
.
registerToken
(
o
,
new
Function
(
"LIKE"
,
x
,
pattern
)
{
return
Db
.
registerToken
(
o
,
new
Function
(
"LIKE"
,
x
,
pattern
)
{
public
<
T
>
void
appendSQL
(
SQLStatement
stat
,
Query
<
T
>
query
)
{
public
<
T
>
void
appendSQL
(
SQLStatement
stat
,
Query
<
T
>
query
)
{
stat
.
appendSQL
(
"("
);
stat
.
appendSQL
(
"("
);
...
...
h2/src/tools/org/h2/jaqu/ValidationRemark.java
浏览文件 @
74e99362
...
@@ -8,7 +8,7 @@ package org.h2.jaqu;
...
@@ -8,7 +8,7 @@ package org.h2.jaqu;
import
org.h2.jaqu.TableDefinition.FieldDefinition
;
import
org.h2.jaqu.TableDefinition.FieldDefinition
;
import
org.h2.jaqu.TableInspector.ColumnInspector
;
import
org.h2.jaqu.TableInspector.ColumnInspector
;
import
org.h2.
jaqu.
util.StringUtils
;
import
org.h2.util.StringUtils
;
/**
/**
* A validation remark is a result of running a model validation. Each remark
* A validation remark is a result of running a model validation. Each remark
...
...
h2/src/tools/org/h2/jaqu/bytecode/ConstantString.java
浏览文件 @
74e99362
...
@@ -8,7 +8,7 @@ package org.h2.jaqu.bytecode;
...
@@ -8,7 +8,7 @@ package org.h2.jaqu.bytecode;
import
org.h2.jaqu.Query
;
import
org.h2.jaqu.Query
;
import
org.h2.jaqu.SQLStatement
;
import
org.h2.jaqu.SQLStatement
;
import
org.h2.
jaqu.
util.StringUtils
;
import
org.h2.util.StringUtils
;
/**
/**
* A string constant.
* A string constant.
...
...
h2/src/tools/org/h2/jaqu/util/ClassUtils.java
浏览文件 @
74e99362
...
@@ -6,34 +6,187 @@
...
@@ -6,34 +6,187 @@
*/
*/
package
org
.
h2
.
jaqu
.
util
;
package
org
.
h2
.
jaqu
.
util
;
//## Java 1.5 begin ##
import
java.io.IOException
;
import
java.io.Reader
;
import
java.io.StringWriter
;
import
java.lang.reflect.Constructor
;
import
java.math.BigDecimal
;
import
java.math.BigInteger
;
import
java.sql.Clob
;
import
java.util.ArrayList
;
import
java.util.Collection
;
import
java.util.Collections
;
import
java.util.HashMap
;
import
java.util.HashSet
;
import
java.util.IdentityHashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.concurrent.atomic.AtomicLong
;
import
org.h2.util.IOUtils
;
//## Java 1.5 end ##
/**
/**
* This utility class contains functions related to class loading.
* Generic utility methods.
* There is a mechanism to restrict class loading.
*/
*/
public
class
ClassUtils
{
public
class
ClassUtils
{
//## Java 1.5 begin ##
int
todoDelete
;
private
static
final
AtomicLong
COUNTER
=
new
AtomicLong
(
0
)
;
private
ClassUtils
()
{
private
static
final
boolean
MAKE_ACCESSIBLE
=
true
;
// utility class
}
//## Java 1.5 begin ##
private
static
final
int
BUFFER_BLOCK_SIZE
=
4
*
1024
;
@SuppressWarnings
(
"unchecked"
)
// public static <T> ArrayList<T> newArrayList() {
public
static
<
X
>
Class
<
X
>
getClass
(
X
x
)
{
// return new ArrayList<T>();
return
(
Class
<
X
>)
x
.
getClass
();
// }
//
// public static <T> ArrayList<T> newArrayList(Collection<T> c) {
// return new ArrayList<T>(c);
// }
//
// public static <T> HashSet<T> newHashSet() {
// return new HashSet<T>();
// }
// public static <T> HashSet<T> newHashSet(Collection<T> list) {
// return new HashSet<T>(list);
// }
//
// public static <A, B> HashMap<A, B> newHashMap() {
// return new HashMap<A, B>();
// }
//
// public static <A, B> Map<A, B> newSynchronizedHashMap() {
// HashMap<A, B> map = newHashMap();
// return Collections.synchronizedMap(map);
// }
public
static
<
A
,
B
>
IdentityHashMap
<
A
,
B
>
newIdentityHashMap
()
{
return
new
IdentityHashMap
<
A
,
B
>();
}
}
public
static
Class
<?>
loadClass
(
String
className
)
{
@SuppressWarnings
(
"unchecked"
)
public
static
<
T
>
T
newObject
(
Class
<
T
>
clazz
)
{
// must create new instances
if
(
clazz
==
Integer
.
class
)
{
return
(
T
)
new
Integer
((
int
)
COUNTER
.
getAndIncrement
());
}
else
if
(
clazz
==
String
.
class
)
{
return
(
T
)
(
""
+
COUNTER
.
getAndIncrement
());
}
else
if
(
clazz
==
Long
.
class
)
{
return
(
T
)
new
Long
(
COUNTER
.
getAndIncrement
());
}
else
if
(
clazz
==
Short
.
class
)
{
return
(
T
)
new
Short
((
short
)
COUNTER
.
getAndIncrement
());
}
else
if
(
clazz
==
Byte
.
class
)
{
return
(
T
)
new
Byte
((
byte
)
COUNTER
.
getAndIncrement
());
}
else
if
(
clazz
==
Float
.
class
)
{
return
(
T
)
new
Float
(
COUNTER
.
getAndIncrement
());
}
else
if
(
clazz
==
Double
.
class
)
{
return
(
T
)
new
Double
(
COUNTER
.
getAndIncrement
());
}
else
if
(
clazz
==
Boolean
.
class
)
{
return
(
T
)
new
Boolean
(
false
);
}
else
if
(
clazz
==
BigDecimal
.
class
)
{
return
(
T
)
new
BigDecimal
(
COUNTER
.
getAndIncrement
());
}
else
if
(
clazz
==
BigInteger
.
class
)
{
return
(
T
)
new
BigInteger
(
""
+
COUNTER
.
getAndIncrement
());
}
else
if
(
clazz
==
java
.
sql
.
Date
.
class
)
{
return
(
T
)
new
java
.
sql
.
Date
(
COUNTER
.
getAndIncrement
());
}
else
if
(
clazz
==
java
.
sql
.
Time
.
class
)
{
return
(
T
)
new
java
.
sql
.
Time
(
COUNTER
.
getAndIncrement
());
}
else
if
(
clazz
==
java
.
sql
.
Timestamp
.
class
)
{
return
(
T
)
new
java
.
sql
.
Timestamp
(
COUNTER
.
getAndIncrement
());
}
else
if
(
clazz
==
java
.
util
.
Date
.
class
)
{
return
(
T
)
new
java
.
util
.
Date
(
COUNTER
.
getAndIncrement
());
}
else
if
(
clazz
==
List
.
class
)
{
return
(
T
)
new
ArrayList
();
}
try
{
try
{
return
Class
.
forName
(
className
);
return
clazz
.
newInstance
(
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
e
);
if
(
MAKE_ACCESSIBLE
)
{
Constructor
[]
constructors
=
clazz
.
getDeclaredConstructors
();
// try 0 length constructors
for
(
Constructor
c
:
constructors
)
{
if
(
c
.
getParameterTypes
().
length
==
0
)
{
c
.
setAccessible
(
true
);
try
{
return
clazz
.
newInstance
();
}
catch
(
Exception
e2
)
{
// ignore
}
}
}
// try 1 length constructors
for
(
Constructor
c
:
constructors
)
{
if
(
c
.
getParameterTypes
().
length
==
1
)
{
c
.
setAccessible
(
true
);
try
{
return
(
T
)
c
.
newInstance
(
new
Object
[
1
]);
}
catch
(
Exception
e2
)
{
// ignore
}
}
}
}
throw
new
RuntimeException
(
"Exception trying to create "
+
clazz
.
getName
()
+
": "
+
e
,
e
);
}
}
}
}
//## Java 1.5 end ##
public
static
<
T
>
boolean
isSimpleType
(
Class
<
T
>
clazz
)
{
if
(
Number
.
class
.
isAssignableFrom
(
clazz
))
{
return
true
;
}
else
if
(
clazz
==
String
.
class
)
{
return
true
;
}
return
false
;
}
public
static
Object
convert
(
Object
o
,
Class
<?>
targetType
)
{
if
(
o
==
null
)
{
return
null
;
}
Class
<?>
currentType
=
o
.
getClass
();
if
(
targetType
.
isAssignableFrom
(
currentType
))
{
return
o
;
}
if
(
targetType
==
String
.
class
)
{
if
(
Clob
.
class
.
isAssignableFrom
(
currentType
))
{
Clob
c
=
(
Clob
)
o
;
try
{
Reader
r
=
c
.
getCharacterStream
();
return
IOUtils
.
readStringAndClose
(
r
,
-
1
);
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
"Error converting CLOB to String: "
+
e
.
toString
(),
e
);
}
}
return
o
.
toString
();
}
if
(
Number
.
class
.
isAssignableFrom
(
currentType
))
{
Number
n
=
(
Number
)
o
;
if
(
targetType
==
Byte
.
class
)
{
return
n
.
byteValue
();
}
else
if
(
targetType
==
Short
.
class
)
{
return
n
.
shortValue
();
}
else
if
(
targetType
==
Integer
.
class
)
{
return
n
.
intValue
();
}
else
if
(
targetType
==
Long
.
class
)
{
return
n
.
longValue
();
}
else
if
(
targetType
==
Double
.
class
)
{
return
n
.
doubleValue
();
}
else
if
(
targetType
==
Float
.
class
)
{
return
n
.
floatValue
();
}
}
throw
new
RuntimeException
(
"Can not convert the value "
+
o
+
" from "
+
currentType
+
" to "
+
targetType
);
}
@SuppressWarnings
(
"unchecked"
)
public
static
<
X
>
Class
<
X
>
getClass
(
X
x
)
{
return
(
Class
<
X
>)
x
.
getClass
();
}
//## Java 1.5 end ##
}
}
h2/src/tools/org/h2/jaqu/util/GenerateModels.java
浏览文件 @
74e99362
...
@@ -22,6 +22,8 @@ import java.util.regex.Pattern;
...
@@ -22,6 +22,8 @@ import java.util.regex.Pattern;
import
org.h2.jaqu.Db
;
import
org.h2.jaqu.Db
;
import
org.h2.jaqu.DbInspector
;
import
org.h2.jaqu.DbInspector
;
import
org.h2.message.DbException
;
import
org.h2.message.DbException
;
import
org.h2.util.JdbcUtils
;
import
org.h2.util.StringUtils
;
/**
/**
* Generates JaQu models.
* Generates JaQu models.
...
...
h2/src/tools/org/h2/jaqu/util/JdbcUtils.java
deleted
100644 → 0
浏览文件 @
e31a44a4
/*
* Copyright 2004-2011 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
.
util
;
import
java.sql.Connection
;
import
java.sql.DriverManager
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
import
java.util.Properties
;
import
javax.naming.Context
;
import
javax.sql.DataSource
;
import
javax.sql.XAConnection
;
/**
* This is a utility class with JDBC helper functions.
*/
public
class
JdbcUtils
{
private
static
final
int
todoDeleteClass
=
0
;
private
static
final
String
[]
DRIVERS
=
{
"h2:"
,
"org.h2.Driver"
,
"Cache:"
,
"com.intersys.jdbc.CacheDriver"
,
"daffodilDB://"
,
"in.co.daffodil.db.rmi.RmiDaffodilDBDriver"
,
"daffodil"
,
"in.co.daffodil.db.jdbc.DaffodilDBDriver"
,
"db2:"
,
"COM.ibm.db2.jdbc.net.DB2Driver"
,
"derby:net:"
,
"org.apache.derby.jdbc.ClientDriver"
,
"derby://"
,
"org.apache.derby.jdbc.ClientDriver"
,
"derby:"
,
"org.apache.derby.jdbc.EmbeddedDriver"
,
"FrontBase:"
,
"com.frontbase.jdbc.FBJDriver"
,
"firebirdsql:"
,
"org.firebirdsql.jdbc.FBDriver"
,
"hsqldb:"
,
"org.hsqldb.jdbcDriver"
,
"informix-sqli:"
,
"com.informix.jdbc.IfxDriver"
,
"jtds:"
,
"net.sourceforge.jtds.jdbc.Driver"
,
"microsoft:"
,
"com.microsoft.jdbc.sqlserver.SQLServerDriver"
,
"mimer:"
,
"com.mimer.jdbc.Driver"
,
"mysql:"
,
"com.mysql.jdbc.Driver"
,
"odbc:"
,
"sun.jdbc.odbc.JdbcOdbcDriver"
,
"oracle:"
,
"oracle.jdbc.driver.OracleDriver"
,
"pervasive:"
,
"com.pervasive.jdbc.v2.Driver"
,
"pointbase:micro:"
,
"com.pointbase.me.jdbc.jdbcDriver"
,
"pointbase:"
,
"com.pointbase.jdbc.jdbcUniversalDriver"
,
"postgresql:"
,
"org.postgresql.Driver"
,
"sybase:"
,
"com.sybase.jdbc3.jdbc.SybDriver"
,
"sqlserver:"
,
"com.microsoft.sqlserver.jdbc.SQLServerDriver"
,
"teradata:"
,
"com.ncr.teradata.TeraDriver"
,
};
private
JdbcUtils
()
{
// utility class
}
/**
* Close a statement without throwing an exception.
*
* @param stat the statement or null
*/
public
static
void
closeSilently
(
Statement
stat
)
{
if
(
stat
!=
null
)
{
try
{
stat
.
close
();
}
catch
(
SQLException
e
)
{
// ignore
}
}
}
/**
* Close a connection without throwing an exception.
*
* @param conn the connection or null
*/
public
static
void
closeSilently
(
Connection
conn
)
{
if
(
conn
!=
null
)
{
try
{
conn
.
close
();
}
catch
(
SQLException
e
)
{
// ignore
}
}
}
/**
* Close a result set without throwing an exception.
*
* @param rs the result set or null
*/
public
static
void
closeSilently
(
ResultSet
rs
)
{
closeSilently
(
rs
,
false
);
}
/**
* Close a result set, and optionally its statement without throwing an
* exception.
*
* @param rs the result set or null
*/
public
static
void
closeSilently
(
ResultSet
rs
,
boolean
closeStatement
)
{
if
(
rs
!=
null
)
{
Statement
stat
=
null
;
if
(
closeStatement
)
{
try
{
stat
=
rs
.
getStatement
();
}
catch
(
SQLException
e
)
{
//ignore
}
}
try
{
rs
.
close
();
}
catch
(
SQLException
e
)
{
// ignore
}
closeSilently
(
stat
);
}
}
/**
* Close an XA connection set without throwing an exception.
*
* @param conn the XA connection or null
*/
//## Java 1.4 begin ##
public
static
void
closeSilently
(
XAConnection
conn
)
{
if
(
conn
!=
null
)
{
try
{
conn
.
close
();
}
catch
(
SQLException
e
)
{
// ignore
}
}
}
//## Java 1.4 end ##
/**
* Open a new database connection with the given settings.
*
* @param driver the driver class name
* @param url the database URL
* @param user the user name
* @param password the password
* @return the database connection
*/
public
static
Connection
getConnection
(
String
driver
,
String
url
,
String
user
,
String
password
)
throws
SQLException
{
Properties
prop
=
new
Properties
();
if
(
user
!=
null
)
{
prop
.
setProperty
(
"user"
,
user
);
}
if
(
password
!=
null
)
{
prop
.
setProperty
(
"password"
,
password
);
}
return
getConnection
(
driver
,
url
,
prop
);
}
/**
* Escape table or schema patterns used for DatabaseMetaData functions.
*
* @param pattern the pattern
* @return the escaped pattern
*/
public
static
String
escapeMetaDataPattern
(
String
pattern
)
{
if
(
pattern
==
null
||
pattern
.
length
()
==
0
)
{
return
pattern
;
}
return
StringUtils
.
replaceAll
(
pattern
,
"\\"
,
"\\\\"
);
}
/**
* Open a new database connection with the given settings.
*
* @param driver the driver class name
* @param url the database URL
* @param prop the properties containing at least the user name and password
* @return the database connection
*/
public
static
Connection
getConnection
(
String
driver
,
String
url
,
Properties
prop
)
throws
SQLException
{
if
(
StringUtils
.
isNullOrEmpty
(
driver
))
{
JdbcUtils
.
load
(
url
);
}
else
{
Class
<?>
d
=
ClassUtils
.
loadClass
(
driver
);
if
(
java
.
sql
.
Driver
.
class
.
isAssignableFrom
(
d
))
{
return
DriverManager
.
getConnection
(
url
,
prop
);
//## Java 1.4 begin ##
}
else
if
(
javax
.
naming
.
Context
.
class
.
isAssignableFrom
(
d
))
{
// JNDI context
try
{
Context
context
=
(
Context
)
d
.
newInstance
();
DataSource
ds
=
(
DataSource
)
context
.
lookup
(
url
);
String
user
=
prop
.
getProperty
(
"user"
);
String
password
=
prop
.
getProperty
(
"password"
);
if
(
StringUtils
.
isNullOrEmpty
(
user
)
&&
StringUtils
.
isNullOrEmpty
(
password
))
{
return
ds
.
getConnection
();
}
return
ds
.
getConnection
(
user
,
password
);
}
catch
(
Exception
e
)
{
throw
Message
.
convert
(
e
);
}
//## Java 1.4 end ##
}
else
{
// Don't know, but maybe it loaded a JDBC Driver
return
DriverManager
.
getConnection
(
url
,
prop
);
}
}
return
DriverManager
.
getConnection
(
url
,
prop
);
}
/**
* Get the driver class name for the given URL, or null if the URL is
* unknown.
*
* @param url the database URL
* @return the driver class name
*/
public
static
String
getDriver
(
String
url
)
{
if
(
url
.
startsWith
(
"jdbc:"
))
{
url
=
url
.
substring
(
"jdbc:"
.
length
());
for
(
int
i
=
0
;
i
<
DRIVERS
.
length
;
i
+=
2
)
{
String
prefix
=
DRIVERS
[
i
];
if
(
url
.
startsWith
(
prefix
))
{
return
DRIVERS
[
i
+
1
];
}
}
}
return
null
;
}
/**
* Load the driver class for the given URL, if the database URL is known.
*
* @param url the database URL
*/
public
static
void
load
(
String
url
)
{
String
driver
=
getDriver
(
url
);
if
(
driver
!=
null
)
{
ClassUtils
.
loadClass
(
driver
);
}
}
}
h2/src/tools/org/h2/jaqu/util/StatementBuilder.java
deleted
100644 → 0
浏览文件 @
e31a44a4
/*
* Copyright 2004-2011 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
.
util
;
/**
* A utility class to build a statement. In addition to the methods supported by
* StringBuilder, it allows to add a text only in the second iteration. This
* simplified constructs such as:
* <pre>
* StringBuilder buff = new StringBuilder();
* for (int i = 0; i < args.length; i++) {
* if (i > 0) {
* buff.append(", ");
* }
* buff.append(args[i]);
* }
* </pre>
* to
* <pre>
* StatementBuilder buff = new StatementBuilder();
* for (String s : args) {
* buff.appendExceptFirst(", ");
* buff.append(a);
* }
*</pre>
*/
public
class
StatementBuilder
{
private
int
todoDelete
;
private
final
StringBuilder
builder
=
new
StringBuilder
();
private
int
index
;
/**
* Create a new builder.
*/
public
StatementBuilder
()
{
// nothing to do
}
/**
* Create a new builder.
*
* @param string the initial string
*/
public
StatementBuilder
(
String
string
)
{
builder
.
append
(
string
);
}
/**
* Append a text.
*
* @param s the text to append
* @return itself
*/
public
StatementBuilder
append
(
String
s
)
{
builder
.
append
(
s
);
return
this
;
}
/**
* Append a character.
*
* @param c the character to append
* @return itself
*/
public
StatementBuilder
append
(
char
c
)
{
builder
.
append
(
c
);
return
this
;
}
/**
* Append a number.
*
* @param x the number to append
* @return itself
*/
public
StatementBuilder
append
(
long
x
)
{
builder
.
append
(
x
);
return
this
;
}
/**
* Reset the loop counter.
*
* @return itself
*/
public
StatementBuilder
resetCount
()
{
index
=
0
;
return
this
;
}
/**
* Append a text, but only if appendExceptFirst was never called.
*
* @param s the text to append
*/
public
void
appendOnlyFirst
(
String
s
)
{
if
(
index
==
0
)
{
builder
.
append
(
s
);
}
}
/**
* Append a text, except when this method is called the first time.
*
* @param s the text to append
*/
public
void
appendExceptFirst
(
String
s
)
{
if
(
index
++
>
0
)
{
builder
.
append
(
s
);
}
}
public
void
append
(
StatementBuilder
sb
)
{
builder
.
append
(
sb
);
}
public
void
insert
(
int
offset
,
char
c
)
{
builder
.
insert
(
offset
,
c
);
}
public
String
toString
()
{
return
builder
.
toString
();
}
/**
* Get the length.
*
* @return the length
*/
public
int
length
()
{
return
builder
.
length
();
}
}
h2/src/tools/org/h2/jaqu/util/StringUtils.java
deleted
100644 → 0
浏览文件 @
e31a44a4
/*
* Copyright 2004-2011 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
.
util
;
/**
* Common string utilities. I expect that this class will be removed.
*
*/
public
class
StringUtils
{
private
int
todoDelete
;
/**
* Replace all occurrences of the before string with the after string.
*
* @param s the string
* @param before the old text
* @param after the new text
* @return the string with the before string replaced
*/
public
static
String
replaceAll
(
String
s
,
String
before
,
String
after
)
{
int
next
=
s
.
indexOf
(
before
);
if
(
next
<
0
)
{
return
s
;
}
StringBuilder
buff
=
new
StringBuilder
(
s
.
length
()
-
before
.
length
()
+
after
.
length
());
int
index
=
0
;
while
(
true
)
{
buff
.
append
(
s
.
substring
(
index
,
next
)).
append
(
after
);
index
=
next
+
before
.
length
();
next
=
s
.
indexOf
(
before
,
index
);
if
(
next
<
0
)
{
buff
.
append
(
s
.
substring
(
index
));
break
;
}
}
return
buff
.
toString
();
}
/**
* Check if a String is null or empty (the length is null).
*
* @param s the string to check
* @return true if it is null or empty
*/
public
static
boolean
isNullOrEmpty
(
String
s
)
{
return
s
==
null
||
s
.
length
()
==
0
;
}
/**
* Convert a string to a Java literal using the correct escape sequences.
* The literal is not enclosed in double quotes. The result can be used in
* properties files or in Java source code.
*
* @param s the text to convert
* @return the Java representation
*/
public
static
String
javaEncode
(
String
s
)
{
int
length
=
s
.
length
();
StringBuilder
buff
=
new
StringBuilder
(
length
);
for
(
int
i
=
0
;
i
<
length
;
i
++)
{
char
c
=
s
.
charAt
(
i
);
switch
(
c
)
{
// case '\b':
// // BS backspace
// // not supported in properties files
// buff.append("\\b");
// break;
case
'\t'
:
// HT horizontal tab
buff
.
append
(
"\\t"
);
break
;
case
'\n'
:
// LF linefeed
buff
.
append
(
"\\n"
);
break
;
case
'\f'
:
// FF form feed
buff
.
append
(
"\\f"
);
break
;
case
'\r'
:
// CR carriage return
buff
.
append
(
"\\r"
);
break
;
case
'"'
:
// double quote
buff
.
append
(
"\\\""
);
break
;
case
'\\'
:
// backslash
buff
.
append
(
"\\\\"
);
break
;
default
:
int
ch
=
c
&
0xffff
;
if
(
ch
>=
' '
&&
(
ch
<
0x80
))
{
buff
.
append
(
c
);
// not supported in properties files
// } else if(ch < 0xff) {
// buff.append("\\");
// // make sure it's three characters (0x200 is octal 1000)
// buff.append(Integer.toOctalString(0x200 |
// ch).substring(1));
}
else
{
buff
.
append
(
"\\u"
);
// make sure it's four characters
buff
.
append
(
Integer
.
toHexString
(
0x10000
|
ch
).
substring
(
1
));
}
}
}
return
buff
.
toString
();
}
/**
* Pad a string. This method is used for the SQL function RPAD and LPAD.
*
* @param string the original string
* @param n the target length
* @param padding the padding string
* @param right true if the padding should be appended at the end
* @return the padded string
*/
public
static
String
pad
(
String
string
,
int
n
,
String
padding
,
boolean
right
)
{
if
(
n
<
0
)
{
n
=
0
;
}
if
(
n
<
string
.
length
())
{
return
string
.
substring
(
0
,
n
);
}
else
if
(
n
==
string
.
length
())
{
return
string
;
}
char
paddingChar
;
if
(
padding
==
null
||
padding
.
length
()
==
0
)
{
paddingChar
=
' '
;
}
else
{
paddingChar
=
padding
.
charAt
(
0
);
}
StringBuilder
buff
=
new
StringBuilder
(
n
);
n
-=
string
.
length
();
if
(
right
)
{
buff
.
append
(
string
);
}
for
(
int
i
=
0
;
i
<
n
;
i
++)
{
buff
.
append
(
paddingChar
);
}
if
(!
right
)
{
buff
.
append
(
string
);
}
return
buff
.
toString
();
}
/**
* Convert a string to a SQL literal. Null is converted to NULL. The text is
* enclosed in single quotes. If there are any special characters, the method
* STRINGDECODE is used.
*
* @param s the text to convert.
* @return the SQL literal
*/
public
static
String
quoteStringSQL
(
String
s
)
{
if
(
s
==
null
)
{
return
"NULL"
;
}
int
length
=
s
.
length
();
StringBuilder
buff
=
new
StringBuilder
(
length
+
2
);
buff
.
append
(
'\''
);
for
(
int
i
=
0
;
i
<
length
;
i
++)
{
char
c
=
s
.
charAt
(
i
);
if
(
c
==
'\''
)
{
buff
.
append
(
c
);
}
else
if
(
c
<
' '
||
c
>
127
)
{
// need to start from the beginning because maybe there was a \
// that was not quoted
return
"STRINGDECODE("
+
quoteStringSQL
(
javaEncode
(
s
))
+
")"
;
}
buff
.
append
(
c
);
}
buff
.
append
(
'\''
);
return
buff
.
toString
();
}
}
h2/src/tools/org/h2/jaqu/util/Utils.java
deleted
100644 → 0
浏览文件 @
e31a44a4
/*
* Copyright 2004-2011 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
.
util
;
//## Java 1.5 begin ##
import
java.io.IOException
;
import
java.io.Reader
;
import
java.io.StringWriter
;
import
java.lang.reflect.Constructor
;
import
java.math.BigDecimal
;
import
java.math.BigInteger
;
import
java.sql.Clob
;
import
java.util.ArrayList
;
import
java.util.Collection
;
import
java.util.Collections
;
import
java.util.HashMap
;
import
java.util.HashSet
;
import
java.util.IdentityHashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.concurrent.atomic.AtomicLong
;
//## Java 1.5 end ##
/**
* Generic utility methods.
*/
public
class
Utils
{
//## Java 1.5 begin ##
private
static
final
AtomicLong
COUNTER
=
new
AtomicLong
(
0
);
private
static
final
boolean
MAKE_ACCESSIBLE
=
true
;
private
static
final
int
BUFFER_BLOCK_SIZE
=
4
*
1024
;
public
static
<
T
>
ArrayList
<
T
>
newArrayList
()
{
return
new
ArrayList
<
T
>();
}
public
static
<
T
>
ArrayList
<
T
>
newArrayList
(
Collection
<
T
>
c
)
{
return
new
ArrayList
<
T
>(
c
);
}
public
static
<
T
>
HashSet
<
T
>
newHashSet
()
{
return
new
HashSet
<
T
>();
}
public
static
<
T
>
HashSet
<
T
>
newHashSet
(
Collection
<
T
>
list
)
{
return
new
HashSet
<
T
>(
list
);
}
public
static
<
A
,
B
>
HashMap
<
A
,
B
>
newHashMap
()
{
return
new
HashMap
<
A
,
B
>();
}
public
static
<
A
,
B
>
Map
<
A
,
B
>
newSynchronizedHashMap
()
{
HashMap
<
A
,
B
>
map
=
newHashMap
();
return
Collections
.
synchronizedMap
(
map
);
}
public
static
<
A
,
B
>
IdentityHashMap
<
A
,
B
>
newIdentityHashMap
()
{
return
new
IdentityHashMap
<
A
,
B
>();
}
@SuppressWarnings
(
"unchecked"
)
public
static
<
T
>
T
newObject
(
Class
<
T
>
clazz
)
{
// must create new instances
if
(
clazz
==
Integer
.
class
)
{
return
(
T
)
new
Integer
((
int
)
COUNTER
.
getAndIncrement
());
}
else
if
(
clazz
==
String
.
class
)
{
return
(
T
)
(
""
+
COUNTER
.
getAndIncrement
());
}
else
if
(
clazz
==
Long
.
class
)
{
return
(
T
)
new
Long
(
COUNTER
.
getAndIncrement
());
}
else
if
(
clazz
==
Short
.
class
)
{
return
(
T
)
new
Short
((
short
)
COUNTER
.
getAndIncrement
());
}
else
if
(
clazz
==
Byte
.
class
)
{
return
(
T
)
new
Byte
((
byte
)
COUNTER
.
getAndIncrement
());
}
else
if
(
clazz
==
Float
.
class
)
{
return
(
T
)
new
Float
(
COUNTER
.
getAndIncrement
());
}
else
if
(
clazz
==
Double
.
class
)
{
return
(
T
)
new
Double
(
COUNTER
.
getAndIncrement
());
}
else
if
(
clazz
==
Boolean
.
class
)
{
return
(
T
)
new
Boolean
(
false
);
}
else
if
(
clazz
==
BigDecimal
.
class
)
{
return
(
T
)
new
BigDecimal
(
COUNTER
.
getAndIncrement
());
}
else
if
(
clazz
==
BigInteger
.
class
)
{
return
(
T
)
new
BigInteger
(
""
+
COUNTER
.
getAndIncrement
());
}
else
if
(
clazz
==
java
.
sql
.
Date
.
class
)
{
return
(
T
)
new
java
.
sql
.
Date
(
COUNTER
.
getAndIncrement
());
}
else
if
(
clazz
==
java
.
sql
.
Time
.
class
)
{
return
(
T
)
new
java
.
sql
.
Time
(
COUNTER
.
getAndIncrement
());
}
else
if
(
clazz
==
java
.
sql
.
Timestamp
.
class
)
{
return
(
T
)
new
java
.
sql
.
Timestamp
(
COUNTER
.
getAndIncrement
());
}
else
if
(
clazz
==
java
.
util
.
Date
.
class
)
{
return
(
T
)
new
java
.
util
.
Date
(
COUNTER
.
getAndIncrement
());
}
else
if
(
clazz
==
List
.
class
)
{
return
(
T
)
new
ArrayList
();
}
try
{
return
clazz
.
newInstance
();
}
catch
(
Exception
e
)
{
if
(
MAKE_ACCESSIBLE
)
{
Constructor
[]
constructors
=
clazz
.
getDeclaredConstructors
();
// try 0 length constructors
for
(
Constructor
c
:
constructors
)
{
if
(
c
.
getParameterTypes
().
length
==
0
)
{
c
.
setAccessible
(
true
);
try
{
return
clazz
.
newInstance
();
}
catch
(
Exception
e2
)
{
// ignore
}
}
}
// try 1 length constructors
for
(
Constructor
c
:
constructors
)
{
if
(
c
.
getParameterTypes
().
length
==
1
)
{
c
.
setAccessible
(
true
);
try
{
return
(
T
)
c
.
newInstance
(
new
Object
[
1
]);
}
catch
(
Exception
e2
)
{
// ignore
}
}
}
}
throw
new
RuntimeException
(
"Exception trying to create "
+
clazz
.
getName
()
+
": "
+
e
,
e
);
}
}
public
static
<
T
>
boolean
isSimpleType
(
Class
<
T
>
clazz
)
{
if
(
Number
.
class
.
isAssignableFrom
(
clazz
))
{
return
true
;
}
else
if
(
clazz
==
String
.
class
)
{
return
true
;
}
return
false
;
}
public
static
Object
convert
(
Object
o
,
Class
<?>
targetType
)
{
if
(
o
==
null
)
{
return
null
;
}
Class
<?>
currentType
=
o
.
getClass
();
if
(
targetType
.
isAssignableFrom
(
currentType
))
{
return
o
;
}
if
(
targetType
==
String
.
class
)
{
if
(
Clob
.
class
.
isAssignableFrom
(
currentType
))
{
Clob
c
=
(
Clob
)
o
;
try
{
Reader
r
=
c
.
getCharacterStream
();
return
readStringAndClose
(
r
,
-
1
);
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
"Error converting CLOB to String: "
+
e
.
toString
(),
e
);
}
}
return
o
.
toString
();
}
if
(
Number
.
class
.
isAssignableFrom
(
currentType
))
{
Number
n
=
(
Number
)
o
;
if
(
targetType
==
Byte
.
class
)
{
return
n
.
byteValue
();
}
else
if
(
targetType
==
Short
.
class
)
{
return
n
.
shortValue
();
}
else
if
(
targetType
==
Integer
.
class
)
{
return
n
.
intValue
();
}
else
if
(
targetType
==
Long
.
class
)
{
return
n
.
longValue
();
}
else
if
(
targetType
==
Double
.
class
)
{
return
n
.
doubleValue
();
}
else
if
(
targetType
==
Float
.
class
)
{
return
n
.
floatValue
();
}
}
throw
new
RuntimeException
(
"Can not convert the value "
+
o
+
" from "
+
currentType
+
" to "
+
targetType
);
}
/**
* Read a number of characters from a reader and close it.
*
* @param in the reader
* @param length the maximum number of characters to read, or -1 to read
* until the end of file
* @return the string read
*/
public
static
String
readStringAndClose
(
Reader
in
,
int
length
)
throws
IOException
{
try
{
if
(
length
<=
0
)
{
length
=
Integer
.
MAX_VALUE
;
}
int
block
=
Math
.
min
(
BUFFER_BLOCK_SIZE
,
length
);
StringWriter
out
=
new
StringWriter
(
length
==
Integer
.
MAX_VALUE
?
block
:
length
);
char
[]
buff
=
new
char
[
block
];
while
(
length
>
0
)
{
int
len
=
Math
.
min
(
block
,
length
);
len
=
in
.
read
(
buff
,
0
,
len
);
if
(
len
<
0
)
{
break
;
}
out
.
write
(
buff
,
0
,
len
);
length
-=
len
;
}
return
out
.
toString
();
}
finally
{
in
.
close
();
}
}
//## Java 1.5 end ##
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论