Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation table index modification #796

Merged
merged 3 commits into from
May 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

import javax.xml.bind.JAXBException;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
Expand Down Expand Up @@ -68,7 +69,7 @@ public final class StartTest {
private final String id;

@Parameters(name = "{0} ({2}) -> {1}")
public static Collection<String[]> getParameters() throws IOException, JAXBException {
public static Collection<String[]> getParameters() throws IOException, JAXBException, URISyntaxException {
URL integrateResources = StartTest.class.getClassLoader().getResource(INTEGRATION_RESOURCES_PATH);
assertNotNull(integrateResources);
for (String each : getAssertFiles(integrateResources)) {
Expand All @@ -94,9 +95,9 @@ public static Collection<String[]> getParameters() throws IOException, JAXBExcep
return RESULT_ASSERT;
}

private static List<String> getAssertFiles(final URL integrateResources) throws IOException {
private static List<String> getAssertFiles(final URL integrateResources) throws IOException, URISyntaxException {
final List<String> result = new LinkedList<>();
Files.walkFileTree(Paths.get(integrateResources.getPath()), new SimpleFileVisitor<Path>() {
Files.walkFileTree(Paths.get(integrateResources.toURI()), new SimpleFileVisitor<Path>() {

@Override
public FileVisitResult visitFile(final Path file, final BasicFileAttributes basicFileAttributes) {
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import io.shardingjdbc.dbtest.config.bean.ColumnDefinition;
import io.shardingjdbc.dbtest.config.bean.DatasetDefinition;
import io.shardingjdbc.dbtest.config.bean.IndexDefinition;
import io.shardingjdbc.dbtest.exception.DbTestException;
import org.apache.commons.lang3.StringUtils;
import org.w3c.dom.Attr;
Expand All @@ -49,13 +50,14 @@ public class AnalyzeDataset {
* Parsing the Dataset file.
*
* @param path path
* @param tableName tableName
* @return DatasetDefinition
* @throws IOException IOException
* @throws SAXException SAXException
* @throws ParserConfigurationException ParserConfigurationException
* @throws XPathExpressionException XPathExpressionException
*/
public static DatasetDefinition analyze(final String path, String tableName)
public static DatasetDefinition analyze(final String path, final String tableName)
throws IOException, SAXException, ParserConfigurationException, XPathExpressionException {
return analyze(new File(path), tableName);
}
Expand All @@ -64,20 +66,27 @@ public static DatasetDefinition analyze(final String path, String tableName)
* Parsing the Dataset file.
*
* @param file file
* @param tableName tableName
* @return DatasetDefinition
* @throws IOException IOException
* @throws SAXException SAXException
* @throws ParserConfigurationException ParserConfigurationException
* @throws XPathExpressionException XPathExpressionException
*/
public static DatasetDefinition analyze(final File file, String tableName)
public static DatasetDefinition analyze(final File file, final String tableName)
throws IOException, SAXException, ParserConfigurationException, XPathExpressionException {

Document doc = parseFile(file);
Node rootNode = getNode(doc, "/init");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
XPathFactory factory = XPathFactory.newInstance();
XPath oXpath = factory.newXPath();
Node rootNode = (Node) oXpath.evaluate("/init", doc, XPathConstants.NODE);
if (rootNode == null) {
throw new DbTestException("file :" + file.getPath() + "analyze error,Missing init tag");
}

NodeList firstNodeList = rootNode.getChildNodes();
DatasetDefinition result = new DatasetDefinition();
for (int i = 0; i < firstNodeList.getLength(); i++) {
Expand Down Expand Up @@ -138,6 +147,11 @@ private static void analyzeTableConfig(final DatasetDefinition datasetDefinition
if (StringUtils.isNotEmpty(numPrecRadix)) {
cd.setSize(Integer.valueOf(numPrecRadix));
}

NodeList indexNodeList = attNode.getChildNodes();
if (indexNodeList != null && indexNodeList.getLength() != 0) {
getIndexs(indexNodeList, cd);
}
}

}
Expand All @@ -146,6 +160,33 @@ private static void analyzeTableConfig(final DatasetDefinition datasetDefinition

}

private static void getIndexs(final NodeList indexNodeList, final ColumnDefinition cd) {
List<IndexDefinition> indexs = new ArrayList<>();
cd.setIndexs(indexs);
for (int w = 0; w < indexNodeList.getLength(); w++) {
Node indexNode = indexNodeList.item(w);
if (indexNode.getNodeType() == Node.ELEMENT_NODE) {
IndexDefinition index = new IndexDefinition();
String nameIndex = getAttr("name", indexNode);
if (StringUtils.isNotEmpty(nameIndex)) {
index.setName(nameIndex);
}

String typeIndex = getAttr("type", indexNode);
if (StringUtils.isNotEmpty(typeIndex)) {
index.setType(typeIndex);
}

String uniqueIndex = getAttr("unique", indexNode);
if (StringUtils.isNotEmpty(uniqueIndex)) {
index.setUnique(Boolean.valueOf(uniqueIndex));
}

indexs.add(index);
}
}
}

private static String getAttr(final String nodeName, final Node node) {
NamedNodeMap attNodeList = node.getAttributes();
for (int n = 0; n < attNodeList.getLength(); n++) {
Expand Down Expand Up @@ -188,39 +229,4 @@ private static void analyzeDataset(final DatasetDefinition result, final String
}
}

/**
* Parse the file to Document.
*
* @param file file
* @return Document
* @throws ParserConfigurationException ParserConfigurationException
* @throws IOException IOException
* @throws SAXException SAXException
*/
private 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;
}

/**
* Acquisition node.
*
* @param node node
* @param xpath xpath
* @return node
* @throws XPathExpressionException XPathExpressionException
*/
private 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;
}


}
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
/*
* 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.dbtest.config.bean;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

@Data
public class ColumnDefinition {

Expand All @@ -19,4 +39,6 @@ public class ColumnDefinition {

private int isAutoincrement;

private List<IndexDefinition> indexs = new ArrayList<>();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.dbtest.config.bean;

import lombok.Data;

@Data
public class IndexDefinition {

private String name;

private String type;

private boolean unique;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
<asserts sharding-rule-config="./config/test" init-data-file="./init" database="h2,mysql,oracle,sqlserver,postgresql">
<!--<assertDDL id="assertCreateTable" clean-sql="dropTLog" expected-update="1" sql="assertCreateTable"
<assertDDL id="assertCreateTable" clean-sql="dropTLog" expected-update="1" sql="assertCreateTable"
table="t_log"
expected-data-file="assertCreateTable.xml">
</assertDDL>-->
</assertDDL>
<assertDDL id="assertCreateIndex" init-sql="createTLog" clean-sql="dropTLog" expected-update="1" sql="assertCreateIndex"
table="t_log"
expected-data-file="assertCreateIndex.xml">
</assertDDL>
<assertDDL id="assertCreateUniqueIndex" init-sql="createTLog" clean-sql="dropTLog" expected-update="1" sql="assertCreateUniqueIndex"
table="t_log"
expected-data-file="assertCreateUniqueIndex.xml">
</assertDDL>
</asserts>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<init>
<metadata>
<table name="t_log">
<cloumn name="id" type="integer" >
<index name="t_log_index_t_log" type="3" unique="false"/>
</cloumn>
<cloumn name="status" type="varchar"/>
</table>
</metadata>
</init>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<init>
<metadata>
<table name="t_log">
<cloumn name="id" type="integer" >
<index name="t_log_index_t_log" type="3" unique="true"/>
</cloumn>
<cloumn name="status" type="varchar"/>
</table>
</metadata>
</init>