-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
231 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/su/nightexpress/nightcore/database/sql/SQLUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package su.nightexpress.nightcore.database.sql; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class SQLUtils { | ||
|
||
@NotNull | ||
public static String escape(@NotNull String string) { | ||
if (string.isBlank()) return string; | ||
if (string.charAt(0) == '`') return string; | ||
|
||
return "`" + string + "`"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/main/java/su/nightexpress/nightcore/database/sql/query/AbstractQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package su.nightexpress.nightcore.database.sql.query; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class AbstractQuery { | ||
|
||
private final String sql; | ||
|
||
protected AbstractQuery(@NotNull String sql) { | ||
this.sql = sql; | ||
} | ||
|
||
@NotNull | ||
public String getSQL() { | ||
return sql; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/main/java/su/nightexpress/nightcore/database/sql/query/UpdateQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package su.nightexpress.nightcore.database.sql.query; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import su.nightexpress.nightcore.database.sql.SQLCondition; | ||
import su.nightexpress.nightcore.database.sql.SQLUtils; | ||
import su.nightexpress.nightcore.database.sql.SQLValue; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class UpdateQuery extends AbstractQuery { | ||
|
||
private final List<String> values; | ||
private final List<String> wheres; | ||
|
||
private UpdateQuery(@NotNull String sql, @NotNull List<String> values, @NotNull List<String> wheres) { | ||
super(sql); | ||
this.values = values; | ||
this.wheres = wheres; | ||
} | ||
|
||
@NotNull | ||
public static UpdateQuery create(@NotNull String table, @NotNull List<SQLValue> values) { | ||
return create(table, values, Collections.emptyList()); | ||
} | ||
|
||
@NotNull | ||
public static UpdateQuery create(@NotNull String table, @NotNull List<SQLValue> values, @NotNull List<SQLCondition> conditions) { | ||
List<String> columnNames = new ArrayList<>(); | ||
List<String> columnValues = new ArrayList<>(); | ||
List<String> whereColumns = new ArrayList<>(); | ||
List<String> whereValues = new ArrayList<>(); | ||
|
||
values.forEach(value -> { | ||
columnNames.add(value.getColumn().getNameEscaped() + " = ?"); | ||
columnValues.add(value.getValue()); | ||
}); | ||
|
||
conditions.forEach(condition -> { | ||
whereColumns.add(condition.getColumn().getNameEscaped() + " " + condition.getType().getOperator() + " ?"); | ||
whereValues.add(condition.getValue().getValue()); | ||
}); | ||
|
||
StringBuilder builder = new StringBuilder(); | ||
builder.append("UPDATE ").append(SQLUtils.escape(table)); | ||
builder.append(" SET ").append(String.join(",", columnNames)); | ||
if (!whereColumns.isEmpty()) { | ||
builder.append(" WHERE ").append(String.join(" AND ", whereColumns)); | ||
} | ||
|
||
String sql = builder.toString(); | ||
|
||
return new UpdateQuery(sql, columnValues, whereValues); | ||
} | ||
|
||
@NotNull | ||
public List<String> getValues() { | ||
return values; | ||
} | ||
|
||
@NotNull | ||
public List<String> getWheres() { | ||
return wheres; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters