-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from mnbjhu/Adding_tests
Adding tests
- Loading branch information
Showing
46 changed files
with
1,013 additions
and
318 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
plugins { | ||
kotlin("jvm") | ||
kotlin("plugin.serialization") version "1.8.0" | ||
`maven-publish` | ||
} | ||
|
||
group = "uk.gibby.dsl" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,54 @@ | ||
package uk.gibby.dsl.core | ||
|
||
import kotlinx.serialization.encodeToString | ||
import uk.gibby.dsl.driver.surrealJson | ||
import uk.gibby.dsl.scopes.FilterScope | ||
import uk.gibby.dsl.scopes.FilterScopeImpl | ||
import uk.gibby.dsl.scopes.SetScope | ||
import uk.gibby.dsl.scopes.* | ||
import uk.gibby.dsl.types.* | ||
|
||
data class Table<T, U: RecordType<T>>(val name: String, val recordType: U) { | ||
fun delete(deleteScope: context(FilterScope) U.() -> Unit = {}): ListType<String?, NullableType<String, StringType>> { | ||
val filter = FilterScopeImpl() | ||
filter.deleteScope(recordType) | ||
return ListType(NullableType("_", stringType), "DELETE FROM $name${filter.getFilterString()}") | ||
context(TransactionScope) | ||
fun delete(): ListType<String?, NullableType<String, StringType>> { | ||
return ListType(NullableType("_", stringType), "(DELETE FROM $name)") | ||
} | ||
context(TransactionScope) | ||
fun <a, A: Reference<a>>delete(deleteScope: context(FilterScope, ReturningScope<T, U>) U.() -> A): ListType<String?, NullableType<String, StringType>> { | ||
val filter = FilterScopeImpl(recordType) | ||
val returned = filter.deleteScope(ReturningScopeImpl(recordType), recordType) | ||
return ListType(NullableType("_", stringType), "(DELETE FROM $name${filter.getFilterString()} RETURN ${returned.getReference()})") | ||
} | ||
|
||
context(TransactionScope) | ||
fun selectAll(selectScope: context(FilterScope) U.() -> Unit = {}): ListType<T, U> { | ||
val filter = FilterScopeImpl() | ||
val filter = FilterScopeImpl(recordType) | ||
with(filter) { selectScope(recordType) } | ||
return ListType(recordType, "SELECT * FROM $name${filter.getFilterString()}") | ||
return ListType(recordType, "(SELECT * FROM $name${filter.getFilterString()})") | ||
} | ||
|
||
context(TransactionScope) | ||
fun <r, R: Reference<r>>select(projection: context(FilterScope) U.() -> R): ListType<r, R> { | ||
val filter = FilterScopeImpl() | ||
val filter = FilterScopeImpl(recordType) | ||
val toSelect = with(filter) { | ||
projection(recordType) | ||
} | ||
return ListType( | ||
toSelect, | ||
"SELECT VALUE ${toSelect.getReference()} FROM $name${filter.getFilterString()}" | ||
"(SELECT VALUE ${toSelect.getReference()} FROM $name${filter.getFilterString()})" | ||
) | ||
TODO() | ||
} | ||
|
||
fun update(updateScope: context(SetScope, FilterScope) U.() -> Unit): ListType<T, U> { | ||
context(TransactionScope) | ||
fun <a, A: Reference<a>>update(updateScope: context(SetScope, FilterScope, ReturningScope<T, U>) U.() -> A): ListType<a, A> { | ||
val setScope = SetScope() | ||
val filterScope = FilterScopeImpl() | ||
updateScope(setScope, filterScope, recordType) | ||
|
||
return ListType(recordType, "UPDATE $name ${setScope.getSetString()} ${filterScope.getFilterString()}") | ||
val filterScope = FilterScopeImpl(recordType) | ||
val returned = updateScope(setScope, filterScope, ReturningScopeImpl(recordType), recordType) | ||
return ListType(returned, "UPDATE $name ${setScope.getSetString()} ${filterScope.getFilterString()} " + | ||
"RETURN ${when(val ref = returned.getReference()){ | ||
"AFTER" -> "AFTER" | ||
"BEFORE" -> "BEFORE" | ||
"NONE" -> "NONE" | ||
else -> "VALUE $ref" | ||
}}") | ||
} | ||
operator fun get(id: String): Table<T, U> { | ||
return Table("$name:$id", recordType) | ||
operator fun get(id: String): TableId<T, U> { | ||
return TableId("$name:$id", recordType) | ||
} | ||
} | ||
|
||
inline fun <reified T, U: RecordType<T>>Table<T, U>.insert(vararg items: T): ListType<T, U>{ | ||
return ListType(recordType, "INSERT INTO $name ${surrealJson.encodeToString(items.toList())}") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.