Skip to content

Commit

Permalink
Merge branch 'master' into add-postgresql-alter-column-default
Browse files Browse the repository at this point in the history
  • Loading branch information
AlecKazakova authored Jan 29, 2024
2 parents 14a42c1 + 96f4329 commit 363e1e4
Show file tree
Hide file tree
Showing 29 changed files with 276 additions and 56 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/PR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
distribution: 'zulu'
java-version-file: .github/workflows/.ci-java-version
- name: Setup gradle
uses: gradle/gradle-build-action@v2
uses: gradle/gradle-build-action@v3
with:
gradle-home-cache-cleanup: true

Expand Down Expand Up @@ -95,15 +95,15 @@ jobs:
# Build the samples
- name: Build the mobile sample
if: matrix.os == 'macOS-latest' && matrix.job == 'test'
uses: gradle/gradle-build-action@v2
uses: gradle/gradle-build-action@v3
with:
arguments: build --stacktrace --parallel
build-root-directory: sample
gradle-home-cache-cleanup: true

- name: Build the web sample
if: matrix.os == 'macOS-latest' && matrix.job == 'test'
uses: gradle/gradle-build-action@v2
uses: gradle/gradle-build-action@v3
with:
arguments: kotlinUpgradeYarnLock build --stacktrace --parallel
build-root-directory: sample-web
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/Publish-Website.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
distribution: 'zulu'
java-version-file: .github/workflows/.ci-java-version
- name: Setup gradle
uses: gradle/gradle-build-action@v2
uses: gradle/gradle-build-action@v3
with:
gradle-home-cache-cleanup: true

Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/Release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
distribution: 'zulu'
java-version-file: .github/workflows/.ci-java-version
- name: Setup gradle
uses: gradle/gradle-build-action@v2
uses: gradle/gradle-build-action@v3
with:
gradle-home-cache-cleanup: true

Expand Down Expand Up @@ -82,7 +82,7 @@ jobs:
cd ~/.gradle
echo "org.gradle.java.installations.paths=${{ steps.setup-java.outputs.path }}" >> gradle.properties
- name: Setup gradle
uses: gradle/gradle-build-action@v2
uses: gradle/gradle-build-action@v3
with:
gradle-home-cache-cleanup: true

Expand Down Expand Up @@ -120,7 +120,7 @@ jobs:
cd ~/.gradle
echo "org.gradle.java.installations.paths=${{ steps.setup-java.outputs.path }}" >> gradle.properties
- name: Setup gradle
uses: gradle/gradle-build-action@v2
uses: gradle/gradle-build-action@v3
with:
gradle-home-cache-cleanup: true

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/requirements.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mike==1.1.2
mkdocs==1.5.3
mkdocs-macros-plugin==1.0.5
mkdocs-material==9.5.2
mkdocs-material==9.5.6
mkdocs-material-extensions==1.3.1
2 changes: 1 addition & 1 deletion .github/workflows/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ mkdocs==1.5.3
# mkdocs-material
mkdocs-macros-plugin==1.0.5
# via -r requirements.in
mkdocs-material==9.5.2
mkdocs-material==9.5.6
# via -r requirements.in
mkdocs-material-extensions==1.3.1
# via
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ class PostgreSqlTypeResolver(private val parentResolver: TypeResolver) : TypeRes
"json_typeof", "jsonb_typeof",
"json_agg", "jsonb_agg", "json_object_agg", "jsonb_object_agg",
-> IntermediateType(TEXT)
"string_agg" -> IntermediateType(TEXT)
"json_array_length", "jsonb_array_length" -> IntermediateType(INTEGER)
"jsonb_path_exists", "jsonb_path_match", "jsonb_path_exists_tz", "jsonb_path_match_tz" -> IntermediateType(BOOLEAN)
"currval", "lastval", "nextval", "setval" -> IntermediateType(BIG_INT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ default_constraint ::= [ NOT NULL | NULL ] DEFAULT (
current_timestamp_with_optional_interval |
{signed_number} |
{literal_value} |
{function_expr} |
LP <<expr '-1'>> RP
) {
extends = "com.alecstrong.sql.psi.core.psi.impl.SqlDefaultConstraintImpl"
Expand Down Expand Up @@ -172,7 +173,7 @@ create_index_stmt ::= CREATE [ UNIQUE ] INDEX [ 'CONCURRENTLY' ] [ IF NOT EXISTS
pin = 6
}

identity_clause ::= 'IDENTITY'
identity_clause ::= 'IDENTITY' [ LP [ 'SEQUENCE' 'NAME' sequence_name ] [ sequence_parameters* ] RP ]

generated_clause ::= GENERATED ( (ALWAYS AS LP <<expr '-1'>> RP 'STORED') | ( (ALWAYS | BY DEFAULT) AS identity_clause ) ) {
extends = "com.alecstrong.sql.psi.core.psi.impl.SqlGeneratedClauseImpl"
Expand Down Expand Up @@ -325,7 +326,7 @@ alter_table_alter_column ::= ALTER [COLUMN] {column_name}
( [ SET data_clause ] type_clause {column_type} [USING {column_name}'::'{column_type}]
| column_not_null_clause
| column_default_clause | DROP identity_clause [ IF EXISTS ] | ADD {generated_clause}
| SET GENERATED (ALWAYS | BY DEFAULT )
| ( SET GENERATED (ALWAYS | BY DEFAULT) | SET sequence_parameters | 'RESTART' [ WITH {signed_number} ] ) *
) {
mixin = "app.cash.sqldelight.dialects.postgresql.grammar.mixins.AlterTableAlterColumnMixin"
pin = 1
Expand Down Expand Up @@ -370,7 +371,7 @@ json_expression ::= {column_name} ( jsona_binary_operator | jsonb_binary_operato
jsona_binary_operator ::= '->' | '->>' | '#>'
jsonb_binary_operator ::= '@>' | '<@' | '?|' | '?&' | '?' | '#-'

extension_stmt ::= create_sequence_stmt | copy_stdin | truncate_stm {
extension_stmt ::= create_sequence_stmt | copy_stdin | truncate_stmt | set_stmt {
extends = "com.alecstrong.sql.psi.core.psi.impl.SqlExtensionStmtImpl"
implements = "com.alecstrong.sql.psi.core.psi.SqlExtensionStmt"
override = true
Expand All @@ -387,14 +388,17 @@ sequence_data_type ::= ( small_int_data_type
| big_int_data_type )

create_sequence_stmt ::= CREATE [ (TEMPORARY | TEMP) | 'UNLOGGED' ] 'SEQUENCE' [ IF NOT EXISTS ] sequence_name
[ AS sequence_data_type ]
[ 'INCREMENT' [ BY ] {signed_number} ]
[ 'MINVALUE' {signed_number} | NO 'MINVALUE' ] [ 'MAXVALUE' {signed_number} | NO 'MAXVALUE' ]
[ 'START' [ WITH ] {signed_number} ] [ 'CACHE' {signed_number} ] [ [ NO ] 'CYCLE' ]
[ 'OWNED' BY ( {table_name} DOT {column_name} ) | 'NONE' ] {
[ AS sequence_data_type ] (sequence_parameters) * {
mixin = "app.cash.sqldelight.dialects.postgresql.grammar.mixins.CreateSequenceMixin"
}

sequence_parameters ::= 'INCREMENT' [ BY ] {signed_number}
| 'MINVALUE' {signed_number} | NO 'MINVALUE' | 'MAXVALUE' {signed_number} | NO 'MAXVALUE'
| 'START' [ WITH ] {signed_number} | 'CACHE' {signed_number} | [ NO ] 'CYCLE'
| 'OWNED' BY ( {table_name} DOT {column_name} ) | 'NONE' {
pin=2
}

copy_option ::= copy_option_format | copy_option_freeze | copy_option_delimiter | copy_option_null | copy_option_header | copy_option_quote | copy_option_escape | copy_option_force_not_null | copy_option_force_null | copy_option_encoding
copy_option_format ::= 'FORMAT' ('TEXT' | 'CSV' | 'BINARY')
copy_option_freeze ::= 'FREEZE' [ (boolean_literal) ]
Expand All @@ -407,9 +411,22 @@ copy_option_force_not_null ::= 'FORCE_NOT_NULL' LP {column_name} ( COMMA {column
copy_option_force_null ::= 'FORCE_NULL' LP {column_name} ( COMMA {column_name}) * RP
copy_option_encoding ::= 'ENCODING' string_literal

truncate_stm ::= 'TRUNCATE' [ 'TABLE' ] ( truncate_only | truncate_descendant ) [ truncate_option * ]
truncate_stmt ::= 'TRUNCATE' [ 'TABLE' ] ( truncate_only | truncate_descendant ) [ truncate_option * ]
truncate_only ::= 'ONLY' {table_name} ( COMMA {table_name} ) *
truncate_descendant ::= {table_name} ['*'] ( COMMA {table_name} ['*'] ) *
truncate_option ::= truncate_option_identity | truncate_option_cascade
truncate_option_identity ::= ( 'RESTART' | 'CONTINUE' ) 'IDENTITY'
truncate_option_cascade ::= 'CASCADE' | 'RESTRICT'

set_stmt ::= 'SET' [ ('SESSION' | 'LOCAL') ] ( set_config | set_timezone | set_schema | set_names | set_seed )
set_value ::= literal_value | {identifier} | DEFAULT
set_config ::= {identifier} ( TO | EQ ) set_value
set_schema ::= 'SCHEMA' string_literal
set_names ::= 'NAMES' set_value
set_seed ::= 'SEED' TO ( set_value | [PLUS | MINUS]{numeric_literal} )
set_timezone ::= 'TIME' 'ZONE'
( [PLUS | MINUS]{numeric_literal}
| interval_expression ['HOUR' TO 'MINUTE']
| 'LOCAL'
| set_value
)
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
CREATE TABLE t2 (
c1 INTEGER GENERATED ALWAYS AS IDENTITY (
SEQUENCE NAME t2_seq
MAXVALUE 44444
MINVALUE 11
START 11
INCREMENT BY 10
CACHE 20
NO CYCLE
),
c2 INTEGER DEFAULT 0,
c3 VARCHAR(25),
c4 TEXT,
c5 NUMERIC(10, 2)
);


CREATE TABLE t1 (
c1 INTEGER,
c2 INTEGER DEFAULT 0,
Expand Down Expand Up @@ -26,3 +43,34 @@ ALTER TABLE t1 ALTER COLUMN c4 SET DEFAULT 'Test';
ALTER TABLE t1 ALTER COLUMN c1 SET GENERATED BY DEFAULT;
ALTER TABLE t1 ALTER COLUMN c1 SET GENERATED ALWAYS;

ALTER TABLE t1
ALTER COLUMN c1 ADD GENERATED ALWAYS AS IDENTITY (
SEQUENCE NAME t1_seq
INCREMENT BY 10
MINVALUE 100
MAXVALUE 9223372
START 1
CACHE 20
NO CYCLE
);

ALTER TABLE t1
ALTER COLUMN c1 ADD GENERATED BY DEFAULT AS IDENTITY (
START 101
CACHE 1
NO CYCLE
);

ALTER TABLE t1 ALTER COLUMN c1_id
SET GENERATED BY DEFAULT
SET INCREMENT 1
SET MINVALUE 1
SET MAXVALUE 11
SET START WITH 1
SET CACHE 111
SET NO CYCLE
RESTART WITH 1;

ALTER TABLE t1 ALTER COLUMN c1_id
SET GENERATED BY DEFAULT
RESTART WITH 1;
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ CREATE TABLE all_types(

some_interval_d TIMESTAMP NOT NULL DEFAULT NOW() - INTERVAL '5 days',

some_interval_e INTERVAL DEFAULT INTERVAL '3h' + INTERVAL '20m'
some_interval_e INTERVAL DEFAULT INTERVAL '3h' + INTERVAL '20m',

some_default_uuid UUID DEFAULT gen_random_uuid(),

some_default_sequence INTEGER DEFAULT nextval('some_seq')

);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
SET test = yes;
SET test = 'yes';
SET test = DEFAULT;
SET test TO yes;
SET test TO 'yes';
SET test TO DEFAULT;

SET SESSION test = yes;
SET LOCAL test = yes;

SET TIME ZONE 'PST8PDT';
SET TIME ZONE 'Europe/Paris';
SET TIME ZONE +1;
SET TIME ZONE -7;
SET TIME ZONE INTERVAL '-08:00' HOUR TO MINUTE;
SET TIME ZONE LOCAL;
SET TIME ZONE DEFAULT;

SET SCHEMA 'postgres';

SET NAMES 'utf8';
SET NAMES DEFAULT;

SET SEED TO 0.1;
SET SEED TO -0.5;
SET SEED TO DEFAULT;
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package app.cash.sqldelight.dialects.sqlite_3_25.grammar.mixins

import app.cash.sqldelight.dialects.sqlite_3_25.grammar.SqliteParser
import app.cash.sqldelight.dialects.sqlite_3_25.grammar.psi.SqliteAlterTableRenameColumn
import com.alecstrong.sql.psi.core.psi.SqlColumnAlias
import com.alecstrong.sql.psi.core.psi.SqlColumnName
import com.alecstrong.sql.psi.core.psi.impl.SqlColumnAliasImpl
import com.intellij.lang.ASTNode
import com.intellij.lang.PsiBuilder
import com.intellij.psi.util.PsiTreeUtil

internal abstract class AlterTableColumnAliasMixin(
node: ASTNode,
) : SqlColumnAliasImpl(node),
SqlColumnAlias {
override val parseRule: (PsiBuilder, Int) -> Boolean = SqliteParser::alter_table_column_alias_real

override fun source() = PsiTreeUtil.getChildOfType(parent as SqliteAlterTableRenameColumn, SqlColumnName::class.java)!!
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,44 @@ import app.cash.sqldelight.dialects.sqlite_3_25.grammar.psi.SqliteAlterTableRena
import com.alecstrong.sql.psi.core.SqlAnnotationHolder
import com.alecstrong.sql.psi.core.psi.AlterTableApplier
import com.alecstrong.sql.psi.core.psi.LazyQuery
import com.alecstrong.sql.psi.core.psi.NamedElement
import com.alecstrong.sql.psi.core.psi.QueryElement
import com.alecstrong.sql.psi.core.psi.SqlColumnAlias
import com.alecstrong.sql.psi.core.psi.SqlColumnConstraint
import com.alecstrong.sql.psi.core.psi.SqlColumnDef
import com.alecstrong.sql.psi.core.psi.SqlColumnName
import com.alecstrong.sql.psi.core.psi.SqlCompositeElementImpl
import com.alecstrong.sql.psi.core.psi.SqlColumnType
import com.alecstrong.sql.psi.core.psi.alterStmt
import com.alecstrong.sql.psi.core.psi.impl.SqlColumnDefImpl
import com.alecstrong.sql.psi.core.psi.mixins.ColumnDefMixin
import com.intellij.lang.ASTNode

internal abstract class AlterTableRenameColumnMixin(
abstract class AlterTableRenameColumnMixin(
node: ASTNode,
) : SqlCompositeElementImpl(node),
) : SqlColumnDefImpl(node),
SqliteAlterTableRenameColumn,
AlterTableApplier {
private val columnName
get() = children.filterIsInstance<SqlColumnName>().single()

override fun getColumnConstraintList(): MutableList<SqlColumnConstraint> {
return alterStmt.tablesAvailable(this).first { it.tableName.textMatches(alterStmt.tableName) }
.query.columns.first { it.element.textMatches(columnName) }.element.let {
(it.parent as SqlColumnDef).columnConstraintList
}
}

override fun getColumnName(): SqlColumnName {
return children.filterIsInstance<SqlColumnName>().first()
}

override fun getColumnType(): SqlColumnType {
val sqlColumnType = children.filterIsInstance<SqlColumnType>().firstOrNull()
if (sqlColumnType != null) return sqlColumnType

val columnName = columnName
val element = tablesAvailable(this).first { it.tableName.textMatches(alterStmt.tableName) }
.query.columns.first { it.element.textMatches(columnName) }.element
return (element.parent as ColumnDefMixin).columnType
}

private val columnAlias
get() = children.filterIsInstance<SqlColumnAlias>().single()
Expand All @@ -27,9 +51,9 @@ internal abstract class AlterTableRenameColumnMixin(
tableName = lazyQuery.tableName,
query = {
val columns = lazyQuery.query.columns
val column = QueryElement.QueryColumn(element = columnAlias)
val column: QueryElement.QueryColumn = QueryElement.QueryColumn(element = columnAlias)
val replace = columns.singleOrNull {
(it.element as SqlColumnName).textMatches(columnName)
(it.element as NamedElement).textMatches(columnName)
}
lazyQuery.query.copy(
columns = lazyQuery.query.columns.map { if (it == replace) column else it },
Expand All @@ -44,7 +68,7 @@ internal abstract class AlterTableRenameColumnMixin(
if (tablesAvailable(this)
.filter { it.tableName.textMatches(alterStmt.tableName) }
.flatMap { it.query.columns }
.none { (it.element as? SqlColumnName)?.textMatches(columnName) == true }
.none { (it.element as? NamedElement)?.textMatches(columnName) == true }
) {
annotationHolder.createErrorAnnotation(
element = columnName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,21 @@ select_stmt ::= SELECT [ DISTINCT | ALL ] {result_column} ( COMMA {result_column
pin = 1
}

alter_table_rename_column ::= RENAME [ COLUMN ] {column_name} TO {column_alias} {
alter_table_rename_column ::= RENAME [ COLUMN ] {column_name} TO alter_table_column_alias {
mixin = "app.cash.sqldelight.dialects.sqlite_3_25.grammar.mixins.AlterTableRenameColumnMixin"
pin = 1
}

alter_table_column_alias ::= id | string {
mixin = "app.cash.sqldelight.dialects.sqlite_3_25.grammar.mixins.AlterTableColumnAliasMixin"
implements = [
"com.alecstrong.sql.psi.core.psi.AliasElement";
"com.alecstrong.sql.psi.core.psi.NamedElement";
"com.alecstrong.sql.psi.core.psi.SqlCompositeElement"
"com.alecstrong.sql.psi.core.psi.SqlColumnName"
]
}

window_function_invocation ::=
window_func LP [ MULTIPLY | ( <<expr '-1'>> ( COMMA <<expr '-1'>> ) * ) ] RP [ 'FILTER' LP WHERE <<expr '-1'>> RP] 'OVER' ( window_defn | window_name ) {
pin = 6
Expand Down
Loading

0 comments on commit 363e1e4

Please sign in to comment.