mirror of
https://github.com/russ-p/sqlbuilder.git
synced 2025-12-14 17:14:24 +00:00
Compare commits
8 Commits
9f2d00619a
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 8f23030c2c | |||
| 5d1c92425e | |||
|
|
00c72a9cfe | ||
| a723e4cf2b | |||
| 20ee041f01 | |||
| 2c6562637e | |||
| 9976c65fb3 | |||
| f4079b5663 |
24
.drone.yml
Normal file
24
.drone.yml
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
kind: pipeline
|
||||||
|
name: default
|
||||||
|
|
||||||
|
steps:
|
||||||
|
|
||||||
|
- name: install
|
||||||
|
image: maven:3.6.1-jdk-8-slim
|
||||||
|
volumes:
|
||||||
|
- name: mvnrepo
|
||||||
|
path: /root/.m2
|
||||||
|
commands:
|
||||||
|
- mvn install deploy -Ddeployment.releases.id=$DEPREPO -Ddeployment.releases.url=$DEPREPOURL -Ddeployment.snapshots.id=$DEPREPO -Ddeployment.snapshots.url=$DEPREPOSNAPURL
|
||||||
|
environment:
|
||||||
|
DEPREPO:
|
||||||
|
from_secret: deprepo
|
||||||
|
DEPREPOURL:
|
||||||
|
from_secret: deprepourl
|
||||||
|
DEPREPOSNAPURL:
|
||||||
|
from_secret: depreposnapurl
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
- name: mvnrepo
|
||||||
|
host:
|
||||||
|
path: /media/data/storage/.m2
|
||||||
6
.gitignore
vendored
6
.gitignore
vendored
@@ -1 +1,7 @@
|
|||||||
|
#mvn
|
||||||
/target/
|
/target/
|
||||||
|
|
||||||
|
#Eclipse
|
||||||
|
.project
|
||||||
|
.classpath
|
||||||
|
.settings/
|
||||||
|
|||||||
27
README.md
Normal file
27
README.md
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
# sqlbuilder
|
||||||
|
simple sql builder
|
||||||
|
|
||||||
|
From
|
||||||
|
````java
|
||||||
|
String sql = Sql.select()
|
||||||
|
.column("id").as("col_id")
|
||||||
|
.column("code").as("col_code")
|
||||||
|
.from("test_table")
|
||||||
|
.where("id = 1")
|
||||||
|
.groupBy("id")
|
||||||
|
.orderBy("id").by("code").desc()
|
||||||
|
.toString();
|
||||||
|
````
|
||||||
|
to
|
||||||
|
````
|
||||||
|
SELECT
|
||||||
|
id AS col_id,
|
||||||
|
code AS col_code
|
||||||
|
FROM
|
||||||
|
test_table
|
||||||
|
WHERE
|
||||||
|
id = 1
|
||||||
|
GROUP BY
|
||||||
|
id
|
||||||
|
ORDER BY id, code DESC
|
||||||
|
````
|
||||||
29
pom.xml
29
pom.xml
@@ -20,7 +20,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
<version>4.12</version>
|
<version>4.13.1</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
@@ -30,4 +30,31 @@
|
|||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>${java.version}</source>
|
||||||
|
<target>${java.version}</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<distributionManagement>
|
||||||
|
<repository>
|
||||||
|
<id>${deployment.releases.id}</id>
|
||||||
|
<name>Releases</name>
|
||||||
|
<url>${deployment.releases.url}</url>
|
||||||
|
</repository>
|
||||||
|
<snapshotRepository>
|
||||||
|
<id>${deployment.snapshots.id}</id>
|
||||||
|
<name>Snapshots</name>
|
||||||
|
<url>${deployment.snapshots.url}</url>
|
||||||
|
</snapshotRepository>
|
||||||
|
</distributionManagement>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package com.github.russ_p.sqlbuilder.select;
|
package com.github.russ_p.sqlbuilder.select;
|
||||||
|
|
||||||
public class FromBuilder extends AbstractQueryBuilder<SelectQuery> {
|
public class FromBuilder extends AbstractQueryBuilder<SelectQuery> implements JoinMixin {
|
||||||
|
|
||||||
FromBuilder(SelectQuery query, String table) {
|
FromBuilder(SelectQuery query, String table) {
|
||||||
super(query);
|
super(query);
|
||||||
@@ -15,23 +15,9 @@ public class FromBuilder extends AbstractQueryBuilder<SelectQuery> {
|
|||||||
return new WhereBuilder(query, condition);
|
return new WhereBuilder(query, condition);
|
||||||
}
|
}
|
||||||
|
|
||||||
public JoinBuilder join(String table) {
|
@Override
|
||||||
return innerJoin(table);
|
public SelectQuery getQuery() {
|
||||||
|
return query;
|
||||||
}
|
}
|
||||||
|
|
||||||
public JoinBuilder innerJoin(String table) {
|
|
||||||
return new JoinBuilder(query, "inner", table);
|
|
||||||
}
|
|
||||||
|
|
||||||
public JoinBuilder leftJoin(String table) {
|
|
||||||
return new JoinBuilder(query, "left", table);
|
|
||||||
}
|
|
||||||
|
|
||||||
public JoinBuilder rightJoin(String table) {
|
|
||||||
return new JoinBuilder(query, "right", table);
|
|
||||||
}
|
|
||||||
|
|
||||||
public JoinBuilder crossJoin(String table) {
|
|
||||||
return new JoinBuilder(query, "cross", table);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,13 +2,41 @@ package com.github.russ_p.sqlbuilder.select;
|
|||||||
|
|
||||||
public class JoinBuilder extends AbstractQueryBuilder<SelectQuery> {
|
public class JoinBuilder extends AbstractQueryBuilder<SelectQuery> {
|
||||||
|
|
||||||
public JoinBuilder(SelectQuery query, String joinType, String table) {
|
JoinBuilder(SelectQuery query, JoinType joinType, String table) {
|
||||||
super(query);
|
super(query);
|
||||||
query.addJoin("\n " + joinType.toUpperCase() + " JOIN " + table);
|
join(joinType, table);
|
||||||
}
|
}
|
||||||
|
|
||||||
public JoinConditionBuilder on(String condition) {
|
public JoinConditionBuilder on(String condition) {
|
||||||
return new JoinConditionBuilder(query, condition);
|
return new JoinConditionBuilder(query, condition);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public JoinBuilder join(String table) {
|
||||||
|
return innerJoin(table);
|
||||||
|
}
|
||||||
|
|
||||||
|
public JoinBuilder innerJoin(String table) {
|
||||||
|
return join(JoinType.INNER, table);
|
||||||
|
}
|
||||||
|
|
||||||
|
public JoinBuilder leftJoin(String table) {
|
||||||
|
return join(JoinType.LEFT, table);
|
||||||
|
}
|
||||||
|
|
||||||
|
public JoinBuilder rightJoin(String table) {
|
||||||
|
return join(JoinType.RIGHT, table);
|
||||||
|
}
|
||||||
|
|
||||||
|
public JoinBuilder crossJoin(String table) {
|
||||||
|
return join(JoinType.CROSS, table);
|
||||||
|
}
|
||||||
|
|
||||||
|
public WhereBuilder where(String condition) {
|
||||||
|
return new WhereBuilder(query, condition);
|
||||||
|
}
|
||||||
|
|
||||||
|
private JoinBuilder join(JoinType joinType, String table) {
|
||||||
|
query.addJoin(" " + joinType.toString() + " JOIN " + table);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package com.github.russ_p.sqlbuilder.select;
|
package com.github.russ_p.sqlbuilder.select;
|
||||||
|
|
||||||
public class JoinConditionBuilder extends AbstractQueryBuilder<SelectQuery> {
|
public class JoinConditionBuilder extends AbstractQueryBuilder<SelectQuery> implements JoinMixin {
|
||||||
|
|
||||||
public JoinConditionBuilder(SelectQuery query, String condition) {
|
public JoinConditionBuilder(SelectQuery query, String condition) {
|
||||||
super(query);
|
super(query);
|
||||||
@@ -11,23 +11,9 @@ public class JoinConditionBuilder extends AbstractQueryBuilder<SelectQuery> {
|
|||||||
return new WhereBuilder(query, condition);
|
return new WhereBuilder(query, condition);
|
||||||
}
|
}
|
||||||
|
|
||||||
public JoinBuilder join(String table) {
|
@Override
|
||||||
return innerJoin(table);
|
public SelectQuery getQuery() {
|
||||||
|
return query;
|
||||||
}
|
}
|
||||||
|
|
||||||
public JoinBuilder innerJoin(String table) {
|
|
||||||
return new JoinBuilder(query, "inner", table);
|
|
||||||
}
|
|
||||||
|
|
||||||
public JoinBuilder leftJoin(String table) {
|
|
||||||
return new JoinBuilder(query, "left", table);
|
|
||||||
}
|
|
||||||
|
|
||||||
public JoinBuilder rightJoin(String table) {
|
|
||||||
return new JoinBuilder(query, "right", table);
|
|
||||||
}
|
|
||||||
|
|
||||||
public JoinBuilder crossJoin(String table) {
|
|
||||||
return new JoinBuilder(query, "cross", table);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package com.github.russ_p.sqlbuilder.select;
|
||||||
|
|
||||||
|
interface JoinMixin {
|
||||||
|
|
||||||
|
default JoinBuilder join(String table) {
|
||||||
|
return innerJoin(table);
|
||||||
|
}
|
||||||
|
|
||||||
|
default JoinBuilder innerJoin(String table) {
|
||||||
|
return new JoinBuilder(getQuery(), JoinType.INNER, table);
|
||||||
|
}
|
||||||
|
|
||||||
|
default JoinBuilder leftJoin(String table) {
|
||||||
|
return new JoinBuilder(getQuery(), JoinType.LEFT, table);
|
||||||
|
}
|
||||||
|
|
||||||
|
default JoinBuilder rightJoin(String table) {
|
||||||
|
return new JoinBuilder(getQuery(), JoinType.RIGHT, table);
|
||||||
|
}
|
||||||
|
|
||||||
|
default JoinBuilder crossJoin(String table) {
|
||||||
|
return new JoinBuilder(getQuery(), JoinType.CROSS, table);
|
||||||
|
}
|
||||||
|
|
||||||
|
SelectQuery getQuery();
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.github.russ_p.sqlbuilder.select;
|
||||||
|
|
||||||
|
enum JoinType {
|
||||||
|
|
||||||
|
INNER,
|
||||||
|
LEFT,
|
||||||
|
RIGHT,
|
||||||
|
CROSS,
|
||||||
|
|
||||||
|
}
|
||||||
@@ -79,6 +79,7 @@ class SelectQuery {
|
|||||||
sb.append(tables.stream().collect(Collectors.joining(", ")));
|
sb.append(tables.stream().collect(Collectors.joining(", ")));
|
||||||
|
|
||||||
if (!join.isEmpty()) {
|
if (!join.isEmpty()) {
|
||||||
|
sb.append("\n");
|
||||||
sb.append(join.stream().collect(Collectors.joining("\n ")));
|
sb.append(join.stream().collect(Collectors.joining("\n ")));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user