Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Break up string literals which are longer than the allowed length #1258

Merged
merged 1 commit into from Mar 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,31 @@
package com.squareup.sqldelight.core.compiler

import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.MemberName
import com.squareup.kotlinpoet.buildCodeBlock

/**
* The maximum string length that we can emit as a literal. This value is 2^15 instead of the real
* value, 2^16, to account for the indentation that KotlinPoet will add by using `trimMargin`.
*/
private const val MAX_STRING_LENGTH = 32_768

private val BUILD_STRING = MemberName("kotlin.text", "buildString")

/**
* Returns a [CodeBlock] which contains a representation of [this] as a literal. If longer than
* the maximum allowed length for a string, it will be broken up into multiple literals with code
* to re-assemble at runtime.
*/
internal fun String.toCodeLiteral(): CodeBlock {
if (length < MAX_STRING_LENGTH) {
return CodeBlock.of("%S", this)
}
return buildCodeBlock {
add("%M(%L) {\n", BUILD_STRING, length)
for (i in 0 until length step MAX_STRING_LENGTH) {
add("append(%S)\n", substring(i, (i + MAX_STRING_LENGTH).coerceAtMost(length)))
}
add("}")
}
}
Expand Up @@ -174,7 +174,7 @@ internal class QueryWrapperGenerator(

sourceFolders.flatMap { it.findChildrenOfType<SqlDelightFile>() }
.forInitializationStatements { sqlText ->
createFunction.addStatement("$DRIVER_NAME.execute(null, %S, 0)", sqlText)
createFunction.addStatement("$DRIVER_NAME.execute(null, %L, 0)", sqlText.toCodeLiteral())
}

var maxVersion = 1
Expand Down
Expand Up @@ -365,4 +365,98 @@ class QueryWrapperTest {
|
""".trimMargin())
}

@Test fun `string longer than 2^16 is chunked`() {
val sqString = buildString {
append("""
|CREATE TABLE class_ability_test (
| id TEXT PRIMARY KEY NOT NULL,
| class_id TEXT NOT NULL,
| name TEXT NOT NULL,
| level_id INTEGER NOT NULL DEFAULT 1,
| special TEXT,
| url TEXT NOT NULL
|);
|
|INSERT INTO class_ability_test(id, class_id, name, level_id, special, url)
|VALUES
""".trimMargin())
repeat(500) {
if (it > 0) append(',')
append("\n ('class_01_ability_$it', 'class_01', 'aaaaaaaaaaaaaaa', 1, NULL, 'https://stuff.example.com/this/is/a/bunch/of/path/data/class_01_ability_$it.png')")
}
append(';')
}
val result = FixtureCompiler.compileSql(sqString, tempFolder)

assertThat(result.errors).isEmpty()

val queryWrapperFile = result.compilerOutput[File(result.outputDirectory, "com/example/testmodule/TestDatabaseImpl.kt")]
assertThat(queryWrapperFile.toString()).apply {
startsWith("""
|package com.example.testmodule
|
|import com.example.TestDatabase
|import com.example.TestQueries
|import com.squareup.sqldelight.TransacterImpl
|import com.squareup.sqldelight.db.SqlDriver
|import kotlin.Int
|import kotlin.reflect.KClass
|import kotlin.text.buildString
|
|internal val KClass<TestDatabase>.schema: SqlDriver.Schema
| get() = TestDatabaseImpl.Schema
|
|internal fun KClass<TestDatabase>.newInstance(driver: SqlDriver): TestDatabase =
| TestDatabaseImpl(driver)
|
|private class TestDatabaseImpl(driver: SqlDriver) : TransacterImpl(driver), TestDatabase {
| override val testQueries: TestQueriesImpl = TestQueriesImpl(this, driver)
|
| object Schema : SqlDriver.Schema {
| override val version: Int
| get() = 1
|
| override fun create(driver: SqlDriver) {
| driver.execute(null, ""${'"'}
| |CREATE TABLE class_ability_test (
| | id TEXT PRIMARY KEY NOT NULL,
| | class_id TEXT NOT NULL,
| | name TEXT NOT NULL,
| | level_id INTEGER NOT NULL DEFAULT 1,
| | special TEXT,
| | url TEXT NOT NULL
| |)
| ""${'"'}.trimMargin(), 0)
| driver.execute(null, buildString(75360) {
| append(""${'"'}
| |INSERT INTO class_ability_test(id, class_id, name, level_id, special, url)
| |VALUES
| | ('class_01_ability_0', 'class_01', 'aaaaaaaaaaaaaaa', 1, NULL, 'https://stuff.example.com/this/is/a/bunch/of/path/data/class_01_ability_0.png')
""".trimMargin())
contains("""
| ""${'"'}.trimMargin())
| append(""${'"'}
""".trimMargin())
endsWith("""
| | ('class_01_ability_499', 'class_01', 'aaaaaaaaaaaaaaa', 1, NULL, 'https://stuff.example.com/this/is/a/bunch/of/path/data/class_01_ability_499.png')
| ""${'"'}.trimMargin())
| }, 0)
| }
|
| override fun migrate(
| driver: SqlDriver,
| oldVersion: Int,
| newVersion: Int
| ) {
| }
| }
|}
|
|private class TestQueriesImpl(private val database: TestDatabaseImpl, private val driver: SqlDriver)
| : TransacterImpl(driver), TestQueries
|
""".trimMargin())
}
}
}