Comments (6)
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.
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.
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.
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.
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.
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)
- ktorm-support-mysql 在maven仓库中没有找到 HOT 1
- Sequence update not as expected when table column with an ON UPDATE CURRENT_TIMESTAMP clause HOT 1
- Is this still actively maintained? HOT 6
- Apache Derby DB: In case of reference entity API and using table references, ktorm generates SQL aliases with first underscore character(_) that invalid for Apache DB HOT 2
- Type mismatch.
- [Feature Request]:Field update strategist, pre positioned during insertion and update
- Connectivity problem HOT 1
- 关于join的优化
- Multiple onConflict at bulkInsertOrUpdate HOT 1
- MySQL存储UUID报错 HOT 8
- 自定义支持MySQL的find_in_set函数 HOT 5
- 适配 solon框架 HOT 1
- Union功能存在问题? HOT 2
- 关于列绑定,能不能先references再transform?
- 🧐为什么不添加支持分页的函数呢 HOT 2
- Oracle Timestamp support
- Database#exceptionTranslator should return a nullable throwable
- 4.0版本使用KSP生成实体类运行出错 HOT 8
- ksp column converter in version 4.0.0 disappeared HOT 1
- Ktorm 别名机制似乎有问题 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ktorm.