GithubHelp home page GithubHelp logo

querybuilder's Introduction

QueryBuilder

Gradle Build and Test GitHub release (with filter)

Guide

It all starts with the SelectBuilder. When a SelectBuilder is created you can start nesting methods to build the queries.

Fields

.select()

Select the fields that are going to be queried.

.select("{u}name")                  // Generates 'SELECT u.name AS u_name'
.select("{u}name{custom_alias}")    // Generates 'SELECT u.name AS custom_alias'
.select("{_}name")                  // Generates 'SELECT name'
.select("{u}name{_}")               // Generates 'SELECT u.name'
.select("{u}*name")                 // Generates 'SELECT DISTINCT u.name AS u_name'

Tables

.from()
.from("users{u}")   // Generates 'FROM users u'
.from("users{_}")   // Generates 'FROM users'
.from("users u")    // Generates 'FROM users u'
.from("users")      // Generates 'FROM users'

Conditions

.where()
.where("{u}name", Comparison.EQUALS, "Augusto") // Generates 'WHERE u.name = 'Augusto''
.where(Condition.eq("{u}name", "Augusto"))      // Generates 'WHERE u.name = 'Augusto''
.where(
        Condition.or(
                Condition.eq("{u}name", "Augusto"),
                Condition.eq("{u}las_name", "Silva")
        )
)                                               // Generates 'WHERE u.name = 'Augusto' OR u.last_name = 'Silva''

All possible Condition builders are defined in Condition description.

Joins

Possible Join builders are listed in his description.

.join()
.join(new Join(Join.INNER).table("users_profile{up}").on("{u}id", "{up}user_id")) // Generates 'INNER JOIN users_profile up ON u.id = up.user_id'
.joins()
.joins(
        new Join(Join.INNER).table("users_profile{up}").on("{u}id", "{up}user_id"),
        new Join(Join.INNER, "users_data{ud}", "{u}id", "{ud}user_id")
)   // Generates 'INNER JOIN users_profile up ON u.id = up.user_id INNER JOIN users_data ud ON u.id = ud.user_id'
.innerJoin()
.innerJoin("users_profile{up}", "{u}id", "{up}user_id") // Generates 'INNER JOIN users_profile up ON u.id = up.user_id'
.leftJoin()
.leftJoin("users_profile{up}", "{u}id", "{up}user_id") // Generates 'LEFT JOIN users_profile up ON u.id = up.user_id'
.rigthJoin()
.rigthJoin("users_profile{up}", "{u}id", "{up}user_id") // Generates 'RIGHT JOIN users_profile up ON u.id = up.user_id'

Order

.order()
.order(Order.by("{u}id", Order.DESC))   // Generates 'ORDER BY u.id DESC'
.order(Order.asc("{u}id"))              // Generates 'ORDER BY u.id ASC'
.order(Order.desc("{u}id"))             // Generates 'ORDER BY u.id DESC'

Limit

.limit(10) // Generates 'LIMIT 10'

Union

.union()
.union(new SelectBuilder().select("{u}id", "{u}name").from("users{u}")) // Generates 'UNION (SELECT u.id AS u_id, u.name AS u_name FROM users u)'
.unionAll()
.unionAll(new SelectBuilder().select("{u}id", "{u}name").from("users{u}")) // Generates 'UNION ALL (SELECT u.id AS u_id, u.name AS u_name FROM users u)'

Aggregation

All possible Aggregation are listed in his description

.select(Aggregation.count("{u}id")) // Generates 'COUNT (u.id) AS count_u_id'
.select(Aggregation.count("{u}id", "custom_alias")) // Generates 'COUNT (u.id) AS custom_alias'

And the Group By clause constructed by

.groupBy("{u}name") // Generates 'GROUP BY u.name'

Constructors

Condition

Build conditions to be used in the .where(). Possible options:

Condition.eq("{u}name", "Augusto")                              // Generates 'u.name = 'Augusto''
Condition.neq("{u}name", "John")                                // Generates 'u.name <> 'John''
Condition.in("{u}job", Arrays.asList("Developer", "Designer"))  // Generates 'u.job IN ('Developer', 'Designer')'
Condition.isNull("{u}deleted_at")                               // Generates 'u.deleted_at IS NULL'
Condition.isNotNull("{u}last_login")                            // Generates 'u.last_login IS NOT NULL'
Condition.like("{u}name", "usto")                               // Generates 'u.name LIKE '%usto%''
Condition.nlike("{u}name", "esar")                              // Generates 'u.name NOT LIKE '%esar%''
Condition.gt("{u}salary", 4000)                                 // Generates 'u.salary > 4000'
Condition.gte("{u}age", 21)                                     // Generates 'u.age >= 21'
Condition.lt("{u}login_attempts", 5)                            // Generates 'u.login_attempts < 5'
Condition.lte("{u}products", 5)                                 // Generates 'u.products <= 5'
Condition.and(
        Condition.eq("{u}name", "Augusto"),
        Condition.eq("{u}age", 23)
)                                                               // Generates 'u.name = 'Augusto' AND u.age = 23'
Condition.or(
        Condition.eq("{u}name", "Augusto"),
        Condition.eq("{u}last_name", "Silva")
)                                                               // Generates 'u.name = 'Augusto' OR u.last_name = 'Silva''
Condition.and(
        Condition.eq("{_}name", "Augusto"),
        Condition.gte("{_}age", 21),
        Condition.or(
                Condition.eq("{_}nationality", "Brazilian"),
                Condition.eq("{_}nationality", "Japanese"),
                Condition.and(
                        Condition.eq("{_}job", "Diplomat"),
                        Condition.eq("{_}job_active", true)
                )
        )
)                                                               // Generates 'name = 'Augusto' AND age >= 21 AND ( nationality = 'Brazilian' OR nationality = 'Japanese' OR ( job = 'Diplomat' AND job_active = true ) )'

Join

new Join(Join.LEFT).table("user_profile{up}").on("{u}id", "{up}user_id")    // Generates 'LEFT JOIN user_profile up ON u.id = up.user_id'
new Join(Join.LEFT).table("user_profile{up}").on("{u}id = {up}user_id")     // Generates 'LEFT JOIN user_profile up ON u.id = up.user_id'
new Join(Join.INNER, "user_profile{up}", "{u}id", "{up}user_id")            // Generates 'INNER JOIN user_profile up ON u.id = up.user_id'
new Join(Join.INNER, "user_profile{up}", "{u}id = {up}user_id" )            // Generates 'INNER JOIN user_profile up ON u.id = up.user_id'

Comparison

Comparisons used if building inline [.where()] conditions.

Comparison.EQUALS
Comparison.IN
Comparison.IS_NULL
Comparison.IS_NOT_NULL
Comparison.LIKE
Comparison.NOT_LIKE
Comparison.DIFFERENT
Comparison.GREATER_THAN
Comparison.GREATER_THAN_OR_EQUALS
Comparison.LESS_THAN
Comparison.LESS_THAN_OR_EQUALS

Aggregation

Aggregations used for creating [.select()] clause aggregations.

Aggregation.AVERAGE
Aggregation.COUNT
Aggregation.MAX
Aggregation.MIN
Aggregation.SUM

Aggregation.average("{u}score") // Generates 'AVG (u.score) AS avg_u_score'
Aggregation.count("{u}id")      // Generates 'COUNT (u.id) AS cout_u_id'
Aggregation.max("{u}score")     // Generates 'MAX (u.score) AS max_u_score'
Aggregation.min("{u}score")     // Generates 'MIN (u.score) AS min_u_score'
Aggregation.sum("{u}score")     // Generates 'SUM (u.score) AS sum_u_score'

All Aggregation builders can have and extra parameter that is used to sed a custom alias to the aggregation

querybuilder's People

Contributors

augustoccesar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

querybuilder's Issues

Table markdown

A queried Table can be defined as:

Markdown String => GENERATES => Formatted String for Query
users{u} => users u

Write examples of usage

Write concise examples of usage for each function of the SelectBuilder and for each separated objects like Condition, Join, etc.
It should be written in separated .md files for each object, and one general .md for full examples.

Aggregation and Group By

Create a way to make group by aggregations on the SelectBuilder with something like:

new SelectBuilder()
             .select(Aggregation.count("{u}id"))
             .groupBy("{u}city");

Or create it outside and append to SelectBuilder.

Aggregation agg = Aggregation.count("{u}id").groupBy("{u}city");
new SelectBuilder()
             .aggregation(agg);

Union all

I would like to see the possibility to have union all in query builder. Union all with pagination. Is it possible? Thanks in advance.

Select multiple fields for same table

Create a way to select multiple fields for the same table to avoid something like:

.select("{u}name", "{u}age", "{u}username")

And be able to do something like:

.select(SameTableColumns.tableAlias("u").fields("name", "age", "username"))

Union and Union All

There should be a way to create a SelectBuilder that combines multiple SelectBuilder's and create a Union or Union All clause. Something like:

SelectBuilder sb1 = new SelectBuilder().select("{u}name").from("users{u}").where(Condition.eq("{u}name", "Augusto"));
SelectBuilder sb2 = new SelectBuilder().select("{u}name").from("users{u}").where(Condition.eq("{u}name", "Teste");

new SelectBuilder().union(sb1, sb2);
//OR
new SelectBuilder().unionAll(sb1, sb2);

Define limit to the query

SelectBuilder should contain a method to limit the result size

new SelectBuilder().limit(10) // Limit the query results to 10

Create new README.md

Create a new README.md with the description of created functions until 2.1.0 , installation guide and license.

Modularize Database Support

Initially, QueryBuilder is built to support MySQL. But its support should be modularized to the user to be able to select which database is going to use the queries, and so developers can create modules for each database

Ways to create Condition

The Condition object should be created as:

Condition.eq("{u}name", "Augusto")

Considering the possible comparisons:

Condition.EQUALS // Condition.eq(field, value)
Condition.DIFFERENT// Condition.neq(field, value)
Condition.IN // Conditon.in(field, listValue)
Condition.IS_NULL // Condition.isNull(field)
Condition.IS_NOT_NULL // Condition.isNotNull(field)
Condition.LIKE // Condition.like(field, value)
Condition.NOT_LIKE // Condition.nlike(field, value)
Condition.GREATER_THAN // Condition.gt(field, value)
Condition.GREATER_THAN_OR_EQUAL // Condition.gte(field, value)
Condition.LESS_THAN // Condition.lt(field, value)
Condition.LESS_OR_EQUAL // Condition.lte(field, value)

Column markdown

A queried Column can be defined as:

Markdown String => GENERATES => Formatted String for Query
{u}name => u.name AS u_name
{u}name{custom_alias} => u.name AS custom_alias
{u}name{_} => u.name
{u}*name => DISTINCT u.name

Replace Column custom .build(boolean, boolean)

Replace the custom .build() method existing for Column for the new method .sqlColumnRepresentation that build only the column prefix (if present) and name

column.build(false, false)
// Will become
column.sqlColumnRepresentation()

Unit Tests

Create unit tests for existing classes

Ways to create Order

There should be a way to define an order to the results of the SelectBuilder

new Order().by("{u}id", Order.DESC);
Order.orderBy("{u}id", Order.ASC);
Order.orderDesc("{u}id");
Order.orderAsc("{u}id");

Select without table alias

In Android Sqlite we doesn't need the table alias in the select ".select("i.name")".
Fix it ASAP, thanks

Kotlin code

Rewrite the project using kotlin, both for study propose and for better code.

Ways to create Join

A Join should be created in one of the many ways:

new Join(Join.LEFT).table("user_profile{up}").on("{u}id", "{up}user_id");

new Join(Join.LEFT).table("user_profile{up}").on("{u}id = {up}user_id");

new Join(Join.INNER, "user_profile{up}", "{u}id", "{up}user_id" )

new Join(Join.INNER, "user_profile{up}", "{u}id = {up}user_id" )

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.