Skip to content

Commit c4da22e

Browse files
committed
1 parent 6334d57 commit c4da22e

File tree

8 files changed

+45
-30
lines changed

8 files changed

+45
-30
lines changed

src/main/kotlin/com/github/mgramin/sqlboot/model/resource/DbResource.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,5 @@ interface DbResource : Serializable {
7575
*/
7676
@JsonProperty
7777
fun body(): String
78+
7879
}

src/main/kotlin/com/github/mgramin/sqlboot/model/resource/impl/FakeDbResource.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ class FakeDbResource(private val uri: Uri) : DbResource {
5050
return mapOf(
5151
"schema" to "hr",
5252
"table" to "persons",
53-
"file" to "table.hr.persons.sql")
53+
"file" to "table.hr.persons.sql",
54+
"size" to "100")
5455
}
5556

5657
override fun body(): String {

src/main/kotlin/com/github/mgramin/sqlboot/model/resourcetype/Metadata.kt

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ import java.util.*
88
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
99
data class Metadata(
1010
private val name: String,
11-
private val type: String,
11+
private var type: String,
1212
private val description: String
1313
) : Comparable<Metadata> {
1414

15-
override fun compareTo(other: Metadata): Int = if (name > other.name()) -1 else 1
16-
1715
private val properties: MutableMap<String, Any>
1816

1917
constructor(name: String, description: String) : this(name, "String", description)
@@ -24,25 +22,28 @@ data class Metadata(
2422
val map: Map<String, Any> = Gson().fromJson(description, object : TypeToken<Map<String, Any>>() {}.type)
2523
this.properties.putAll(map)
2624
this.properties["key"] = name.replace("@", "")
25+
if (map.containsKey("type")) {
26+
this.type = map["type"].toString()
27+
} else {
28+
this.type = "String"
29+
}
2730
} catch (ignored: Exception) {
2831
this.properties.clear()
2932
val prop = HashMap<String, Any>()
3033
prop["key"] = name.replace("@", "")
3134
prop["label"] = name.replace("@", "")
3235
prop["description"] = description
36+
prop["type"] = type
3337
this.properties.putAll(prop)
3438
}
3539
}
3640

37-
fun name(): String {
38-
return name
39-
}
41+
fun name(): String = name
4042

41-
fun description(): String {
42-
return description
43-
}
43+
fun description(): String = description
44+
45+
fun properties(): Map<String, Any> = properties
46+
47+
override fun compareTo(other: Metadata): Int = if (name > other.name()) -1 else 1
4448

45-
fun properties(): Map<String, Any> {
46-
return properties
47-
}
4849
}

src/main/kotlin/com/github/mgramin/sqlboot/model/resourcetype/impl/FakeResourceType.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class FakeResourceType : ResourceType {
5252
override fun metaData(uri: Uri) =
5353
listOf(Metadata("@schema", "Schema name"),
5454
Metadata("@table", "Table name"),
55-
Metadata("@index", "Index name"))
55+
Metadata("@index", "Index name"),
56+
Metadata("size", "Number", "Table size"))
5657

5758
}

src/main/kotlin/com/github/mgramin/sqlboot/model/resourcetype/impl/FsResourceType.kt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import com.github.mgramin.sqlboot.model.resourcetype.Metadata
3131
import com.github.mgramin.sqlboot.model.resourcetype.ResourceType
3232
import com.github.mgramin.sqlboot.model.resourcetype.wrappers.body.BodyWrapper
3333
import com.github.mgramin.sqlboot.model.resourcetype.wrappers.header.SelectWrapper
34+
import com.github.mgramin.sqlboot.model.resourcetype.wrappers.header.TypeWrapper
3435
import com.github.mgramin.sqlboot.model.resourcetype.wrappers.list.SortWrapper
3536
import com.github.mgramin.sqlboot.model.uri.Uri
3637
import com.github.mgramin.sqlboot.template.generator.impl.GroovyTemplateGenerator
@@ -62,15 +63,16 @@ class FsResourceType(
6263
.toList()
6364

6465
private fun createObjectType(it: com.github.mgramin.sqlboot.tools.files.file.File) =
65-
SelectWrapper(
66-
SortWrapper(
67-
BodyWrapper(
68-
SqlResourceType(
69-
aliases = listOf(File(it.name()).nameWithoutExtension),
70-
sql = it.content().toString(Charset.defaultCharset()),
71-
connections = dbConnections,
72-
dialects = dialects),
73-
templateGenerator = GroovyTemplateGenerator("[EMPTY BODY]"))))
66+
TypeWrapper(
67+
SelectWrapper(
68+
SortWrapper(
69+
BodyWrapper(
70+
SqlResourceType(
71+
aliases = listOf(File(it.name()).nameWithoutExtension),
72+
sql = it.content().toString(Charset.defaultCharset()),
73+
connections = dbConnections,
74+
dialects = dialects),
75+
templateGenerator = GroovyTemplateGenerator("[EMPTY BODY]")))))
7476

7577
@Deprecated("")
7678
fun resourceTypes() = resourceTypes

src/main/kotlin/com/github/mgramin/sqlboot/model/resourcetype/impl/SqlResourceType.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ class SqlResourceType(
8989
}
9090
}
9191

92-
override fun metaData(uri: Uri): List<Metadata> = simpleSelectQuery.columns()
92+
override fun metaData(uri: Uri): List<Metadata> = simpleSelectQuery
93+
.columns()
9394
.map { Metadata(it.key, it.value) } +
9495
Metadata("database", """{"label": "Database", "description": "Database name", "visible": true}""")
9596

src/main/kotlin/com/github/mgramin/sqlboot/model/resourcetype/wrappers/header/TypeWrapper.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,14 @@ class TypeWrapper(private val origin: ResourceType) : ResourceType {
2424
override fun dbUri() = it.dbUri()
2525
override fun body() = it.body()
2626
override fun headers(): Map<String, Any> {
27-
return it.headers().toMutableMap()
27+
val newHeaders = it.headers().toMutableMap()
28+
metaData(uri).forEach { m ->
29+
val type = m.properties()["type"].toString()
30+
if (type.equals("number", ignoreCase = true)) {
31+
newHeaders[m.name()] = newHeaders[m.name()].toString().toInt()
32+
}
33+
}
34+
return newHeaders
2835
}
2936

3037
}

src/test/kotlin/com/github/mgramin/sqlboot/model/resourcetype/wrappers/ResourceTypeWrapperTest.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ class ResourceTypeWrapperTest {
163163
Metadata("@schema", "String", "Schema name"),
164164
Metadata("@table", "String", "Table name"),
165165
Metadata("@index", "String", "Index name"),
166+
Metadata("size", "Number", "Table size"),
166167
Metadata("database", "String", "Database name")).sorted(),
167168
w.metaData(FakeUri()).sorted())
168169
}
@@ -178,10 +179,8 @@ class ResourceTypeWrapperTest {
178179
fun read() {
179180
StepVerifier
180181
.create(w.read(FakeUri()))
181-
.expectNextMatches { v -> v.headers().count() == 3 }
182-
.expectNextMatches { v -> v.headers().count() == 3 }
183-
.expectNextMatches { v -> v.headers().count() == 3 }
184-
.expectNextCount(0)
182+
.expectNextMatches { v -> v.headers().count() == 4 && v.headers()["size"] is Int }
183+
.expectNextCount(2)
185184
.verifyComplete()
186185
}
187186

@@ -224,7 +223,9 @@ class ResourceTypeWrapperTest {
224223
arrayListOf(
225224
Metadata("@schema", "String", "Schema name"),
226225
Metadata("@table", "String", "Table name"),
227-
Metadata("@index", "String", "Index name")).sorted(),
226+
Metadata("@index", "String", "Index name"),
227+
Metadata("size", "Number", "Table size")
228+
).sorted(),
228229
w.metaData(FakeUri()).sorted())
229230
}
230231

0 commit comments

Comments
 (0)