com.zeoflow.depot.vo.DatabaseView.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of depot-compiler Show documentation
Show all versions of depot-compiler Show documentation
The Depot persistence library provides an abstraction layer over SQLite to allow for more robust database access while using the full power of SQLite.
The newest version!
/*
* Copyright (C) 2021 ZeoFlow SRL
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.zeoflow.depot.vo
import com.zeoflow.depot.compiler.processing.XType
import com.zeoflow.depot.migration.bundle.BundleUtil
import com.zeoflow.depot.parser.ParsedQuery
import com.zeoflow.depot.compiler.processing.XTypeElement
class DatabaseView(
element: XTypeElement,
val viewName: String,
val query: ParsedQuery,
type: XType,
fields: List,
embeddedFields: List,
constructor: Constructor?
) : Pojo(element, type, fields, embeddedFields, emptyList(), constructor),
HasSchemaIdentity,
EntityOrView {
override val tableName = viewName
val createViewQuery by lazy {
createViewQuery(viewName)
}
/**
* List of all the underlying tables including those that are indirectly referenced.
*
* This is populated by DatabaseProcessor. This cannot be an immutable constructor parameter
* as it can only be known after all the other views are initialized and parsed.
*/
val tables = mutableSetOf()
fun toBundle() = com.zeoflow.depot.migration.bundle.DatabaseViewBundle(
viewName,
createViewQuery(BundleUtil.VIEW_NAME_PLACEHOLDER)
)
override fun getIdKey(): String {
val identityKey = SchemaIdentityKey()
identityKey.append(query.original)
return identityKey.hash()
}
private fun createViewQuery(viewName: String): String {
// This query should match exactly like it is stored in sqlite_master. The query is
// trimmed. "IF NOT EXISTS" should not be included.
return "CREATE VIEW `$viewName` AS ${query.original.trim()}"
}
}