Coder Social home page Coder Social logo

exasol / sql-statement-builder Goto Github PK

View Code? Open in Web Editor NEW
25.0 13.0 4.0 4.01 MB

Build SQL statements safely without string concatenation

License: MIT License

Java 100.00%
sql java exasol builder-pattern compile-time-checking exasol-integration foundation-library

sql-statement-builder's Introduction

SQL Statement Builder

Build Status Maven Central – Exasol SQL Statement Builder

Quality Gate Status

Security Rating Reliability Rating Maintainability Rating Technical Debt

Code Smells Coverage Duplicated Lines (%) Lines of Code

The Exasol SQL Statement Builder abstracts programmatic creation of SQL statements and is intended to replace ubiquitous string concatenation solutions which make the code hard to read and are prone to error and security risks.

Goals:

  1. Foster clean and readable code
  2. Allow for thorough validation of dynamic parts
  3. Detect as many errors as possible at compile time
  4. Don't repeat yourself (DRY)
  5. Allow extension for different SQL dialects

In a Nutshell

The following example gives you an idea about what you can do with the SQL Statement Builder. Check our user guide for more details.

Select select = StatementFactory.getInstance().select()
    .field("fieldA", "tableA.fieldB", "tableB.*");
select.from().table("schemaA.tableA");
select.limit(10);
StringRendererConfig config = StringRendererConfig.builder().quoteIdentifiers(true).build();
SelectRenderer renderer = new SelectRenderer(config);
select.accept(renderer);
String sql = renderer.render();

Table of Contents

Information for Users

"Users" from the perspective of the sql-statement-builder are developers integrating the module into their own software.

Information for Developers

sql-statement-builder's People

Contributors

anastasiiasergienko avatar jakobbraun avatar kaklakariada avatar morazow avatar pj-spoelders avatar redcatbear avatar

Stargazers

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

Watchers

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

sql-statement-builder's Issues

Always add day to string representation of INTERVAL DAY TO SECOND

Description

Writing to a column of type DAY TO SECOND fails if the day is omitted.

Steps to Reproduce

  1. Try writing a string that contains the time only (e.g. 00:00:01.001)
  2. Error occurs

Expected behavior

Rendered string representation can be used in INSERT command without modification.

Environment

  • SQL Statement Builder: 0.1.1

Add support for special cases of scalar functions

Currently unsupported or partly supported scalar function:

String Functions:

Date/Time Functions:

Conversion Functions:

Functions for Hierarchical Queries:

Other Scalar Functions

We need to decide which of them we want to support and implement them if needed.

Support `WHERE` in `MERGE`

Situation

Basic MERGE support was introduced with #51. Support for WHERE is still missing in the THEN UPDATE, THEN DELETE and THEN INSERT clauses.

Acceptance Criteria

  1. WHERE supported in THEN UPDATE, THEN DELETE and THEN INSERT.

Refactor `BooleanExpressionVisitor`

Currently, BooleanExpressionVisitor uses visit and leave. I've got the feeling that it would be better style to only use visit in combination with recursive calls, but I'm not sure about that.

Fluent interface broken

the fluent programming pattern is not used consequently any more:

Designed was:

final Select select = StatementFactory.getInstance()
    .select()
    .field("fieldA", "tableA.fieldB", "tableB.*")
    .from()
    .table("schemaA.tableA")
    .limit(10);

Now it is: (see #64 )

final Select select = StatementFactory.getInstance()
        .select();
select.field("fieldA", "tableA.fieldB", "tableB.*")
        .from()
        .table("schemaA.tableA");
select.limit(10);

Refactor tests in com.exasol.sql.expression.function.exasol

Currently the tests in com.exasol.sql.expression.function.exasol directly use the select().function( API. These tests should however only test the API of their corresponding class. The select() API should be tested in its corresponding test.

Fix Travis CI Link in Readme

Description

Currently the Travis CI link in the Readme use EXASOL. It should be changed to exasol.

Steps to Reproduce

  1. Update readme by renaming EXASOL -> exasol

Expected behavior

Should show travis ci results on readme instead of unknown.

Add Sonar

As a developer,
I want the CI build to automatically run static code analysis on all my branches,
so that I catch code smells that I overlooked as early as possible.

Support column references in value expressions

Situation

Column references — i.e. names of columns in an expression — are valid value expressions. That means that a column reference can stand where a value expression is expected.

Example:

MERGE INTO ... AS ...
USING ... AS "SRC" ON ...
WHEN NOT MATCHED THEN INSERT ("ID", ...) VALUES ("SRC"."ID", ...)

In the example above the content of VALUES is a list of value expressions. Column references are useful in that scenario.

Acceptance Criteria

  1. Column references are accepted where value expressions are required.

Add support for unsupported Aggregate and Analytic functions

Aggregate Functions

Unsupported

Partly supported without keywords and OVER clause

Analytic Functions

Unsupported

Partly supported without keywords and OVER clause

Other

User guide is 404

The user guide can't be opened. Page 404 appears. What's the problem?

ColumnReference not quoted

@Test
    void testQuotedIdentifiers() {
        final StringRendererConfig config = StringRendererConfig.builder().quoteIdentifiers(true).build();
        assertThat(this.select.where(eq(ExpressionTerm.stringLiteral("foo"), ColumnReference.of("test"))),
                rendersWithConfigTo(config, "SELECT * FROM \"person\" WHERE 'foo' = \"test\""));
    }
Expected: SELECT * FROM "person" WHERE 'foo' = "test"
     but: SELECT * FROM "person" WHERE 'foo' = test

Add support for UDFs

Add support for UDF calls like:

SELECT sample_simple(id, user_name, page_visits, 20)
 EMITS (id INT, user_name VARCHAR(100), PAGE_VISITS int)
 FROM people;

In contrast to a normal function an UDF needs the EMITS clause with a DDL of the result columns.

Injection safe interface for building literals

Right now now there is no possibility to build injection save statements directly. The only possible way is to use SQL prepared statements and place holders. This does however not work for all use cases. For example in a virtual schema the push down statement is just an string, not a SQL Statement. Thus place holders can't be used.

Travis CI fails to build with openjdk11

Description

I did a small commit with one change in README.md file and after that Travis CI failed to build the project with openjdk11.

Steps to Reproduce

  1. Commit into a new branch with pull request
  2. Build with openjdk8 passed.
  3. Build with openjdk11 failed.

Expected behavior

I expected build to be successfully passed.

Environment

  • Java Version: openjdk11

Additional context

Error message:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 13.766 s
[INFO] Finished at: 2019-01-30T11:43:14Z
[INFO] Final Memory: 23M/84M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-javadoc-plugin:3.0.1:jar (attach-javadocs) on project sql-statement-builder: MavenReportException: Error while generating Javadoc:
[ERROR] Exit code: 1 - javadoc: error - The code being documented uses modules but the packages defined in https://docs.oracle.com/javase/8/docs/api/ are in the unnamed module.
[ERROR]
[ERROR] Command line was: /home/travis/openjdk11/bin/javadoc @options @packages
[ERROR]
[ERROR] Refer to the generated Javadoc files in '/home/travis/build/exasol/sql-statement-builder/target/apidocs' dir.
[ERROR]
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
The command "mvn clean install" exited with 1.

Implement "create table" functionality

As a software developer, I want to use the sql-statement-builder to create new tables in my database.

Acceptance criteria

  1. "CREATE TABLE table name table element list" is implemented.

Release to maven central

Is your feature request related to a problem? Please describe.
No.

Describe the solution you'd like
I want to use the sql-statement-builder by including as a dependency from another project.

Describe alternatives you've considered
None.

Additional context
Should be released to maven central and easy to reference from another project.

INSERT documentation missing

Situation

Documentation for the INSERT statement is missing.

Acceptance Criteria

  1. Documentation for the INSERT statement exists.

Improve `MERGE` documentation and example

Situation

It is confusing that the MERGE documentation does not explain that you have to pick at least one of the merge strategies for a full merge statement.

Acceptance Criteria

  • MERGE strategy constraints are explained
  • Examples are improved.

Support `FROM (VALUES('a', 'b))`

Situation

If you have a proprietary data format and want to convert the contents into a SELECT statement that list all contents, the VALUES list constructor is very helpful.

Acceptance Criteria

  1. SSB support
    SELECT * FROM (VALUES('a', 'b'))

Support MERGE Statement

Situation

The MERGE statement provides a combination of INSERT, UPDATE and DELETE.

It is especially useful in cases where new data should be combined with data that already exists in a table.

Acceptance Criteria

  1. MERGE supported without WHEN

Add Exasol functions

As a user, I want to use sql-statement-builder to build Exasol SQL functions(Aggregate, Scalar etc)

Example in Readme broken

in the example in the README.md in line StringRendererConfig.builder().useQuotes().build(); the useQuotes() method does not exist.

Replace validation in DropSchema class

Problem

At the moment we don't have validation for SQL statements. In the DropSchema class private void validateCascadeAndRestrict() was created, but it should be replaced with a proper validation.

Java primitive types as literals

Situation

Currently primitive types are not all supported where literals are expected (e.g. in value expressions). For the sake of uniformity and convenience all Java primitive types should be supported as value literals.

Acceptance

  1. All primitive Java types are supported as value literals

Support negative intervals

Is your feature request related to a problem? Please describe.
If you calculate how many days ago some data was created, you get a negative integer as result.

Describe the solution you'd like
The SQL query builder must support rendering and parsing negative intervals.

Refactor `ValueExpressionVisitor`

With adding more and more functions, ValueExpressionVisitor gets longish. We could split it up into two visitors. One for functions and one for literals, or similar.

Review and fix outdated documentation

Description

The documentation has not been updated for a while and contains at least a few outdated (wrong) examples:

#65
#61

We have to review and update all documentation files.

Fix JavaDoc in combination with Java 11

Description

When running a Travis CI build with Java 11 we get the following error:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-javadoc-plugin:3.0.1:jar (attach-javadocs) on project sql-statement-builder: MavenReportException: Error while generating Javadoc: 
[ERROR] Exit code: 1 - javadoc: error - The code being documented uses modules but the packages defined in https://docs.oracle.com/javase/8/docs/api/ are in the unnamed module.
[ERROR] 
[ERROR] Command line was: /home/travis/openjdk11/bin/javadoc @options @packages
[ERROR] 
[ERROR] Refer to the generated Javadoc files in '/home/travis/build/exasol/sql-statement-builder/target/apidocs' dir.

Steps to Reproduce

  1. Enable OpenJDK11 in .travis.yml
  2. Run build in Travis CI

Expected behavior

Build succeeds.

Environment

  • SQL Statement Builder: 0.3.0 SNAPSHOT
  • Java Version: openjdk version "11.0.2" 2019-01-15
    OpenJDK Runtime Environment 18.9 (build 11.0.2+9)
    OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)

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.