Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
Shardingsphere
提交
a7d2675a
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,发现更多精彩内容 >>
未验证
提交
a7d2675a
编写于
4月 23, 2018
作者:
ShardingSphere
提交者:
GitHub
4月 23, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #743 from tristaZero/dev
proxy meta data
上级
664e9415
7114aced
变更
15
隐藏空白更改
内联
并排
Showing
15 changed file
with
240 addition
and
40 deletion
+240
-40
sharding-core/src/main/java/io/shardingjdbc/core/metadata/ShardingMetaData.java
.../java/io/shardingjdbc/core/metadata/ShardingMetaData.java
+0
-1
sharding-core/src/main/java/io/shardingjdbc/core/metadata/TableMetaData.java
...ain/java/io/shardingjdbc/core/metadata/TableMetaData.java
+0
-1
sharding-core/src/main/java/io/shardingjdbc/core/parsing/parser/clause/SelectListClauseParser.java
...bc/core/parsing/parser/clause/SelectListClauseParser.java
+25
-11
sharding-jdbc-core/src/main/java/io/shardingjdbc/core/jdbc/core/ShardingContext.java
.../java/io/shardingjdbc/core/jdbc/core/ShardingContext.java
+4
-0
sharding-jdbc-core/src/main/java/io/shardingjdbc/core/jdbc/core/datasource/ShardingDataSource.java
...ingjdbc/core/jdbc/core/datasource/ShardingDataSource.java
+14
-2
sharding-jdbc-core/src/main/java/io/shardingjdbc/core/jdbc/metadata/JDBCShardingMetaData.java
...shardingjdbc/core/jdbc/metadata/JDBCShardingMetaData.java
+5
-2
sharding-jdbc-core/src/main/java/io/shardingjdbc/core/jdbc/metadata/ShardingMetaDataHandlerFactory.java
...bc/core/jdbc/metadata/ShardingMetaDataHandlerFactory.java
+4
-4
sharding-jdbc-core/src/main/java/io/shardingjdbc/core/jdbc/metadata/dialect/DefaultShardingMetaDataHandler.java
...jdbc/metadata/dialect/DefaultShardingMetaDataHandler.java
+42
-0
sharding-jdbc-core/src/main/java/io/shardingjdbc/core/jdbc/metadata/dialect/MySQLShardingMetaDataHandler.java
...e/jdbc/metadata/dialect/MySQLShardingMetaDataHandler.java
+4
-3
sharding-jdbc-core/src/main/java/io/shardingjdbc/core/jdbc/metadata/dialect/ShardingMetaDataHandler.java
...c/core/jdbc/metadata/dialect/ShardingMetaDataHandler.java
+0
-1
sharding-jdbc-core/src/test/java/io/shardingjdbc/core/jdbc/core/connection/ShardingConnectionTest.java
...dbc/core/jdbc/core/connection/ShardingConnectionTest.java
+5
-2
sharding-jdbc-core/src/test/java/io/shardingjdbc/core/jdbc/core/datasource/ShardingDataSourceTest.java
...dbc/core/jdbc/core/datasource/ShardingDataSourceTest.java
+8
-12
sharding-proxy/src/main/java/io/shardingjdbc/proxy/config/ShardingRuleRegistry.java
...va/io/shardingjdbc/proxy/config/ShardingRuleRegistry.java
+12
-1
sharding-proxy/src/main/java/io/shardingjdbc/proxy/metadata/ProxyShardingMetaData.java
...io/shardingjdbc/proxy/metadata/ProxyShardingMetaData.java
+48
-0
sharding-proxy/src/main/java/io/shardingjdbc/proxy/metadata/ShardingMetaDataHandler.java
.../shardingjdbc/proxy/metadata/ShardingMetaDataHandler.java
+69
-0
未找到文件。
sharding-core/src/main/java/io/shardingjdbc/core/metadata/ShardingMetaData.java
浏览文件 @
a7d2675a
...
...
@@ -22,7 +22,6 @@ import io.shardingjdbc.core.rule.DataNode;
import
io.shardingjdbc.core.rule.ShardingRule
;
import
io.shardingjdbc.core.rule.TableRule
;
import
lombok.Getter
;
import
java.sql.SQLException
;
import
java.util.Collection
;
import
java.util.HashMap
;
...
...
sharding-core/src/main/java/io/shardingjdbc/core/metadata/TableMetaData.java
浏览文件 @
a7d2675a
...
...
@@ -20,7 +20,6 @@ package io.shardingjdbc.core.metadata;
import
lombok.EqualsAndHashCode
;
import
lombok.Getter
;
import
lombok.RequiredArgsConstructor
;
import
java.util.Collection
;
/**
...
...
sharding-core/src/main/java/io/shardingjdbc/core/parsing/parser/clause/SelectListClauseParser.java
浏览文件 @
a7d2675a
...
...
@@ -83,7 +83,10 @@ public class SelectListClauseParser implements SQLClauseParser {
result
=
parseAggregationSelectItem
(
selectStatement
);
parseRestSelectItem
(
selectStatement
);
}
else
{
result
=
new
CommonSelectItem
(
SQLUtil
.
getExactlyValue
(
parseCommonSelectItem
(
selectStatement
)
+
parseRestSelectItem
(
selectStatement
)),
aliasExpressionParser
.
parseSelectItemAlias
());
SelectItem
selectItem
=
parseCommonOrStarSelectItem
(
selectStatement
);
result
=
selectItem
instanceof
StarSelectItem
?
selectItem
:
new
CommonSelectItem
(
SQLUtil
.
getExactlyValue
(
selectItem
.
getExpression
()
+
parseRestSelectItem
(
selectStatement
)),
aliasExpressionParser
.
parseSelectItemAlias
());
}
return
result
;
}
...
...
@@ -110,17 +113,13 @@ public class SelectListClauseParser implements SQLClauseParser {
return
new
StarSelectItem
(
Optional
.<
String
>
absent
());
}
private
boolean
isAggregationSelectItem
()
{
return
lexerEngine
.
equalAny
(
DefaultKeyword
.
MAX
,
DefaultKeyword
.
MIN
,
DefaultKeyword
.
SUM
,
DefaultKeyword
.
AVG
,
DefaultKeyword
.
COUNT
);
}
private
SelectItem
parseAggregationSelectItem
(
final
SelectStatement
selectStatement
)
{
AggregationType
aggregationType
=
AggregationType
.
valueOf
(
lexerEngine
.
getCurrentToken
().
getLiterals
().
toUpperCase
());
private
SelectItem
parseStarSelectItem
(
final
String
owner
)
{
lexerEngine
.
nextToken
();
return
new
AggregationSelectItem
(
aggregationType
,
lexerEngine
.
skipParentheses
(
selectStatement
),
aliasExpressionParser
.
parseSelectItemAlias
());
aliasExpressionParser
.
parseSelectItemAlias
();
return
new
StarSelectItem
(
Optional
.
fromNullable
(
owner
));
}
private
S
tring
parseCommon
SelectItem
(
final
SelectStatement
selectStatement
)
{
private
S
electItem
parseCommonOrStar
SelectItem
(
final
SelectStatement
selectStatement
)
{
String
literals
=
lexerEngine
.
getCurrentToken
().
getLiterals
();
int
position
=
lexerEngine
.
getCurrentToken
().
getEndPosition
()
-
literals
.
length
();
StringBuilder
result
=
new
StringBuilder
();
...
...
@@ -135,10 +134,24 @@ public class SelectListClauseParser implements SQLClauseParser {
}
result
.
append
(
lexerEngine
.
getCurrentToken
().
getLiterals
());
lexerEngine
.
nextToken
();
if
(
lexerEngine
.
equalAny
(
Symbol
.
STAR
))
{
return
parseStarSelectItem
(
literals
);
}
result
.
append
(
lexerEngine
.
getCurrentToken
().
getLiterals
());
lexerEngine
.
nextToken
();
}
return
result
.
toString
();
return
new
CommonSelectItem
(
result
.
toString
(),
Optional
.<
String
>
absent
());
}
private
boolean
isAggregationSelectItem
()
{
return
lexerEngine
.
equalAny
(
DefaultKeyword
.
MAX
,
DefaultKeyword
.
MIN
,
DefaultKeyword
.
SUM
,
DefaultKeyword
.
AVG
,
DefaultKeyword
.
COUNT
);
}
private
SelectItem
parseAggregationSelectItem
(
final
SelectStatement
selectStatement
)
{
AggregationType
aggregationType
=
AggregationType
.
valueOf
(
lexerEngine
.
getCurrentToken
().
getLiterals
().
toUpperCase
());
lexerEngine
.
nextToken
();
return
new
AggregationSelectItem
(
aggregationType
,
lexerEngine
.
skipParentheses
(
selectStatement
),
aliasExpressionParser
.
parseSelectItemAlias
());
}
private
String
parseRestSelectItem
(
final
SelectStatement
selectStatement
)
{
...
...
@@ -146,7 +159,8 @@ public class SelectListClauseParser implements SQLClauseParser {
while
(
lexerEngine
.
equalAny
(
Symbol
.
getOperators
()))
{
result
.
append
(
lexerEngine
.
getCurrentToken
().
getLiterals
());
lexerEngine
.
nextToken
();
result
.
append
(
parseCommonSelectItem
(
selectStatement
));
SelectItem
selectItem
=
parseCommonOrStarSelectItem
(
selectStatement
);
result
.
append
(
selectItem
.
getExpression
());
}
return
result
.
toString
();
}
...
...
sharding-jdbc-core/src/main/java/io/shardingjdbc/core/jdbc/core/ShardingContext.java
浏览文件 @
a7d2675a
...
...
@@ -19,6 +19,7 @@ package io.shardingjdbc.core.jdbc.core;
import
io.shardingjdbc.core.constant.DatabaseType
;
import
io.shardingjdbc.core.executor.ExecutorEngine
;
import
io.shardingjdbc.core.metadata.ShardingMetaData
;
import
io.shardingjdbc.core.rule.ShardingRule
;
import
lombok.Getter
;
import
lombok.RequiredArgsConstructor
;
...
...
@@ -30,6 +31,7 @@ import java.util.Map;
* Sharding runtime context.
*
* @author gaohongtao
* @author panjuan
*/
@RequiredArgsConstructor
@Getter
...
...
@@ -43,5 +45,7 @@ public final class ShardingContext {
private
final
ExecutorEngine
executorEngine
;
private
final
ShardingMetaData
shardingMetaData
;
private
final
boolean
showSQL
;
}
sharding-jdbc-core/src/main/java/io/shardingjdbc/core/jdbc/core/datasource/ShardingDataSource.java
浏览文件 @
a7d2675a
...
...
@@ -20,10 +20,13 @@ package io.shardingjdbc.core.jdbc.core.datasource;
import
io.shardingjdbc.core.api.ConfigMapContext
;
import
io.shardingjdbc.core.constant.ShardingProperties
;
import
io.shardingjdbc.core.constant.ShardingPropertiesConstant
;
import
io.shardingjdbc.core.exception.ShardingJdbcException
;
import
io.shardingjdbc.core.executor.ExecutorEngine
;
import
io.shardingjdbc.core.jdbc.adapter.AbstractDataSourceAdapter
;
import
io.shardingjdbc.core.jdbc.core.ShardingContext
;
import
io.shardingjdbc.core.jdbc.core.connection.ShardingConnection
;
import
io.shardingjdbc.core.jdbc.metadata.JDBCShardingMetaData
;
import
io.shardingjdbc.core.metadata.ShardingMetaData
;
import
io.shardingjdbc.core.rule.ShardingRule
;
import
lombok.Getter
;
...
...
@@ -59,8 +62,10 @@ public class ShardingDataSource extends AbstractDataSourceAdapter implements Aut
shardingProperties
=
new
ShardingProperties
(
null
==
props
?
new
Properties
()
:
props
);
int
executorSize
=
shardingProperties
.
getValue
(
ShardingPropertiesConstant
.
EXECUTOR_SIZE
);
executorEngine
=
new
ExecutorEngine
(
executorSize
);
ShardingMetaData
shardingMetaData
=
new
JDBCShardingMetaData
(
dataSourceMap
,
getDatabaseType
());
shardingMetaData
.
init
(
shardingRule
);
boolean
showSQL
=
shardingProperties
.
getValue
(
ShardingPropertiesConstant
.
SQL_SHOW
);
shardingContext
=
new
ShardingContext
(
dataSourceMap
,
shardingRule
,
getDatabaseType
(),
executorEngine
,
showSQL
);
shardingContext
=
new
ShardingContext
(
dataSourceMap
,
shardingRule
,
getDatabaseType
(),
executorEngine
,
sh
ardingMetaData
,
sh
owSQL
);
}
/**
...
...
@@ -80,8 +85,15 @@ public class ShardingDataSource extends AbstractDataSourceAdapter implements Aut
originalExecutorEngine
.
close
();
}
boolean
newShowSQL
=
newShardingProperties
.
getValue
(
ShardingPropertiesConstant
.
SQL_SHOW
);
ShardingMetaData
shardingMetaData
=
new
JDBCShardingMetaData
(
newDataSourceMap
,
getDatabaseType
());
try
{
shardingMetaData
.
init
(
newShardingRule
);
}
catch
(
SQLException
ex
)
{
throw
new
ShardingJdbcException
(
ex
);
}
shardingProperties
=
newShardingProperties
;
shardingContext
=
new
ShardingContext
(
newDataSourceMap
,
newShardingRule
,
getDatabaseType
(),
executorEngine
,
newShowSQL
);
shardingContext
=
new
ShardingContext
(
newDataSourceMap
,
newShardingRule
,
getDatabaseType
(),
executorEngine
,
shardingMetaData
,
newShowSQL
);
}
@Override
...
...
sharding-jdbc-core/src/main/java/io/shardingjdbc/core/jdbc/metadata/JDBCShardingMetaData.java
浏览文件 @
a7d2675a
...
...
@@ -17,6 +17,7 @@
package
io.shardingjdbc.core.jdbc.metadata
;
import
io.shardingjdbc.core.constant.DatabaseType
;
import
io.shardingjdbc.core.metadata.ColumnMetaData
;
import
io.shardingjdbc.core.metadata.ShardingMetaData
;
import
io.shardingjdbc.core.rule.DataNode
;
...
...
@@ -28,7 +29,7 @@ import java.util.Collection;
import
java.util.Map
;
/**
* Sharding metadata.
* Sharding metadata
for JDBC
.
*
* @author panjuan
*/
...
...
@@ -38,9 +39,11 @@ public final class JDBCShardingMetaData extends ShardingMetaData {
private
final
Map
<
String
,
DataSource
>
dataSourceMap
;
private
final
DatabaseType
databaseType
;
@Override
protected
Collection
<
ColumnMetaData
>
getColumnMetaDataList
(
final
DataNode
dataNode
)
throws
SQLException
{
return
ShardingMetaDataHandlerFactory
.
newInstance
(
dataSourceMap
.
get
(
dataNode
.
getDataSourceName
()),
dataNode
.
getTableName
()).
getColumnMetaDataList
();
return
ShardingMetaDataHandlerFactory
.
newInstance
(
dataSourceMap
.
get
(
dataNode
.
getDataSourceName
()),
dataNode
.
getTableName
()
,
databaseType
).
getColumnMetaDataList
();
}
}
...
...
sharding-jdbc-core/src/main/java/io/shardingjdbc/core/jdbc/metadata/ShardingMetaDataHandlerFactory.java
浏览文件 @
a7d2675a
...
...
@@ -18,11 +18,11 @@
package
io.shardingjdbc.core.jdbc.metadata
;
import
io.shardingjdbc.core.constant.DatabaseType
;
import
io.shardingjdbc.core.jdbc.metadata.dialect.DefaultShardingMetaDataHandler
;
import
io.shardingjdbc.core.jdbc.metadata.dialect.MySQLShardingMetaDataHandler
;
import
io.shardingjdbc.core.jdbc.metadata.dialect.ShardingMetaDataHandler
;
import
lombok.AccessLevel
;
import
lombok.NoArgsConstructor
;
import
javax.sql.DataSource
;
import
java.sql.SQLException
;
...
...
@@ -39,16 +39,16 @@ public final class ShardingMetaDataHandlerFactory {
*
* @param dataSource data source.
* @param actualTableName actual table name.
* @param databaseType database type.
* @return abstract table metadata handler.
* @throws SQLException SQL exception.
*/
public
static
ShardingMetaDataHandler
newInstance
(
final
DataSource
dataSource
,
final
String
actualTableName
)
throws
SQLException
{
DatabaseType
databaseType
=
DatabaseType
.
valueFrom
(
dataSource
.
getConnection
().
getMetaData
().
getDatabaseProductName
());
public
static
ShardingMetaDataHandler
newInstance
(
final
DataSource
dataSource
,
final
String
actualTableName
,
final
DatabaseType
databaseType
)
throws
SQLException
{
switch
(
databaseType
)
{
case
MySQL:
return
new
MySQLShardingMetaDataHandler
(
dataSource
,
actualTableName
);
default
:
throw
new
UnsupportedOperationException
(
String
.
format
(
"Cannot support database [%s]."
,
databaseType
)
);
return
new
DefaultShardingMetaDataHandler
(
dataSource
,
actualTableName
);
}
}
}
sharding-jdbc-core/src/main/java/io/shardingjdbc/core/jdbc/metadata/dialect/DefaultShardingMetaDataHandler.java
0 → 100644
浏览文件 @
a7d2675a
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/
package
io.shardingjdbc.core.jdbc.metadata.dialect
;
import
io.shardingjdbc.core.metadata.ColumnMetaData
;
import
javax.sql.DataSource
;
import
java.sql.SQLException
;
import
java.util.Collection
;
import
java.util.LinkedList
;
/**
* MySQL table metadata handler.
*
* @author panjuan
*/
public
final
class
DefaultShardingMetaDataHandler
extends
ShardingMetaDataHandler
{
public
DefaultShardingMetaDataHandler
(
final
DataSource
dataSource
,
final
String
actualTableName
)
{
super
(
dataSource
,
actualTableName
);
}
@Override
public
Collection
<
ColumnMetaData
>
getColumnMetaDataList
()
throws
SQLException
{
return
new
LinkedList
<>();
}
}
sharding-jdbc-core/src/main/java/io/shardingjdbc/core/jdbc/metadata/dialect/MySQLShardingMetaDataHandler.java
浏览文件 @
a7d2675a
...
...
@@ -19,6 +19,7 @@ package io.shardingjdbc.core.jdbc.metadata.dialect;
import
io.shardingjdbc.core.metadata.ColumnMetaData
;
import
javax.sql.DataSource
;
import
java.sql.Connection
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
...
...
@@ -40,9 +41,9 @@ public final class MySQLShardingMetaDataHandler extends ShardingMetaDataHandler
@Override
public
Collection
<
ColumnMetaData
>
getColumnMetaDataList
()
throws
SQLException
{
List
<
ColumnMetaData
>
result
=
new
LinkedList
<>();
try
(
Statement
statement
=
getDataSource
().
getConnection
().
createStatement
())
{
statement
.
executeQuery
(
String
.
format
(
"desc %s;"
,
getActualTableName
()));
ResultSet
resultSet
=
statement
.
getResultSet
(
);
try
(
Connection
connection
=
getDataSource
().
getConnection
();
Statement
statement
=
connection
.
createStatement
())
{
ResultSet
resultSet
=
statement
.
executeQuery
(
String
.
format
(
"desc %s;"
,
getActualTableName
())
);
while
(
resultSet
.
next
())
{
result
.
add
(
new
ColumnMetaData
(
resultSet
.
getString
(
"Field"
),
resultSet
.
getString
(
"Type"
),
resultSet
.
getString
(
"Key"
)));
}
...
...
sharding-jdbc-core/src/main/java/io/shardingjdbc/core/jdbc/metadata/dialect/ShardingMetaDataHandler.java
浏览文件 @
a7d2675a
...
...
@@ -21,7 +21,6 @@ import io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource;
import
io.shardingjdbc.core.metadata.ColumnMetaData
;
import
lombok.AccessLevel
;
import
lombok.Getter
;
import
javax.sql.DataSource
;
import
java.sql.SQLException
;
import
java.util.Collection
;
...
...
sharding-jdbc-core/src/test/java/io/shardingjdbc/core/jdbc/core/connection/ShardingConnectionTest.java
浏览文件 @
a7d2675a
...
...
@@ -24,6 +24,8 @@ import io.shardingjdbc.core.constant.SQLType;
import
io.shardingjdbc.core.fixture.TestDataSource
;
import
io.shardingjdbc.core.jdbc.core.ShardingContext
;
import
io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource
;
import
io.shardingjdbc.core.jdbc.metadata.JDBCShardingMetaData
;
import
io.shardingjdbc.core.metadata.ShardingMetaData
;
import
io.shardingjdbc.core.rule.ShardingRule
;
import
org.junit.After
;
import
org.junit.Before
;
...
...
@@ -61,14 +63,15 @@ public final class ShardingConnectionTest {
}
@Before
public
void
setUp
()
{
public
void
setUp
()
throws
SQLException
{
ShardingRuleConfiguration
shardingRuleConfig
=
new
ShardingRuleConfiguration
();
TableRuleConfiguration
tableRuleConfig
=
new
TableRuleConfiguration
();
tableRuleConfig
.
setLogicTable
(
"test"
);
shardingRuleConfig
.
getTableRuleConfigs
().
add
(
tableRuleConfig
);
Map
<
String
,
DataSource
>
dataSourceMap
=
new
HashMap
<>(
1
,
1
);
dataSourceMap
.
put
(
DS_NAME
,
masterSlaveDataSource
);
ShardingContext
shardingContext
=
new
ShardingContext
(
dataSourceMap
,
new
ShardingRule
(
shardingRuleConfig
,
dataSourceMap
.
keySet
()),
null
,
null
,
false
);
ShardingMetaData
shardingMetaData
=
new
JDBCShardingMetaData
(
dataSourceMap
,
null
);
ShardingContext
shardingContext
=
new
ShardingContext
(
dataSourceMap
,
new
ShardingRule
(
shardingRuleConfig
,
dataSourceMap
.
keySet
()),
null
,
null
,
shardingMetaData
,
false
);
connection
=
new
ShardingConnection
(
shardingContext
);
}
...
...
sharding-jdbc-core/src/test/java/io/shardingjdbc/core/jdbc/core/datasource/ShardingDataSourceTest.java
浏览文件 @
a7d2675a
...
...
@@ -27,25 +27,17 @@ import io.shardingjdbc.core.constant.ShardingPropertiesConstant;
import
io.shardingjdbc.core.executor.ExecutorEngine
;
import
io.shardingjdbc.core.rule.ShardingRule
;
import
org.junit.Test
;
import
org.mockito.ArgumentMatchers
;
import
javax.sql.DataSource
;
import
java.lang.reflect.Field
;
import
java.sql.Connection
;
import
java.sql.DatabaseMetaData
;
import
java.sql.SQLException
;
import
java.util.Collections
;
import
java.util.HashMap
;
import
java.util.LinkedList
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Properties
;
import
java.sql.*
;
import
java.util.*
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
hamcrest
.
CoreMatchers
.
not
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
static
org
.
mockito
.
Mockito
.*;
public
final
class
ShardingDataSourceTest
{
...
...
@@ -121,9 +113,13 @@ public final class ShardingDataSourceTest {
DataSource
result
=
mock
(
DataSource
.
class
);
Connection
connection
=
mock
(
Connection
.
class
);
DatabaseMetaData
databaseMetaData
=
mock
(
DatabaseMetaData
.
class
);
Statement
statement
=
mock
(
Statement
.
class
);
ResultSet
resultSet
=
mock
(
ResultSet
.
class
);
when
(
connection
.
getMetaData
()).
thenReturn
(
databaseMetaData
);
when
(
databaseMetaData
.
getDatabaseProductName
()).
thenReturn
(
dataBaseProductName
);
when
(
result
.
getConnection
()).
thenReturn
(
connection
);
when
(
connection
.
createStatement
()).
thenReturn
(
statement
);
when
(
statement
.
executeQuery
(
ArgumentMatchers
.<
String
>
any
())).
thenReturn
(
resultSet
);
return
result
;
}
...
...
sharding-proxy/src/main/java/io/shardingjdbc/proxy/config/ShardingRuleRegistry.java
浏览文件 @
a7d2675a
...
...
@@ -18,13 +18,16 @@
package
io.shardingjdbc.proxy.config
;
import
io.shardingjdbc.core.exception.ShardingJdbcException
;
import
io.shardingjdbc.core.metadata.ShardingMetaData
;
import
io.shardingjdbc.core.rule.ShardingRule
;
import
io.shardingjdbc.core.yaml.sharding.YamlShardingConfiguration
;
import
io.shardingjdbc.proxy.metadata.ProxyShardingMetaData
;
import
lombok.Getter
;
import
javax.sql.DataSource
;
import
java.io.File
;
import
java.io.IOException
;
import
java.sql.SQLException
;
import
java.util.Collections
;
import
java.util.Map
;
...
...
@@ -42,15 +45,23 @@ public final class ShardingRuleRegistry {
private
final
ShardingRule
shardingRule
;
private
final
ShardingMetaData
shardingMetaData
;
private
ShardingRuleRegistry
()
{
YamlShardingConfiguration
yamlShardingConfig
;
try
{
yamlShardingConfig
=
YamlShardingConfiguration
.
unmarshal
(
new
File
(
getClass
().
getResource
(
"/conf/sharding-config.yaml"
).
getFile
()));
}
catch
(
IOException
ex
)
{
}
catch
(
final
IOException
ex
)
{
throw
new
ShardingJdbcException
(
ex
);
}
dataSourceMap
=
yamlShardingConfig
.
getDataSources
();
shardingRule
=
yamlShardingConfig
.
getShardingRule
(
Collections
.<
String
>
emptyList
());
try
{
shardingMetaData
=
new
ProxyShardingMetaData
(
dataSourceMap
);
shardingMetaData
.
init
(
shardingRule
);
}
catch
(
final
SQLException
ex
)
{
throw
new
ShardingJdbcException
(
ex
);
}
}
/**
...
...
sharding-proxy/src/main/java/io/shardingjdbc/proxy/metadata/ProxyShardingMetaData.java
0 → 100644
浏览文件 @
a7d2675a
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/
package
io.shardingjdbc.proxy.metadata
;
import
io.shardingjdbc.core.metadata.ColumnMetaData
;
import
io.shardingjdbc.core.metadata.ShardingMetaData
;
import
io.shardingjdbc.core.rule.DataNode
;
import
lombok.Getter
;
import
lombok.RequiredArgsConstructor
;
import
javax.sql.DataSource
;
import
java.sql.SQLException
;
import
java.util.Collection
;
import
java.util.Map
;
/**
* Sharding metadata for proxy.
*
* @author panjuan
*/
@RequiredArgsConstructor
@Getter
public
final
class
ProxyShardingMetaData
extends
ShardingMetaData
{
private
final
Map
<
String
,
DataSource
>
dataSourceMap
;
@Override
protected
Collection
<
ColumnMetaData
>
getColumnMetaDataList
(
final
DataNode
dataNode
)
throws
SQLException
{
return
new
ShardingMetaDataHandler
(
dataSourceMap
.
get
(
dataNode
.
getDataSourceName
()),
dataNode
.
getTableName
()).
getColumnMetaDataList
();
}
}
sharding-proxy/src/main/java/io/shardingjdbc/proxy/metadata/ShardingMetaDataHandler.java
0 → 100644
浏览文件 @
a7d2675a
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/
package
io.shardingjdbc.proxy.metadata
;
import
io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource
;
import
io.shardingjdbc.core.metadata.ColumnMetaData
;
import
lombok.Getter
;
import
javax.sql.DataSource
;
import
java.sql.Connection
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
import
java.util.Collection
;
import
java.util.LinkedList
;
import
java.util.List
;
/**
* Sharding meta data handler.
*
* @author panjuan
*/
@Getter
public
final
class
ShardingMetaDataHandler
{
private
final
DataSource
dataSource
;
private
final
String
actualTableName
;
public
ShardingMetaDataHandler
(
final
DataSource
dataSource
,
final
String
actualTableName
)
{
this
.
dataSource
=
dataSource
instanceof
MasterSlaveDataSource
?
((
MasterSlaveDataSource
)
dataSource
).
getMasterDataSource
().
values
().
iterator
().
next
()
:
dataSource
;
this
.
actualTableName
=
actualTableName
;
}
/**
* Get column meta data list.
*
* @return column meta data list
* @throws SQLException SQL exception
*/
public
Collection
<
ColumnMetaData
>
getColumnMetaDataList
()
throws
SQLException
{
List
<
ColumnMetaData
>
result
=
new
LinkedList
<>();
try
(
Connection
connection
=
getDataSource
().
getConnection
();
Statement
statement
=
connection
.
createStatement
())
{
statement
.
executeQuery
(
String
.
format
(
"desc %s;"
,
getActualTableName
()));
ResultSet
resultSet
=
statement
.
getResultSet
();
while
(
resultSet
.
next
())
{
result
.
add
(
new
ColumnMetaData
(
resultSet
.
getString
(
"Field"
),
resultSet
.
getString
(
"Type"
),
resultSet
.
getString
(
"Key"
)));
}
}
return
result
;
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录