Skip to content

Commit

Permalink
Merge pull request #4 from darkredz/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
darkredz authored Dec 17, 2018
2 parents 18ca72c + 5421664 commit 635ddd2
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 2 deletions.
13 changes: 11 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.zeko.model</groupId>
<artifactId>data-mapper</artifactId>
<version>1.1</version>
<version>1.2</version>
<packaging>jar</packaging>

<name>com.zeko.model data-mapper</name>
Expand Down Expand Up @@ -43,12 +43,21 @@
<artifactId>vertx-core</artifactId>
<version>${vertx.version}</version>
</dependency>

<dependency>
<groupId>com.caucho</groupId>
<artifactId>quercus</artifactId>
<version>4.0.45</version>
</dependency>
<dependency>
<groupId>com.github.jasync-sql</groupId>
<artifactId>jasync-common</artifactId>
<version>0.8.54</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.7</version>
</dependency>

<!-- Test dependencies-->
<dependency>
Expand Down
69 changes: 69 additions & 0 deletions src/main/kotlin/com.zeko.model/DateTimeHelper.kt
Original file line number Diff line number Diff line change
@@ -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
}
}
}
64 changes: 64 additions & 0 deletions src/main/kotlin/com.zeko.model/ResultSetHelper.kt
Original file line number Diff line number Diff line change
@@ -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<String>, timeProcessor: ((BaseLocal, DateTimeZone?, DateTimeZone?) -> Any)? = null, tzFrom: DateTimeZone?, tzTo: DateTimeZone?): List<LinkedHashMap<String, Any?>> {
val results = ArrayList<LinkedHashMap<String, Any?>>(rows.size)

for (row in rows) {
var i = 0
val obj = java.util.LinkedHashMap<String, Any?>()

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
}
}
}
77 changes: 77 additions & 0 deletions src/main/kotlin/com.zeko.model/Select.kt
Original file line number Diff line number Diff line change
@@ -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<String>, val sqlFields: String)

class Select {
private val fieldsToSelect by lazy {
LinkedHashMap<String, Array<String>>()
}
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<String>
}
return this
}

fun prepare(): SelectInfo {
val selectFields = mutableListOf<String>()
val columns = mutableListOf<String>()

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)
}
}

0 comments on commit 635ddd2

Please sign in to comment.