Skip to content

Commit 92aad60

Browse files
authored
Explicit Api (#42)
Binary api and explicite code style Co-authored-by: hfhbd <[email protected]>
1 parent d7d051f commit 92aad60

File tree

6 files changed

+101
-63
lines changed

6 files changed

+101
-63
lines changed

build.gradle.kts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,31 @@ plugins {
55
`maven-publish`
66
signing
77
id("io.github.gradle-nexus.publish-plugin") version "1.1.0"
8+
id("org.jetbrains.kotlinx.binary-compatibility-validator") version "0.10.1"
89
}
910

1011
repositories {
1112
mavenCentral()
1213
}
1314

14-
allprojects {
15+
subprojects {
1516
if (this.name == "testing") {
16-
return@allprojects
17+
return@subprojects
1718
}
1819
plugins.apply("org.gradle.maven-publish")
1920
plugins.apply("org.gradle.signing")
2021

22+
afterEvaluate {
23+
configure<org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension> {
24+
explicitApi()
25+
sourceSets {
26+
all {
27+
languageSettings.progressiveMode = true
28+
}
29+
}
30+
}
31+
}
32+
2133
val emptyJar by tasks.creating(Jar::class) { }
2234

2335
group = "app.softwork"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public final class app/softwork/sqldelight/postgresdialect/PostgresNativeDialect : app/cash/sqldelight/dialect/api/SqlDelightDialect {
2+
public fun <init> ()V
3+
public fun getAllowsReferenceCycles ()Z
4+
public fun getAsyncRuntimeTypes ()Lapp/cash/sqldelight/dialect/api/RuntimeTypes;
5+
public fun getConnectionManager ()Lapp/cash/sqldelight/dialect/api/ConnectionManager;
6+
public fun getIcon ()Ljavax/swing/Icon;
7+
public fun getMigrationStrategy ()Lapp/cash/sqldelight/dialect/api/SqlGeneratorStrategy;
8+
public fun getRuntimeTypes ()Lapp/cash/sqldelight/dialect/api/RuntimeTypes;
9+
public fun isSqlite ()Z
10+
public fun migrationSquasher (Lapp/cash/sqldelight/dialect/api/MigrationSquasher;)Lapp/cash/sqldelight/dialect/api/MigrationSquasher;
11+
public fun setup ()V
12+
public fun typeResolver (Lapp/cash/sqldelight/dialect/api/TypeResolver;)Lapp/cash/sqldelight/dialect/api/TypeResolver;
13+
}
14+

postgres-native-sqldelight-dialect/build.gradle.kts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ dependencies {
3131
testImplementation(kotlin("test-junit"))
3232
}
3333

34+
kotlin {
35+
target.compilations.all {
36+
kotlinOptions.allWarningsAsErrors = true
37+
}
38+
}
39+
3440
configurations.all {
3541
exclude(group = "com.jetbrains.rd")
3642
exclude(group = "com.github.jetbrains", module = "jetCheck")

postgres-native-sqldelight-dialect/src/main/kotlin/app/softwork/sqldelight/postgresdialect/PostgresNativeDialect.kt

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,53 @@ import app.cash.sqldelight.dialects.postgresql.grammar.psi.*
66
import com.alecstrong.sql.psi.core.psi.*
77
import com.squareup.kotlinpoet.*
88

9-
class PostgresNativeDialect : SqlDelightDialect by PostgreSqlDialect() {
10-
override val runtimeTypes = RuntimeTypes(
9+
public class PostgresNativeDialect : SqlDelightDialect by PostgreSqlDialect() {
10+
override val runtimeTypes: RuntimeTypes = RuntimeTypes(
1111
driverType = ClassName("app.softwork.sqldelight.postgresdriver", "PostgresNativeDriver"),
1212
cursorType = ClassName("app.softwork.sqldelight.postgresdriver", "PostgresCursor"),
1313
preparedStatementType = ClassName("app.softwork.sqldelight.postgresdriver", "PostgresPreparedStatement")
1414
)
1515

1616
override fun typeResolver(parentResolver: TypeResolver): TypeResolver = PostgresNativeTypeResolver(parentResolver)
17+
}
1718

18-
class PostgresNativeTypeResolver(parentResolver: TypeResolver) : TypeResolver by PostgreSqlTypeResolver(parentResolver) {
19-
override fun definitionType(typeName: SqlTypeName): IntermediateType = with(typeName) {
20-
check(this is PostgreSqlTypeName)
21-
val type = IntermediateType(
22-
when {
23-
smallIntDataType != null -> PostgreSqlType.SMALL_INT
24-
intDataType != null -> PostgreSqlType.INTEGER
25-
bigIntDataType != null -> PostgreSqlType.BIG_INT
26-
approximateNumericDataType != null -> PrimitiveType.REAL
27-
stringDataType != null -> PrimitiveType.TEXT
28-
uuidDataType != null -> PostgreSqlType.UUID
29-
smallSerialDataType != null -> PostgreSqlType.SMALL_INT
30-
serialDataType != null -> PostgreSqlType.INTEGER
31-
bigSerialDataType != null -> PostgreSqlType.BIG_INT
32-
dateDataType != null -> {
33-
when (dateDataType!!.firstChild.text) {
34-
"DATE" -> PostgreSqlType.DATE
35-
"TIME" -> PostgreSqlType.TIME
36-
"TIMESTAMP" -> if (dateDataType!!.node.getChildren(null)
37-
.any { it.text == "WITH" }
38-
) PostgreSqlType.TIMESTAMP_TIMEZONE else PostgreSqlType.TIMESTAMP
39-
"TIMESTAMPTZ" -> PostgreSqlType.TIMESTAMP_TIMEZONE
40-
"INTERVAL" -> PostgreSqlType.INTERVAL
41-
else -> throw IllegalArgumentException("Unknown date type ${dateDataType!!.text}")
42-
}
19+
private class PostgresNativeTypeResolver(parentResolver: TypeResolver) : TypeResolver by PostgreSqlTypeResolver(parentResolver) {
20+
override fun definitionType(typeName: SqlTypeName): IntermediateType = with(typeName) {
21+
check(this is PostgreSqlTypeName)
22+
val type = IntermediateType(
23+
when {
24+
smallIntDataType != null -> PostgreSqlType.SMALL_INT
25+
intDataType != null -> PostgreSqlType.INTEGER
26+
bigIntDataType != null -> PostgreSqlType.BIG_INT
27+
approximateNumericDataType != null -> PrimitiveType.REAL
28+
stringDataType != null -> PrimitiveType.TEXT
29+
uuidDataType != null -> PostgreSqlType.UUID
30+
smallSerialDataType != null -> PostgreSqlType.SMALL_INT
31+
serialDataType != null -> PostgreSqlType.INTEGER
32+
bigSerialDataType != null -> PostgreSqlType.BIG_INT
33+
dateDataType != null -> {
34+
when (dateDataType!!.firstChild.text) {
35+
"DATE" -> PostgreSqlType.DATE
36+
"TIME" -> PostgreSqlType.TIME
37+
"TIMESTAMP" -> if (dateDataType!!.node.getChildren(null)
38+
.any { it.text == "WITH" }
39+
) PostgreSqlType.TIMESTAMP_TIMEZONE else PostgreSqlType.TIMESTAMP
40+
"TIMESTAMPTZ" -> PostgreSqlType.TIMESTAMP_TIMEZONE
41+
"INTERVAL" -> PostgreSqlType.INTERVAL
42+
else -> throw IllegalArgumentException("Unknown date type ${dateDataType!!.text}")
4343
}
44-
jsonDataType != null -> PrimitiveType.TEXT
45-
booleanDataType != null -> PrimitiveType.BOOLEAN
46-
blobDataType != null -> PrimitiveType.BLOB
47-
else -> throw IllegalArgumentException("Unknown kotlin type for sql type ${this.text}")
4844
}
49-
)
50-
return type
51-
}
45+
jsonDataType != null -> PrimitiveType.TEXT
46+
booleanDataType != null -> PrimitiveType.BOOLEAN
47+
blobDataType != null -> PrimitiveType.BLOB
48+
else -> throw IllegalArgumentException("Unknown kotlin type for sql type ${this.text}")
49+
}
50+
)
51+
return type
5252
}
5353
}
5454

55-
internal enum class PostgreSqlType(override val javaType: TypeName): DialectType {
55+
private enum class PostgreSqlType(override val javaType: TypeName): DialectType {
5656
SMALL_INT(SHORT) {
5757
override fun decode(value: CodeBlock) = CodeBlock.of("%L.toShort()", value)
5858

postgres-native-sqldelight-driver/build.gradle.kts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ kotlin {
2424
linuxX64 { config() }
2525
// mingwX64 { config() }
2626

27+
targets.all {
28+
compilations.all {
29+
kotlinOptions.allWarningsAsErrors = true
30+
}
31+
}
32+
2733
sourceSets {
2834
commonMain {
2935
dependencies {

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

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import kotlinx.uuid.*
88
import libpq.*
99
import kotlin.time.*
1010

11-
class PostgresNativeDriver(private var conn: CPointer<PGconn>) : SqlDriver {
11+
public class PostgresNativeDriver(private var conn: CPointer<PGconn>) : SqlDriver {
1212
private var transaction: Transacter.Transaction? = null
1313

1414
init {
@@ -35,7 +35,7 @@ class PostgresNativeDriver(private var conn: CPointer<PGconn>) : SqlDriver {
3535

3636
}
3737

38-
override fun currentTransaction() = transaction
38+
override fun currentTransaction(): Transacter.Transaction? = transaction
3939

4040
override fun execute(
4141
identifier: Int?,
@@ -200,7 +200,7 @@ class PostgresNativeDriver(private var conn: CPointer<PGconn>) : SqlDriver {
200200
}
201201
}
202202

203-
fun copy(stdin: String): Long {
203+
public fun copy(stdin: String): Long {
204204
val status = PQputCopyData(conn, stdin, stdin.encodeToByteArray().size)
205205
check(status == 1) {
206206
conn.error()
@@ -241,7 +241,7 @@ private fun CPointer<PGresult>?.check(conn: CPointer<PGconn>): CPointer<PGresult
241241
/**
242242
* Must be inside a transaction!
243243
*/
244-
class PostgresCursor(
244+
public class PostgresCursor(
245245
private var result: CPointer<PGresult>,
246246
private val name: String,
247247
private val conn: CPointer<PGconn>
@@ -252,7 +252,7 @@ class PostgresCursor(
252252
conn.exec("END")
253253
}
254254

255-
override fun getBoolean(index: Int) = getString(index)?.toBoolean()
255+
override fun getBoolean(index: Int): Boolean? = getString(index)?.toBoolean()
256256

257257
override fun getBytes(index: Int): ByteArray? {
258258
val isNull = PQgetisnull(result, tup_num = 0, field_num = index) == 1
@@ -285,9 +285,9 @@ class PostgresCursor(
285285
return array
286286
}
287287

288-
override fun getDouble(index: Int) = getString(index)?.toDouble()
288+
override fun getDouble(index: Int): Double? = getString(index)?.toDouble()
289289

290-
override fun getLong(index: Int) = getString(index)?.toLong()
290+
override fun getLong(index: Int): Long? = getString(index)?.toLong()
291291

292292
override fun getString(index: Int): String? {
293293
val isNull = PQgetisnull(result, tup_num = 0, field_num = index) == 1
@@ -299,24 +299,24 @@ class PostgresCursor(
299299
}
300300
}
301301

302-
fun getDate(index: Int): LocalDate? = getString(index)?.toLocalDate()
303-
fun getTime(index: Int): LocalTime? = getString(index)?.toLocalTime()
304-
fun getLocalTimestamp(index: Int): LocalDateTime? = getString(index)?.replace(" ", "T")?.toLocalDateTime()
305-
fun getTimestamp(index: Int): Instant? = getString(index)?.let {
302+
public fun getDate(index: Int): LocalDate? = getString(index)?.toLocalDate()
303+
public fun getTime(index: Int): LocalTime? = getString(index)?.toLocalTime()
304+
public fun getLocalTimestamp(index: Int): LocalDateTime? = getString(index)?.replace(" ", "T")?.toLocalDateTime()
305+
public fun getTimestamp(index: Int): Instant? = getString(index)?.let {
306306
Instant.parse(it.replace(" ", "T"))
307307
}
308308

309-
fun getInterval(index: Int): Duration? = getString(index)?.let { Duration.parseIsoString(it) }
310-
fun getUUID(index: Int): UUID? = getString(index)?.toUUID()
309+
public fun getInterval(index: Int): Duration? = getString(index)?.let { Duration.parseIsoString(it) }
310+
public fun getUUID(index: Int): UUID? = getString(index)?.toUUID()
311311

312312
override fun next(): Boolean {
313313
result = PQexec(conn, "FETCH NEXT IN $name").check(conn)
314314
return PQcmdTuples(result)!!.toKString().toInt() == 1
315315
}
316316
}
317317

318-
class PostgresPreparedStatement(private val parameters: Int) : SqlPreparedStatement {
319-
fun values(scope: AutofreeScope): CValuesRef<CPointerVar<ByteVar>> = createValues(parameters) {
318+
public class PostgresPreparedStatement(private val parameters: Int) : SqlPreparedStatement {
319+
internal fun values(scope: AutofreeScope): CValuesRef<CPointerVar<ByteVar>> = createValues(parameters) {
320320
value = when (val value = _values[it]) {
321321
null -> null
322322
is Data.Bytes -> value.bytes.refTo(0).getPointer(scope)
@@ -330,9 +330,9 @@ class PostgresPreparedStatement(private val parameters: Int) : SqlPreparedStatem
330330
}
331331

332332
private val _values = arrayOfNulls<Data>(parameters)
333-
val lengths = IntArray(parameters)
334-
val formats = IntArray(parameters)
335-
val types = UIntArray(parameters)
333+
internal val lengths = IntArray(parameters)
334+
internal val formats = IntArray(parameters)
335+
internal val types = UIntArray(parameters)
336336

337337
private fun bind(index: Int, value: String?, oid: UInt) {
338338
lengths[index] = if (value != null) {
@@ -368,32 +368,32 @@ class PostgresPreparedStatement(private val parameters: Int) : SqlPreparedStatem
368368
bind(index, string, textOid)
369369
}
370370

371-
fun bindDate(index: Int, value: LocalDate?) {
371+
public fun bindDate(index: Int, value: LocalDate?) {
372372
bind(index, value?.toString(), dateOid)
373373
}
374374

375375

376-
fun bindTime(index: Int, value: LocalTime?) {
376+
public fun bindTime(index: Int, value: LocalTime?) {
377377
bind(index, value?.toString(), timeOid)
378378
}
379379

380-
fun bindLocalTimestamp(index: Int, value: LocalDateTime?) {
380+
public fun bindLocalTimestamp(index: Int, value: LocalDateTime?) {
381381
bind(index, value?.toString(), timestampOid)
382382
}
383383

384-
fun bindTimestamp(index: Int, value: Instant?) {
384+
public fun bindTimestamp(index: Int, value: Instant?) {
385385
bind(index, value?.toString(), timestampTzOid)
386386
}
387387

388-
fun bindInterval(index: Int, value: Duration?) {
388+
public fun bindInterval(index: Int, value: Duration?) {
389389
bind(index, value?.toIsoString(), intervalOid)
390390
}
391391

392-
fun bindUUID(index: Int, value: UUID?) {
392+
public fun bindUUID(index: Int, value: UUID?) {
393393
bind(index, value?.toString(), uuidOid)
394394
}
395395

396-
companion object {
396+
private companion object {
397397
// Hardcoded, because not provided in libpq-fe.h for unknown reasons...
398398
// select * from pg_type;
399399
private const val boolOid = 16u
@@ -411,7 +411,7 @@ class PostgresPreparedStatement(private val parameters: Int) : SqlPreparedStatem
411411
}
412412
}
413413

414-
fun PostgresNativeDriver(
414+
public fun PostgresNativeDriver(
415415
host: String, database: String, user: String, password: String, port: Int = 5432, options: String? = null
416416
): PostgresNativeDriver {
417417
val conn = PQsetdbLogin(

0 commit comments

Comments
 (0)