Git Product home page Git Product logo

squiggle-sql's Introduction

Build Status via Travis CI Coverage Status Dependency Status Maven Central

Squiggle is a little Java library for dynamically generating SQL SELECT statements. It's sweet spot is for applications that need to build up complicated queries with criteria that changes at runtime. Ordinarily it can be quite painful to figure out how to build this string. Squiggle takes much of this pain away.

The code for Squiggle is intentionally clean and simple. Rather than provide support for every thing you could ever do with SQL, it provides support for the most common situations and allows you to easily modify the source to suit your needs.

#Features Concise and intuitive API. Simple code, so easy to customize. No dependencies on classes outside of JDK 1.5 Small, lightweight, fast. Generates clean SQL designed that is very human readable. Supports joins and sub-selects. Combine criteria with AND, OR and NOT operators. Supports functions in selects and WHERE criteria

Example Here's a very simple example:

SelectQuery select = new SelectQuery();

Table people = new Table("people");

select.addColumn(people, "firstname");
select.addColumn(people, "lastname");

select.addOrder(people, "age", Order.DESCENDING);

System.out.println(select);

Which produces:

SELECT
   people.firstname ,
   people.lastname
FROM
    people
ORDER BY
    people.age DESC

squiggle-sql's People

Contributors

andymoreland avatar gchauvet avatar gilgulim avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

squiggle-sql's Issues

Querstion: Support for UNION ALL and LEFT OUTER JOIN ON for SAP HANA

Do you support UNION and LEFT OUTER JOIN ON as well?

We are working with SAP HANA.

We have quite complex SQLs to create views driven by a config file. See here an example.

select a1.subject_uuid, a2.DT, measure_type, measure_value, measure_unit from 
(select distinct subject_uuid from clinical_audit_record where item_oid = 'VS_H_SCN.WEIGHT') as a1
left outer join
(select subject_uuid, item_value as DT
from clinical_audit_record where item_oid = 'VS_H_SCN.VS_DT' and audit_subcategory_name = 'Entered') as a2
on a1.subject_uuid = a2.subject_uuid
left outer join
(select subject_uuid, 'VS_H_SCN.WEIGHT' measure_type, item_value as measure_value
from clinical_audit_record where item_oid = 'VS_H_SCN.WEIGHT' and audit_subcategory_name = 'Entered') as a3 
on a1.subject_uuid = a3.subject_uuid
left outer join
(select subject_uuid, item_value as measure_unit
from clinical_audit_record where item_oid = 'VS_H_SCN.WEIGHT_UNIT' and audit_subcategory_name = 'Entered') as a4
on a1.subject_uuid = a4.subject_uuid
union all
select a1.subject_uuid, a2.DT, measure_type, measure_value, measure_unit from 
(select distinct subject_uuid from clinical_audit_record where item_oid = 'VS_H_SCN.SYSBP') as a1
left outer join
(select subject_uuid, item_value as DT
from clinical_audit_record where item_oid = 'VS_H_SCN.VS_DT' and audit_subcategory_name = 'Entered') as a2
on a1.subject_uuid = a2.subject_uuid
left outer join
(select subject_uuid, 'VS_H_SCN.SYSBP' measure_type, item_value as measure_value, 'mmHg' measure_unit
from clinical_audit_record where item_oid = 'VS_H_SCN.SYSBP' and audit_subcategory_name = 'Entered') as a3
on a1.subject_uuid = a3.subject_uuid

Major changes in code

Hi Guillaume

Squiggle is an incredible library that I was looking for. I plan to use it in my future projects. I hate ORM concept and everything that relies too much on magic of reflection. I just need a simple tool to build SQL queries with a standard OOD toolkit.

However, in attempt to use Squiggle I found some major issues that didn't allow me to complete my task. I've decided to tinker into the library and fix these issues. In the result of 2 days of work, it looks like I've reworked the whole code base. See my fork https://github.com/enepomnyaschih/squiggle-sql

I have the following questions:

  • Are you interested in merging this code? I'll open a pull request if you are.
  • If not, should I leave you and Joe Walnes as authors of the code even though 80% of code has been rewritten? Should I choose a different package name and maven artifact name?

Here is the log of changes:

  • Added PreparedStatement compilation support with SQL "?" query parameters. See StatementTest. Just replace MockStatementCompiler with JdbcStatementCompiler are you'll get a real JDBC PreparedStatement to work with.
  • Added conflict-free automated alias generation logic. Call "table.refer()" method to obtain a TableReference - a new alias to use in the query. The queries should be built with TableReferences now, not with Tables. See the updated TutorialTest.
  • Added ResultColumn class returned by addToSelection method. You can use its getIndex method to get the values from the ResultSet by index. You can also use it to order the results by a ResultColumn.
  • Added Literal.of method to build all kinds of supported literals with NullPointerException protection.
  • Added InsertQuery and UpdateQuery.
  • Added TypeCast selectable.
  • Slightly improved implementation of criterias, made it more coincise.
  • Unified and tested CollectionWriter algorithm.
  • Added Java 8 support with its related parameter types (such as java.time.Instant). Left legacy Java 6 version in java6legacy branch.

Here is some work that I plan to do in the future:

  • Escape table/column names that are SQL reserved words.
  • Add possibility to force used-defined table and result aliases to be used (this feature existed in the previous implementation, but I've decided to give alias auto-generation a higher value and priority than maintain this feature).
  • Add more parameter types.
  • Increase test coverage.

Thank you for your time.

Best regards,
Egor.

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.