mirror of
https://github.com/russ-p/sqlbuilder.git
synced 2025-12-14 09:14:23 +00:00
Compare commits
7 Commits
9976c65fb3
...
dependabot
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1c86d3fafa | ||
| 8f23030c2c | |||
| 5d1c92425e | |||
|
|
00c72a9cfe | ||
| a723e4cf2b | |||
| 20ee041f01 | |||
| 2c6562637e |
@@ -9,7 +9,14 @@ steps:
|
||||
- name: mvnrepo
|
||||
path: /root/.m2
|
||||
commands:
|
||||
- mvn install
|
||||
- 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
|
||||
|
||||
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
|
||||
````
|
||||
31
pom.xml
31
pom.xml
@@ -20,14 +20,41 @@
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<version>4.13.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>1.4.194</version>
|
||||
<version>2.1.210</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</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>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
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) {
|
||||
super(query);
|
||||
@@ -15,23 +15,9 @@ public class FromBuilder extends AbstractQueryBuilder<SelectQuery> {
|
||||
return new WhereBuilder(query, condition);
|
||||
}
|
||||
|
||||
public JoinBuilder join(String table) {
|
||||
return innerJoin(table);
|
||||
@Override
|
||||
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 JoinBuilder(SelectQuery query, String joinType, String table) {
|
||||
JoinBuilder(SelectQuery query, JoinType joinType, String table) {
|
||||
super(query);
|
||||
query.addJoin("\n " + joinType.toUpperCase() + " JOIN " + table);
|
||||
join(joinType, table);
|
||||
}
|
||||
|
||||
public JoinConditionBuilder on(String 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;
|
||||
|
||||
public class JoinConditionBuilder extends AbstractQueryBuilder<SelectQuery> {
|
||||
public class JoinConditionBuilder extends AbstractQueryBuilder<SelectQuery> implements JoinMixin {
|
||||
|
||||
public JoinConditionBuilder(SelectQuery query, String condition) {
|
||||
super(query);
|
||||
@@ -11,23 +11,9 @@ public class JoinConditionBuilder extends AbstractQueryBuilder<SelectQuery> {
|
||||
return new WhereBuilder(query, condition);
|
||||
}
|
||||
|
||||
public JoinBuilder join(String table) {
|
||||
return innerJoin(table);
|
||||
@Override
|
||||
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(", ")));
|
||||
|
||||
if (!join.isEmpty()) {
|
||||
sb.append("\n");
|
||||
sb.append(join.stream().collect(Collectors.joining("\n ")));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user