https://github.com/ivanceras/keywordsql
All the SQL keywords in Java
https://github.com/ivanceras/keywordsql
Last synced: 10 months ago
JSON representation
All the SQL keywords in Java
- Host: GitHub
- URL: https://github.com/ivanceras/keywordsql
- Owner: ivanceras
- License: apache-2.0
- Created: 2014-12-07T17:04:47.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2015-01-14T08:23:12.000Z (almost 11 years ago)
- Last Synced: 2024-05-23T01:33:05.896Z (over 1 year ago)
- Language: Java
- Size: 465 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
keywordSQL
==========
[](https://travis-ci.org/ivanceras/keywordSQL)
[](https://maven-badges.herokuapp.com/maven-central/com.ivanceras/keywordSQL)
I list down all the keywords of Major Database vendors and [convert](https://github.com/ivanceras/keywordSQL/blob/master/src/main/java/com/ivanceras/keyword/sql/generator/KeywordGenerator.java) them into methods in a class.
Now you can call these methods as though you are writing a series of texts in a Stringbuilder.
##How much complexity of SQL statements I can write?
* As complex as you want it to get.
Example: List down products that is sold by somebody that is within 5 k.m from me.
```java
//import static com.ivanceras.keyword.sql.KeywordsStatics.*;
//import static com.ivanceras.keyword.sql.SQLStatics.*;
@Test
public void test1(){
double myLatitude = 10.0000d;
double myLongitude = 123.000d;
double km = 5.0d;
double earthRadius = 6371.0d;
SQL sql = WITH("distanceSQL", SELECT()
.ACOS(
SIN(RADIANS(VALUE( myLatitude )))
.MULTIPLY() .SIN(RADIANS(FIELD( "latitude" )))
.PLUS() .COS(RADIANS(VALUE( myLatitude )))
.MULTIPLY() .COS(RADIANS(FIELD( "latitude" )))
.MULTIPLY() .COS(RADIANS(FIELD( "longitude" ))
.MINUS() .RADIANS(VALUE( myLongitude )))
).MULTIPLY() .VALUE(earthRadius)
.AS("computed_distance")
.FIELD("address.address_id")
.FROM("address")
,"withinKm", SELECT("computed_distance", "address_id")
.FROM("distanceSQL")
.WHERE("computed_distance").LESS_THAN(km)
)
.SELECT("*")
.FIELD("computed_distance")
.FROM("distanceSQL")
.INNER_JOIN("user_info")
.ON("distanceSQL.address_id", "user_info.address_id")
.INNER_JOIN("users")
.ON("user_info.user_id", "users.user_id")
.INNER_JOIN("product")
.ON("users.user_id", "product.owner_id");
Breakdown bk = sql.build();
System.out.println("SQL: "+bk.getSql());
System.out.println("\nparameters: "+Arrays.asList(bk.getParameters()));
}
```
The output
```sql
SQL:
WITH distanceSQL AS (
SELECT ACOS ( SIN ( RADIANS ( ? ) )
* SIN ( RADIANS ( latitude ) )
+ COS ( RADIANS ( ? ) )
* COS ( RADIANS ( latitude ) )
* COS ( RADIANS ( longitude )
- RADIANS ( ? ) ) )
* ? AS computed_distance , address.address_id
FROM address
)
,withinKm AS
(
SELECT computed_distance , address_id
FROM distanceSQL WHERE computed_distance < ?
)
SELECT * , computed_distance
FROM distanceSQL
INNER JOIN user_info
ON distanceSQL.address_id = user_info.address_id
INNER JOIN users
ON user_info.user_id = users.user_id
INNER JOIN product
ON users.user_id = product.owner_id
parameters: [10.0, 10.0, 123.0, 6371.0, 5.0]
```
##Advantages
* No SQL typo anymore, since IDE checks it for you.
* SQL injection resistance while writing the values as it where supposed to be.
##Supported SQL keywords
* postgresql
* sqlite
* oracle
* mysql
* mariadb
##Roadmap
* thorough support for functions
* Make a lightweight version of Table/Column generators from ivanceras ORM, this way all text will be check by the IDE