add raw string to the query
Query()
.raw("SELECT * FROM table WHERE column = ? AND other = ?", "abc", 123)Resulting query: SELECT * FROM table WHERE column = "abc" AND other = 123
simply adds "SELECT" in the query
Query()
.selectRaw("column, other").raw("FROM table")Resulting query: SELECT column, other FROM table
select all columns
Query()
.selectAll()
.raw("FROM table")Resulting query: SELECT * FROM table
select the fields by name
Query()
.select("column", "other")
.raw("FROM table")Resulting query: SELECT column, other FROM table
select the fields by name, using an array
val fields = arrayOf("column", "other")
Query()
.selectFields(fields)
.raw("FROM table")Resulting query: SELECT column, other FROM table
select counting for something
Query()
.countRaw("column")
.raw("FROM table")Resulting query: SELECT COUNT(column) FROM table
select counting all
Query()
.countAll()
.raw("FROM table")Resulting query: SELECT COUNT(*) FROM table
select counting the fields by name
Query()
.count("column", "other")
.raw("FROM table")Resulting query: SELECT COUNT(column, other) FROM table
simply adds "FROM" in the query
Query("SELECT column")
.from("table")Resulting query: SELECT column FROM table
uses FROM with an inner query
Query("SELECT column")
.from(Query("SELECT column FROM table ORDER BY other", "myinner"))
.raw("FROM table")Resulting query: SELECT column FROM ( SELECT column FROM table ORDER BY other ) myinner FROM table
adds "WHERE" if there isn't any "WHERE" yet on the query, otherwise adds "AND"
Query("SELECT column FROM table")
.where("column = ?", "abc")
.where("other = ?", 123)Resulting query: SELECT column FROM table WHERE column = "abc" AND other = 123
adds a WHERE with multiple conditions, all of them needs to be true
Query("SELECT column FROM table").whereAll {
where("column = ?", "abc")
where("other = ?", 123)
}Resulting query: SELECT column FROM table WHERE (column = "abc" AND other = 123)
adds a WHERE with multiple conditions, only one of them needs to be true
Query("SELECT column FROM table").whereSome {
where("column = ?", "abc")
where("other = ?", 123)
}Resulting query: SELECT column FROM table WHERE (column = "abc" OR other = 123)
adds a WHERE with multiple conditions, all of them needs to be equal to the specified value
Query("SELECT column FROM table").whereAllEq(
"column" to "abc",
"other" to 123,
"password" to Query("SHA1(?)", "ultrasecret")
)Resulting query: SELECT column FROM table WHERE (column = "abc" AND other = 123 AND password = SHA1("ultrasecret"))
You can use a map as well
val conditionsMap = mapOf(
"column" to "abc",
"other" to 123,
"password" to Query("SHA1(?)", "ultrasecret")
)
Query("SELECT column FROM table").whereAllEq(conditionsMap)Resulting query: SELECT column FROM table WHERE (column = "abc" AND other = 123 AND password = SHA1("ultrasecret"))
adds a WHERE with multiple conditions, only one of them needs to be equal to the specified value
Query("SELECT column FROM table").whereSomeEq(
"column" to "abc",
"other" to 123,
"password" to Query("SHA1(?)", "ultrasecret")
)Resulting query: SELECT column FROM table WHERE (column = "abc" OR other = 123 OR password = SHA1("ultrasecret"))
You can use a map as well
val conditionsMap = mapOf(
"column" to "abc",
"other" to 123,
"password" to Query("SHA1(?)", "ultrasecret")
)
Query("SELECT column FROM table").whereSomeEq(conditionsMap)Resulting query: SELECT column FROM table WHERE (column = "abc" OR other = 123 OR password = SHA1("ultrasecret"))
adds a WHERE with multiple conditions matching LIKE the param, only one of them needs to be equal to the specified value
Query("SELECT column FROM table")
.whereSomeLikeThis(arrayOf("column", "other"), "%abc%")Resulting query: SELECT column FROM table WHERE (column LIKE "%abc%" OR other LIKE "%abc%")
Query()
.selectAll()
.from("table")
.whereEq("column", "abc") // WHERE column = "abc"
.whereNotEq("other", 456) // AND other != 456
.whereGt("numbii", 3) // AND numbii > 3
.whereLt("numbii", 6) // AND numbii < 6
.whereGtEq("nomboo", 5) // AND nomboo >= 5
.whereLtEq("nomboo", 7) // AND nomboo <= 7
.whereNull("thatnullable") // AND thatnullable IS NULL
.whereNotNull("thatothernulable") // AND thatothernulable IS NOT NULL
.whereBetween("rangeable", 3, 7) // AND rangeable BETWEEN 3 AND 7
.whereLike("name", "%john%") // AND name LIKE "%john%"
.whereIn("id", 2, 3, 4) // AND id IN (2, 3, 4)
.whereNotIn("anotherid", 6, 7, 8) // AND anotherid NOT IN 6, 7, 8simply adds "INNER JOIN" in the query
Query("SELECT column FROM table").innerJoinRaw("othertable ON primarykey = foreignkey")Resulting query: SELECT column FROM table INNER JOIN othertable ON primarykey = foreignkey
add an inner join with the joining columns
Query("SELECT column FROM table").innerJoin("othertable", "primarykey", "foreignkey")Resulting query: SELECT column FROM table INNER JOIN othertable ON primarykey = foreignkey
simply adds "LEFT JOIN" in the query
Query("SELECT column FROM table").leftJoinRaw("othertable ON primarykey = foreignkey")Resulting query: SELECT column FROM table LEFT JOIN othertable ON primarykey = foreignkey
add a left join with the joining columns
Query("SELECT column FROM table").leftJoin("othertable", "primarykey", "foreignkey")Resulting query: SELECT column FROM table LEFT JOIN othertable ON primarykey = foreignkey
simply adds "GROUP BY" in the query
Query("SELECT COUNT(column) FROM table").groupByRaw("column")Resulting query: SELECT COUNT(column) FROM table GROUP BY column
adds group by of the columns
Query("SELECT COUNT(column) FROM table").groupBy("column", "other")Resulting query: SELECT COUNT(column) FROM table GROUP BY column, other
simply adds "HAVING" in the query
Query("SELECT COUNT(column) AS countt FROM table GROUP BY column").having("countt > 3")Resulting query: SELECT COUNT(column) AS countt FROM table GROUP BY column HAVING countt > 3
Query()
.selectAll()
.from("table")
.havingEq("column", "abc") // HAVING column = "abc"
.havingNotEq("other", 456) // HAVING other != 456
.havingGt("numbii", 3) // HAVING numbii > 3
.havingLt("numbii", 6) // HAVING numbii < 6
.havingGtEq("nomboo", 5) // HAVING nomboo >= 5
.havingLtEq("nomboo", 7) // HAVING nomboo <= 7
.havingNull("thatnullable") // HAVING thatnullable IS NULL
.havingNotNull("thatothernulable") // HAVING thatothernulable IS NOT NULL
.havingBetween("rangeable", 3, 7) // HAVING rangeable BETWEEN 3 AND 7
.havingLike("name", "%john%") // HAVING name LIKE "%john%"
.havingIn("id", 2, 3, 4) // HAVING id IN (2, 3, 4)
.havingNotIn("anotherid", 6, 7, 8) // HAVING anotherid NOT IN 6, 7, 8simply adds "ORDER BY" in the query
Query("SELECT column FROM table").orderByRaw("column ASC")Resulting query: SELECT column FROM table ORDER BY column
add order by with a string identifing if ASC or DESC
Query("SELECT column FROM table").orderBy("column", "ASC")Resulting query: SELECT column FROM table ORDER BY column ASC
add order by with optional true for ASC and false for DESC
Query("SELECT column FROM table").orderByAsc("column")Resulting query: SELECT column FROM table ORDER BY column ASC
Query("SELECT column FROM table").orderByAsc("column", false)Resulting query: SELECT column FROM table ORDER BY column DESC
adds limit with index and size
Query("SELECT column FROM table ORDER BY column ASC").limit(3, 20)Resulting query: SELECT column FROM table ORDER BY column ASC LIMIT 3, 20
adds insert into in the query
Query().insertInto("table").raw("(column, other) VALUES (?, ?)", "abc", 123)Resulting query: INSERT INTO table (column, other) VALUES ("abc", 123)
easily adds the values of insert in the query
Query("INSERT INTO table").insertValues(
"column" to "abc",
"other" to 123,
"password" to Query("SHA1(?)", "ultrasecret")
)Resulting query: INSERT INTO table (column, other, password) VALUES ("abc", 123, SHA1("ultrasecret"))
You can use a map as well
val insertMap = mapOf(
"column" to "abc",
"other" to 123,
"password" to Query("SHA1(?)", "ultrasecret")
)
Query("INSERT INTO table").insertValues(insertMap)
Resulting query: INSERT INTO table (column, other, password) VALUES ("abc", 123, SHA1("ultrasecret"))
adds update to the query
Query().updateTable("table").raw("SET column = ?, other = ? WHERE id = ?", "abc", 123, 1)Resulting query: UPDATE table SET column = "abc", other = 123 WHERE id = 1
easily adds the set of changes of update in the query
Query("UPDATE table").updateSet(
"column" to "abc",
"other" to 123,
"password" to Query("SHA1(?)", "ultrasecret")
).raw("WHERE id = ?", 1)Resulting query: UPDATE table SET column = "abc", other = 123, password = SHA1("ultrasecret") WHERE id = 1
You can use a map as well
val updateMap = mapOf(
"column" to "abc",
"other" to 123,
"password" to Query("SHA1(?)", "ultrasecret")
)
Query("UPDATE table").updateSet(updateMap).raw("WHERE id = ?", 1)Resulting query: UPDATE table SET column = "abc", other = 123, password = SHA1("ultrasecret") WHERE id = 1
adds delete from on the query
Query().deleteFrom("table").raw("WHERE id = ?", 1)Resulting query: DELETE FROM table WHERE id = 1
concatenate all queries
Query("SELECT * FROM table").concat(Query("WHERE column = ?", "abc"), Query("AND other = ?", 123))Resulting query: SELECT * FROM table WHERE column = "abc" AND other = 123
utility function to tokenize
Query().oddText(3, "?", ",")Resulting query: ?,?,?
execute the callback only if the boolean is true, util to avoid breaking the Query chain
Query("SELECT * FROM table").ifThen(compareColumn) {
where("column = ?", "abc")
}.where("other = ?", 123)Resulting query when compareColumn == true: SELECT * FROM table WHERE column = "abc" AND other = 123
Resulting query when compareColumn == false: SELECT * FROM table WHERE other = 123
executes the callback only if the boolean is true and the previous ifThen (or letThen) was not fulfilled
Query("SELECT * FROM table").ifThen(compareColumn) {
where("column = ?", "abc")
}.elseIf(compareOther) {
where("other = ?", 123)
}executes the callback only if the previous ifThen (or letThen) was not fulfilled
Query("SELECT * FROM table").ifThen(compareColumn) {
where("column = ?", "abc")
}.elseThen {
where("other = ?", 123)
}Resulting query when compareColumn == true: SELECT * FROM table WHERE column = "abc"
Resulting query when compareColumn == false: SELECT * FROM table WHERE other = 123
execute the callback only if the variable is not null, util to avoid breaking the Query chain
Query("SELECT * FROM table").letThen(columnSearch) {
where("column = ?", columnSearch)
}.where("other = ?", 123)Resulting query when columnSearch == "abc": SELECT * FROM table WHERE column = "abc" AND other = 123
Resulting query when compareColumn == null: SELECT * FROM table WHERE other = 123
utility function to organize name and value pairs that may have an inner query
Query().fieldsQuestionsAndParams(
"column" to "abc",
"other" to Query("SHA1(?)", 123)
)Resulting list: [{ field: "column", question: "?", params: ["abc"] }, { field: "other", question: "SHA1(?)", params: [123] }]