Skip to content

Commit c3281a3

Browse files
authored
Use a sequence in copy in (#136)
* Fix test results Fix test results Use DateTimePeriod for Interval * Update copy Co-authored-by: hfhbd <[email protected]>
1 parent 59e55e3 commit c3281a3

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,15 @@ public class PostgresNativeDriver(
276276

277277
// Custom functions
278278

279-
public fun copy(stdin: String): Long {
280-
val status = PQputCopyData(conn, stdin, stdin.encodeToByteArray().size)
281-
check(status == 1) {
282-
conn.error()
279+
/**
280+
* Each element of stdin can be up to 2 GB.
281+
*/
282+
public fun copy(stdin: Sequence<String>): Long {
283+
for (stdin in stdin) {
284+
val status = PQputCopyData(conn, stdin, stdin.encodeToByteArray().size)
285+
check(status == 1) {
286+
conn.error()
287+
}
283288
}
284289
val end = PQputCopyEnd(conn, null)
285290
check(end == 1) {

testing-sqldelight/src/commonTest/kotlin/app/softwork/sqldelight/postgresdriver/PostgresNativeDriverTest.kt renamed to testing-sqldelight/src/commonTest/kotlin/app/softwork/sqldelight/postgresdriver/PostgresNativeSqldelightDriverTest.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import kotlin.test.*
1010
import kotlin.time.Duration.Companion.seconds
1111

1212
@ExperimentalCoroutinesApi
13-
class PostgresNativeDriverTest {
13+
class PostgresNativeSqldelightDriverTest {
1414
private val driver = PostgresNativeDriver(
1515
host = "localhost",
1616
port = 5432,
@@ -53,8 +53,11 @@ class PostgresNativeDriverTest {
5353
val queries = NativePostgres(driver).fooQueries
5454
NativePostgres.Schema.migrate(driver, 0, NativePostgres.Schema.version)
5555
queries.startCopy()
56-
val result =
57-
driver.copy("42,answer,2020-12-12,12:42:00.0000,2014-08-01T12:01:02.0000,1970-01-01T00:00:00.010Z,P45Y6M42DT42H42M42.424242S,00000000-0000-0000-0000-000000000000")
56+
val result = driver.copy(
57+
sequenceOf(
58+
"42,answer,2020-12-12,12:42:00.0000,2014-08-01T12:01:02.0000,1970-01-01T00:00:00.010Z,P45Y6M42DT42H42M42.424242S,00000000-0000-0000-0000-000000000000"
59+
)
60+
)
5861
assertEquals(1, result)
5962
val foo = Foo(
6063
a = 42,
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import kotlin.test.*
88
import kotlin.time.Duration.Companion.seconds
99

1010
@ExperimentalCoroutinesApi
11-
class PostgresNativeSqlDelightDriverTest {
11+
class PostgresNativeDriverTest {
1212
@Test
1313
fun simpleTest() = runTest {
1414
val driver = PostgresNativeDriver(
@@ -227,8 +227,18 @@ class PostgresNativeSqlDelightDriverTest {
227227
assertEquals(0, driver.execute(null, "DROP TABLE IF EXISTS copying;", parameters = 0).value)
228228
assertEquals(0, driver.execute(null, "CREATE TABLE copying(a int primary key);", parameters = 0).value)
229229
driver.execute(-42, "COPY copying FROM STDIN (FORMAT CSV);", 0)
230-
val results = driver.copy("1\n2\n")
231-
assertEquals(2, results)
230+
val results = driver.copy(sequenceOf("1\n2\n", "3\n4\n"))
231+
assertEquals(4, results)
232+
assertEquals(
233+
listOf(1, 2, 3, 4),
234+
driver.executeQuery(null, "SELECT * FROM copying", parameters = 0, binders = null, mapper = {
235+
buildList {
236+
while (it.next()) {
237+
add(it.getLong(0)!!.toInt())
238+
}
239+
}
240+
}).value
241+
)
232242
}
233243

234244
@Test

0 commit comments

Comments
 (0)