Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* Create branch croc-code#104

* add md visitor

* add md visitor

* save register for headers element
  • Loading branch information
mgramin committed Mar 30, 2018
1 parent 7db1f35 commit 3d60318
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 17 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Expand Up @@ -193,6 +193,11 @@
<version>4.5</version>
</dependency>

<dependency>
<groupId>com.atlassian.commonmark</groupId>
<artifactId>commonmark</artifactId>
<version>0.11.0</version>
</dependency>

<!-- TMP dependency -->
<dependency>
Expand Down
Expand Up @@ -27,13 +27,15 @@
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import com.github.mgramin.sqlboot.exceptions.BootException;
import com.github.mgramin.sqlboot.model.connection.DbConnection;
import com.github.mgramin.sqlboot.model.resource.DbResource;
import com.github.mgramin.sqlboot.model.resource_type.ResourceType;
import com.github.mgramin.sqlboot.model.resource_type.impl.composite.md.MarkdownFile;
import com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.SchemaJdbcResourceType;
import com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.function.FunctionJdbcResourceType;
import com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.procedure.ProcedureJdbcResourceType;
Expand Down Expand Up @@ -151,20 +153,16 @@ private List<ResourceType> walk(final String path) {

String sql = null;
try {
sql = substringBetween(readFileToString(sqlFile, UTF_8), "````sql", "````");
MarkdownFile markdownFile = new MarkdownFile(readFileToString(sqlFile, UTF_8));
Map<String, String> parse = markdownFile.parse();
Iterator<Map.Entry<String, String>> iterator = parse.entrySet().iterator();
if (iterator.hasNext()) {
sql = iterator.next().getValue();
}
} catch (IOException e) {
// TODO catch and process this exception
}

String ddlSql = null;
try {
ddlSql = substringBetween(readFileToString(sqlFile, UTF_8), "```sql-template", "```");
} catch (IOException e) {
// TODO catch and process this exception
}
if (ddlSql == null) {
ddlSql = "";
}

final ResourceType baseResourceType;
if (sqlFile.exists() && sql != null) {
Expand All @@ -175,15 +173,16 @@ private List<ResourceType> walk(final String path) {
baseResourceType = null;
}
final ResourceType resourceType = new SelectWrapper(
new SqlBodyWrapper(
// new SqlBodyWrapper(
new TemplateBodyWrapper(
new PageWrapper(
new LimitWrapper(
// new WhereWrapper(
baseResourceType)
),
new GroovyTemplateGenerator(ddlSql)),
dataSource));
new GroovyTemplateGenerator("EMPTY BODY ..."))
// dataSource)
);
if (baseResourceType != null) {
list.add(resourceType);
}
Expand Down
@@ -0,0 +1,58 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2016-2017 Maksim Gramin
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.github.mgramin.sqlboot.model.resource_type.impl.composite.md;

import org.commonmark.node.AbstractVisitor;
import org.commonmark.node.FencedCodeBlock;
import org.commonmark.node.Heading;
import org.commonmark.node.Text;

import java.util.LinkedHashMap;
import java.util.Map;

public class CustomVisitor extends AbstractVisitor {

private String currentTag;
private Map<String, String> map = new LinkedHashMap<>();

@Override
public void visit(Text text) {
if (text.getParent() instanceof Heading && ((Heading)text.getParent()).getLevel()>=3) {
currentTag = text.getLiteral();
}
}

@Override
public void visit(FencedCodeBlock fencedCodeBlock) {
if (fencedCodeBlock.getFenceLength() == 4) {
map.put(currentTag, fencedCodeBlock.getLiteral());
}
}

public Map<String, String> getMap() {
return map;
}

}
@@ -0,0 +1,54 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2016-2017 Maksim Gramin
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.github.mgramin.sqlboot.model.resource_type.impl.composite.md;

import org.commonmark.node.Node;
import org.commonmark.parser.Parser;

import java.util.Map;

/**
* @author Maksim Gramin (mgramin@gmail.com)
* @version $Id$
* @since 0.1
*/
// TODO implement com.github.mgramin.sqlboot.tools.files.file.File ?
public class MarkdownFile {

private final String text;

public MarkdownFile(final String text) {
this.text = text;
}

public Map<String, String> parse() {
Parser parser = Parser.builder().build();
Node document = parser.parse(text);
CustomVisitor visitor = new CustomVisitor();
document.accept(visitor);
return visitor.getMap();
}

}
Expand Up @@ -143,7 +143,7 @@ public Map<String, Object> next() {
return new SimpleEntry<>(v, object);
})
.collect(toMap(
k -> k.getKey().toLowerCase(),
k -> k.getKey()/*.toLowerCase()*/,
v -> ofNullable(v.getValue()).orElse(nullAlias),
(a, b) -> a,
LinkedHashMap::new));
Expand Down
10 changes: 9 additions & 1 deletion src/main/resources/application.yml
Expand Up @@ -4,12 +4,20 @@ conf:
baseFolder: file:conf/common/database
url: jdbc:h2:mem:;INIT=RUNSCRIPT FROM 'classpath:schema.sql';
driverClassName: org.h2.Driver
- name: oracle
- name: oracle_jdbc
# baseFolder: file:conf/sql-boot-oracle/database
baseFolder: file:conf/common/database
url: jdbc:oracle:thin:@localhost:1521:XE
driverClassName: oracle.jdbc.OracleDriver
user: system
password: oracle
- name: oracle
baseFolder: file:conf/sql-boot-oracle/database
# baseFolder: file:conf/common/database
url: jdbc:oracle:thin:@localhost:1521:XE
driverClassName: oracle.jdbc.OracleDriver
user: system
password: oracle
- name: postgres
baseFolder: file:conf/common/database
url: jdbc:postgresql://localhost:9876/postgres
Expand Down
@@ -0,0 +1,47 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2016-2017 Maksim Gramin
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.github.mgramin.sqlboot.model.resource_type.impl.composite.md;

import org.apache.commons.io.FileUtils;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.Map;

public class MarkdownFileTest {

private Map<String, String> map;

@Test
public void parse() throws IOException {
String text = FileUtils.readFileToString(new File("test.md"));

MarkdownFile markdownFile = new MarkdownFile(text);
Map<String, String> parse = markdownFile.parse();
System.out.println(parse);
}

}
Expand Up @@ -51,9 +51,9 @@ public class JdbcSqlQueryTest {
DataSource dataSource;

@Test
public void select() throws Exception {
public void select() {
final List<Map<String, Object>> select = new JdbcSqlQuery(dataSource,
"select * from (select name AS n, email as mail from main_schema.users)")
"select * from (select name AS \"n\", email as \"mail\" from main_schema.users)")
.select().collect(toList());
assertEquals(select.toString(),
"[{n=mkyong, mail=mkyong@gmail.com}, {n=alex, mail=alex@yahoo.com}, {n=joel, mail=joel@gmail.com}]");
Expand Down
22 changes: 22 additions & 0 deletions test.md
@@ -0,0 +1,22 @@
# Schema

### get_all_tables
````sql
select u.username as "@schema"
, u.user_id as "user_id"
, u.created as "created"
from all_users u
order by u.username
````
```sql
select DBMS_DDL.get_metadata(...)
from dual
```

### row_count

````sql
select count(1)
from all_users u
order by u.username
````

0 comments on commit 3d60318

Please sign in to comment.