Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
Shardingsphere
提交
61b90a38
Shardingsphere
项目概览
apache
/
Shardingsphere
通知
56
Star
3
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Shardingsphere
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
61b90a38
编写于
3月 14, 2018
作者:
L
liu ze jian
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
提交tab 退格问题
上级
ddf0c75c
变更
16
展开全部
隐藏空白更改
内联
并排
Showing
16 changed file
with
1018 addition
and
1019 deletion
+1018
-1019
sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/StartTest.java
...-core/src/test/java/io/shardingjdbc/dbtest/StartTest.java
+62
-62
sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/common/ConfigRuntime.java
...est/java/io/shardingjdbc/dbtest/common/ConfigRuntime.java
+98
-98
sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/common/ConfigUtils.java
.../test/java/io/shardingjdbc/dbtest/common/ConfigUtils.java
+22
-22
sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/common/DatabaseTypeUtils.java
...java/io/shardingjdbc/dbtest/common/DatabaseTypeUtils.java
+9
-9
sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/common/DatabaseUtils.java
...est/java/io/shardingjdbc/dbtest/common/DatabaseUtils.java
+446
-446
sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/common/FileUtils.java
...rc/test/java/io/shardingjdbc/dbtest/common/FileUtils.java
+53
-53
sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/common/PathUtils.java
...rc/test/java/io/shardingjdbc/dbtest/common/PathUtils.java
+31
-31
sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/common/XMLUtil.java
.../src/test/java/io/shardingjdbc/dbtest/common/XMLUtil.java
+35
-35
sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/config/AnalyzeConfig.java
...est/java/io/shardingjdbc/dbtest/config/AnalyzeConfig.java
+6
-6
sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/config/bean/ParameterDefinition.java
.../shardingjdbc/dbtest/config/bean/ParameterDefinition.java
+0
-1
sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/config/bean/parseContext/AggregationSelectItem.java
...btest/config/bean/parseContext/AggregationSelectItem.java
+12
-12
sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/data/AnalyzeDataset.java
...test/java/io/shardingjdbc/dbtest/data/AnalyzeDataset.java
+43
-43
sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/data/DatasetDefinition.java
...t/java/io/shardingjdbc/dbtest/data/DatasetDefinition.java
+8
-8
sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/dataSource/DataSourceUtil.java
...ava/io/shardingjdbc/dbtest/dataSource/DataSourceUtil.java
+30
-30
sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/exception/DbTestException.java
...ava/io/shardingjdbc/dbtest/exception/DbTestException.java
+2
-2
sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/init/InItCreateSchema.java
...st/java/io/shardingjdbc/dbtest/init/InItCreateSchema.java
+161
-161
未找到文件。
sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/StartTest.java
浏览文件 @
61b90a38
...
...
@@ -28,67 +28,67 @@ import io.shardingjdbc.dbtest.init.InItCreateSchema;
@RunWith
(
value
=
Parameterized
.
class
)
public
class
StartTest
{
private
String
path
;
private
String
id
;
public
StartTest
(
final
String
path
,
final
String
id
)
{
this
.
path
=
path
;
this
.
id
=
id
;
}
@Parameters
public
static
Collection
<
String
[]>
getParams
()
{
String
assertPath
=
ConfigRuntime
.
getAssertPath
();
assertPath
=
PathUtils
.
getPath
(
assertPath
);
List
<
String
>
paths
=
FileUtils
.
getAllFilePaths
(
new
File
(
assertPath
),
"assert-"
,
"xml"
);
List
<
String
[]>
result
=
new
ArrayList
<>();
try
{
for
(
String
each
:
paths
)
{
AssertsDefinition
assertsDefinition
=
AnalyzeConfig
.
analyze
(
each
);
List
<
AssertDefinition
>
asserts
=
assertsDefinition
.
getAsserts
();
List
<
String
>
ls
=
new
ArrayList
<>();
for
(
AssertDefinition
eachAssertDefinition
:
asserts
)
{
if
(
ls
.
contains
(
eachAssertDefinition
.
getId
()))
{
throw
new
DbTestException
(
"ID can't be repeated"
);
}
result
.
add
(
new
String
[]
{
each
,
eachAssertDefinition
.
getId
()
});
}
AssertEngine
.
addAssertDefinition
(
each
,
assertsDefinition
);
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
catch
(
JAXBException
e
)
{
e
.
printStackTrace
();
}
return
result
;
}
@BeforeClass
public
static
void
beforeClass
()
{
if
(
ConfigRuntime
.
isInitialized
())
{
InItCreateSchema
.
createDatabase
();
InItCreateSchema
.
initTable
();
}
}
@Test
public
void
test
()
{
try
{
AssertEngine
.
runAssert
(
path
,
id
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
@AfterClass
public
static
void
afterClass
()
{
if
(
ConfigRuntime
.
isInitialized
())
{
InItCreateSchema
.
dropDatabase
();
}
}
private
String
path
;
private
String
id
;
public
StartTest
(
final
String
path
,
final
String
id
)
{
this
.
path
=
path
;
this
.
id
=
id
;
}
@Parameters
public
static
Collection
<
String
[]>
getParams
()
{
String
assertPath
=
ConfigRuntime
.
getAssertPath
();
assertPath
=
PathUtils
.
getPath
(
assertPath
);
List
<
String
>
paths
=
FileUtils
.
getAllFilePaths
(
new
File
(
assertPath
),
"assert-"
,
"xml"
);
List
<
String
[]>
result
=
new
ArrayList
<>();
try
{
for
(
String
each
:
paths
)
{
AssertsDefinition
assertsDefinition
=
AnalyzeConfig
.
analyze
(
each
);
List
<
AssertDefinition
>
asserts
=
assertsDefinition
.
getAsserts
();
List
<
String
>
ls
=
new
ArrayList
<>();
for
(
AssertDefinition
eachAssertDefinition
:
asserts
)
{
if
(
ls
.
contains
(
eachAssertDefinition
.
getId
()))
{
throw
new
DbTestException
(
"ID can't be repeated"
);
}
result
.
add
(
new
String
[]{
each
,
eachAssertDefinition
.
getId
()
});
}
AssertEngine
.
addAssertDefinition
(
each
,
assertsDefinition
);
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
catch
(
JAXBException
e
)
{
e
.
printStackTrace
();
}
return
result
;
}
@BeforeClass
public
static
void
beforeClass
()
{
if
(
ConfigRuntime
.
isInitialized
())
{
InItCreateSchema
.
createDatabase
();
InItCreateSchema
.
initTable
();
}
}
@Test
public
void
test
()
{
try
{
AssertEngine
.
runAssert
(
path
,
id
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
@AfterClass
public
static
void
afterClass
()
{
if
(
ConfigRuntime
.
isInitialized
())
{
InItCreateSchema
.
dropDatabase
();
}
}
}
\ No newline at end of file
sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/common/ConfigRuntime.java
浏览文件 @
61b90a38
...
...
@@ -9,110 +9,110 @@ import java.util.Set;
public
class
ConfigRuntime
{
public
static
Set
<
String
>
getDbs
()
{
Map
<
String
,
String
>
dbs
=
ConfigUtils
.
getDatas
(
"database"
);
Set
<
String
>
result
=
new
HashSet
<>();
for
(
Map
.
Entry
<
String
,
String
>
each
:
dbs
.
entrySet
())
{
String
[]
dbsts
=
each
.
getKey
().
split
(
"\\."
);
result
.
add
(
dbsts
[
1
]);
}
return
result
;
}
public
static
Set
<
String
>
getDbs
()
{
Map
<
String
,
String
>
dbs
=
ConfigUtils
.
getDatas
(
"database"
);
Set
<
String
>
result
=
new
HashSet
<>();
for
(
Map
.
Entry
<
String
,
String
>
each
:
dbs
.
entrySet
())
{
String
[]
dbsts
=
each
.
getKey
().
split
(
"\\."
);
result
.
add
(
dbsts
[
1
]);
}
return
result
;
}
private
static
String
getJdbcConfig
(
final
String
key
)
{
return
ConfigUtils
.
getString
(
"database."
+
key
,
""
);
}
private
static
String
getJdbcConfig
(
final
String
key
)
{
return
ConfigUtils
.
getString
(
"database."
+
key
,
""
);
}
public
static
String
getDriverClassName
(
final
DatabaseType
type
)
{
switch
(
type
)
{
case
H2:
return
getJdbcConfig
(
"h2.driver"
);
case
MySQL:
return
getJdbcConfig
(
"mysql.driver"
);
case
Oracle:
return
getJdbcConfig
(
"oracle.driver"
);
case
SQLServer:
return
getJdbcConfig
(
"sqlserver.driver"
);
case
PostgreSQL:
return
getJdbcConfig
(
"postgresql.driver"
);
default
:
return
getJdbcConfig
(
"h2.driver"
);
}
}
public
static
String
getDriverClassName
(
final
DatabaseType
type
)
{
switch
(
type
)
{
case
H2:
return
getJdbcConfig
(
"h2.driver"
);
case
MySQL:
return
getJdbcConfig
(
"mysql.driver"
);
case
Oracle:
return
getJdbcConfig
(
"oracle.driver"
);
case
SQLServer:
return
getJdbcConfig
(
"sqlserver.driver"
);
case
PostgreSQL:
return
getJdbcConfig
(
"postgresql.driver"
);
default
:
return
getJdbcConfig
(
"h2.driver"
);
}
}
public
static
String
getURL
(
final
DatabaseType
type
,
final
String
dbName
)
{
switch
(
type
)
{
case
H2:
return
String
.
format
(
getJdbcConfig
(
"h2.url"
),
dbName
);
case
MySQL:
return
String
.
format
(
getJdbcConfig
(
"mysql.url"
),
dbName
);
case
Oracle:
return
String
.
format
(
getJdbcConfig
(
"oracle.url"
),
dbName
);
case
SQLServer:
return
String
.
format
(
getJdbcConfig
(
"sqlserver.url"
),
dbName
);
case
PostgreSQL:
return
String
.
format
(
getJdbcConfig
(
"postgresql.url"
),
dbName
);
default
:
return
String
.
format
(
getJdbcConfig
(
"h2.url"
),
dbName
);
}
}
public
static
String
getURL
(
final
DatabaseType
type
,
final
String
dbName
)
{
switch
(
type
)
{
case
H2:
return
String
.
format
(
getJdbcConfig
(
"h2.url"
),
dbName
);
case
MySQL:
return
String
.
format
(
getJdbcConfig
(
"mysql.url"
),
dbName
);
case
Oracle:
return
String
.
format
(
getJdbcConfig
(
"oracle.url"
),
dbName
);
case
SQLServer:
return
String
.
format
(
getJdbcConfig
(
"sqlserver.url"
),
dbName
);
case
PostgreSQL:
return
String
.
format
(
getJdbcConfig
(
"postgresql.url"
),
dbName
);
default
:
return
String
.
format
(
getJdbcConfig
(
"h2.url"
),
dbName
);
}
}
public
static
String
getUsername
(
final
DatabaseType
type
)
{
switch
(
type
)
{
case
H2:
return
getJdbcConfig
(
"h2.username"
);
case
MySQL:
return
getJdbcConfig
(
"mysql.username"
);
case
Oracle:
return
getJdbcConfig
(
"oracle.username"
);
case
SQLServer:
return
getJdbcConfig
(
"sqlserver.username"
);
case
PostgreSQL:
return
getJdbcConfig
(
"postgresql.username"
);
default
:
return
getJdbcConfig
(
"h2.driver"
);
}
}
public
static
String
getUsername
(
final
DatabaseType
type
)
{
switch
(
type
)
{
case
H2:
return
getJdbcConfig
(
"h2.username"
);
case
MySQL:
return
getJdbcConfig
(
"mysql.username"
);
case
Oracle:
return
getJdbcConfig
(
"oracle.username"
);
case
SQLServer:
return
getJdbcConfig
(
"sqlserver.username"
);
case
PostgreSQL:
return
getJdbcConfig
(
"postgresql.username"
);
default
:
return
getJdbcConfig
(
"h2.driver"
);
}
}
public
static
String
getPassword
(
final
DatabaseType
type
)
{
switch
(
type
)
{
case
H2:
return
getJdbcConfig
(
"h2.password"
);
case
MySQL:
return
getJdbcConfig
(
"mysql.password"
);
case
Oracle:
return
getJdbcConfig
(
"oracle.password"
);
case
SQLServer:
return
getJdbcConfig
(
"sqlserver.password"
);
case
PostgreSQL:
return
getJdbcConfig
(
"postgresql.password"
);
default
:
return
getJdbcConfig
(
"h2.driver"
);
}
}
public
static
String
getPassword
(
final
DatabaseType
type
)
{
switch
(
type
)
{
case
H2:
return
getJdbcConfig
(
"h2.password"
);
case
MySQL:
return
getJdbcConfig
(
"mysql.password"
);
case
Oracle:
return
getJdbcConfig
(
"oracle.password"
);
case
SQLServer:
return
getJdbcConfig
(
"sqlserver.password"
);
case
PostgreSQL:
return
getJdbcConfig
(
"postgresql.password"
);
default
:
return
getJdbcConfig
(
"h2.driver"
);
}
}
public
static
String
getDefualtdb
(
final
DatabaseType
type
)
{
switch
(
type
)
{
case
H2:
return
getJdbcConfig
(
"h2.defualtdb"
);
case
MySQL:
return
getJdbcConfig
(
"mysql.defualtdb"
);
case
Oracle:
return
getJdbcConfig
(
"oracle.defualtdb"
);
case
SQLServer:
return
getJdbcConfig
(
"sqlserver.defualtdb"
);
case
PostgreSQL:
return
getJdbcConfig
(
"postgresql.defualtdb"
);
default
:
return
getJdbcConfig
(
"h2.defualtdb"
);
}
}
public
static
String
getDefualtdb
(
final
DatabaseType
type
)
{
switch
(
type
)
{
case
H2:
return
getJdbcConfig
(
"h2.defualtdb"
);
case
MySQL:
return
getJdbcConfig
(
"mysql.defualtdb"
);
case
Oracle:
return
getJdbcConfig
(
"oracle.defualtdb"
);
case
SQLServer:
return
getJdbcConfig
(
"sqlserver.defualtdb"
);
case
PostgreSQL:
return
getJdbcConfig
(
"postgresql.defualtdb"
);
default
:
return
getJdbcConfig
(
"h2.defualtdb"
);
}
}
public
static
String
getAssertPath
()
{
return
ConfigUtils
.
getString
(
"assert.path"
,
null
);
}
public
static
String
getAssertPath
()
{
return
ConfigUtils
.
getString
(
"assert.path"
,
null
);
}
public
static
boolean
isInitialized
()
{
return
Boolean
.
valueOf
(
ConfigUtils
.
getString
(
"initialized"
,
"false"
));
}
public
static
boolean
isInitialized
()
{
return
Boolean
.
valueOf
(
ConfigUtils
.
getString
(
"initialized"
,
"false"
));
}
}
sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/common/ConfigUtils.java
浏览文件 @
61b90a38
...
...
@@ -6,30 +6,30 @@ import java.util.Properties;
public
class
ConfigUtils
{
private
static
Properties
config
=
null
;
private
static
Properties
config
=
null
;
static
{
try
{
config
=
new
Properties
();
config
.
load
(
ConfigUtils
.
class
.
getClassLoader
().
getResourceAsStream
(
"integrate/env.properties"
));
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
static
{
try
{
config
=
new
Properties
();
config
.
load
(
ConfigUtils
.
class
.
getClassLoader
().
getResourceAsStream
(
"integrate/env.properties"
));
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
public
static
String
getString
(
final
String
key
,
final
String
defaultValue
)
{
return
config
.
getProperty
(
key
,
defaultValue
);
}
public
static
String
getString
(
final
String
key
,
final
String
defaultValue
)
{
return
config
.
getProperty
(
key
,
defaultValue
);
}
public
static
Map
<
String
,
String
>
getDatas
(
final
String
startKey
)
{
Map
<
String
,
String
>
result
=
new
HashMap
<>();
for
(
Map
.
Entry
<
Object
,
Object
>
eachEntry
:
config
.
entrySet
())
{
String
key
=
(
String
)
eachEntry
.
getKey
();
if
(
key
.
startsWith
(
startKey
))
{
result
.
put
(
key
,
(
String
)
eachEntry
.
getValue
());
}
}
return
result
;
}
public
static
Map
<
String
,
String
>
getDatas
(
final
String
startKey
)
{
Map
<
String
,
String
>
result
=
new
HashMap
<>();
for
(
Map
.
Entry
<
Object
,
Object
>
eachEntry
:
config
.
entrySet
())
{
String
key
=
(
String
)
eachEntry
.
getKey
();
if
(
key
.
startsWith
(
startKey
))
{
result
.
put
(
key
,
(
String
)
eachEntry
.
getValue
());
}
}
return
result
;
}
}
sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/common/DatabaseTypeUtils.java
浏览文件 @
61b90a38
...
...
@@ -4,15 +4,15 @@ import io.shardingjdbc.core.constant.DatabaseType;
public
class
DatabaseTypeUtils
{
public
static
DatabaseType
getDatabaseType
(
final
String
type
)
{
public
static
DatabaseType
getDatabaseType
(
final
String
type
)
{
DatabaseType
[]
databaseTypes
=
DatabaseType
.
values
();
for
(
DatabaseType
each
:
databaseTypes
)
{
if
(
type
.
equalsIgnoreCase
(
each
.
name
()))
{
return
each
;
}
}
return
DatabaseType
.
H2
;
}
DatabaseType
[]
databaseTypes
=
DatabaseType
.
values
();
for
(
DatabaseType
each
:
databaseTypes
)
{
if
(
type
.
equalsIgnoreCase
(
each
.
name
()))
{
return
each
;
}
}
return
DatabaseType
.
H2
;
}
}
sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/common/DatabaseUtils.java
浏览文件 @
61b90a38
此差异已折叠。
点击以展开。
sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/common/FileUtils.java
浏览文件 @
61b90a38
...
...
@@ -9,61 +9,61 @@ import java.util.List;
*/
public
class
FileUtils
{
/**
* 查找某个目录下的所有文件
*
* @param filePath
* @param prefixFile
* @return
*/
public
static
List
<
String
>
getAllFilePaths
(
final
File
filePath
,
final
String
prefixFile
,
final
String
suffix
)
{
List
<
String
>
result
=
new
ArrayList
<>();
File
[]
files
=
filePath
.
listFiles
();
if
(
files
==
null
)
{
return
result
;
}
for
(
File
each
:
files
)
{
if
(
each
.
isDirectory
())
{
getSubFilePaths
(
each
,
result
,
prefixFile
,
suffix
);
}
else
{
getFiles
(
prefixFile
,
suffix
,
result
,
each
);
/**
* 查找某个目录下的所有文件
*
* @param filePath
* @param prefixFile
* @return
*/
public
static
List
<
String
>
getAllFilePaths
(
final
File
filePath
,
final
String
prefixFile
,
final
String
suffix
)
{
List
<
String
>
result
=
new
ArrayList
<>();
File
[]
files
=
filePath
.
listFiles
();
if
(
files
==
null
)
{
return
result
;
}
for
(
File
each
:
files
)
{
if
(
each
.
isDirectory
())
{
getSubFilePaths
(
each
,
result
,
prefixFile
,
suffix
);
}
else
{
getFiles
(
prefixFile
,
suffix
,
result
,
each
);
}
}
return
result
;
}
}
}
return
result
;
}
private
static
void
getFiles
(
final
String
prefixFile
,
final
String
suffix
,
final
List
<
String
>
filePaths
,
final
File
f
)
{
if
(
prefixFile
!=
null
)
{
if
(
f
.
getName
().
startsWith
(
prefixFile
))
{
if
(
suffix
!=
null
)
{
if
(
f
.
getName
().
endsWith
(
"."
+
suffix
))
{
filePaths
.
add
(
f
.
getPath
());
}
}
else
{
filePaths
.
add
(
f
.
getPath
());
}
}
}
else
{
filePaths
.
add
(
f
.
getPath
());
}
}
private
static
void
getFiles
(
final
String
prefixFile
,
final
String
suffix
,
final
List
<
String
>
filePaths
,
final
File
f
)
{
if
(
prefixFile
!=
null
)
{
if
(
f
.
getName
().
startsWith
(
prefixFile
))
{
if
(
suffix
!=
null
)
{
if
(
f
.
getName
().
endsWith
(
"."
+
suffix
))
{
filePaths
.
add
(
f
.
getPath
());
}
}
else
{
filePaths
.
add
(
f
.
getPath
());
}
}
}
else
{
filePaths
.
add
(
f
.
getPath
());
}
}
private
static
List
<
String
>
getSubFilePaths
(
final
File
filePath
,
final
List
<
String
>
filePaths
,
final
String
prefixFile
,
final
String
suffix
)
{
File
[]
files
=
filePath
.
listFiles
();
List
<
String
>
result
=
filePaths
;
if
(
files
==
null
)
{
return
result
;
}
for
(
File
each
:
files
)
{
if
(
each
.
isDirectory
())
{
getSubFilePaths
(
each
,
result
,
prefixFile
,
suffix
);
}
else
{
getFiles
(
prefixFile
,
suffix
,
result
,
each
);
}
}
return
result
;
}
private
static
List
<
String
>
getSubFilePaths
(
final
File
filePath
,
final
List
<
String
>
filePaths
,
final
String
prefixFile
,
final
String
suffix
)
{
File
[]
files
=
filePath
.
listFiles
();
List
<
String
>
result
=
filePaths
;
if
(
files
==
null
)
{
return
result
;
}
for
(
File
each
:
files
)
{
if
(
each
.
isDirectory
())
{
getSubFilePaths
(
each
,
result
,
prefixFile
,
suffix
);
}
else
{
getFiles
(
prefixFile
,
suffix
,
result
,
each
);
}
}
return
result
;
}
}
sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/common/PathUtils.java
浏览文件 @
61b90a38
...
...
@@ -4,39 +4,39 @@ import io.shardingjdbc.dbtest.exception.DbTestException;
public
class
PathUtils
{
private
static
final
String
BasePath
=
PathUtils
.
class
.
getClassLoader
().
getResource
(
""
).
getPath
();
private
static
final
String
BasePath
=
PathUtils
.
class
.
getClassLoader
().
getResource
(
""
).
getPath
();
/**
* 获取资源路径
*
* @param path
* @return
*/
public
static
String
getPath
(
final
String
path
,
final
String
parent
)
{
if
(
path
==
null
)
{
throw
new
DbTestException
(
"路径不能为空"
);
}
/**
* 获取资源路径
*
* @param path
* @return
*/
public
static
String
getPath
(
final
String
path
,
final
String
parent
)
{
if
(
path
==
null
)
{
throw
new
DbTestException
(
"路径不能为空"
);
}
String
result
=
path
;
if
(
result
.
startsWith
(
"classpath:"
))
{
result
=
result
.
substring
(
"classpath:"
.
length
());
result
=
BasePath
+
result
;
return
result
;
}
if
(
parent
!=
null
)
{
return
parent
+
result
;
}
return
result
;
}
String
result
=
path
;
if
(
result
.
startsWith
(
"classpath:"
))
{
result
=
result
.
substring
(
"classpath:"
.
length
());
result
=
BasePath
+
result
;
return
result
;
}
if
(
parent
!=
null
)
{
return
parent
+
result
;
}
return
result
;
}
/**
* 获取资源路径
*
* @param path
* @return
*/
public
static
String
getPath
(
final
String
path
)
{
return
getPath
(
path
,
null
);
}
/**
* 获取资源路径
*
* @param path
* @return
*/
public
static
String
getPath
(
final
String
path
)
{
return
getPath
(
path
,
null
);
}
}
sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/common/XMLUtil.java
浏览文件 @
61b90a38
...
...
@@ -19,39 +19,39 @@ import org.xml.sax.SAXException;
public
class
XMLUtil
{
public
static
Document
parseFile
(
final
File
file
)
throws
ParserConfigurationException
,
IOException
,
SAXException
{
DocumentBuilderFactory
dbf
=
DocumentBuilderFactory
.
newInstance
();
dbf
.
setValidating
(
false
);
DocumentBuilder
db
=
dbf
.
newDocumentBuilder
();
Document
result
=
db
.
parse
(
file
);
return
result
;
}
public
static
Document
parseStream
(
final
InputStream
in
)
throws
ParserConfigurationException
,
IOException
,
SAXException
{
DocumentBuilderFactory
dbf
=
DocumentBuilderFactory
.
newInstance
();
dbf
.
setValidating
(
false
);
DocumentBuilder
db
=
dbf
.
newDocumentBuilder
();
Document
result
=
db
.
parse
(
in
);
return
result
;
}
public
static
String
getNodeValue
(
final
Node
node
)
{
return
node
.
getTextContent
();
}
public
static
NodeList
getNodeList
(
final
Node
node
,
final
String
xpath
)
throws
XPathExpressionException
{
XPathFactory
factory
=
XPathFactory
.
newInstance
();
XPath
oXpath
=
factory
.
newXPath
();
NodeList
result
=
(
NodeList
)
oXpath
.
evaluate
(
xpath
,
node
,
XPathConstants
.
NODESET
);
return
result
;
}
public
static
Node
getNode
(
final
Node
node
,
final
String
xpath
)
throws
XPathExpressionException
{
XPathFactory
factory
=
XPathFactory
.
newInstance
();
XPath
oXpath
=
factory
.
newXPath
();
Node
result
=
(
Node
)
oXpath
.
evaluate
(
xpath
,
node
,
XPathConstants
.
NODE
);
return
result
;
}
public
static
Document
parseFile
(
final
File
file
)
throws
ParserConfigurationException
,
IOException
,
SAXException
{
DocumentBuilderFactory
dbf
=
DocumentBuilderFactory
.
newInstance
();
dbf
.
setValidating
(
false
);
DocumentBuilder
db
=
dbf
.
newDocumentBuilder
();
Document
result
=
db
.
parse
(
file
);
return
result
;
}
public
static
Document
parseStream
(
final
InputStream
in
)
throws
ParserConfigurationException
,
IOException
,
SAXException
{
DocumentBuilderFactory
dbf
=
DocumentBuilderFactory
.
newInstance
();
dbf
.
setValidating
(
false
);
DocumentBuilder
db
=
dbf
.
newDocumentBuilder
();
Document
result
=
db
.
parse
(
in
);
return
result
;
}
public
static
String
getNodeValue
(
final
Node
node
)
{
return
node
.
getTextContent
();
}
public
static
NodeList
getNodeList
(
final
Node
node
,
final
String
xpath
)
throws
XPathExpressionException
{
XPathFactory
factory
=
XPathFactory
.
newInstance
();
XPath
oXpath
=
factory
.
newXPath
();
NodeList
result
=
(
NodeList
)
oXpath
.
evaluate
(
xpath
,
node
,
XPathConstants
.
NODESET
);
return
result
;
}
public
static
Node
getNode
(
final
Node
node
,
final
String
xpath
)
throws
XPathExpressionException
{
XPathFactory
factory
=
XPathFactory
.
newInstance
();
XPath
oXpath
=
factory
.
newXPath
();
Node
result
=
(
Node
)
oXpath
.
evaluate
(
xpath
,
node
,
XPathConstants
.
NODE
);
return
result
;
}
}
sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/config/AnalyzeConfig.java
浏览文件 @
61b90a38
...
...
@@ -11,13 +11,13 @@ import io.shardingjdbc.dbtest.config.bean.AssertsDefinition;
public
class
AnalyzeConfig
{
public
static
AssertsDefinition
analyze
(
final
String
path
)
throws
IOException
,
JAXBException
{
JAXBContext
context
=
JAXBContext
.
newInstance
(
AssertsDefinition
.
class
);
public
static
AssertsDefinition
analyze
(
final
String
path
)
throws
IOException
,
JAXBException
{
JAXBContext
context
=
JAXBContext
.
newInstance
(
AssertsDefinition
.
class
);
Unmarshaller
unmarshal
=
context
.
createUnmarshaller
();
FileReader
reader
=
new
FileReader
(
path
);
return
(
AssertsDefinition
)
unmarshal
.
unmarshal
(
reader
);
Unmarshaller
unmarshal
=
context
.
createUnmarshaller
();
FileReader
reader
=
new
FileReader
(
path
);
return
(
AssertsDefinition
)
unmarshal
.
unmarshal
(
reader
);
}
}
}
sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/config/bean/ParameterDefinition.java
浏览文件 @
61b90a38
package
io.shardingjdbc.dbtest.config.bean
;
import
javax.xml.bind.annotation.XmlAccessType
;
import
javax.xml.bind.annotation.XmlAccessorType
;
import
javax.xml.bind.annotation.XmlAttribute
;
...
...
sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/config/bean/parseContext/AggregationSelectItem.java
浏览文件 @
61b90a38
...
...
@@ -33,21 +33,21 @@ import lombok.Setter;
@XmlAccessorType
(
XmlAccessType
.
FIELD
)
public
final
class
AggregationSelectItem
{
@XmlAttribute
(
name
=
"inner-expression"
)
private
String
innerExpression
;
@XmlAttribute
(
name
=
"inner-expression"
)
private
String
innerExpression
;
@XmlAttribute
(
name
=
"aggregation-type"
)
private
String
aggregationType
;
@XmlAttribute
(
name
=
"aggregation-type"
)
private
String
aggregationType
;
@XmlAttribute
private
String
alias
;
@XmlAttribute
private
String
alias
;
@XmlAttribute
private
String
option
;
@XmlAttribute
private
String
option
;
@XmlAttribute
private
Integer
index
;
@XmlAttribute
private
Integer
index
;
@XmlElement
(
name
=
"derived-column"
)
private
List
<
AggregationSelectItem
>
derivedColumns
=
new
ArrayList
<>(
2
);
@XmlElement
(
name
=
"derived-column"
)
private
List
<
AggregationSelectItem
>
derivedColumns
=
new
ArrayList
<>(
2
);
}
sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/data/AnalyzeDataset.java
浏览文件 @
61b90a38
...
...
@@ -21,54 +21,54 @@ import io.shardingjdbc.dbtest.common.XMLUtil;
public
class
AnalyzeDataset
{
public
static
DatasetDefinition
analyze
(
final
String
path
)
throws
IOException
,
SAXException
,
ParserConfigurationException
,
XPathExpressionException
{
return
analyze
(
new
File
(
path
));
}
public
static
DatasetDefinition
analyze
(
final
String
path
)
throws
IOException
,
SAXException
,
ParserConfigurationException
,
XPathExpressionException
{
return
analyze
(
new
File
(
path
));
}
public
static
DatasetDefinition
analyze
(
final
File
file
)
throws
IOException
,
SAXException
,
ParserConfigurationException
,
XPathExpressionException
{
public
static
DatasetDefinition
analyze
(
final
File
file
)
throws
IOException
,
SAXException
,
ParserConfigurationException
,
XPathExpressionException
{
Document
doc
=
XMLUtil
.
parseFile
(
file
);
Node
rootNode
=
XMLUtil
.
getNode
(
doc
,
"/dataset"
);
NodeList
firstNodeList
=
rootNode
.
getChildNodes
();
DatasetDefinition
result
=
new
DatasetDefinition
();
for
(
int
i
=
0
;
i
<
firstNodeList
.
getLength
();
i
++)
{
Node
firstNode
=
firstNodeList
.
item
(
i
);
if
(
firstNode
.
getNodeType
()
==
Node
.
ELEMENT_NODE
)
{
Document
doc
=
XMLUtil
.
parseFile
(
file
);
Node
rootNode
=
XMLUtil
.
getNode
(
doc
,
"/dataset"
);
NodeList
firstNodeList
=
rootNode
.
getChildNodes
();
DatasetDefinition
result
=
new
DatasetDefinition
();
for
(
int
i
=
0
;
i
<
firstNodeList
.
getLength
();
i
++)
{
Node
firstNode
=
firstNodeList
.
item
(
i
);
if
(
firstNode
.
getNodeType
()
==
Node
.
ELEMENT_NODE
)
{
if
(
"table-config"
.
equals
(
firstNode
.
getNodeName
()))
{
analyzeTableConfig
(
result
,
firstNode
);
}
else
{
Map
<
String
,
List
<
Map
<
String
,
String
>>>
datas
=
result
.
getDatas
();
String
tableName
=
firstNode
.
getNodeName
();
List
<
Map
<
String
,
String
>>
datalists
=
datas
.
get
(
tableName
);
if
(
datalists
==
null
)
{
datalists
=
new
ArrayList
<>();
datas
.
put
(
tableName
,
datalists
);
}
if
(
"table-config"
.
equals
(
firstNode
.
getNodeName
()))
{
analyzeTableConfig
(
result
,
firstNode
);
}
else
{
Map
<
String
,
List
<
Map
<
String
,
String
>>>
datas
=
result
.
getDatas
();
String
tableName
=
firstNode
.
getNodeName
();
List
<
Map
<
String
,
String
>>
datalists
=
datas
.
get
(
tableName
);
if
(
datalists
==
null
)
{
datalists
=
new
ArrayList
<>();
datas
.
put
(
tableName
,
datalists
);
}
NamedNodeMap
attrMap
=
firstNode
.
getAttributes
();
NamedNodeMap
attrMap
=
firstNode
.
getAttributes
();
Map
<
String
,
String
>
datacols
=
new
HashMap
<>();
datalists
.
add
(
datacols
);
for
(
int
j
=
0
;
j
<
attrMap
.
getLength
();
j
++)
{
Node
nodeAttr
=
attrMap
.
item
(
j
);
Attr
attr
=
(
Attr
)
nodeAttr
;
String
attrName
=
attr
.
getName
();
String
attrValue
=
attr
.
getValue
();
datacols
.
put
(
attrName
,
attrValue
);
}
}
}
}
return
result
;
}
Map
<
String
,
String
>
datacols
=
new
HashMap
<>();
datalists
.
add
(
datacols
);
for
(
int
j
=
0
;
j
<
attrMap
.
getLength
();
j
++)
{
Node
nodeAttr
=
attrMap
.
item
(
j
);
Attr
attr
=
(
Attr
)
nodeAttr
;
String
attrName
=
attr
.
getName
();
String
attrValue
=
attr
.
getValue
();
datacols
.
put
(
attrName
,
attrValue
);
}
}
}
}
return
result
;
}
private
static
void
analyzeTableConfig
(
final
DatasetDefinition
datasetDefinition
,
final
Node
firstNode
)
{
NodeList
secondNodeList
=
firstNode
.
getChildNodes
();
Map
<
String
,
Map
<
String
,
String
>>
configs
=
datasetDefinition
.
getConfigs
();
for
(
int
j
=
0
;
j
<
secondNodeList
.
getLength
();
j
++)
{
private
static
void
analyzeTableConfig
(
final
DatasetDefinition
datasetDefinition
,
final
Node
firstNode
)
{
NodeList
secondNodeList
=
firstNode
.
getChildNodes
();
Map
<
String
,
Map
<
String
,
String
>>
configs
=
datasetDefinition
.
getConfigs
();
for
(
int
j
=
0
;
j
<
secondNodeList
.
getLength
();
j
++)
{
Node
secondNode
=
secondNodeList
.
item
(
j
);
if
(
secondNode
.
getNodeType
()
==
Node
.
ELEMENT_NODE
)
{
Map
<
String
,
String
>
maps
=
new
HashMap
<>();
...
...
@@ -82,6 +82,6 @@ public class AnalyzeDataset {
}
}
}
}
}
}
sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/data/DatasetDefinition.java
浏览文件 @
61b90a38
...
...
@@ -9,14 +9,14 @@ import lombok.Data;
@Data
public
class
DatasetDefinition
{
/**
* Map<table,Map<column,type>>
*/
private
Map
<
String
,
Map
<
String
,
String
>>
configs
=
new
HashMap
<>();
/**
* Map<table,Map<column,type>>
*/
private
Map
<
String
,
Map
<
String
,
String
>>
configs
=
new
HashMap
<>();
/**
* Map<table,List<Map<column,data>>>
*/
private
Map
<
String
,
List
<
Map
<
String
,
String
>>>
datas
=
new
HashMap
<>();
/**
* Map<table,List<Map<column,data>>>
*/
private
Map
<
String
,
List
<
Map
<
String
,
String
>>>
datas
=
new
HashMap
<>();
}
sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/dataSource/DataSourceUtil.java
浏览文件 @
61b90a38
...
...
@@ -18,35 +18,35 @@ import io.shardingjdbc.core.rule.ShardingRule;
*/
public
class
DataSourceUtil
{
public
static
DataSource
getDataSource
(
final
String
path
)
throws
IOException
,
SQLException
{
return
ShardingDataSourceFactory
.
createDataSource
(
new
File
(
path
));
}
public
static
Map
<
String
,
DataSource
>
getDataSourceMap
(
final
ShardingDataSource
shardingDataSource
)
throws
NoSuchFieldException
,
SecurityException
,
IllegalArgumentException
,
IllegalAccessException
{
ShardingContext
shardingContext
=
getShardingContext
(
shardingDataSource
);
return
shardingContext
.
getShardingRule
().
getDataSourceMap
();
}
public
static
ShardingRule
getShardingRule
(
final
ShardingDataSource
shardingDataSource
)
throws
NoSuchFieldException
,
SecurityException
,
IllegalArgumentException
,
IllegalAccessException
{
ShardingContext
shardingContext
=
getShardingContext
(
shardingDataSource
);
return
shardingContext
.
getShardingRule
();
}
public
static
ShardingContext
getShardingContext
(
final
ShardingDataSource
shardingDataSource
)
throws
NoSuchFieldException
,
SecurityException
,
IllegalArgumentException
,
IllegalAccessException
{
Field
field
=
shardingDataSource
.
getClass
().
getDeclaredField
(
"shardingContext"
);
field
.
setAccessible
(
true
);
return
(
ShardingContext
)
field
.
get
(
shardingDataSource
);
}
public
static
String
getDatabaseName
(
final
String
dataSetFile
)
{
String
fileName
=
new
File
(
dataSetFile
).
getName
();
if
(-
1
==
fileName
.
lastIndexOf
(
"."
))
{
return
fileName
;
}
return
fileName
.
substring
(
0
,
fileName
.
lastIndexOf
(
"."
));
}
public
static
DataSource
getDataSource
(
final
String
path
)
throws
IOException
,
SQLException
{
return
ShardingDataSourceFactory
.
createDataSource
(
new
File
(
path
));
}
public
static
Map
<
String
,
DataSource
>
getDataSourceMap
(
final
ShardingDataSource
shardingDataSource
)
throws
NoSuchFieldException
,
SecurityException
,
IllegalArgumentException
,
IllegalAccessException
{
ShardingContext
shardingContext
=
getShardingContext
(
shardingDataSource
);
return
shardingContext
.
getShardingRule
().
getDataSourceMap
();
}
public
static
ShardingRule
getShardingRule
(
final
ShardingDataSource
shardingDataSource
)
throws
NoSuchFieldException
,
SecurityException
,
IllegalArgumentException
,
IllegalAccessException
{
ShardingContext
shardingContext
=
getShardingContext
(
shardingDataSource
);
return
shardingContext
.
getShardingRule
();
}
public
static
ShardingContext
getShardingContext
(
final
ShardingDataSource
shardingDataSource
)
throws
NoSuchFieldException
,
SecurityException
,
IllegalArgumentException
,
IllegalAccessException
{
Field
field
=
shardingDataSource
.
getClass
().
getDeclaredField
(
"shardingContext"
);
field
.
setAccessible
(
true
);
return
(
ShardingContext
)
field
.
get
(
shardingDataSource
);
}
public
static
String
getDatabaseName
(
final
String
dataSetFile
)
{
String
fileName
=
new
File
(
dataSetFile
).
getName
();
if
(-
1
==
fileName
.
lastIndexOf
(
"."
))
{
return
fileName
;
}
return
fileName
.
substring
(
0
,
fileName
.
lastIndexOf
(
"."
));
}
}
sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/exception/DbTestException.java
浏览文件 @
61b90a38
...
...
@@ -2,9 +2,9 @@ package io.shardingjdbc.dbtest.exception;
public
class
DbTestException
extends
RuntimeException
{
private
static
final
long
serialVersionUID
=
8269224755642356888L
;
private
static
final
long
serialVersionUID
=
8269224755642356888L
;
public
DbTestException
(
final
String
message
)
{
public
DbTestException
(
final
String
message
)
{
super
(
message
);
}
...
...
sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/init/InItCreateSchema.java
浏览文件 @
61b90a38
...
...
@@ -17,166 +17,166 @@ import io.shardingjdbc.dbtest.common.DatabaseTypeUtils;
public
class
InItCreateSchema
{
/**
* 初始化数据库表
*/
public
static
synchronized
void
initTable
()
{
for
(
String
db
:
ConfigRuntime
.
getDbs
())
{
DatabaseType
databaseType
=
DatabaseTypeUtils
.
getDatabaseType
(
db
);
createSchema
(
databaseType
);
}
}
/**
* 创建数据库
*/
public
static
void
createDatabase
()
{
Connection
conn
=
null
;
try
{
for
(
String
each
:
ConfigRuntime
.
getDbs
())
{
DatabaseType
databaseType
=
DatabaseTypeUtils
.
getDatabaseType
(
each
);
conn
=
initialConnection
(
null
,
databaseType
);
String
packing
=
"default"
;
if
(
DatabaseType
.
Oracle
==
databaseType
)
{
packing
=
"oracle"
;
}
RunScript
.
execute
(
conn
,
new
InputStreamReader
(
InItCreateSchema
.
class
.
getClassLoader
()
.
getResourceAsStream
(
"integrate/schema/"
+
packing
+
"/manual_schema_create.sql"
)));
}
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
}
finally
{
if
(
conn
!=
null
)
{
try
{
conn
.
close
();
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
}
}
}
}
/**
* 初始化数据库表
*/
public
static
synchronized
void
dropDatabase
()
{
Connection
conn
=
null
;
try
{
for
(
String
each
:
ConfigRuntime
.
getDbs
())
{
DatabaseType
databaseType
=
DatabaseTypeUtils
.
getDatabaseType
(
each
);
conn
=
initialConnection
(
null
,
databaseType
);
String
packing
=
"default"
;
if
(
DatabaseType
.
Oracle
==
databaseType
)
{
packing
=
"oracle"
;
}
RunScript
.
execute
(
conn
,
new
InputStreamReader
(
InItCreateSchema
.
class
.
getClassLoader
()
.
getResourceAsStream
(
"integrate/schema/"
+
packing
+
"/manual_schema_drop.sql"
)));
}
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
}
finally
{
if
(
conn
!=
null
)
{
try
{
conn
.
close
();
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
}
}
}
}
public
static
void
main
(
String
[]
args
)
{
initTable
();
}
private
static
void
createSchema
(
final
DatabaseType
dbType
)
{
createJdbcSchema
(
dbType
);
createMasterSlaveOnlySchema
(
dbType
);
createShardingSchema
(
dbType
);
}
private
static
void
createShardingSchema
(
final
DatabaseType
dbType
)
{
try
{
Connection
conn
;
for
(
int
i
=
0
;
i
<
10
;
i
++)
{
for
(
String
database
:
Arrays
.
asList
(
"db"
,
"dbtbl"
,
"nullable"
,
"master"
,
"slave"
))
{
conn
=
initialConnection
(
database
+
"_"
+
i
,
dbType
);
RunScript
.
execute
(
conn
,
new
InputStreamReader
(
InItCreateSchema
.
class
.
getClassLoader
()
.
getResourceAsStream
(
"integrate/schema/table/"
+
database
+
".sql"
)));
conn
.
close
();
}
}
conn
=
initialConnection
(
"tbl"
,
dbType
);
RunScript
.
execute
(
conn
,
new
InputStreamReader
(
InItCreateSchema
.
class
.
getClassLoader
().
getResourceAsStream
(
"integrate/schema/table/tbl.sql"
)));
conn
.
close
();
}
catch
(
final
SQLException
ex
)
{
ex
.
printStackTrace
();
}
}
private
static
void
createMasterSlaveOnlySchema
(
final
DatabaseType
dbType
)
{
try
{
Connection
conn
;
for
(
String
database
:
Arrays
.
asList
(
"master_only"
,
"slave_only"
))
{
conn
=
initialConnection
(
database
,
dbType
);
RunScript
.
execute
(
conn
,
new
InputStreamReader
(
InItCreateSchema
.
class
.
getClassLoader
()
.
getResourceAsStream
(
"integrate/schema/table/"
+
database
+
".sql"
)));
conn
.
close
();
}
}
catch
(
final
SQLException
ex
)
{
ex
.
printStackTrace
();
}
}
private
static
void
createJdbcSchema
(
final
DatabaseType
dbType
)
{
try
{
Connection
conn
;
for
(
int
i
=
0
;
i
<
2
;
i
++)
{
conn
=
initialConnection
(
"jdbc_"
+
i
,
dbType
);
RunScript
.
execute
(
conn
,
new
InputStreamReader
(
InItCreateSchema
.
class
.
getClassLoader
()
.
getResourceAsStream
(
"integrate/schema/table/jdbc.sql"
)));
conn
.
close
();
}
}
catch
(
final
SQLException
ex
)
{
ex
.
printStackTrace
();
}
}
protected
static
String
getDatabaseName
(
final
String
dataSetFile
)
{
String
fileName
=
new
File
(
dataSetFile
).
getName
();
if
(-
1
==
fileName
.
lastIndexOf
(
"."
))
{
return
fileName
;
}
return
fileName
.
substring
(
0
,
fileName
.
lastIndexOf
(
"."
));
}
private
static
BasicDataSource
buildDataSource
(
final
String
dbName
,
final
DatabaseType
type
)
{
String
newDbName
=
dbName
;
BasicDataSource
result
=
new
BasicDataSource
();
result
.
setDriverClassName
(
ConfigRuntime
.
getDriverClassName
(
type
));
if
(
newDbName
==
null
)
{
newDbName
=
ConfigRuntime
.
getDefualtdb
(
type
);
}
result
.
setUrl
(
ConfigRuntime
.
getURL
(
type
,
newDbName
));
result
.
setUsername
(
ConfigRuntime
.
getUsername
(
type
));
result
.
setPassword
(
ConfigRuntime
.
getPassword
(
type
));
result
.
setMaxActive
(
1
);
if
(
DatabaseType
.
Oracle
==
type
)
{
result
.
setConnectionInitSqls
(
Collections
.
singleton
(
"ALTER SESSION SET CURRENT_SCHEMA = "
+
newDbName
));
}
return
result
;
}
private
static
Connection
initialConnection
(
final
String
dbName
,
final
DatabaseType
type
)
throws
SQLException
{
return
buildDataSource
(
dbName
,
type
).
getConnection
();
}
/**
* 初始化数据库表
*/
public
static
synchronized
void
initTable
()
{
for
(
String
db
:
ConfigRuntime
.
getDbs
())
{
DatabaseType
databaseType
=
DatabaseTypeUtils
.
getDatabaseType
(
db
);
createSchema
(
databaseType
);
}
}
/**
* 创建数据库
*/
public
static
void
createDatabase
()
{
Connection
conn
=
null
;
try
{
for
(
String
each
:
ConfigRuntime
.
getDbs
())
{
DatabaseType
databaseType
=
DatabaseTypeUtils
.
getDatabaseType
(
each
);
conn
=
initialConnection
(
null
,
databaseType
);
String
packing
=
"default"
;
if
(
DatabaseType
.
Oracle
==
databaseType
)
{
packing
=
"oracle"
;
}
RunScript
.
execute
(
conn
,
new
InputStreamReader
(
InItCreateSchema
.
class
.
getClassLoader
()
.
getResourceAsStream
(
"integrate/schema/"
+
packing
+
"/manual_schema_create.sql"
)));
}
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
}
finally
{
if
(
conn
!=
null
)
{
try
{
conn
.
close
();
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
}
}
}
}
/**
* 初始化数据库表
*/
public
static
synchronized
void
dropDatabase
()
{
Connection
conn
=
null
;
try
{
for
(
String
each
:
ConfigRuntime
.
getDbs
())
{
DatabaseType
databaseType
=
DatabaseTypeUtils
.
getDatabaseType
(
each
);
conn
=
initialConnection
(
null
,
databaseType
);
String
packing
=
"default"
;
if
(
DatabaseType
.
Oracle
==
databaseType
)
{
packing
=
"oracle"
;
}
RunScript
.
execute
(
conn
,
new
InputStreamReader
(
InItCreateSchema
.
class
.
getClassLoader
()
.
getResourceAsStream
(
"integrate/schema/"
+
packing
+
"/manual_schema_drop.sql"
)));
}
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
}
finally
{
if
(
conn
!=
null
)
{
try
{
conn
.
close
();
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
}
}
}
}
public
static
void
main
(
String
[]
args
)
{
initTable
();
}
private
static
void
createSchema
(
final
DatabaseType
dbType
)
{
createJdbcSchema
(
dbType
);
createMasterSlaveOnlySchema
(
dbType
);
createShardingSchema
(
dbType
);
}
private
static
void
createShardingSchema
(
final
DatabaseType
dbType
)
{
try
{
Connection
conn
;
for
(
int
i
=
0
;
i
<
10
;
i
++)
{
for
(
String
database
:
Arrays
.
asList
(
"db"
,
"dbtbl"
,
"nullable"
,
"master"
,
"slave"
))
{
conn
=
initialConnection
(
database
+
"_"
+
i
,
dbType
);
RunScript
.
execute
(
conn
,
new
InputStreamReader
(
InItCreateSchema
.
class
.
getClassLoader
()
.
getResourceAsStream
(
"integrate/schema/table/"
+
database
+
".sql"
)));
conn
.
close
();
}
}
conn
=
initialConnection
(
"tbl"
,
dbType
);
RunScript
.
execute
(
conn
,
new
InputStreamReader
(
InItCreateSchema
.
class
.
getClassLoader
().
getResourceAsStream
(
"integrate/schema/table/tbl.sql"
)));
conn
.
close
();
}
catch
(
final
SQLException
ex
)
{
ex
.
printStackTrace
();
}
}
private
static
void
createMasterSlaveOnlySchema
(
final
DatabaseType
dbType
)
{
try
{
Connection
conn
;
for
(
String
database
:
Arrays
.
asList
(
"master_only"
,
"slave_only"
))
{
conn
=
initialConnection
(
database
,
dbType
);
RunScript
.
execute
(
conn
,
new
InputStreamReader
(
InItCreateSchema
.
class
.
getClassLoader
()
.
getResourceAsStream
(
"integrate/schema/table/"
+
database
+
".sql"
)));
conn
.
close
();
}
}
catch
(
final
SQLException
ex
)
{
ex
.
printStackTrace
();
}
}
private
static
void
createJdbcSchema
(
final
DatabaseType
dbType
)
{
try
{
Connection
conn
;
for
(
int
i
=
0
;
i
<
2
;
i
++)
{
conn
=
initialConnection
(
"jdbc_"
+
i
,
dbType
);
RunScript
.
execute
(
conn
,
new
InputStreamReader
(
InItCreateSchema
.
class
.
getClassLoader
()
.
getResourceAsStream
(
"integrate/schema/table/jdbc.sql"
)));
conn
.
close
();
}
}
catch
(
final
SQLException
ex
)
{
ex
.
printStackTrace
();
}
}
protected
static
String
getDatabaseName
(
final
String
dataSetFile
)
{
String
fileName
=
new
File
(
dataSetFile
).
getName
();
if
(-
1
==
fileName
.
lastIndexOf
(
"."
))
{
return
fileName
;
}
return
fileName
.
substring
(
0
,
fileName
.
lastIndexOf
(
"."
));
}
private
static
BasicDataSource
buildDataSource
(
final
String
dbName
,
final
DatabaseType
type
)
{
String
newDbName
=
dbName
;
BasicDataSource
result
=
new
BasicDataSource
();
result
.
setDriverClassName
(
ConfigRuntime
.
getDriverClassName
(
type
));
if
(
newDbName
==
null
)
{
newDbName
=
ConfigRuntime
.
getDefualtdb
(
type
);
}
result
.
setUrl
(
ConfigRuntime
.
getURL
(
type
,
newDbName
));
result
.
setUsername
(
ConfigRuntime
.
getUsername
(
type
));
result
.
setPassword
(
ConfigRuntime
.
getPassword
(
type
));
result
.
setMaxActive
(
1
);
if
(
DatabaseType
.
Oracle
==
type
)
{
result
.
setConnectionInitSqls
(
Collections
.
singleton
(
"ALTER SESSION SET CURRENT_SCHEMA = "
+
newDbName
));
}
return
result
;
}
private
static
Connection
initialConnection
(
final
String
dbName
,
final
DatabaseType
type
)
throws
SQLException
{
return
buildDataSource
(
dbName
,
type
).
getConnection
();
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录