Skip to content

Commit 550388f

Browse files
authored
Fix negative identifier in cursor (#86)
* Fix negative identifier in cursor * Fix Int escaping Co-authored-by: hfhbd <[email protected]>
1 parent 63107aa commit 550388f

File tree

5 files changed

+48
-17
lines changed

5 files changed

+48
-17
lines changed

postgres-native-sqldelight-driver/src/commonMain/kotlin/app/softwork/sqldelight/postgresdriver/PostgresNativeDriver.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,16 @@ public class PostgresNativeDriver(private var conn: CPointer<PGconn>) : SqlDrive
104104
return result.value != null
105105
}
106106

107+
private fun Int.escapeNegative(): String = if(this < 0) "_${toString().substring(1)}" else toString()
108+
107109
override fun <R> executeQuery(
108110
identifier: Int?,
109111
sql: String,
110112
mapper: (SqlCursor) -> R,
111113
parameters: Int,
112114
binders: (SqlPreparedStatement.() -> Unit)?
113115
): QueryResult.Value<R> {
114-
val cursorName = if (identifier == null) "myCursor" else "cursor$identifier"
116+
val cursorName = if (identifier == null) "myCursor" else "cursor${identifier.escapeNegative()}"
115117
val cursor = "DECLARE $cursorName CURSOR FOR"
116118
val preparedStatement = if (parameters != 0) {
117119
PostgresPreparedStatement(parameters).apply {

postgres-native-sqldelight-driver/src/commonTest/kotlin/app/softwork/sqldelight/postgresdriver/PostgresNativeDriverTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class PostgresNativeDriverTest {
131131
)
132132
assertEquals(0, driver.execute(null, "DROP TABLE IF EXISTS copying;", parameters = 0).value)
133133
assertEquals(0, driver.execute(null, "CREATE TABLE copying(a int primary key);", parameters = 0).value)
134-
driver.execute(42, "COPY copying FROM STDIN (FORMAT CSV);", 0)
134+
driver.execute(-42, "COPY copying FROM STDIN (FORMAT CSV);", 0)
135135
val results = driver.copy("1\n2\n")
136136
assertEquals(2, results)
137137
}

testing/src/commonMain/sqldelight/app/softwork/sqldelight/postgresdriver/1.sqm

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
DROP TABLE IF EXISTS foo;
2+
DROP TABLE IF EXISTS users;
23

34
CREATE TABLE foo(
45
a INT PRIMARY KEY,
@@ -10,3 +11,11 @@ instant TIMESTAMPTZ NOT NULL,
1011
interval INTERVAL NOT NULL,
1112
uuid UUID NOT NULL
1213
);
14+
15+
CREATE TABLE IF NOT EXISTS users(
16+
id BIGSERIAL PRIMARY KEY,
17+
email VARCHAR(200) NOT NULL UNIQUE,
18+
username VARCHAR(100) NOT NULL UNIQUE,
19+
bio VARCHAR(1000) NOT NULL DEFAULT '',
20+
image VARCHAR(255) NOT NULL DEFAULT ''
21+
);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
insert:
2+
INSERT INTO users(email, username, bio, image)
3+
VALUES (?, ?, ?, ?);
4+
5+
selectByUsername:
6+
SELECT email, username, bio, image
7+
FROM users
8+
WHERE username = :username;

testing/src/commonTest/kotlin/app/softwork/sqldelight/postgresdriver/PostgresNativeDriverTest.kt

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ import kotlin.test.*
66
import kotlin.time.Duration.Companion.seconds
77

88
class PostgresNativeDriverTest {
9+
private val driver = PostgresNativeDriver(
10+
host = "localhost",
11+
port = 5432,
12+
user = "postgres",
13+
database = "postgres",
14+
password = "password"
15+
)
16+
917
@Test
1018
fun allTypes() {
11-
val driver = PostgresNativeDriver(
12-
host = "localhost",
13-
port = 5432,
14-
user = "postgres",
15-
database = "postgres",
16-
password = "password"
17-
)
1819
val queries = NativePostgres(driver).fooQueries
1920
NativePostgres.Schema.migrate(driver, 0, NativePostgres.Schema.version)
2021
assertEquals(emptyList(), queries.get().executeAsList())
@@ -44,17 +45,11 @@ class PostgresNativeDriverTest {
4445

4546
@Test
4647
fun copyTest() {
47-
val driver = PostgresNativeDriver(
48-
host = "localhost",
49-
port = 5432,
50-
user = "postgres",
51-
database = "postgres",
52-
password = "password"
53-
)
5448
val queries = NativePostgres(driver).fooQueries
5549
NativePostgres.Schema.migrate(driver, 0, NativePostgres.Schema.version)
5650
queries.startCopy()
57-
val result = driver.copy("42,answer,2020-12-12,12:42:00.0000,2014-08-01T12:01:02.0000,1970-01-01T00:00:00.010Z,PT42S,00000000-0000-0000-0000-000000000000")
51+
val result =
52+
driver.copy("42,answer,2020-12-12,12:42:00.0000,2014-08-01T12:01:02.0000,1970-01-01T00:00:00.010Z,PT42S,00000000-0000-0000-0000-000000000000")
5853
assertEquals(1, result)
5954
val foo = Foo(
6055
a = 42,
@@ -68,4 +63,21 @@ class PostgresNativeDriverTest {
6863
)
6964
assertEquals(foo, queries.get().executeAsOne())
7065
}
66+
67+
@Test
68+
fun userTest() {
69+
val queries = NativePostgres(driver).usersQueries
70+
NativePostgres.Schema.migrate(driver, 0, NativePostgres.Schema.version)
71+
queries.insert("test@test", "test", "bio", "")
72+
val testUser = queries.selectByUsername("test").executeAsOne()
73+
assertEquals(
74+
SelectByUsername(
75+
"test@test",
76+
"test",
77+
"bio",
78+
""
79+
),
80+
testUser
81+
)
82+
}
7183
}

0 commit comments

Comments
 (0)