Skip to content

Commit 876eafb

Browse files
authored
Support extra BLOB types (BYTEA, BYTES) (#26)
Supporting [this](https://www.cockroachlabs.com/docs/stable/bytes.html)
1 parent 9cb716b commit 876eafb

File tree

6 files changed

+49
-1
lines changed

6 files changed

+49
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ This doesn't fully cover the whole SQL syntax of CockroachDB. It's a work in pro
1212
* Supports STRING data type
1313
* Supports storing index
1414
* Supports unnamed index
15-
* Index tracking and validation has been disabled until there's a way to track unnamed indexes
15+
* Index tracking and validation has been disabled until there's a way to track unnamed indexes
16+
* Blob types (BYTEA, BLOB, BYTES)

cockroachdb-dialect/src/main/kotlin/com/faire/sqldelight/dialects/cockroachdb/grammar/CockroachDB.bnf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474

7575
overrides ::= table_constraint
7676
| string_data_type
77+
| blob_data_type
7778
| generated_clause
7879
| alter_table_rules
7980

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

97+
blob_data_type ::= 'BYTEA' | 'BLOB' | 'BYTES' {
98+
extends = "app.cash.sqldelight.dialects.postgresql.grammar.psi.impl.PostgreSqlBlobDataTypeImpl"
99+
implements = "app.cash.sqldelight.dialects.postgresql.grammar.psi.PostgreSqlBlobDataType"
100+
override = true
101+
}
102+
96103
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'>> ] {
97104
extends = "com.faire.sqldelight.dialects.cockroachdb.grammar.mixins.CreateIndexMixin"
98105
implements = "com.alecstrong.sql.psi.core.psi.SqlCreateIndexStmt"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CREATE TABLE foo(
2+
id INT NOT NULL,
3+
bar1 BYTEA NOT NULL,
4+
bar2 BLOB NOT NULL,
5+
bar3 BYTES NOT NULL,
6+
PRIMARY KEY (id)
7+
);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CREATE TABLE blob_data_types(
2+
id INT NOT NULL,
3+
bytea_col BYTEA NOT NULL,
4+
blob_col BLOB NOT NULL,
5+
bytes_col BYTES NOT NULL,
6+
PRIMARY KEY (id)
7+
);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
create:
2+
INSERT INTO blob_data_types VALUES ?;
3+
4+
selectAll:
5+
SELECT *
6+
FROM blob_data_types;

integration-testing/src/test/kotlin/com/faire/sqldelight/dialects/cockroachdb/IntegrationTest.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.faire.sqldelight.dialects.cockroachdb
22

3+
import Blob_data_types
34
import Computed_column
45
import String_type
56
import app.cash.sqldelight.driver.jdbc.JdbcDriver
@@ -44,6 +45,25 @@ class IntegrationTest {
4445
).containsExactlyInAnyOrder("hello", "world")
4546
}
4647

48+
@Test
49+
fun `persist BLOB types`() {
50+
val charset = Charsets.UTF_8
51+
database.blobTypesQueries.create(
52+
Blob_data_types(
53+
id = 1,
54+
bytea_col = "foo".toByteArray(charset),
55+
blob_col = "bar".toByteArray(charset),
56+
bytes_col = "baz".toByteArray(charset),
57+
),
58+
)
59+
with(database.blobTypesQueries.selectAll().executeAsOne()) {
60+
assertThat(id).isEqualTo(1)
61+
assertThat(bytea_col.toString(charset)).isEqualTo("foo")
62+
assertThat(blob_col.toString(charset)).isEqualTo("bar")
63+
assertThat(bytes_col.toString(charset)).isEqualTo("baz")
64+
}
65+
}
66+
4767
companion object {
4868
private lateinit var database: CockroachDBIntegrationTesting
4969

0 commit comments

Comments
 (0)