diff --git a/pom.xml b/pom.xml index a518a4c..2f7cf6f 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.zeko.model data-mapper - 1.1 + 1.2 jar com.zeko.model data-mapper @@ -43,12 +43,21 @@ vertx-core ${vertx.version} - com.caucho quercus 4.0.45 + + com.github.jasync-sql + jasync-common + 0.8.54 + + + joda-time + joda-time + 2.9.7 + diff --git a/src/main/kotlin/com.zeko.model/DateTimeHelper.kt b/src/main/kotlin/com.zeko.model/DateTimeHelper.kt new file mode 100644 index 0000000..3253bd8 --- /dev/null +++ b/src/main/kotlin/com.zeko.model/DateTimeHelper.kt @@ -0,0 +1,69 @@ +/** + * Copyright (c) 2018 Leng Sheng Hong + * ------------------------------------------------------ + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.zeko.model + +import org.joda.time.DateTimeZone +import org.joda.time.LocalDateTime +import org.joda.time.LocalDate +import org.joda.time.LocalTime +import org.joda.time.DateTime +import org.joda.time.base.BaseLocal +import org.joda.time.format.ISODateTimeFormat + +class DateTimeHelper { + companion object { + fun toDateTimeStrUTC(value: BaseLocal): Any { + val dt = when (value) { + is LocalDateTime -> value.toString(ISODateTimeFormat.dateTime()) + "Z" + is LocalDate -> value.toString(ISODateTimeFormat.date()) + is LocalTime -> value.toString(ISODateTimeFormat.time()) + else -> value + } + return dt + } + + fun toDateTimeStrZone(value: BaseLocal, tzFrom: DateTimeZone?, tzTo: DateTimeZone?): Any { + val dt = when (value) { + is LocalDateTime -> DateTime(value.toDateTime(tzFrom).millis).toDateTime(tzTo).toString("yyyy-MM-dd'T'HH:mm:ss.SSSZZ") + is LocalDate -> value.toString(ISODateTimeFormat.date()) + is LocalTime -> value.toString(ISODateTimeFormat.time()) + else -> value + } + return dt + } + + fun toUnixTimeMilis(value: BaseLocal): Any { + val dt = when (value) { + is LocalDateTime -> value.toDateTime(DateTimeZone.UTC).millis + else -> value + } + return dt + } + + fun toUnixTime(value: BaseLocal): Any { + val milis = toUnixTimeMilis(value) + if (milis is Long) { + return milis / 1000 + } + return milis + } + } +} diff --git a/src/main/kotlin/com.zeko.model/ResultSetHelper.kt b/src/main/kotlin/com.zeko.model/ResultSetHelper.kt new file mode 100644 index 0000000..95e37c5 --- /dev/null +++ b/src/main/kotlin/com.zeko.model/ResultSetHelper.kt @@ -0,0 +1,64 @@ +/** + * Copyright (c) 2018 Leng Sheng Hong + * ------------------------------------------------------ + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.zeko.model + +import com.github.jasync.sql.db.ResultSet +import org.joda.time.DateTimeZone +import org.joda.time.LocalDateTime +import org.joda.time.LocalDate +import org.joda.time.LocalTime +import org.joda.time.base.BaseLocal +import java.util.LinkedHashMap + +class ResultSetHelper { + companion object { + fun toMaps(rows: ResultSet, columns: List, timeProcessor: ((BaseLocal, DateTimeZone?, DateTimeZone?) -> Any)? = null, tzFrom: DateTimeZone?, tzTo: DateTimeZone?): List> { + val results = ArrayList>(rows.size) + + for (row in rows) { + var i = 0 + val obj = java.util.LinkedHashMap() + + for (value in row) { + val colName = columns[i] + if (timeProcessor != null) { + obj[colName] = when (value) { + is LocalDateTime -> timeProcessor(value, tzFrom, tzTo) + is LocalDate -> timeProcessor(value, tzFrom, tzTo) + is LocalTime -> timeProcessor(value, tzFrom, tzTo) + else -> value + } + } else { + obj[colName] = when (value) { + is LocalDateTime -> DateTimeHelper.toDateTimeStrUTC(value) + is LocalDate -> DateTimeHelper.toDateTimeStrUTC(value) + is LocalTime -> DateTimeHelper.toDateTimeStrUTC(value) + else -> value + } + } + i++ + } + results.add(obj) + } + return results + } + } +} diff --git a/src/main/kotlin/com.zeko.model/Select.kt b/src/main/kotlin/com.zeko.model/Select.kt new file mode 100644 index 0000000..b566333 --- /dev/null +++ b/src/main/kotlin/com.zeko.model/Select.kt @@ -0,0 +1,77 @@ +/** + * Copyright (c) 2018 Leng Sheng Hong + * ------------------------------------------------------ + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.zeko.model + +import java.util.LinkedHashMap + +data class SelectInfo(val columns: List, val sqlFields: String) + +class Select { + private val fieldsToSelect by lazy { + LinkedHashMap>() + } + private lateinit var currentTable: String + private var espChar: String + private var asChar: String + + constructor(espChar: String = "`", asChar: String = "=") { + this.espChar = espChar + this.asChar = asChar + } + + fun table(name: String): Select { + currentTable = name + return this + } + + fun fields(vararg names: String): Select { + if (!currentTable.isNullOrEmpty()) { + fieldsToSelect[currentTable] = names as Array + } + return this + } + + fun prepare(): SelectInfo { + val selectFields = mutableListOf() + val columns = mutableListOf() + + for ((tbl, cols) in fieldsToSelect) { + for (colName in cols) { + if (colName.indexOf("=") != -1) { + val parts = colName.split(asChar) + val tblLinkedCol = parts[0].trim() + val selfCol = parts[1].trim() + + val aliasName = "$tbl-$selfCol" + columns.add(aliasName) + selectFields.add("$tblLinkedCol as $espChar$aliasName$espChar") + } else { + val aliasName = "$tbl-$colName" + columns.add(aliasName) + selectFields.add("$tbl.$colName as $espChar$aliasName$espChar") + } + } + } + + val sqlFields = selectFields.joinToString(", ") + return SelectInfo(columns, sqlFields) + } +}