class Database {
private var query: String = ""
infix fun SELECT(string: String): Database {
query += "SELECT $string "
return this
}
infix fun FROM(string: String): Database {
query += "FROM $string "
return this
}
infix fun WHERE(string: String): Database {
query += "WHERE $string"
return this
}
infix fun UPDATE(string: String): Database {
query += "UPDATE $string"
return this
}
infix fun SET(string: String): Database {
query += "SET $string"
return this
}
fun run(): Any {
return "" /* TODO: query result */
}
}
fun main() {
val query1 = Database() SELECT "*" FROM "SomeTable" WHERE "id=12"
val result1 = query1.run()
val query2 = Database() UPDATE "table_1" SET "name='some name'" WHERE "id=45"
val result2 = query2.run()
}
Here is a simple example of sql statements in kotlin with infix functions.
I’m not a expert in sql and databases so I decided to share my idea with the community to let others create such library if they liked this idea