Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
8a0f4354
提交
8a0f4354
authored
9月 22, 2009
作者:
Thomas Mueller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Issue 121: JaQu simple update and merge methods
上级
279da7c5
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
98 行增加
和
8 行删除
+98
-8
Db.java
h2/src/tools/org/h2/jaqu/Db.java
+10
-0
TableDefinition.java
h2/src/tools/org/h2/jaqu/TableDefinition.java
+87
-7
Utils.java
h2/src/tools/org/h2/jaqu/util/Utils.java
+1
-1
没有找到文件。
h2/src/tools/org/h2/jaqu/Db.java
浏览文件 @
8a0f4354
...
...
@@ -85,6 +85,16 @@ public class Db {
define
(
clazz
).
createTableIfRequired
(
this
).
insert
(
this
,
t
);
}
public
<
T
>
void
merge
(
T
t
)
{
Class
<
?
>
clazz
=
t
.
getClass
();
define
(
clazz
).
createTableIfRequired
(
this
).
merge
(
this
,
t
);
}
public
<
T
>
void
update
(
T
t
)
{
Class
<
?
>
clazz
=
t
.
getClass
();
define
(
clazz
).
createTableIfRequired
(
this
).
update
(
this
,
t
);
}
public
<
T
extends
Object
>
Query
<
T
>
from
(
T
alias
)
{
Class
<
?
>
clazz
=
alias
.
getClass
();
define
(
clazz
).
createTableIfRequired
(
this
);
...
...
h2/src/tools/org/h2/jaqu/TableDefinition.java
浏览文件 @
8a0f4354
...
...
@@ -12,6 +12,7 @@ import java.sql.ResultSet;
import
java.sql.SQLException
;
import
java.util.ArrayList
;
import
java.util.IdentityHashMap
;
import
java.util.List
;
import
java.util.Map
;
import
org.h2.jaqu.util.Utils
;
...
...
@@ -34,7 +35,7 @@ class TableDefinition<T> {
static
class
IndexDefinition
{
boolean
unique
;
String
indexName
;
String
[]
columnNames
;
List
<
String
>
columnNames
;
}
//## Java 1.5 end ##
...
...
@@ -47,6 +48,7 @@ class TableDefinition<T> {
Field
field
;
String
dataType
;
int
maxLength
;
boolean
isPrimaryKey
;
Object
getValue
(
Object
obj
)
{
try
{
...
...
@@ -84,7 +86,7 @@ class TableDefinition<T> {
private
ArrayList
<
FieldDefinition
>
fields
=
Utils
.
newArrayList
();
private
IdentityHashMap
<
Object
,
FieldDefinition
>
fieldMap
=
Utils
.
newIdentityHashMap
();
private
String
[]
primaryKeyColumnNames
;
private
List
<
String
>
primaryKeyColumnNames
;
private
ArrayList
<
IndexDefinition
>
indexes
=
Utils
.
newArrayList
();
TableDefinition
(
Class
<
T
>
clazz
)
{
...
...
@@ -98,6 +100,11 @@ class TableDefinition<T> {
void
setPrimaryKey
(
Object
[]
primaryKeyColumns
)
{
this
.
primaryKeyColumnNames
=
mapColumnNames
(
primaryKeyColumns
);
// set isPrimaryKey flag for all field definitions
for
(
FieldDefinition
fieldDefinition
:
fieldMap
.
values
())
{
fieldDefinition
.
isPrimaryKey
=
this
.
primaryKeyColumnNames
.
contains
(
fieldDefinition
.
columnName
);
}
}
<
A
>
String
getColumnName
(
A
fieldObject
)
{
...
...
@@ -105,11 +112,10 @@ class TableDefinition<T> {
return
def
==
null
?
null
:
def
.
columnName
;
}
private
String
[]
mapColumnNames
(
Object
[]
columns
)
{
int
len
=
columns
.
length
;
String
[]
columnNames
=
new
String
[
len
];
for
(
int
i
=
0
;
i
<
len
;
i
++)
{
columnNames
[
i
]
=
getColumnName
(
columns
[
i
]);
private
List
<
String
>
mapColumnNames
(
Object
[]
columns
)
{
List
<
String
>
columnNames
=
Utils
.
newArrayList
();
for
(
Object
column
:
columns
)
{
columnNames
.
add
(
getColumnName
(
column
));
}
return
columnNames
;
}
...
...
@@ -186,6 +192,80 @@ class TableDefinition<T> {
stat
.
executeUpdate
();
}
void
merge
(
Db
db
,
Object
obj
)
{
if
(
primaryKeyColumnNames
==
null
||
primaryKeyColumnNames
.
size
()
==
0
)
{
throw
new
IllegalStateException
(
"No primary key columns defined "
+
"for table "
+
obj
.
getClass
()
+
" - no update possible"
);
}
SQLStatement
stat
=
new
SQLStatement
(
db
);
StatementBuilder
buff
=
new
StatementBuilder
(
"MERGE INTO "
);
buff
.
append
(
tableName
).
append
(
" ("
);
buff
.
resetCount
();
for
(
FieldDefinition
field
:
fields
)
{
buff
.
appendExceptFirst
(
", "
);
buff
.
append
(
field
.
columnName
);
}
buff
.
append
(
") KEY("
);
buff
.
resetCount
();
for
(
FieldDefinition
field
:
fields
)
{
if
(
field
.
isPrimaryKey
)
{
buff
.
appendExceptFirst
(
", "
);
buff
.
append
(
field
.
columnName
);
}
}
buff
.
append
(
") "
);
buff
.
resetCount
();
buff
.
append
(
"VALUES ("
);
for
(
FieldDefinition
field
:
fields
)
{
buff
.
appendExceptFirst
(
", "
);
buff
.
append
(
'?'
);
Object
value
=
field
.
getValue
(
obj
);
stat
.
addParameter
(
value
);
}
buff
.
append
(
')'
);
stat
.
setSQL
(
buff
.
toString
());
stat
.
executeUpdate
();
}
void
update
(
Db
db
,
Object
obj
)
{
if
(
primaryKeyColumnNames
==
null
||
primaryKeyColumnNames
.
size
()
==
0
)
{
throw
new
IllegalStateException
(
"No primary key columns defined "
+
"for table "
+
obj
.
getClass
()
+
" - no update possible"
);
}
SQLStatement
stat
=
new
SQLStatement
(
db
);
StatementBuilder
buff
=
new
StatementBuilder
(
"UPDATE "
);
buff
.
append
(
tableName
).
append
(
" SET "
);
buff
.
resetCount
();
for
(
FieldDefinition
field
:
fields
)
{
if
(!
field
.
isPrimaryKey
)
{
buff
.
appendExceptFirst
(
", "
);
buff
.
append
(
field
.
columnName
);
buff
.
append
(
" = ?"
);
Object
value
=
field
.
getValue
(
obj
);
stat
.
addParameter
(
value
);
}
}
Object
alias
=
Utils
.
newObject
(
obj
.
getClass
());
Query
<
Object
>
query
=
Query
.
from
(
db
,
alias
);
boolean
firstCondition
=
true
;
for
(
FieldDefinition
field
:
fields
)
{
if
(
field
.
isPrimaryKey
)
{
Object
aliasValue
=
field
.
getValue
(
alias
);
Object
value
=
field
.
getValue
(
obj
);
if
(!
firstCondition
)
{
query
.
addConditionToken
(
ConditionAndOr
.
AND
);
}
firstCondition
=
false
;
query
.
addConditionToken
(
new
Condition
<
Object
>(
aliasValue
,
value
,
CompareType
.
EQUAL
));
}
}
stat
.
setSQL
(
buff
.
toString
());
query
.
appendWhere
(
stat
);
stat
.
executeUpdate
();
}
TableDefinition
<
T
>
createTableIfRequired
(
Db
db
)
{
SQLStatement
stat
=
new
SQLStatement
(
db
);
StatementBuilder
buff
=
new
StatementBuilder
(
"CREATE TABLE IF NOT EXISTS "
);
...
...
h2/src/tools/org/h2/jaqu/util/Utils.java
浏览文件 @
8a0f4354
...
...
@@ -130,7 +130,7 @@ public class Utils {
return
null
;
}
Class
<
?
>
currentType
=
o
.
getClass
();
if
(
currentType
==
targetType
)
{
if
(
targetType
.
isAssignableFrom
(
currentType
)
)
{
return
o
;
}
if
(
targetType
==
String
.
class
)
{
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论