English | 中文
This project is a SQL statement builder, allow you write SQL in Java.
It's not a fast way to build SQL statement, but it may make your codes more clear and more intuitive.
The simple demo like this:
Column fruit_name = new Column("name");
SQL.delete("fruit", Clause.equal(fruit_name, "apple"));
As the output sql is:
DELETE FROM `fruit` WHERE `name` = "apple";
String sql = new Insert(String table)
.addAll(Map<String, Object> cv) // by map<column, value>
.add(String column, Object value) // by values
.toString();
SQL.delete(String table, Clause where);
String sql = new Update(String table)
.addAll(Map<String, Object> cv) // by map<column, value>
.add(String column, Object value) // by values
.where(clause)
.toString();
Simple way to select all colunm in one table.
SQL.select(String table, Clause where, int limit);
Most of time you may use the Select Builder for complex query.
You're no need to input the table name for the Builder, but the table name of the Column must be NON-NULL
, especially in multi-table queries!
Column price_fruitId = new Column("price", "fruitId");
Column fruit_fruitId = new Column("fruit", "fruitId");
Column fruit_name = new Column("fruit", "name");
String sql = new Select()
.addColumn(new Column("mylike", null)) // no column
.addColumn(new Column("fruit", "*")) // all column in table
.addColumn(new Column("price", xxx)) // any column in table
.innerJoin(price_fruitId, fruit_fruitId) // join
.where(Clause.like(fruit_name, "apple")) // where
.orderBy(new Asc(price_fruitId).desc(xxx)) // order by
.limit(2) // limit
// Sigle clause:
// in, between, isNull, isNotNull, like,
// equal(=), lessThan(<), lessEqual(<=), moreThan(>), moreEqual(>=)
Clause.equal(column, pattern)
// Multi-clause
MultiClause clause = new MultiClause(clause1)
.and(clause2)
.or(new MultiClause(xxx)...);
Column(String name); // no table
Column(String table, String name); // with table
Column(table, name).as(String alias); // with column alias
Column(table, name).as(tableAlias, nameAlias); // with table alias