Giter VIP home page Giter VIP logo

Comments (6)

vincentlauvlwj avatar vincentlauvlwj commented on July 17, 2024

Most SQL database engines uses static, rigid typing. But SQLite uses a more general dynamic type system.

SQLite only supports five data types, they are NULL, INTEGER, REAL, TEXT, BLOB. There is not a data type set aside for storing dates and/or times. Dates and times are stored stored as TEXT, REAL, or INTEGER values:

  • TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
  • REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.
  • INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.

More details can be found at: https://www.sqlite.org/datatype3.html

from ktorm.

vincentlauvlwj avatar vincentlauvlwj commented on July 17, 2024

In your case, you should not use the datetime function to store datetimes to SQLite. If you want to save datetimes as integers, you can write a custom implementation of SqlType:

fun <E : Any> BaseTable<E>.datetimeAsInteger(name: String): BaseTable<E>.ColumnRegistration<LocalDateTime> {
    return registerColumn(name, DateTimeAsIntegerSqlType)
}

object DateTimeAsIntegerSqlType : SqlType<LocalDateTime>(Types.INTEGER, "integer") {
    override fun doSetParameter(ps: PreparedStatement, index: Int, parameter: LocalDateTime) {
        val seconds = parameter.atZone(ZoneId.systemDefault()).toEpochSecond()
        ps.setLong(index, seconds)
    }

    override fun doGetResult(rs: ResultSet, index: Int): LocalDateTime? {
        val seconds = rs.getLong(index)
        return Instant.ofEpochSecond(seconds).atZone(ZoneId.systemDefault()).toLocalDateTime()
    }
}

Then define the hireAt column using this datetimeAsInteger function:

object Employees : Table<Employee>("employees") {
    val id by int("id").primaryKey().bindTo { it.id }
    val name by varchar("name").bindTo { it.name }
    val hiredAt by datetimeAsInteger("hired_at").bindTo { it.hiredAt }
}

from ktorm.

ashokgelal avatar ashokgelal commented on July 17, 2024

I didn't know that. Thanks.

Any suggestion on a good way to make the conversion "automatic" without forcing users to change the column type when they switch from, say, MySQL, to SQLite?

from ktorm.

ashokgelal avatar ashokgelal commented on July 17, 2024

Just saw your last reply. That works but then I have to force users to change their column type everytime they switch from one db provider to SQLite.

You might be wondering what is my use case. Well, I want to run unit tests on a sqlite database and real app with a full MySQL server. Ideally with other ORMs I do with a in-memory sqlite database. (On a side note, I could never get sqlite in-memory database to work with Ktorm.)

from ktorm.

vincentlauvlwj avatar vincentlauvlwj commented on July 17, 2024

Every database engine has its special features. It is difficult to find a perfect way to handle their differences. In you case, for compatibility reasons, you can always define your datetime columns using datetimeAsInteger, that works both in SQLite and MySQL.

BTW, SQLiteTest is an example using SQLite in memory mode with Ktorm.

from ktorm.

vincentlauvlwj avatar vincentlauvlwj commented on July 17, 2024

And if you are using SQLite just for running unit tests, testcontainers might be a better choice to do that.

from ktorm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.