GithubHelp home page GithubHelp logo

yeamy / sqlbuilder Goto Github PK

View Code? Open in Web Editor NEW
15.0 3.0 2.0 137 KB

This project is a SQL statement builder, allow you to write SQL in Java.

License: GNU General Public License v2.0

Java 100.00%
javaee mysql sql

sqlbuilder's Introduction

SQL Statement Builder

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.

0. Example

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";

1. INSERT

String sql = new Insert(String table)
		.addAll(Map<String, Object> cv)          // by map<column, value>
		.add(String column, Object value)        // by values
		.toString();

2. DELETE

SQL.delete(String table, Clause where);

3. UPDATE

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();

4. SELECT

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

5. WHERE

// 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)...);

6. Column

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

sqlbuilder's People

Contributors

yeamy avatar

Stargazers

Juan Pablo Rodríguez Macías avatar Code Revisited avatar Eric Wong avatar Jeff Carpenter avatar  avatar  avatar kazuki avatar lin yan avatar Mocker avatar  avatar HunnHu avatar a8105 avatar Dmitrii Volykhin avatar WayChan avatar  avatar

Watchers

James Cloos avatar cckingxu avatar  avatar

Forkers

volyx wangzji

sqlbuilder's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.