-
Notifications
You must be signed in to change notification settings - Fork 513
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support JSON table functions (#3090)
* Support the json table function names * Json Integration test
- Loading branch information
Alec Strong
committed
Apr 16, 2022
1 parent
1db6713
commit 0aa008d
Showing
25 changed files
with
296 additions
and
12 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
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,11 +1,25 @@ | ||
plugins { | ||
alias(deps.plugins.kotlin.jvm) | ||
alias(deps.plugins.grammarKitComposer) | ||
alias(deps.plugins.publish) | ||
alias(deps.plugins.dokka) | ||
} | ||
|
||
grammarKit { | ||
intellijRelease.set(deps.versions.idea) | ||
} | ||
|
||
dependencies { | ||
compileOnly project(':sqldelight-compiler:dialect') | ||
compileOnly deps.intellij.lang | ||
|
||
testImplementation project(':dialects:sqlite-3-18') | ||
testImplementation deps.intellij.core | ||
testImplementation deps.intellij.lang | ||
testImplementation deps.junit | ||
testImplementation deps.truth | ||
testImplementation project(':sqldelight-compiler:dialect') | ||
testImplementation deps.sqlPsiTestFixtures | ||
} | ||
|
||
apply from: "$rootDir/gradle/gradle-mvn-push.gradle" |
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
27 changes: 27 additions & 0 deletions
27
...n-module/src/main/kotlin/app/cash/sqldelight/dialects/sqlite/json/module/grammar/json.bnf
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
// Specify the parent parser. | ||
overrides="com.alecstrong.sql.psi.core.SqlParser" | ||
elementTypeClass = "com.alecstrong.sql.psi.core.SqlElementType" | ||
|
||
implements="com.alecstrong.sql.psi.core.psi.SqlCompositeElement" | ||
extends="com.alecstrong.sql.psi.core.psi.SqlCompositeElementImpl" | ||
psiClassPrefix = "SqliteJson" | ||
} | ||
overrides ::= table_or_subquery | ||
|
||
table_or_subquery ::= ( json_function_name '(' <<expr '-1'>> ( ',' <<expr '-1'>> ) * ')' | ||
| [ {database_name} '.' ] {table_name} [ [ 'AS' ] {table_alias} ] [ 'INDEXED' 'BY' {index_name} | 'NOT' 'INDEXED' ] | ||
| '(' ( {table_or_subquery} ( ',' {table_or_subquery} ) * | {join_clause} ) ')' | ||
| '(' {compound_select_stmt} ')' [ [ 'AS' ] {table_alias} ] ) { | ||
mixin = "app.cash.sqldelight.dialects.sqlite.json.module.grammar.mixins.TableOrSubqueryMixin" | ||
implements = "com.alecstrong.sql.psi.core.psi.SqlTableOrSubquery" | ||
override = true | ||
} | ||
|
||
json_function_name ::= 'json_each' | 'json_tree' { | ||
mixin = "app.cash.sqldelight.dialects.sqlite.json.module.grammar.mixins.JsonFunctionNameMixin" | ||
implements = [ | ||
"com.alecstrong.sql.psi.core.psi.NamedElement"; | ||
"com.alecstrong.sql.psi.core.psi.SqlCompositeElement" | ||
] | ||
} |
52 changes: 52 additions & 0 deletions
52
...in/app/cash/sqldelight/dialects/sqlite/json/module/grammar/mixins/TableOrSubqueryMixin.kt
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package app.cash.sqldelight.dialects.sqlite.json.module.grammar.mixins | ||
|
||
import app.cash.sqldelight.dialect.api.ExposableType | ||
import app.cash.sqldelight.dialect.api.IntermediateType | ||
import app.cash.sqldelight.dialect.api.PrimitiveType | ||
import app.cash.sqldelight.dialects.sqlite.json.module.grammar.JsonParser | ||
import app.cash.sqldelight.dialects.sqlite.json.module.grammar.psi.SqliteJsonTableOrSubquery | ||
import com.alecstrong.sql.psi.core.ModifiableFileLazy | ||
import com.alecstrong.sql.psi.core.psi.QueryElement.QueryResult | ||
import com.alecstrong.sql.psi.core.psi.QueryElement.SynthesizedColumn | ||
import com.alecstrong.sql.psi.core.psi.SqlExpr | ||
import com.alecstrong.sql.psi.core.psi.SqlJoinClause | ||
import com.alecstrong.sql.psi.core.psi.SqlNamedElementImpl | ||
import com.alecstrong.sql.psi.core.psi.SqlTableName | ||
import com.alecstrong.sql.psi.core.psi.impl.SqlTableOrSubqueryImpl | ||
import com.intellij.lang.ASTNode | ||
import com.intellij.lang.PsiBuilder | ||
import com.intellij.psi.PsiElement | ||
|
||
internal abstract class TableOrSubqueryMixin(node: ASTNode?) : SqlTableOrSubqueryImpl(node), SqliteJsonTableOrSubquery { | ||
private val queryExposed = ModifiableFileLazy lazy@{ | ||
if (jsonFunctionName != null) { | ||
return@lazy listOf( | ||
QueryResult( | ||
table = jsonFunctionName!!, | ||
columns = emptyList(), | ||
synthesizedColumns = listOf( | ||
SynthesizedColumn(jsonFunctionName!!, acceptableValues = listOf("key", "value", "type", "atom", "id", "parent", "fullkey", "path", "json", "root")) | ||
) | ||
) | ||
) | ||
} | ||
super.queryExposed() | ||
} | ||
|
||
override fun queryExposed() = queryExposed.forFile(containingFile) | ||
|
||
override fun queryAvailable(child: PsiElement): Collection<QueryResult> { | ||
if (child is SqlExpr) { | ||
val parent = parent as SqlJoinClause | ||
return parent.tableOrSubqueryList.takeWhile { it != this }.flatMap { it.queryExposed() } | ||
} | ||
return super.queryAvailable(child) | ||
} | ||
} | ||
|
||
internal abstract class JsonFunctionNameMixin(node: ASTNode) : SqlNamedElementImpl(node), SqlTableName, ExposableType { | ||
override fun getId(): PsiElement? = null | ||
override fun getString(): PsiElement? = null | ||
override val parseRule: (PsiBuilder, Int) -> Boolean = JsonParser::json_function_name_real | ||
override fun type() = IntermediateType(PrimitiveType.TEXT) | ||
} |
27 changes: 27 additions & 0 deletions
27
dialects/sqlite/json-module/src/test/fixtures/json_table_functions/Test.s
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
CREATE TABLE user(name TEXT, phone TEXT); | ||
|
||
SELECT DISTINCT user.name | ||
FROM user, json_each(user.phone) | ||
WHERE json_each.value LIKE '704-%'; | ||
|
||
SELECT name FROM user WHERE phone LIKE '704-%' | ||
UNION | ||
SELECT user.name | ||
FROM user, json_each(user.phone) | ||
WHERE json_valid(user.phone) | ||
AND json_each.value LIKE '704-%'; | ||
|
||
CREATE TABLE big(json TEXT); | ||
|
||
SELECT big.rowid, fullkey, value | ||
FROM big, json_tree(big.json) | ||
WHERE json_tree.type NOT IN ('object','array'); | ||
|
||
SELECT big.rowid, fullkey, atom | ||
FROM big, json_tree(big.json) | ||
WHERE atom IS NOT NULL; | ||
|
||
SELECT DISTINCT json_extract(big.json,'$.id') | ||
FROM big, json_tree(big.json, '$.partlist') | ||
WHERE json_tree.key='uuid' | ||
AND json_tree.value='6fa5181e-5721-11e5-a04e-57f3d7b32808'; |
28 changes: 28 additions & 0 deletions
28
...-module/src/test/kotlin/app/cash/sqldelight/dialects/sqlite/json/module/JsonModuleTest.kt
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package app.cash.sqldelight.dialects.sqlite.json.module | ||
|
||
import app.cash.sqldelight.dialects.sqlite_3_18.SqliteDialect | ||
import com.alecstrong.sql.psi.test.fixtures.FixturesTest | ||
import org.junit.runner.RunWith | ||
import org.junit.runners.Parameterized | ||
import org.junit.runners.Parameterized.Parameters | ||
import java.io.File | ||
|
||
@RunWith(Parameterized::class) | ||
class JsonModuleTest(name: String, fixtureRoot: File) : FixturesTest(name, fixtureRoot) { | ||
override fun setupDialect() { | ||
SqliteDialect().setup() | ||
JsonModule().setup() | ||
} | ||
|
||
companion object { | ||
private val fixtures = arrayOf("src/test/fixtures") | ||
|
||
@Suppress("unused") // Used by Parameterized JUnit runner reflectively. | ||
@Parameters(name = "{0}") | ||
@JvmStatic fun parameters() = fixtures.flatMap { fixtureFolder -> | ||
File(fixtureFolder).listFiles()!! | ||
.filter { it.isDirectory } | ||
.map { arrayOf(it.name, it) } | ||
} + ansiFixtures | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
sqldelight-compiler/dialect/src/main/kotlin/app/cash/sqldelight/dialect/api/ExposableType.kt
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package app.cash.sqldelight.dialect.api | ||
|
||
import com.alecstrong.sql.psi.core.psi.SqlAnnotatedElement | ||
|
||
interface ExposableType : SqlAnnotatedElement { | ||
fun type(): IntermediateType | ||
} |
2 changes: 1 addition & 1 deletion
2
sqldelight-compiler/dialect/src/main/kotlin/app/cash/sqldelight/dialect/api/NoOp.kt
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
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
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
1 change: 1 addition & 0 deletions
1
sqldelight-compiler/src/test/kotlin/app/cash/sqldelight/core/BindArgsTest.kt
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
28 changes: 28 additions & 0 deletions
28
sqldelight-gradle-plugin/src/test/integration-sqlite-json/build.gradle
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
buildscript { | ||
apply from: "${projectDir.absolutePath}/../buildscript.gradle" | ||
} | ||
|
||
apply plugin: 'org.jetbrains.kotlin.jvm' | ||
apply plugin: 'app.cash.sqldelight' | ||
|
||
repositories { | ||
maven { | ||
url "file://${projectDir.absolutePath}/../../../../build/localMaven" | ||
} | ||
mavenCentral() | ||
} | ||
|
||
sqldelight { | ||
QueryWrapper { | ||
packageName = "app.cash.sqldelight.integration" | ||
dialect("app.cash.sqldelight:sqlite-3-18-dialect:${app.cash.sqldelight.VersionKt.VERSION}") | ||
module("app.cash.sqldelight:sqlite-json-module:${app.cash.sqldelight.VersionKt.VERSION}") | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation deps.sqliteJdbc | ||
implementation "app.cash.sqldelight:sqlite-driver:${app.cash.sqldelight.VersionKt.VERSION}" | ||
implementation deps.truth | ||
implementation("com.squareup.moshi:moshi:1.13.0") | ||
} |
1 change: 1 addition & 0 deletions
1
sqldelight-gradle-plugin/src/test/integration-sqlite-json/settings.gradle
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
apply from: "../settings.gradle" |
21 changes: 21 additions & 0 deletions
21
.../integration-sqlite-json/src/main/sqldelight/app/cash/sqldelight/integration/JsonTable.sq
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
CREATE TABLE user( | ||
name TEXT NOT NULL PRIMARY KEY, | ||
phone TEXT | ||
); | ||
|
||
insertUser: | ||
INSERT INTO user | ||
VALUES (?, ?); | ||
|
||
byAreaCode: | ||
SELECT DISTINCT user.name | ||
FROM user, json_each(user.phone) | ||
WHERE json_each.value LIKE :areaCode || '-%'; | ||
|
||
byAreaCode2: | ||
SELECT name FROM user WHERE phone LIKE :areaCode || '-%' | ||
UNION | ||
SELECT user.name | ||
FROM user, json_each(user.phone) | ||
WHERE json_valid(user.phone) | ||
AND json_each.value LIKE :areaCode || '-%'; |
Oops, something went wrong.