SQL Query Builder
set table name
set orders
add where condition by sql string or Where object
add having condition by sql string or Where object
set limit range
set group by fields
add join
set select fields
new Query()
.table("articles")
.select("articles.*", "users.name")
.join(Join.left("users").on("users.id", "articles.user_id"))
.where(Where.field("type").eq(1))
.order(Order.by("articles.created_at").desc)
.limit(0, 10)
.build();
// SELECT `articles`.*, `users`.`name` FROM `articles` LEFT OUTER JOIN `users` ON `users`.`id` = `articles`.`user_id` WHERE `type` = 1 ORDER BY `articles`.`created_at` DESC LIMIT 0, 10
set one or more insert data object(s)
const builder = new Query();
const records = [
{
name: "Enok",
password: "foo",
id: 1
},
{
id: 2,
name: "Man",
password: "bar"
}
];
const sql = builder
.table("users")
.insert(records)
.build();
// INSERT INTO `users` (`name`,`password`,`id`) VALUES ("Enok","foo",1) ("Man","bar",2)
set update data
const builder = new Query();
const record = {
name: "Enok",
password: "foo",
id: 1
};
const sql = builder
.table("users")
.where(Where.field("id").eq(1))
.where(Where.field("name").like("%n%"))
.update(record)
.build();
// UPDATE `users` SET `name` = "Enok", `password` = "foo", `id` = 1 WHERE `id` = 1 AND `name` LIKE "%n%"
set sql type to delete
const builder = new Query();
const sql = builder
.table("users")
.where(Where.field("id").eq(1))
.where(Where.field("name").like("%n%"))
.delete()
.build();
//DELETE FROM `users` WHERE `id` = 1 AND `name` LIKE "%n%"
Generate and return SQL