Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add postgreSql interval column result #4152

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

Merged
Merged
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
Expand Up @@ -21,7 +21,9 @@ import app.cash.sqldelight.dialects.postgresql.grammar.psi.PostgreSqlTypeName
import app.cash.sqldelight.dialects.postgresql.grammar.psi.PostgreSqlUpdateStmtLimited
import com.alecstrong.sql.psi.core.psi.SqlColumnExpr
import com.alecstrong.sql.psi.core.psi.SqlCreateTableStmt
import com.alecstrong.sql.psi.core.psi.SqlExpr
import com.alecstrong.sql.psi.core.psi.SqlFunctionExpr
import com.alecstrong.sql.psi.core.psi.SqlLiteralExpr
import com.alecstrong.sql.psi.core.psi.SqlStmt
import com.alecstrong.sql.psi.core.psi.SqlTypeName
import com.squareup.kotlinpoet.CodeBlock
Expand Down Expand Up @@ -125,4 +127,20 @@ class PostgreSqlTypeResolver(private val parentResolver: TypeResolver) : TypeRes

return parentResolver.simplifyType(intermediateType)
}

override fun resolvedType(expr: SqlExpr): IntermediateType {
return expr.postgreSqlType()
}

private fun SqlExpr.postgreSqlType(): IntermediateType = when (this) {
is SqlLiteralExpr -> when {
literalValue.text == "CURRENT_DATE" -> IntermediateType(PostgreSqlType.DATE)
literalValue.text == "CURRENT_TIME" -> IntermediateType(PostgreSqlType.TIME)
literalValue.text == "CURRENT_TIMESTAMP" -> IntermediateType(PostgreSqlType.TIMESTAMP)
literalValue.text.startsWith("INTERVAL") -> IntermediateType(PostgreSqlType.INTERVAL)
else -> parentResolver.resolvedType(this)
}

else -> parentResolver.resolvedType(this)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"static com.alecstrong.sql.psi.core.psi.SqlTypes.COMMA"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.CONFLICT"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.CONSTRAINT"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.CURRENT_DATE"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.CURRENT_TIME"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.CURRENT_TIMESTAMP"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.DEFAULT"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.DELETE"
Expand Down Expand Up @@ -198,6 +200,19 @@ string_literal ::= string {
override = true
}

literal_value ::= ( {numeric_literal}
| string_literal
| {blob_literal}
| NULL
| CURRENT_TIME
| CURRENT_DATE
| CURRENT_TIMESTAMP
| interval_expression ) {
mixin = "app.cash.sqldelight.dialects.postgresql.grammar.mixins.LiteralValueMixin"
implements = "com.alecstrong.sql.psi.core.psi.SqlLiteralValue"
override = true
}

insert_stmt ::= [ {with_clause} ]
INSERT INTO [ {database_name} DOT ] {table_name} [ AS {table_alias} ]
[ LP {column_name} ( COMMA {column_name} ) * RP ] {insert_stmt_values}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package app.cash.sqldelight.dialects.postgresql.grammar.mixins

import com.alecstrong.sql.psi.core.psi.impl.SqlLiteralValueImpl
import com.intellij.lang.ASTNode

internal abstract class LiteralValueMixin(
node: ASTNode,
) : SqlLiteralValueImpl(node)
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ SELECT date_trunc('hour', timestamp), date_trunc('hour', timestamp_with_timezone

selectNow:
SELECT NOW();

selectInterval:
SELECT INTERVAL '1 day';
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,12 @@ class PostgreSqlTest {
assertThat(now).isGreaterThan(OffsetDateTime.MIN)
}

@Test fun interval() {
val interval = database.datesQueries.selectInterval().executeAsOne()
assertThat(interval).isNotNull()
assertThat(interval.getDays()).isEqualTo(1)
}

@Test fun successfulOptimisticLock() {
with(database.withLockQueries) {
val row = insertText("sup").executeAsOne()
Expand Down