Skip to content

Commit

Permalink
Support extra BLOB types (BYTEA, BYTES) (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
Adriel-M authored Jul 11, 2023
1 parent 9cb716b commit 876eafb
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ This doesn't fully cover the whole SQL syntax of CockroachDB. It's a work in pro
* Supports STRING data type
* Supports storing index
* Supports unnamed index
* Index tracking and validation has been disabled until there's a way to track unnamed indexes
* Index tracking and validation has been disabled until there's a way to track unnamed indexes
* Blob types (BYTEA, BLOB, BYTES)
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@

overrides ::= table_constraint
| string_data_type
| blob_data_type
| generated_clause
| alter_table_rules

Expand All @@ -93,6 +94,12 @@ string_data_type ::= ((( 'CHARACTER' 'VARYING' ) | 'VARCHAR' | 'CHARACTER' | 'CH
override = true
}

blob_data_type ::= 'BYTEA' | 'BLOB' | 'BYTES' {
extends = "app.cash.sqldelight.dialects.postgresql.grammar.psi.impl.PostgreSqlBlobDataTypeImpl"
implements = "app.cash.sqldelight.dialects.postgresql.grammar.psi.PostgreSqlBlobDataType"
override = true
}

create_index_stmt ::= CREATE [ UNIQUE ] INDEX [ IF NOT EXISTS ] [ [ ansi_database_name DOT ] ansi_index_name ] ON ansi_table_name LP ansi_indexed_column ( COMMA ansi_indexed_column ) * RP ['STORING' LP ansi_indexed_column ( COMMA ansi_indexed_column ) * RP ] [ WHERE <<expr '-1'>> ] {
extends = "com.faire.sqldelight.dialects.cockroachdb.grammar.mixins.CreateIndexMixin"
implements = "com.alecstrong.sql.psi.core.psi.SqlCreateIndexStmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE foo(
id INT NOT NULL,
bar1 BYTEA NOT NULL,
bar2 BLOB NOT NULL,
bar3 BYTES NOT NULL,
PRIMARY KEY (id)
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE blob_data_types(
id INT NOT NULL,
bytea_col BYTEA NOT NULL,
blob_col BLOB NOT NULL,
bytes_col BYTES NOT NULL,
PRIMARY KEY (id)
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
create:
INSERT INTO blob_data_types VALUES ?;

selectAll:
SELECT *
FROM blob_data_types;
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.faire.sqldelight.dialects.cockroachdb

import Blob_data_types
import Computed_column
import String_type
import app.cash.sqldelight.driver.jdbc.JdbcDriver
Expand Down Expand Up @@ -44,6 +45,25 @@ class IntegrationTest {
).containsExactlyInAnyOrder("hello", "world")
}

@Test
fun `persist BLOB types`() {
val charset = Charsets.UTF_8
database.blobTypesQueries.create(
Blob_data_types(
id = 1,
bytea_col = "foo".toByteArray(charset),
blob_col = "bar".toByteArray(charset),
bytes_col = "baz".toByteArray(charset),
),
)
with(database.blobTypesQueries.selectAll().executeAsOne()) {
assertThat(id).isEqualTo(1)
assertThat(bytea_col.toString(charset)).isEqualTo("foo")
assertThat(blob_col.toString(charset)).isEqualTo("bar")
assertThat(bytes_col.toString(charset)).isEqualTo("baz")
}
}

companion object {
private lateinit var database: CockroachDBIntegrationTesting

Expand Down

0 comments on commit 876eafb

Please sign in to comment.