Skip to content

Commit

Permalink
loook at me, i did it!
Browse files Browse the repository at this point in the history
  • Loading branch information
suuft (valeriy) committed Mar 5, 2023
1 parent 03e1240 commit 12ce790
Show file tree
Hide file tree
Showing 14 changed files with 231 additions and 80 deletions.
8 changes: 4 additions & 4 deletions .github/DEPEND.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## | `Lithe DI - Add as depend:`
## | `Wonsi - Add as depend:`
Here is how to add this framework depending on your project.
### | `Gradle`:
If you use Gradle with Groovy, then here is an example of adding dependencies:
Expand All @@ -13,7 +13,7 @@ repositories {
dependencies {
// other depends
implementation 'works.naifu:lithe:2.1.1'
implementation 'works.naifu:wonsi:1.0.0'
}
```

Expand All @@ -34,7 +34,7 @@ Depend:

<dependency>
<groupId>works.naifu</groupId>
<artifactId>lithe</artifactId>
<version>2.1.1</version>
<artifactId>wonsi</artifactId>
<version>1.0.0</version>
</dependency>
```
103 changes: 71 additions & 32 deletions .github/USAGE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## | `Lithe DI - Usage:`
*You can found full example-code in:* [LINK](https://github.com/suuft/Lithe/tree/master/src/test/java/net/lithe/test)
*You can found full example-code in:* [LINK](https://github.com/suuft/wonsi/tree/master/src/test/java/net/wonsi/test)

First, lets create an application class that ll run the JVM. E.i. - Main:
```java
Expand All @@ -9,55 +9,94 @@ public class Main {
}
}
```
Lets create an abstract class (an interface, but you can create abstract class or a simple class) that ll have abstract methods for working with some data. I have this as metric, so this class ll count the number of sessions:
Create an object that we will store. For example a user:
```java
public interface SessionCounter {
int getSessions();
void incrementSessions();
// lombok helpers annotation, not required (setters, getters, constructors generation)
@Setter
@Getter
@AllArgsConstructor
// wonsi required annotation
@Table("app_users")
public class User {

@WonsiPrimary
@WonsiColumn(name = "identifier")
private long identifier;

@WonsiColumn(name = "vk")
private String vkLink;

@WonsiColumn(name = "tg")
private String tgId;

public static User deserialize(ResultSet data) {
try {
return new User(data.getInt("identifier"), data.getString("vk"), data.getString("tg"));
} catch (Exception exception) {
throw new RuntimeException(exception);
}
}
}
```
Okay come write it s implementation:
Cool, now lets create a repository that would manage users (CRUD operations):
```java
public class MySessionCounter implements SessionCounter {
@RequiredArgsConstructor
public class UserRepo {

private int sessions;
private final WonsiTable<User> table;

@Override
public int getSessions() {
return sessions;
public User getByLogin(String login) {
return table
.select()
.where(Condition.is("login", login))
.limit(1)
.sync()
.findFirst();
}

@Override
public void incrementSessions() {
sessions++;
public Collection<User> getAll() {
return table
.select()
.sync()
.getAll();
}
}
```
Good job! You should write a class that register dependencies, for further using - Adept, it will extends from `SimpleAdept`:
```java
public class CounterAdept extends SimpleAdept {
@Override
public void install() {
register(SessionCounter.class, MySessionCounter.class);

public void save(User user) {
table.insert()
.updateOnDuplicate()
.data(map -> {
map.put("identifier", user.getIdentifier());
map.put("vk", user.getVkLink());
map.put("tg", user.getTgId());
})
.sync();
}

public void delete(int identifier) {
table.delete()
.where(Condition.is("identifier", identifier))
.limit(1)
.sync();
}
}
```
Done! So you can create an injector through the `Lithe` class and use the registered dependencies. I wrote an example in the Main class.
Super, now we can create an instance of the repository and do anything! Just look:
```java
public class Main {
public class TestBootstrap {

public static void main(String[] args) {
Injector injector = Lithe.createInjector(new CounterAdept());
System.out.println("Current sessions: " + injector.getInstance(SessionCounter.class).getSessions());
Wonsi wonsi = WonsiFactory.createInstance(YOU_SQL_DATABASE_CONNECTION);
WonsiTable<User> table = wonsi.getTable(User.class, User::deserialize);

System.out.println("Try increment #1");
injector.getInstance(SessionCounter.class).incrementSessions();
UserRepo repository = new UserRepo(table);

System.out.println("Try increment #2");
injector.getInstance(SessionCounter.class).incrementSessions();
// okay, i have friends
repository.save(new User(1, "https://vk.com/suuft", "@fuuft"));
repository.save(new User(2, "https://vk.com/otherman", null));

System.out.println("Current sessions: " + injector.getInstance(SessionCounter.class).getSessions());
// OOOHH NOOO!!! Otherman cheated on me and we re not friends now.
repository.delete(2);
}
}
```
The final output will show us the number 2. So you did everything right. Cool!
Well done, I hope this development will help you :heart:
14 changes: 10 additions & 4 deletions LEARN.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
## | `Lithe DI - Learn project`
## | `Wonsi - Learn project`
Hi! This is a learn project, if you have idea or know how to refactor the code, be sure to send me pull requests, issues. Im not adequate criticism.
### | `Road-Map`:
1) Add Automatic-Generation adept.
2) Add Auto-Injection Depends.
3) Refactor all.
1) Add Automatic Serialization
2) Refactor all.
### | `Used patterns`:
1) Builder
2) Factory
3) Template Method
4) Mediator
5) Command
6) Facade
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
## | `Wonsi Objest Mapping`
A small & fast but convenient framework for working with dependencies. I'll add dependency injection soon.

The best, free, fast ORM that will simplify working with SQL-like databases. WITHOUT USING REFLECTION (used only when initializing the table). In project contains protection about sql injections.
### | `Links`:
* [About this learn project](https://github.com/suuft/Lithe/blob/master/LEARN.md)
* [Add as Depend](https://github.com/suuft/Lithe/blob/master/.github/DEPEND.md)
* [Usage Example](https://github.com/suuft/Lithe/blob/master/.github/USAGE.md)
* [About this learn project](https://github.com/suuft/wonsi/blob/master/LEARN.md)
* [Add as Depend](https://github.com/suuft/wonsi/blob/master/.github/DEPEND.md)
* [Usage Example](https://github.com/suuft/wonsi/blob/master/.github/USAGE.md)
13 changes: 13 additions & 0 deletions src/main/java/net/wonsi/api/request/UpdateRequest.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
package net.wonsi.api.request;

import lombok.NonNull;
import net.wonsi.util.Condition;

import java.util.Map;
import java.util.function.Consumer;

public interface UpdateRequest extends Request {

UpdateRequest where(Condition condition);

UpdateRequest data(@NonNull Consumer<Map<String, Object>> insertData);

UpdateRequest limit(int limit);

}
6 changes: 2 additions & 4 deletions src/main/java/net/wonsi/api/table/WonsiTable.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package net.wonsi.api.table;

import lombok.NonNull;
import net.wonsi.api.request.DeleteRequest;
import net.wonsi.api.request.InsertRequest;
import net.wonsi.api.request.Request;
import net.wonsi.api.request.SelectRequest;
import net.wonsi.api.request.*;
import net.wonsi.api.result.ExecutedReturningAction;

public interface WonsiTable<T> {

SelectRequest<T> select();
InsertRequest<T> insert();
DeleteRequest delete();
UpdateRequest update();

ExecutedReturningAction<T> customSelect(@NonNull String query);
Request customQuery(@NonNull String query);
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/net/wonsi/proxy/request/RealDeleteRequest.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package net.wonsi.proxy.request;

import net.wonsi.api.request.DeleteRequest;
import net.wonsi.proxy.result.RealExecutedReturningAction;
import net.wonsi.util.Condition;
import net.wonsi.util.Debugger;
import net.wonsi.util.ExecutorUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;

public class RealDeleteRequest implements DeleteRequest {

Expand Down Expand Up @@ -50,7 +47,7 @@ public void sync() {

statement.setInt(1, limit);

Debugger.execute(statement, connection);
ExecutorUtil.execute(statement, connection);
} catch (Exception exception) {
exception.printStackTrace();
}
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/net/wonsi/proxy/request/RealInsertRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

import com.google.common.base.Joiner;
import net.wonsi.api.request.InsertRequest;
import net.wonsi.proxy.result.RealExecutedReturningAction;
import net.wonsi.util.Debugger;
import net.wonsi.util.ExecutorUtil;
import net.wonsi.util.StringUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.HashMap;
import java.util.Map;
import java.util.StringJoiner;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;

Expand Down Expand Up @@ -77,7 +75,7 @@ public void sync() {
}
}

Debugger.execute(statement, connection);
ExecutorUtil.execute(statement, connection);
} catch (Exception exception) {
exception.printStackTrace();
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/net/wonsi/proxy/request/RealSelectRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import net.wonsi.api.result.ExecutedReturningAction;
import net.wonsi.proxy.result.RealExecutedReturningAction;
import net.wonsi.util.Condition;
import net.wonsi.util.Debugger;
import net.wonsi.util.ExecutorUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
Expand Down Expand Up @@ -40,7 +41,7 @@ public ExecutedReturningAction<T> sync() {

statement.setInt(1, limit);

return new RealExecutedReturningAction<T>(Debugger.getResult(statement, connection), deserializer);
return new RealExecutedReturningAction<T>(ExecutorUtil.getResult(statement, connection), deserializer);
} catch (Exception exception) {
exception.printStackTrace();
return null;
Expand Down
75 changes: 75 additions & 0 deletions src/main/java/net/wonsi/proxy/request/RealUpdateRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package net.wonsi.proxy.request;

import com.google.common.base.Joiner;
import net.wonsi.api.request.UpdateRequest;
import net.wonsi.util.Condition;
import net.wonsi.util.ExecutorUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;

public class RealUpdateRequest implements UpdateRequest {

private final Connection connection;
private final String tableName;
private String condition;
private int limit;
private Map<String, Object> data;

public RealUpdateRequest(Connection connection, String tableName) {
this.connection = connection;
this.tableName = tableName;
condition = "0";
limit = 10;
}


@Override
public CompletableFuture<Void> async() {
return CompletableFuture.runAsync(this::sync);
}

@Override
public void sync() {
try {
PreparedStatement statement = connection.prepareStatement("UPDATE " + tableName + Joiner.on("= ?,").join(data.keySet()) + " = ? LIMIT = ?");

int currentArgument = 1;

for (Object o : data.values()) {
statement.setObject(currentArgument, o);
currentArgument++;
}

statement.setObject(currentArgument, limit);

ExecutorUtil.execute(statement, connection);
} catch (Exception exception) {
exception.printStackTrace();
}
}

@Override
public UpdateRequest where(Condition condition) {
this.condition = condition.toString();
return this;
}

@Override
public UpdateRequest data(Consumer<Map<String, Object>> insertData) {
data = new HashMap<>();
insertData.accept(data);

return this;
}

@Override
public UpdateRequest limit(int limit) {
this.limit = limit;
return this;
}
}
Loading

0 comments on commit 12ce790

Please sign in to comment.