Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pragma function capability to sqlite #4700

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package app.cash.sqldelight.dialects.sqlite_3_18.grammar.mixins

import com.alecstrong.sql.psi.core.SqlAnnotationHolder
import com.alecstrong.sql.psi.core.psi.SqlCompositeElement
import com.alecstrong.sql.psi.core.psi.SqlTypes
import com.alecstrong.sql.psi.core.psi.impl.SqlPragmaNameImpl
import com.intellij.lang.ASTNode
import com.intellij.psi.tree.TokenSet

internal abstract class PragmaFunctionValidatorMixin(node: ASTNode) : SqlPragmaNameImpl(node) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because this is being used in the from clause i expect it will also need to return correct types

you can look at the json mixin for inspiration for doing something similar, since it also adds table valued functions for the sqlite json extension:

https://github.com/cashapp/sqldelight/blob/master/dialects/sqlite/json-module/src/main/kotlin/app/cash/sqldelight/dialects/sqlite/json/module/grammar/json.bnf#L23-L30

private fun SqlCompositeElement.annotateReservedKeywords(annotationHolder: SqlAnnotationHolder) {
children.filterIsInstance<SqlCompositeElement>().forEach {
it.annotateReservedKeywords(annotationHolder)
}
node.getChildren(TokenSet.create(SqlTypes.ID)).forEach {
if (it.text !in acceptedKeys) {
annotationHolder.createErrorAnnotation(this, "not a valid pragma function")
}
}
}
override fun annotate(annotationHolder: SqlAnnotationHolder) {
annotateReservedKeywords(annotationHolder)
super.annotate(annotationHolder)
}

companion object {
// This list is providing the names for all pragma functions that we should support.
// For more information please look at the list of pragma https://www.sqlite.org/pragma.html#syntax
private val acceptedKeys = setOf(
"pragma_application_id",
"pragma_cache_size",
"pragma_cache_spill",
"pragma_compile_options",
"pragma_count_changes",
"pragma_cell_size_check",
"pragma_freelist_count",
"pragma_max_page_count",
"pragma_page_size",
"pragma_page_count",
"pragma_user_version",
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,22 @@
"static com.alecstrong.sql.psi.core.psi.SqlTypes.DIGIT"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.DOT"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.TABLE"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.LP"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.RP"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.SELECT"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.DISTINCT"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.ALL"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.COMMA"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.FROM"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.WHERE"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.HAVING"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.VALUES"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.FOR"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.SEPARATOR"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.MULTIPLY"
]
}
overrides ::= type_name | bind_parameter | alter_table_stmt
overrides ::= type_name | bind_parameter | alter_table_stmt | select_stmt

type_name ::= text_data_type | blob_data_type | int_data_type | real_data_type {
extends = "com.alecstrong.sql.psi.core.psi.impl.SqlTypeNameImpl"
Expand All @@ -40,3 +53,14 @@ alter_table_stmt ::= ALTER TABLE [ {database_name} DOT ] {table_name} {alter_tab
pin = 1
override = true
}

select_stmt ::= SELECT [ DISTINCT | ALL ] {result_column} ( COMMA {result_column} ) * [ FROM ([ {database_name} DOT ] pragma_function_expr | {join_clause})] [ WHERE <<expr '-1'>> ] [{group_by}] [ HAVING <<expr '-1'>> ] | VALUES {values_expression} ( COMMA {values_expression} ) * {
extends = "com.alecstrong.sql.psi.core.psi.impl.SqlSelectStmtImpl"
implements = "com.alecstrong.sql.psi.core.psi.SqlSelectStmt"
override = true
pin = 1
}

pragma_function_expr ::= {pragma_name} LP [ [ DISTINCT ] <<expr'-1'>> ( ( COMMA | FROM | FOR | SEPARATOR ) <<expr'-1'>> ) * | MULTIPLY ] RP {
mixin = "app.cash.sqldelight.dialects.sqlite_3_18.grammar.mixins.PragmaFunctionValidatorMixin"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT * FROM pragma_freelist_count();
SELECT * FROM pragma_freelist_counter(); -- failure
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test.s line 2:14 - not a valid pragma function
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,17 @@ result_column ::= ( MULTIPLY
implements = "com.alecstrong.sql.psi.core.psi.SqlResultColumn"
override = true
}
select_stmt ::= SELECT [ DISTINCT | ALL ] {result_column} ( COMMA {result_column} ) * [ FROM {join_clause} ] [ WHERE <<expr '-1'>> ] [{group_by}] [ HAVING <<expr '-1'>> ] [ WINDOW window_name AS window_defn ( COMMA window_name AS window_defn ) * ] | VALUES {values_expression} ( COMMA {values_expression} ) * {
select_stmt ::= SELECT [ DISTINCT | ALL ] {result_column} ( COMMA {result_column} ) * [ FROM ([ {database_name} DOT ] pragma_function_expr | {join_clause}) ] [ WHERE <<expr '-1'>> ] [{group_by}] [ HAVING <<expr '-1'>> ] [ WINDOW window_name AS window_defn ( COMMA window_name AS window_defn ) * ] | VALUES {values_expression} ( COMMA {values_expression} ) * {
extends = "com.alecstrong.sql.psi.core.psi.impl.SqlSelectStmtImpl"
implements = "com.alecstrong.sql.psi.core.psi.SqlSelectStmt"
override = true
pin = 1
}

pragma_function_expr ::= {pragma_name} LP [ [ DISTINCT ] <<expr'-1'>> ( ( COMMA | FROM | FOR | SEPARATOR ) <<expr'-1'>> ) * | MULTIPLY ] RP {
mixin = "app.cash.sqldelight.dialects.sqlite_3_18.grammar.mixins.PragmaNamed"
}

alter_table_rename_column ::= RENAME [ COLUMN ] {column_name} TO {column_alias} {
mixin = "app.cash.sqldelight.dialects.sqlite_3_25.grammar.mixins.AlterTableRenameColumnMixin"
pin = 1
Expand Down Expand Up @@ -101,4 +105,4 @@ frame_spec ::= ( 'RANGE' | 'ROWS' | 'GROUPS' )

window_func ::= id
window_name ::= id
base_window_name ::= id
base_window_name ::= id