forked from croc-code/sql-boot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FsResourceTypes.java
223 lines (205 loc) · 9.18 KB
/
FsResourceTypes.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**
* The MIT License (MIT)
* <p>
* Copyright (c) 2016-2017 Maksim Gramin
* <p>
* 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:
* <p>
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
* <p>
* 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;
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;
import com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.table.TableJdbcResourceType;
import com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.table.column.ColumnJdbcResourceType;
import com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.table.fk.FkJdbcResourceType;
import com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.table.index.IndexJdbcResourceType;
import com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.table.pk.PkJdbcResourceType;
import com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.table.relation.ChildTableJdbcResourceType;
import com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.table.relation.ParentTableJdbcResourceType;
import com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.view.ViewJdbcResourceType;
import com.github.mgramin.sqlboot.model.resource_type.impl.sql.SqlResourceType;
import com.github.mgramin.sqlboot.model.resource_type.wrappers.body.SqlBodyWrapper;
import com.github.mgramin.sqlboot.model.resource_type.wrappers.body.TemplateBodyWrapper;
import com.github.mgramin.sqlboot.model.resource_type.wrappers.header.SelectWrapper;
import com.github.mgramin.sqlboot.model.resource_type.wrappers.list.LimitWrapper;
import com.github.mgramin.sqlboot.model.resource_type.wrappers.list.PageWrapper;
import com.github.mgramin.sqlboot.model.uri.Uri;
import com.github.mgramin.sqlboot.sql.impl.JdbcSqlQuery;
import com.github.mgramin.sqlboot.template.generator.impl.GroovyTemplateGenerator;
import org.apache.tomcat.jdbc.pool.DataSource;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Collections.singletonList;
import static org.apache.commons.io.FileUtils.readFileToString;
import static org.apache.commons.lang3.StringUtils.substringBetween;
/**
* Created by MGramin on 11.07.2017.
*/
public class FsResourceTypes implements ResourceType {
/**
*
*/
private final DataSource dataSource;
/**
*
*/
private final List<ResourceType> resourceTypes;
/**
* Ctor.
*
* @param dbConnection
* @throws BootException
*/
public FsResourceTypes(final DbConnection dbConnection) throws BootException {
dataSource = dbConnection.getDataSource();
try {
final String baseFolder = dbConnection.getBaseFolder().getFile().getPath();
resourceTypes = walk(baseFolder);
} catch (IOException e) {
throw new BootException(e);
}
}
@Deprecated
public List<ResourceType> resourceTypes() {
return resourceTypes;
}
/**
*
* @param path
* @return
*/
private List<ResourceType> walk(final String path) {
File[] files = new File(path).listFiles();
if (files == null) return null;
List<ResourceType> list = new ArrayList<>();
for (File f : files) {
if (f.isDirectory()) {
File sqlFile = new File(f, "README.md");
list.addAll(walk(f.getAbsolutePath()));
final ResourceType jdbcResourceType;
switch (f.getName()) {
case "schema":
jdbcResourceType = new SchemaJdbcResourceType(dataSource);
break;
case "table":
jdbcResourceType = new TableJdbcResourceType(dataSource);
break;
case "child":
jdbcResourceType = new ChildTableJdbcResourceType(dataSource);
break;
case "parent":
jdbcResourceType = new ParentTableJdbcResourceType(dataSource);
break;
case "pk":
jdbcResourceType = new PkJdbcResourceType(dataSource);
break;
case "index":
jdbcResourceType = new IndexJdbcResourceType(dataSource);
break;
case "fk":
jdbcResourceType = new FkJdbcResourceType(dataSource);
break;
case "view":
jdbcResourceType = new ViewJdbcResourceType(dataSource);
break;
case "column":
jdbcResourceType = new ColumnJdbcResourceType(dataSource);
break;
case "function":
jdbcResourceType = new FunctionJdbcResourceType(dataSource);
break;
case "procedure":
jdbcResourceType = new ProcedureJdbcResourceType(dataSource);
break;
default:
jdbcResourceType = null;
}
String sql = null;
try {
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
}
final ResourceType baseResourceType;
if (sqlFile.exists() && sql != null) {
baseResourceType = new SqlResourceType(new JdbcSqlQuery(dataSource, new GroovyTemplateGenerator(sql)), singletonList(f.getName()));
} else if (jdbcResourceType != null) {
baseResourceType = jdbcResourceType;
} else {
baseResourceType = null;
}
final ResourceType resourceType = new SelectWrapper(
// new SqlBodyWrapper(
new TemplateBodyWrapper(
new PageWrapper(
new LimitWrapper(
// new WhereWrapper(
baseResourceType)
),
new GroovyTemplateGenerator("EMPTY BODY ..."))
// dataSource)
);
if (baseResourceType != null) {
list.add(resourceType);
}
}
}
return list;
}
@Override
public String name() {
throw new BootException("Not implemented!");
}
@Override
public List<String> aliases() {
throw new BootException("Not implemented!");
}
@Override
public List<String> path() {
throw new BootException("Not implemented!");
}
@Override
public Stream<DbResource> read(final Uri uri) throws BootException {
return resourceTypes.stream()
.filter(v -> v.name().equalsIgnoreCase(uri.type()))
.findAny()
.get()
.read(uri);
}
@Override
public Map<String, String> metaData() {
throw new BootException("Not implemented!");
}
}