Code Monkey home page Code Monkey logo

sqlbuilder's Introduction

SQLbuilder

Maven Central Release Nightly build javadoc Quality Gate Status Maintainability Rating Reliability Rating Security Rating Lines of Code Coverage Technical Debt GitHub Gitter

A Java-Library to build SQL-Statements

Dependency

<dependency>
  <groupId>de.jaggl.sqlbuilder</groupId>
  <artifactId>sqlbuilder-core</artifactId>
  <version>2.7.2</version>
</dependency>

Simple Example

private static final Table PERSONS = Table.create("persons");
private static final VarCharColumn FORENAME = PERSONS.varCharColumn("forename").size(50).build();
private static final VarCharColumn LASTNAME = PERSONS.varCharColumn("lastname").size(50).build();

public static final void main(String[] args)
{
  Queries.select()
  	.from(PERSONS)
  	.where(LASTNAME.eq("Doe"))
  	.print();
}	

This will output:

SELECT * FROM `persons` WHERE `persons`.`lastname` = 'Doe'

To get the SQL-statement as a string, call build() instead of print()

Some other examples:

Insert:

Queries.insertInto(PERSONS)
	.set(FORENAME, "John")
	.set(LASTNAME, "Doe")
	.print();
INSERT INTO `persons` SET `persons`.`forename` = 'John', `persons`.`lastname` = 'Doe'

Update:

Queries.update(PERSONS)
	.set(FORENAME, "John")
	.where(LASTNAME.eq("Doe"))
	.print();
UPDATE `persons` SET `persons`.`forename` = 'John', WHERE `persons`.`lastname` = 'Doe'

Delete:

Queries.deleteFrom(PERSONS)
	.where(LASTNAME.eq("Doe"))
	.print();
DELETE FROM `persons` WHERE `persons`.`lastname` = 'Doe'

Create table:

Queries.createTable(PERSONS).println()
CREATE TABLE `persons` (`forename` VARCHAR(50) DEFAULT NULL, `lastname` VARCHAR(50) DEFAULT NULL)

Features

  • Build SQL-queries in different dialects, currently supported are:

    • MySQL
    • Sybase
  • Currently supported queries are:

    • SELECT
    • INSERT
    • UPDATE
    • DELETE
    • CREATE TABLE
  • Type-safe query-building. Currently supported column-datatypes are:

    • CHAR, VARCHAR, TEXT
    • INT, BIGINT, MEDIUMINT, SMALLINT, TINYINT
    • DOUBLE, FLOAT, DECIMAL
    • DATE, DATETIME
  • Supports SQL-Functions, currently supported are:

    • SUM
    • MIN
    • MAX
    • AVG
    • COUNT
    • NOW
  • Build queries with or without indentation

Choose dialect

By default the MySQL-dialect is chosen. To change the dialect, you can pass your wanted dialect to the print() or build()-method. The known dialects are collected in the Utility-Class Dialects. Simple example for choose the known Sybase-dialect:

Queries.select()
  .from(PERSONS)
  .limit(100, 10)
  .print(Dialects.SYBASE);

This will output:

SELECT TOP 100 START AT 11 * FROM `persons`

It is also possible to globally change the default-Dialect. To do so, set the system-property sqlbuilder.defaultDialect to the name of the Dialect you want.

Indentation

Just add Indentation.enabled() to the print() or build()-method as follows:

Queries.select()
  .from(PERSONS)
  .where(LASTNAME.eq("Doe"))
  .print(Indentation.enabled());

This will output:

SELECT
  *
FROM
  `persons`
WHERE `persons`.`lastname` = 'Doe'

sqlbuilder's People

Contributors

schubi1981 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

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.