Skip to content

Commit

Permalink
fix: always primary
Browse files Browse the repository at this point in the history
  • Loading branch information
dynomake committed Jan 12, 2024
1 parent f0cae7f commit 70b3320
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 25 deletions.
6 changes: 3 additions & 3 deletions .github/DEPEND.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ repositories {
dependencies {
// other depends
implementation 'works.naifu:wonsi:1.0.0'
implementation 'io.github.dynomake:wonsi:1.0.2'
}
```

Expand All @@ -33,8 +33,8 @@ Depend:
```xml

<dependency>
<groupId>works.naifu</groupId>
<groupId>io.github.dynomake</groupId>
<artifactId>wonsi</artifactId>
<version>1.0.0</version>
<version>1.0.2</version>
</dependency>
```
13 changes: 11 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ plugins {
id 'maven-publish'
}

group 'works.naifu'
version '1.0.0'
group 'io.github.dynomake'
version '1.0.2'
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
Expand Down Expand Up @@ -39,6 +39,15 @@ publishing {
publications {
gpr(MavenPublication) {
from(components.java)

pom {
licenses {
license {
name = 'MIT License'
url = 'https://github.com/dynomake/wonsi/blob/master/LICENSE'
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package net.wonsi.api.result;

import java.util.Collection;
import java.util.Optional;

public interface ExecutedReturningAction<T> {

Collection<T> getAll();
T findFirst();
Optional<T> findFirst();
boolean isEmpty();
}
4 changes: 4 additions & 0 deletions src/main/java/net/wonsi/column/ColumnUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -36,9 +37,12 @@ public class ColumnUtil {
TYPE_MAP.put(float.class, new FloatType());
TYPE_MAP.put(double.class, new FloatType());
TYPE_MAP.put(BigDecimal.class, new FloatType());
TYPE_MAP.put(Collection.class, new JsonType());
}

public ColumnType get(@NonNull Class<?> javaType) {
if (javaType.isAssignableFrom(Collection.class)) return new JsonType();

return TYPE_MAP.getOrDefault(javaType, new VarcharType());
}
}
9 changes: 9 additions & 0 deletions src/main/java/net/wonsi/column/type/JsonType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.wonsi.column.type;

public class JsonType implements ColumnType {

@Override
public String convertToString(int length) {
return "json";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Optional;
import java.util.function.Function;

public class RealExecutedReturningAction<T> implements ExecutedReturningAction<T> {
Expand Down Expand Up @@ -33,8 +34,8 @@ public Collection<T> getAll() {
}

@Override
public T findFirst() {
return response.stream().findFirst().orElse(null);
public Optional<T> findFirst() {
return response.stream().findFirst();
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/wonsi/proxy/table/RealWonsiTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ 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" : "");
arguments.add(column.name() + ' ' + ColumnUtil.get(field.getType()).convertToString(column.length()) + (field.getAnnotation(WonsiPrimary.class) != null ? " PRIMARY KEY" : ""));
}

StringBuilder query = new StringBuilder("CREATE TABLE IF NOT EXISTS `");
Expand Down
20 changes: 5 additions & 15 deletions src/test/java/net/wonsi/test/mysql/MySqlUtil.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
package net.wonsi.test.mysql;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

@UtilityClass
public class MySqlUtil {

@SneakyThrows
public Connection create() {
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setServerName("localhost");
dataSource.setPort(3306);
dataSource.setUser("root");
dataSource.setPassword("nope");
dataSource.setDatabaseName("wonsitesting");

dataSource.setEncoding("UTF-8");
dataSource.setAutoReconnect(true);
dataSource.setUseSSL(true);
try {
return dataSource.getConnection();
} catch (SQLException e) {
throw new RuntimeException(e);
}
var url = "jdbc:h2:mem:";
return DriverManager.getConnection(url);
}
}
2 changes: 1 addition & 1 deletion src/test/java/net/wonsi/test/repo/UserRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public User getByLogin(String login) {
.where(Condition.is("login", login))
.limit(1)
.sync()
.findFirst();
.findFirst().get();
}

public Collection<User> getAll() {
Expand Down

0 comments on commit 70b3320

Please sign in to comment.