/*
 * (C) Copyright 2017 Vladimir Grechka
 *
 * YOU DON'T MESS AROUND WITH THIS SHIT, IT WAS GENERATED BY A TOOL SMARTER THAN YOU
 */

//
// Generated on Fri May 19 13:31:49 EEST 2017
// Model: e:/fegh/alraune/alraune-back/src/alraune-entities.kt
//

package alraune.back

import kotlin.reflect.KClass
import vgrechka.*
import vgrechka.spew.*
import kotlin.properties.Delegates.notNull
import kotlin.reflect.KMutableProperty1

// ------------------------------------------------------------------
// AlUser
// ------------------------------------------------------------------

// Generated at e-68337cd-2d2d-4aa0-bfcc-e6423096599d
fun newAlUser(
    firstName: String,
    email: String,
    lastName: String,
    passwordHash: String,
    sessionID: String,
    profilePhone: String,
    adminNotes: String,
    aboutMe: String,
    profileRejectionReason: String?,
    banReason: String?,
    subscribedToAllCategories: Boolean
): AlUser {
    return Generated_AlUser().also {
        it.firstName = firstName
        it.email = email
        it.lastName = lastName
        it.passwordHash = passwordHash
        it.sessionID = sessionID
        it.profilePhone = profilePhone
        it.adminNotes = adminNotes
        it.aboutMe = aboutMe
        it.profileRejectionReason = profileRejectionReason
        it.banReason = banReason
        it.subscribedToAllCategories = subscribedToAllCategories
    }
}

val alUserRepo: AlUserRepository by lazy {
    object : AlUserRepository {
        override fun select(prop: KMutableProperty1<AlUser, String>, op: DBPile.Operator, arg: Any?): List<AlUser> {
            println("findBy(prop = ${prop.name}; op = ${op.toString()}; arg = $arg)")
            val params = mutableListOf<Any?>()
            val sql = buildString {
                ln("select")
                ln("    alUser_id,")
                ln("    unix_timestamp(alUser_createdAt),")
                ln("    unix_timestamp(alUser_updatedAt),")
                ln("    alUser_deleted,")
                ln("    alUser_firstName,")
                ln("    alUser_email,")
                ln("    alUser_lastName,")
                ln("    alUser_passwordHash,")
                ln("    alUser_sessionID,")
                ln("    alUser_profilePhone,")
                ln("    alUser_adminNotes,")
                ln("    alUser_aboutMe,")
                ln("    alUser_profileRejectionReason,")
                ln("    alUser_banReason,")
                ln("    alUser_subscribedToAllCategories")
                ln("from `alraune_users`")
                ln("where")
                ln("${propertyToColumnName(prop)} ${op.sql} ?")
                params.add(arg)
            }
            val rows = DBPile.query(sql, params, uuid = "7a25556d-0083-49a0-bd41-bdd4b42764ca")
            println("findBy: Found ${rows.size} rows")
            val items = mutableListOf<AlUser>()
            for (row in rows) {
//                run {
//                    val value = row[13]
//                    println("--- type = ${PHPPile.getType(value)}; value = $value")
//                }
                items += Generated_AlUser().also {
                    it.id = row[0] as String
                    it.createdAt = DBPile.mysqlValueToPHPTimestamp(row[1])
                    it.updatedAt = DBPile.mysqlValueToPHPTimestamp(row[2])
                    it.deleted = DBPile.mysqlValueToBoolean(row[3])
                    it.firstName = row[4] as String
                    it.email = row[5] as String
                    it.lastName = row[6] as String
                    it.passwordHash = row[7] as String
                    it.sessionID = row[8] as String
                    it.profilePhone = row[9] as String
                    it.adminNotes = row[10] as String
                    it.aboutMe = row[11] as String
                    it.profileRejectionReason = row[12] as String?
                    it.banReason = row[13] as String?
                    it.subscribedToAllCategories = DBPile.mysqlValueToBoolean(row[14])
                }
            }
            return items
        }
        override fun insert(x: AlUser): AlUser {
            DBPile.execute(
                sql = buildString {
                    ln("insert into `alraune_users`(")
                    ln("    `alUser_createdAt`,")
                    ln("    `alUser_updatedAt`,")
                    ln("    `alUser_deleted`,")
                    ln("    `alUser_firstName`,")
                    ln("    `alUser_email`,")
                    ln("    `alUser_lastName`,")
                    ln("    `alUser_passwordHash`,")
                    ln("    `alUser_sessionID`,")
                    ln("    `alUser_profilePhone`,")
                    ln("    `alUser_adminNotes`,")
                    ln("    `alUser_aboutMe`,")
                    ln("    `alUser_profileRejectionReason`,")
                    ln("    `alUser_banReason`,")
                    ln("    `alUser_subscribedToAllCategories`")
                    ln(") values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
                },
                params = listOf(
                    phiEval("return gmdate('Y-m-d H:i:s', ${x.createdAt.time});") as String,
                    phiEval("return gmdate('Y-m-d H:i:s', ${x.updatedAt.time});") as String,
                    x.deleted,
                    x.firstName,
                    x.email,
                    x.lastName,
                    x.passwordHash,
                    x.sessionID,
                    x.profilePhone,
                    x.adminNotes,
                    x.aboutMe,
                    x.profileRejectionReason,
                    x.banReason,
                    x.subscribedToAllCategories
                ),
                uuid = "5b5be5e7-5eb9-44e7-8618-c513fb577b0c"
            )

            val res = DBPile.query(
                sql = "select cast(last_insert_id() as char)",
                uuid = "1c969a58-f1d4-4410-923b-bea78697c069"
            )

            x.id = res.first().first() as String
            return x
        }
    }
}

fun AlUserRepository.propertyToColumnName(prop: KMutableProperty1<AlUser, String>): String {
    if (prop.name == AlUser::id.name) return "alUser_id"
    if (prop.name == AlUser::createdAt.name) return "alUser_createdAt"
    if (prop.name == AlUser::updatedAt.name) return "alUser_updatedAt"
    if (prop.name == AlUser::deleted.name) return "alUser_deleted"
    if (prop.name == AlUser::firstName.name) return "alUser_firstName"
    if (prop.name == AlUser::email.name) return "alUser_email"
    if (prop.name == AlUser::lastName.name) return "alUser_lastName"
    if (prop.name == AlUser::passwordHash.name) return "alUser_passwordHash"
    if (prop.name == AlUser::sessionID.name) return "alUser_sessionID"
    if (prop.name == AlUser::profilePhone.name) return "alUser_profilePhone"
    if (prop.name == AlUser::adminNotes.name) return "alUser_adminNotes"
    if (prop.name == AlUser::aboutMe.name) return "alUser_aboutMe"
    if (prop.name == AlUser::profileRejectionReason.name) return "alUser_profileRejectionReason"
    if (prop.name == AlUser::banReason.name) return "alUser_banReason"
    if (prop.name == AlUser::subscribedToAllCategories.name) return "alUser_subscribedToAllCategories"
    throw Exception("03f6ab91-79cf-4633-a7d0-06dd05ab9dd2")

}

val AlUserRepository.dropTableDDL get() = buildString {
    ln("drop table if exists `alraune_users`")
}
val AlUserRepository.createTableDDL get() = buildString {
    ln("create table `alraune_users` (")
    ln("    alUser_id bigint not null auto_increment primary key,")
    ln("    alUser_createdAt datetime not null,")
    ln("    alUser_updatedAt datetime not null,")
    ln("    alUser_deleted boolean not null,")
    ln("    alUser_firstName longtext not null,")
    ln("    alUser_email longtext not null,")
    ln("    alUser_lastName longtext not null,")
    ln("    alUser_passwordHash longtext not null,")
    ln("    alUser_sessionID longtext not null,")
    ln("    alUser_profilePhone longtext not null,")
    ln("    alUser_adminNotes longtext not null,")
    ln("    alUser_aboutMe longtext not null,")
    ln("    alUser_profileRejectionReason longtext,")
    ln("    alUser_banReason longtext,")
    ln("    alUser_subscribedToAllCategories boolean not null")
    ln(") engine=InnoDB;")
}
class Generated_AlUser : AlUser {
    override var id by notNull<String>()
    override var createdAt: PHPTimestamp = DBPile.currentTimestampForEntity()
    override var updatedAt= createdAt
    override var deleted= false
    override var firstName by notNull<String>()
    override var email by notNull<String>()
    override var lastName by notNull<String>()
    override var passwordHash by notNull<String>()
    override var sessionID by notNull<String>()
    override var profilePhone by notNull<String>()
    override var adminNotes by notNull<String>()
    override var aboutMe by notNull<String>()
    override var profileRejectionReason: String? = null
    override var banReason: String? = null
    override var subscribedToAllCategories by notNull<Boolean>()

    override fun toString(): String {
        return buildString {
            append("AlUser(")
            append("id=$id, ")
            append("createdAt=${phiEval("return gmdate('Y-m-d H:i:s', ${createdAt.time});") as String}, ")
            append("updatedAt=${phiEval("return gmdate('Y-m-d H:i:s', ${createdAt.time});") as String}, ")
            append("deleted=$deleted, ")
            append("firstName=$firstName, ")
            append("email=$email, ")
            append("lastName=$lastName, ")
            append("sessionID=$sessionID, ")
            append("profilePhone=$profilePhone, ")
            append("adminNotes=$adminNotes, ")
            append("aboutMe=$aboutMe, ")
            append("profileRejectionReason=$profileRejectionReason, ")
            append("banReason=$banReason, ")
            append("subscribedToAllCategories=$subscribedToAllCategories")
            append(")")
        }
    }
}

object AlGeneratedDBPile {
    object ddl {
        val dropCreateAllScript = """
drop table if exists `alraune_users`;
create table `alraune_users` (
    alUser_id bigint not null auto_increment primary key,
    alUser_createdAt datetime not null,
    alUser_updatedAt datetime not null,
    alUser_deleted boolean not null,
    alUser_firstName longtext not null,
    alUser_email longtext not null,
    alUser_lastName longtext not null,
    alUser_passwordHash longtext not null,
    alUser_sessionID longtext not null,
    alUser_profilePhone longtext not null,
    alUser_adminNotes longtext not null,
    alUser_aboutMe longtext not null,
    alUser_profileRejectionReason longtext,
    alUser_banReason longtext,
    alUser_subscribedToAllCategories boolean not null
) engine=InnoDB;

        """
    }
}


/*
DDL
===

drop table if exists `alraune_users`;
create table `alraune_users` (
    alUser_id bigint not null auto_increment primary key,
    alUser_createdAt datetime not null,
    alUser_updatedAt datetime not null,
    alUser_deleted boolean not null,
    alUser_firstName longtext not null,
    alUser_email longtext not null,
    alUser_lastName longtext not null,
    alUser_passwordHash longtext not null,
    alUser_sessionID longtext not null,
    alUser_profilePhone longtext not null,
    alUser_adminNotes longtext not null,
    alUser_aboutMe longtext not null,
    alUser_profileRejectionReason longtext,
    alUser_banReason longtext,
    alUser_subscribedToAllCategories boolean not null
) engine=InnoDB;

*/