Skip to content

Commit 8f8367c

Browse files
committed
288
1 parent cdc3cfb commit 8f8367c

14 files changed

Lines changed: 107 additions & 188 deletions

File tree

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ package com.github.mgramin.sqlboot.model.resourcetype
22

33
import com.fasterxml.jackson.annotation.JsonAutoDetect
44
import com.google.gson.Gson
5-
import com.google.gson.JsonParser
65
import com.google.gson.reflect.TypeToken
7-
import org.json.JSONException
8-
import org.json.JSONObject
96
import java.util.HashMap
107

118
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ class FakeDbResourceType : ResourceType {
4848

4949
override fun read(uri: Uri): Flux<DbResource> {
5050
return sequenceOf(
51-
FakeDbResource(DbUri("table/hr.persons")),
52-
FakeDbResource(DbUri("table/hr.users")),
53-
FakeDbResource(DbUri("table/hr.jobs"))).toFlux()
51+
FakeDbResource(DbUri("prod/table/hr.persons")),
52+
FakeDbResource(DbUri("prod/table/hr.users")),
53+
FakeDbResource(DbUri("prod/table/hr.jobs"))).toFlux()
5454
}
5555

5656
override fun metaData(): Map<String, String> {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class SqlResourceType(
8686
val headers = o.entries
8787
.map { strip(it.key, "@") to it.value }
8888
.toMap()
89-
DbResourceImpl(name, this, DbUri(this.name(), path), headers) as DbResource
89+
DbResourceImpl(name, this, DbUri(headers["database"].toString(), this.name(), path), headers) as DbResource
9090
}
9191
}
9292

src/main/kotlin/com/github/mgramin/sqlboot/model/uri/Uri.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,6 @@ interface Uri : Serializable {
6060
*/
6161
fun path(index: Int): String
6262

63-
/**
64-
*
65-
* @return
66-
*/
67-
fun recursive(): Boolean
6863

6964
/**
7065
*

src/main/kotlin/com/github/mgramin/sqlboot/model/uri/impl/DbUri.kt

Lines changed: 34 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -27,91 +27,72 @@ package com.github.mgramin.sqlboot.model.uri.impl
2727
import com.github.mgramin.sqlboot.exceptions.BootException
2828
import com.github.mgramin.sqlboot.model.uri.Uri
2929
import java.net.URI
30-
import java.util.Arrays.asList
31-
import java.util.LinkedHashMap
3230

3331
/**
3432
* Created by maksim on 12.06.16.
3533
*/
3634
class DbUri : Uri {
3735

36+
private val connection: String
3837
private val type: String
3938
private val objects: List<String>
40-
private val recursive: Boolean
41-
private val params = LinkedHashMap<String, String>()
39+
private val params: Map<String, String>
4240
private val action: String
4341

44-
constructor(type: String, objects: List<String>) {
42+
constructor(connection: String, type: String, objects: List<String>) {
43+
this.connection = connection
4544
this.type = type
4645
this.objects = objects
4746
this.action = ""
48-
this.recursive = false
47+
this.params = emptyMap()
4948
}
5049

51-
@Throws(BootException::class)
5250
constructor(uriString: String) {
5351
try {
5452
val uri = URI(uriString)
55-
val pathString = uri.path.replace("*", "%")
56-
val queryString = uri.query
5753

58-
val list = asList(*pathString.split("[/]".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray())
59-
type = list[0]
60-
objects = if (list.size == 1) {
61-
asList("%")
62-
} else {
63-
asList(*list[1].split("[.]".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray())
64-
}
65-
action = if (list.size == 3) {
66-
list[2]
67-
} else {
68-
""
69-
}
70-
recursive = pathString[pathString.length - 1] == '/'
54+
val pathMap: Map<Int, String> = uri.path
55+
.split("/")
56+
.filter { it.isNotEmpty() }
57+
.mapIndexed { index, element -> index to element }
58+
.toMap()
59+
60+
connection = pathMap.getValue(0)
61+
type = pathMap.getValue(1)
62+
objects = pathMap.getOrDefault(2, "*").split(".")
63+
action = pathMap.getOrDefault(3, "")
64+
65+
params = if (uri.query != null)
66+
uri.query
67+
.split("&")
68+
.map { it.split("=")[0] to it.split("=")[1] }
69+
.toMap()
70+
else emptyMap()
7171

72-
if (queryString != null)
73-
for (s in queryString.split("&".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()) {
74-
params[s.split("=".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[0]] = s.split("=".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[1]
75-
}
7672
} catch (e: Exception) {
7773
throw BootException(e)
7874
}
7975
}
8076

81-
override fun type(): String {
82-
return type
83-
}
77+
override fun type() = type
8478

85-
override fun path(): List<String> {
86-
return objects
87-
}
79+
override fun path() = objects
8880

89-
override fun path(index: Int): String {
90-
return if (index > objects.size - 1) {
91-
"%"
92-
} else {
93-
objects[index]
94-
}
95-
}
96-
97-
override fun recursive(): Boolean {
98-
return recursive
99-
}
81+
override fun path(index: Int) =
82+
if (index > objects.size - 1) {
83+
"%"
84+
} else {
85+
objects[index]
86+
}
10087

101-
override fun params(): Map<String, String> {
102-
return params
103-
}
88+
override fun params() = params
10489

105-
override fun action(): String {
106-
return this.action
107-
}
90+
override fun action() = this.action
10891

10992
override fun toString(): String {
110-
val result = StringBuilder(type + "/" + objects.joinToString("."))
93+
val result = StringBuilder(connection + "/" + type + "/" + objects.joinToString("."))
11194
if (action != "" && action != "create")
11295
result.append("/").append(action)
113-
if (recursive)
114-
result.append("/")
11596
if (!params.isEmpty()) {
11697
result.append("?")
11798
var i = 0
@@ -123,4 +104,5 @@ class DbUri : Uri {
123104
}
124105
return result.toString().replace("%", "*")
125106
}
107+
126108
}

src/main/kotlin/com/github/mgramin/sqlboot/model/uri/impl/FakeUri.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ class FakeUri : Uri {
4040
return "FAKE_TBL"
4141
}
4242

43-
override fun recursive(): Boolean {
44-
return false
45-
}
46-
4743
override fun params(): Map<String, String> {
4844
return mapOf("page" to "1,20", "sortby" to "schema,table")
4945
}

src/main/kotlin/com/github/mgramin/sqlboot/model/uri/wrappers/JsonWrapper.kt

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,35 +33,20 @@ import com.github.mgramin.sqlboot.model.uri.Uri
3333
*/
3434
class JsonWrapper(private val origin: Uri) : Uri {
3535

36-
override fun type(): String {
37-
return origin.type()
38-
}
36+
override fun type() = origin.type()
3937

40-
override fun path(): List<String> {
41-
return origin.path()
42-
}
38+
override fun path() = origin.path()
4339

44-
override fun path(index: Int): String {
45-
return origin.path(index)
46-
}
40+
override fun path(index: Int) = origin.path(index)
4741

48-
override fun recursive(): Boolean {
49-
return origin.recursive()
50-
}
42+
override fun params() = origin.params()
5143

52-
override fun params(): Map<String, String> {
53-
return origin.params()
54-
}
55-
56-
override fun action(): String {
57-
return origin.action()
58-
}
44+
override fun action() = origin.action()
5945

6046
override fun toString(): String {
6147
var s = "DbUri{" +
6248
"type='" + origin.type() + '\''.toString() +
6349
", path=" + origin.path() +
64-
", recursive=" + origin.recursive() +
6550
", params=" + origin.params() +
6651
"}"
6752
s = s.replace("%", "*")

src/main/kotlin/com/github/mgramin/sqlboot/model/uri/wrappers/SqlPlaceholdersWrapper.kt

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,33 +33,19 @@ import com.github.mgramin.sqlboot.model.uri.Uri
3333
*/
3434
class SqlPlaceholdersWrapper(private val origin: Uri) : Uri {
3535

36-
override fun type(): String {
37-
return origin.type()
38-
}
36+
override fun type() = origin.type()
3937

40-
override fun path(): List<String> {
41-
return origin.path().asSequence()
42-
.map { v -> v.replace("*", "%") }
43-
.toList()
44-
}
38+
override fun path() = origin.path()
39+
.asSequence()
40+
.map { v -> v.replace("*", "%") }
41+
.toList()
4542

46-
override fun path(index: Int): String {
47-
return origin.path(index).replace("*", "%")
48-
}
43+
override fun path(index: Int) = origin.path(index).replace("*", "%")
4944

50-
override fun recursive(): Boolean {
51-
return origin.recursive()
52-
}
45+
override fun params() = origin.params()
5346

54-
override fun params(): Map<String, String> {
55-
return origin.params()
56-
}
47+
override fun action() = origin.action()
5748

58-
override fun action(): String {
59-
return origin.action()
60-
}
49+
override fun toString() = origin.toString()
6150

62-
override fun toString(): String {
63-
return origin.toString()
64-
}
6551
}

src/main/kotlin/com/github/mgramin/sqlboot/rest/controllers/ApiController.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ class ApiController {
7979
.filter { it.isNotEmpty() }
8080
.filter { it != "api" }
8181
.filter { it != "headers" }
82-
.filterIndexed { index, _ -> index != 0 }
8382
.joinToString(separator = "/") { it }
8483
val uri: Uri = SqlPlaceholdersWrapper(DbUri(parseUri(uriString, request)))
8584
return getListResponseEntityHeaders(uri, connectionName)
@@ -120,7 +119,6 @@ class ApiController {
120119
.filter { it.isNotEmpty() }
121120
.filter { it != "api" }
122121
.filter { it != "meta" }
123-
.filterIndexed { index, _ -> index != 0 }
124122
.joinToString(separator = "/") { it }
125123
val uri = SqlPlaceholdersWrapper(DbUri(parseUri(uriString, request)))
126124
return responseEntity(connectionName, uri)

src/test/kotlin/com/github/mgramin/sqlboot/model/resourcetype/impl/composite/FsResourceTypesTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ class FsResourceTypesTest {
5757
@Test
5858
fun read() {
5959
val types = FsResourceTypes(listOf(db), FakeUri())
60-
assertEquals(5, types.read(DbUri("table/bookings")).count().block())
61-
assertEquals(1, types.read(DbUri("table/bookings.airports")).count().block())
60+
assertEquals(5, types.read(DbUri("prod/table/bookings")).count().block())
61+
assertEquals(1, types.read(DbUri("prod/table/bookings.airports")).count().block())
6262
}
6363

6464
@Test

0 commit comments

Comments
 (0)