Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ package sqlest.sql.base

import sqlest.ast._
import sqlest.ast.operations.ColumnOperations._
import org.joda.time.{ LocalDate, DateTime }
import org.joda.time.format.{ DateTimeFormat, DateTimeFormatter }

trait BaseStatementBuilder {
val dateTimeFormat: DateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd-HH.mm.ss.SSSSSS")
val localDateFormat: DateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I probably should have used ISODateTimeFormat


def preprocess(operation: Operation): Operation =
aliasColumnsFromSubselects(operation)

Expand Down Expand Up @@ -158,8 +163,8 @@ trait BaseStatementBuilder {
case DoubleColumnType => value.toString
case BigDecimalColumnType => value.toString
case StringColumnType => "'" + escapeSqlString(value.toString) + "'"
case DateTimeColumnType => value.toString
case LocalDateColumnType => value.toString
case DateTimeColumnType => "'" + dateTimeFormat.print(value.asInstanceOf[DateTime]) + "'"
case LocalDateColumnType => "'" + localDateFormat.print(value.asInstanceOf[LocalDate]) + "'"
case ByteArrayColumnType => javax.xml.bind.DatatypeConverter.printHexBinary(value.asInstanceOf[Array[Byte]])
case optionType: OptionColumnType[_, _] => value.asInstanceOf[Option[_]] match {
case None if optionType.hasNullNullValue => "null"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package sqlest.sql

import org.scalatest._
import org.scalatest.matchers._
import org.joda.time.{ LocalDate, DateTime }
import sqlest._
import sqlest.ast._

Expand Down Expand Up @@ -77,6 +78,13 @@ trait BaseStatementBuilderSpec extends FlatSpec with Matchers {
}
object TableFive extends TableFive(None)

class TableSix(alias: Option[String]) extends Table("six", alias) {
val col1 = column[DateTime]("col1")
val col2 = column[LocalDate]("col2")
val col3 = column[Option[LocalDate]]("col3")
}
object TableSix extends TableSix(None)

class TestTableFunction(alias: Option[String]) extends TableFunction2[String, String]("testTableFunction", alias) {
val col5 = column[String]("col5")
val col6 = column[String]("col6")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package sqlest.sql

import org.joda.time.LocalDate
import org.joda.time.{ DateTime, LocalDate }
import org.scalatest._
import org.scalatest.matchers._
import sqlest._
Expand Down Expand Up @@ -654,6 +654,24 @@ class SelectStatementBuilderSpec extends BaseStatementBuilderSpec {
}
}

"select with constant date and time values" should "produce valid SQL literals" in {
sql {
select(TableSix.col1)
.from(TableSix)
.where(
TableSix.col1 > new DateTime(2017, 11, 7, 18, 33, 54, 883).constant &&
TableSix.col2 >= new LocalDate(1976, 1, 24).constant &&
TableSix.col3 < Option(new LocalDate(1983, 10, 17)).constant
)
} should equal(
s"""
|select six.col1 as six_col1
|from six
|where (((six.col1 > '2017-11-07-18.33.54.883000') and (six.col2 >= '1976-01-24')) and (six.col3 < '1983-10-17'))""".formatSql,
List(Nil)
)
}

"optimize" should "not be supported outside DB2" in {
intercept[UnsupportedOperationException] {
sql {
Expand Down