-
Notifications
You must be signed in to change notification settings - Fork 496
/
SqlDelightDatabase.kt
312 lines (276 loc) · 12.3 KB
/
SqlDelightDatabase.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package app.cash.sqldelight.gradle
import app.cash.sqldelight.VERSION
import app.cash.sqldelight.core.lang.MigrationFileType
import app.cash.sqldelight.core.lang.SqlDelightFileType
import app.cash.sqldelight.gradle.kotlin.Source
import app.cash.sqldelight.gradle.kotlin.sources
import app.cash.sqldelight.gradle.squash.MigrationSquashTask
import groovy.lang.GroovyObject
import org.gradle.api.GradleException
import org.gradle.api.Project
import org.gradle.api.internal.catalog.DelegatingProjectDependency
import org.gradle.api.provider.Provider
import java.io.File
class SqlDelightDatabase(
val project: Project,
var name: String,
var packageName: String? = null,
var schemaOutputDirectory: File? = null,
var sourceFolders: Collection<String>? = null,
var deriveSchemaFromMigrations: Boolean = false,
var verifyMigrations: Boolean = false,
var migrationOutputDirectory: File? = null,
var migrationOutputFileFormat: String = ".sql",
var generateAsync: Boolean = false,
) {
internal val configuration = project.configurations.create("${name}DialectClasspath").apply {
isTransitive = false
}
internal val moduleConfiguration = project.configurations.create("${name}ModuleClasspath").apply {
isTransitive = false
}
internal var addedDialect: Boolean = false
fun module(module: Any) {
moduleConfiguration.dependencies.add(project.dependencies.create(module))
}
fun dialect(dialect: Any) {
if (addedDialect) throw IllegalStateException("Can only set a single dialect.")
configuration.dependencies.add(project.dependencies.create(dialect))
addedDialect = true
}
/**
* When SqlDelight finds an equality operation with a nullable typed rvalue such as:
*
* ```
* SELECT *
* FROM test_table
* WHERE foo = ?
* ```
*
* It will generate:
*
* ```
* private val foo: String?
*
* |SELECT *
* |FROM test_table
* |WHERE foo ${ if (foo == null) "IS" else "=" } ?1
* ```
*
* The = operator is expected to return `false` if comparing to a value that is `null`.
* However, the above code will return true when `foo` is `null`.
*
* By enabling [treatNullAsUnknownForEquality], the `null`
* check will not be generated, resulting in correct SQL behavior:
*
* ```
* private val foo: String?
*
* |SELECT *
* |FROM test_table
* |WHERE foo = ?1
* ```
*
* @see <a href="https://github.com/cashapp/sqldelight/issues/1490">sqldelight#1490</a>
* @see <a href="https://en.wikipedia.org/wiki/Null_%28SQL%29#Null-specific_and_3VL-specific_comparison_predicates">Wikipedia entry on null specific comparisons in SQL</a>
*/
var treatNullAsUnknownForEquality: Boolean = false
private val generatedSourcesDirectory
get() = File(project.buildDir, "generated/sqldelight/code/$name")
private val sources by lazy { sources(project) }
private val dependencies = mutableListOf<SqlDelightDatabase>()
private var recursionGuard = false
fun methodMissing(name: String, args: Any): Any {
return (project as GroovyObject).invokeMethod(name, args)
}
@Suppress("unused") // Public API used in gradle files.
fun dependency(delegatedProject: DelegatingProjectDependency) = dependency(delegatedProject.dependencyProject)
@Suppress("unused") // Public API used in gradle files.
fun dependency(dependencyProject: Project) {
project.evaluationDependsOn(dependencyProject.path)
val dependency = dependencyProject.extensions.findByType(SqlDelightExtension::class.java)
?: throw IllegalStateException("Cannot depend on a module with no sqldelight plugin.")
val database = dependency.databases.singleOrNull { it.name == name }
?: throw IllegalStateException("No database named $name in $dependencyProject")
check(database.packageName != packageName) { "Detected a schema that already has the package name $packageName in project $dependencyProject" }
dependencies.add(database)
}
internal fun getProperties(): SqlDelightDatabasePropertiesImpl {
val packageName = requireNotNull(packageName) { "property packageName for $name database must be provided" }
check(!recursionGuard) { "Found a circular dependency in $project with database $name" }
recursionGuard = true
if (!addedDialect) throw GradleException(
"""
A dialect is needed for SQLDelight. For example for sqlite:
sqldelight {
$name {
dialect("app.cash.sqldelight:sqlite-3-18-dialect:$VERSION")
}
}
""".trimIndent(),
)
try {
return SqlDelightDatabasePropertiesImpl(
packageName = packageName,
compilationUnits = sources.map { source ->
SqlDelightCompilationUnitImpl(
name = source.name,
sourceFolders = sourceFolders(source).sortedBy { it.folder.absolutePath },
outputDirectoryFile = source.outputDir,
)
},
rootDirectory = project.projectDir,
className = name,
dependencies = dependencies.map { SqlDelightDatabaseNameImpl(it.packageName!!, it.name) },
deriveSchemaFromMigrations = deriveSchemaFromMigrations,
treatNullAsUnknownForEquality = treatNullAsUnknownForEquality,
generateAsync = generateAsync,
)
} finally {
recursionGuard = false
}
}
private fun sourceFolders(source: Source): List<SqlDelightSourceFolderImpl> {
val sourceFolders = sourceFolders ?: listOf("sqldelight")
val relativeSourceFolders = sourceFolders.flatMap { folder ->
source.sourceSets.map {
SqlDelightSourceFolderImpl(
folder = File(project.projectDir, "src/$it/$folder"),
dependency = false,
)
}
}
return relativeSourceFolders + dependencies.flatMap { dependency ->
val dependencySource = source.closestMatch(dependency.sources)
?: return@flatMap emptyList<SqlDelightSourceFolderImpl>()
val compilationUnit = dependency.getProperties().compilationUnits
.single { it.name == dependencySource.name }
return@flatMap compilationUnit.sourceFolders.map {
SqlDelightSourceFolderImpl(
folder = File(project.projectDir, project.relativePath(it.folder.absolutePath)),
dependency = true,
)
}
}.distinct()
}
internal fun registerTasks() {
sources.forEach { source ->
val allFiles = sourceFolders(source)
val sourceFiles = project.files(*allFiles.filter { !it.dependency }.map { it.folder }.toTypedArray())
val dependencyFiles = project.files(*allFiles.filter { it.dependency }.map { it.folder }.toTypedArray())
// Register the sqldelight generating task.
val task = project.tasks.register("generate${source.name.capitalize()}${name}Interface", SqlDelightTask::class.java) {
it.projectName.set(project.name)
it.properties = getProperties()
it.compilationUnit = getProperties().compilationUnits.single { it.name == source.name }
it.outputDirectory = source.outputDir
it.source(sourceFiles + dependencyFiles)
it.include("**${File.separatorChar}*.${SqlDelightFileType.defaultExtension}")
it.include("**${File.separatorChar}*.${MigrationFileType.defaultExtension}")
it.group = SqlDelightPlugin.GROUP
it.description = "Generate ${source.name} Kotlin interface for $name"
it.verifyMigrations = verifyMigrations
it.classpath.setFrom(configuration.fileCollection { true })
it.classpath.from(moduleConfiguration.fileCollection { true })
}
val outputDirectoryProvider: Provider<File> = task.map { it.outputDirectory!! }
// Add the source dependency on the generated code.
// Use a Provider generated from the task to carry task dependencies
// See https://github.com/cashapp/sqldelight/issues/2119
source.sourceDirectorySet.srcDir(outputDirectoryProvider)
// And register the output directory to the IDE if needed
source.registerGeneratedDirectory?.invoke(outputDirectoryProvider)
project.tasks.named("generateSqlDelightInterface").configure {
it.dependsOn(task)
}
if (!deriveSchemaFromMigrations) {
addMigrationTasks(sourceFiles.files + dependencyFiles.files, source)
}
if (deriveSchemaFromMigrations) {
addSquashTask(sourceFiles.files + dependencyFiles.files, source)
}
if (migrationOutputDirectory != null) {
addMigrationOutputTasks(sourceFiles.files + dependencyFiles.files, source)
}
}
}
private fun addMigrationTasks(
sourceSet: Collection<File>,
source: Source,
) {
val verifyMigrationTask =
project.tasks.register("verify${source.name.capitalize()}${name}Migration", VerifyMigrationTask::class.java) {
it.projectName.set(project.name)
it.compilationUnit = getProperties().compilationUnits.single { it.name == source.name }
it.source(sourceSet)
it.include("**${File.separatorChar}*.${SqlDelightFileType.defaultExtension}")
it.include("**${File.separatorChar}*.${MigrationFileType.defaultExtension}")
it.workingDirectory = File(project.buildDir, "sqldelight/migration_verification/${source.name.capitalize()}$name")
it.group = SqlDelightPlugin.GROUP
it.description = "Verify ${source.name} $name migrations and CREATE statements match."
it.properties = getProperties()
it.verifyMigrations = verifyMigrations
it.classpath.setFrom(configuration.fileCollection { true })
it.classpath.from(moduleConfiguration.fileCollection { true })
}
if (schemaOutputDirectory != null) {
project.tasks.register("generate${source.name.capitalize()}${name}Schema", GenerateSchemaTask::class.java) {
it.projectName.set(project.name)
it.compilationUnit = getProperties().compilationUnits.single { it.name == source.name }
it.outputDirectory = schemaOutputDirectory
it.source(sourceSet)
it.include("**${File.separatorChar}*.${SqlDelightFileType.defaultExtension}")
it.include("**${File.separatorChar}*.${MigrationFileType.defaultExtension}")
it.group = SqlDelightPlugin.GROUP
it.description = "Generate a .db file containing the current $name schema for ${source.name}."
it.properties = getProperties()
it.verifyMigrations = verifyMigrations
it.classpath.setFrom(configuration.fileCollection { true })
it.classpath.from(moduleConfiguration.fileCollection { true })
}
}
project.tasks.named("check").configure {
it.dependsOn(verifyMigrationTask)
}
project.tasks.named("verifySqlDelightMigration").configure {
it.dependsOn(verifyMigrationTask)
}
}
private fun addMigrationOutputTasks(
sourceSet: Collection<File>,
source: Source,
) {
project.tasks.register("generate${source.name.capitalize()}${name}Migrations", GenerateMigrationOutputTask::class.java) {
it.projectName.set(project.name)
it.compilationUnit = getProperties().compilationUnits.single { it.name == source.name }
it.source(sourceSet)
it.include("**${File.separatorChar}*.${MigrationFileType.defaultExtension}")
it.migrationOutputExtension = migrationOutputFileFormat
it.outputDirectory = migrationOutputDirectory
it.group = SqlDelightPlugin.GROUP
it.description = "Generate valid sql migration files for ${source.name} $name."
it.properties = getProperties()
it.classpath.setFrom(configuration.fileCollection { true })
it.classpath.from(moduleConfiguration.fileCollection { true })
}
}
private fun addSquashTask(
sourceSet: Collection<File>,
source: Source,
) {
project.tasks.register("squash${source.name.capitalize()}${name}Migrations", MigrationSquashTask::class.java) {
it.projectName.set(project.name)
it.compilationUnit = getProperties().compilationUnits.single { it.name == source.name }
it.source(sourceSet)
it.include("**${File.separatorChar}*.${MigrationFileType.defaultExtension}")
it.group = SqlDelightPlugin.GROUP
it.description = "Generate valid sql migration files for ${source.name} $name."
it.properties = getProperties()
it.classpath.setFrom(configuration.fileCollection { true })
it.classpath.from(moduleConfiguration.fileCollection { true })
}
}
private val Source.outputDir get() =
if (sources.size > 1) File(generatedSourcesDirectory, name)
else generatedSourcesDirectory
}