All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sxtanna.database.task.builder.CreateBuilder.kt Maven / Gradle / Ivy

There is a newer version: 3.0-beta
Show newest version
package com.sxtanna.database.task.builder

import com.sxtanna.database.ext.co
import com.sxtanna.database.struct.Resolver
import com.sxtanna.database.struct.base.Duo
import com.sxtanna.database.struct.obj.SqlType
import com.sxtanna.database.type.base.SqlObject
import kotlin.reflect.full.declaredMemberProperties

data class CreateBuilder(val table : String) {

	val columns = mutableListOf>()
	private var inited = false


	fun co(name : String, type : SqlType) = apply { columns.add(name co type) }

	@JvmSynthetic
	fun co(name : String, type : () -> SqlType) = apply { columns.add(name co type()) }


	fun cos(vararg columns : Duo) = apply { columns.forEach { co(it.name, it.value) } }


	@JvmSynthetic
	inline fun  init() = init(T::class.java)

	fun  init(clazz : Class) = apply {
		if (inited) return@apply

		columns.addAll(clazz.kotlin.declaredMemberProperties.map { Duo(it.name, Resolver[it]) })
		inited = true
	}


	companion object Create {

		fun table(table : String) = CreateBuilder(table)

		@JvmSynthetic
		inline fun  from(table : String = T::class.simpleName!!) = CreateBuilder(table).init()

		@JvmOverloads
		fun  from(clazz : Class, table : String = clazz.simpleName!!) = CreateBuilder(table).init(clazz)

	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy