Skip to content

Commit

Permalink
feat: auto-publish
Browse files Browse the repository at this point in the history
  • Loading branch information
dynomake committed Feb 21, 2024
1 parent 70b3320 commit 5de6214
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 16 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Publish

on: push
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'zulu'
server-id: github
settings-path: ${{ github.workspace }}

- name: Run chmod to make gradlew executable
run: chmod +x ./gradlew

- name: Build with Gradle
uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1
with:
arguments: bootJar
- name: Deploy to prod
env:
deployUser: ${{ secrets.CLOJARS_USER }}
deployToken: ${{ secrets.CLOJARS_TOKEN }}
run: |
sshpass -v -p ${{ secrets.DEPLOY_PASSWORD }} scp -o StrictHostKeyChecking=no -P ${{ secrets.DEPLOY_PORT }} ${{ github.workspace }}/build/libs/backend.jar ${{ secrets.DEPLOY_USERNAME }}@${{ secrets.DEPLOY_HOST }}:/home/dynomake/vpn/
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'io.github.dynomake'
version '1.0.2'
version '1.0.4'
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
Expand All @@ -24,14 +24,15 @@ dependencies {
testImplementation group: 'mysql', name: 'mysql-connector-java', version: '5.1.47'
}


publishing {
repositories {
maven {
name = "clojars"
url = uri("https://clojars.org/repo")
credentials {
username = clojarsUserName
password = clojarsDeployToken
username = "$System.env.deployUser"
password = "$System.env.deployToken"
}
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/net/wonsi/api/table/WonsiTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public interface WonsiTable<T> {
DeleteRequest delete();
UpdateRequest update();

String getName();
ExecutedReturningAction<T> customSelect(@NonNull String query);
Request customQuery(@NonNull String query);

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/wonsi/column/type/IntType.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class IntType implements ColumnType {
@Override
public String convertToString(int length) {
if (length <= 5) return "tinyint";
if (length <= 6) return "mediumint";
if (length == 6) return "mediumint";
if (length < 10) return "int";
else return "bigint";
}
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/net/wonsi/proxy/request/RealInsertRequest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.wonsi.proxy.request;

import com.google.common.base.Joiner;
import lombok.val;
import net.wonsi.api.request.InsertRequest;
import net.wonsi.util.ExecutorUtil;
import net.wonsi.util.StringUtil;
Expand All @@ -20,12 +21,15 @@ public class RealInsertRequest<T> implements InsertRequest<T> {
private int limit;
private boolean fixDuplicate;
private Map<String, Object> data;
private final String primaryKeyName;

public RealInsertRequest(Connection connection, String tableName) {
public RealInsertRequest(Connection connection, String tableName, String primaryKeyName) {
this.connection = connection;
this.tableName = tableName;
this.primaryKeyName = primaryKeyName;
condition = "1";
limit = 10;

}

@Override
Expand Down Expand Up @@ -58,7 +62,14 @@ public CompletableFuture<Void> async() {
@Override
public void sync() {
try {
PreparedStatement statement = connection.prepareStatement("INSERT INTO " + tableName + " (" + Joiner.on(",").join(data.keySet()) + ") VALUES (" + StringUtil.repeat(",?", data.size()).replaceFirst(",", "")+ ")" + (fixDuplicate ? " ON DUPLICATE KEY UPDATE " + Joiner.on("= ?,").join(data.keySet()) + " = ?" : ""));
val mapWithoutPrimary = new HashMap<>(data);
if (fixDuplicate) {
mapWithoutPrimary.remove(primaryKeyName);
}
val end = (fixDuplicate ? " ON DUPLICATE KEY UPDATE " + Joiner.on("= ?,").join(mapWithoutPrimary.keySet()) + " = ?" : "");
val query = "INSERT INTO " + tableName + " (" + Joiner.on(",").join(data.keySet()) + ") VALUES (" + StringUtil.repeat(",?", data.size()).replaceFirst(",", "")+ ")" + end;
// System.out.println(query);
PreparedStatement statement = connection.prepareStatement(query);

int currentArgument = 1;

Expand All @@ -69,7 +80,7 @@ public void sync() {

if (fixDuplicate){
// currentArgument = currentArgument-2;
for (Object o : data.values()) {
for (Object o : mapWithoutPrimary.values()) {
statement.setObject(currentArgument, o);
currentArgument++;
}
Expand Down
24 changes: 17 additions & 7 deletions src/main/java/net/wonsi/proxy/table/RealWonsiTable.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package net.wonsi.proxy.table;

import lombok.AllArgsConstructor;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.val;
import net.wonsi.api.mapping.WonsiColumn;
import net.wonsi.api.mapping.WonsiPrimary;
import net.wonsi.api.request.*;
Expand All @@ -23,12 +24,13 @@
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;

@AllArgsConstructor
@RequiredArgsConstructor
public class RealWonsiTable<T> implements WonsiTable<T> {

private Connection connection;
private Function<ResultSet, T> deserializer;
private String tableName;
private final Connection connection;
private final Function<ResultSet, T> deserializer;
private final String tableName;
private String primaryKeyName;

@Override
public SelectRequest<T> select() {
Expand All @@ -37,7 +39,7 @@ public SelectRequest<T> select() {

@Override
public InsertRequest<T> insert() {
return new RealInsertRequest<T>(connection, tableName);
return new RealInsertRequest<T>(connection, tableName, primaryKeyName);
}

@Override
Expand All @@ -50,6 +52,11 @@ public UpdateRequest update() {
return new RealUpdateRequest(connection, tableName);
}

@Override
public String getName() {
return tableName;
}

@Override
public ExecutedReturningAction<T> customSelect(@NonNull String query) {
return new RealExecutedReturningAction<T>(ExecutorUtil.getResult(query, connection), deserializer);
Expand All @@ -76,7 +83,10 @@ public void createIfNotExits(Class<T> tclass) {

for (Field field : tclass.getDeclaredFields()) {
WonsiColumn column = field.getAnnotation(WonsiColumn.class);
arguments.add(column.name() + ' ' + ColumnUtil.get(field.getType()).convertToString(column.length()) + (field.getAnnotation(WonsiPrimary.class) != null ? " PRIMARY KEY" : ""));
val primary = (field.getAnnotation(WonsiPrimary.class) != null ? " PRIMARY KEY" : "");
arguments.add(column.name() + ' ' + ColumnUtil.get(field.getType()).convertToString(column.length()) + primary);
if (!primary.isEmpty())
primaryKeyName = column.name();
}

StringBuilder query = new StringBuilder("CREATE TABLE IF NOT EXISTS `");
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/net/wonsi/util/Condition.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ public class Condition {
private String condition;

public static Condition is(@NonNull String column, @NonNull Object value) {
return new Condition('`' + column + '`' + " = " + value);
if (value instanceof String)
return new Condition(column + " = " + "'" + value + "'");

return new Condition(column + " = " + value);
}

public static Condition over(@NonNull String column, @NonNull Object value) {
Expand Down

0 comments on commit 5de6214

Please sign in to comment.