├── .gitignore ├── README.md ├── memory ├── pom.xml └── src ├── main └── java │ └── io │ └── github │ └── eikefs │ └── sql │ └── provider │ ├── Provider.java │ ├── database │ └── Database.java │ ├── orm │ ├── Model.java │ ├── ORM.java │ └── annotations │ │ ├── Field.java │ │ └── Table.java │ └── query │ ├── Query.java │ ├── TableQuery.java │ ├── field │ └── TableField.java │ └── type │ ├── OrderType.java │ └── WhereType.java └── test └── java └── io └── github └── eikefs └── sql └── provider └── test ├── QueriesTest.java └── orm ├── ModelTests.java ├── ORMTest.java └── User.java /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | target 3 | *.iml -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # sql-provider 2 | A easy way to use SQL on Java. Create new connections and easy queries. 3 | 4 | # Creating database-connections 5 | 6 | ```java 7 | Database database = Provider.getInstance().submit(url, user, password); // Or Provider.getInstance().submit(url) 8 | ``` 9 | 10 | # Creating tables 11 | 12 | ```java 13 | Database#update(new TableQuery() 14 | .name("users", true) 15 | .fields(new TableField() 16 | .name("id") 17 | .type("int") 18 | .size(8) 19 | .autoIncrement()) 20 | .primary("id")); 21 | ``` 22 | 23 | And others examples you may get on `QueriesTest.java` file, on test folder. 24 | 25 | # How to install 26 | 27 | ```xml 28 | 29 | jitpack.io 30 | https://jitpack.io 31 | 32 | ``` 33 | 34 | ```xml 35 | 36 | com.github.eikefs 37 | sql-provider 38 | 1.0.1 39 | 40 | ``` 41 | 42 | -------------------------------------------------------------------------------- /memory: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eikefs/sql-provider/7ac00d565364573f5114feaa7ae7ffe3e95da7a2/memory -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | io.github.eikefs 8 | sql-provider 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 13 | org.apache.maven.plugins 14 | maven-compiler-plugin 15 | 16 | 8 17 | 8 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | org.mockito 26 | mockito-all 27 | 2.0.2-beta 28 | test 29 | 30 | 31 | 32 | junit 33 | junit 34 | 4.13.1 35 | test 36 | 37 | 38 | 39 | mysql 40 | mysql-connector-java 41 | 8.0.16 42 | 43 | 44 | 45 | org.xerial 46 | sqlite-jdbc 47 | 3.8.11.2 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /src/main/java/io/github/eikefs/sql/provider/Provider.java: -------------------------------------------------------------------------------- 1 | package io.github.eikefs.sql.provider; 2 | 3 | import io.github.eikefs.sql.provider.database.Database; 4 | 5 | import java.sql.Connection; 6 | import java.sql.DriverManager; 7 | 8 | public class Provider { 9 | 10 | private Provider() {} 11 | 12 | private static final Provider instance = new Provider(); 13 | 14 | public static Provider getInstance() { 15 | return instance; 16 | } 17 | 18 | private Connection newConnection(String url) { 19 | try { 20 | Class.forName("org.sqlite.JDBC"); 21 | 22 | url = url.startsWith("jdbc:sqlite:") ? url : "jdbc:sqlite:" + url; 23 | 24 | return DriverManager.getConnection(url); 25 | } catch (Exception e) { 26 | e.printStackTrace(); 27 | } 28 | 29 | return null; 30 | } 31 | 32 | private Connection newConnection(String url, String user, String pass) { 33 | try { 34 | Class.forName("com.mysql.cj.jdbc.Driver"); 35 | 36 | url = url.startsWith("jdbc:mysql://") ? "jdbc:mysql://" + url : url; 37 | 38 | return DriverManager.getConnection(url, user, pass); 39 | } catch (Exception e) { 40 | e.printStackTrace(); 41 | } 42 | 43 | return null; 44 | } 45 | 46 | public Database submit(String url) { 47 | return new Database(newConnection(url)); 48 | } 49 | 50 | public Database submit(String url, String user, String pass) { 51 | return new Database(newConnection(url, user, pass)); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/io/github/eikefs/sql/provider/database/Database.java: -------------------------------------------------------------------------------- 1 | package io.github.eikefs.sql.provider.database; 2 | 3 | import io.github.eikefs.sql.provider.query.Query; 4 | 5 | import java.lang.reflect.Constructor; 6 | import java.sql.Connection; 7 | import java.sql.PreparedStatement; 8 | import java.sql.ResultSet; 9 | import java.util.*; 10 | import java.util.concurrent.CompletableFuture; 11 | 12 | public class Database { 13 | 14 | private final Connection connection; 15 | 16 | public Database(Connection connection) { 17 | this.connection = Objects.requireNonNull(connection); 18 | } 19 | 20 | public CompletableFuture> query(Query query) { 21 | return query(query.raw()); 22 | } 23 | 24 | public CompletableFuture> query(String query) { 25 | return CompletableFuture.supplyAsync(() -> querySync(query)); 26 | } 27 | 28 | public List querySync(Query query) { 29 | return querySync(query.raw()); 30 | } 31 | 32 | public List querySync(String query) { 33 | List objects = new ArrayList<>(); 34 | 35 | try (PreparedStatement statement = connection.prepareStatement(query); 36 | ResultSet resultSet = statement.executeQuery()) { 37 | 38 | while (resultSet.next()) { 39 | for (int field = 1; field <= resultSet.getMetaData().getColumnCount(); field++) { 40 | objects.add(resultSet.getObject(field)); 41 | } 42 | } 43 | 44 | } catch (Exception e) { 45 | e.printStackTrace(); 46 | } 47 | 48 | return objects; 49 | } 50 | 51 | public CompletableFuture update(Query query) { 52 | return update(query.raw()); 53 | } 54 | 55 | public CompletableFuture update(String query) { 56 | return CompletableFuture.supplyAsync(() -> { 57 | updateSync(query); 58 | 59 | return null; 60 | }); 61 | } 62 | 63 | public void updateSync(Query query) { 64 | updateSync(query.raw()); 65 | } 66 | 67 | public void updateSync(String query) { 68 | try (PreparedStatement statement = connection.prepareStatement(query)) { 69 | statement.executeUpdate(); 70 | } catch (Exception e) { 71 | e.printStackTrace(); 72 | } 73 | } 74 | 75 | public CompletableFuture build(Class type, String query) { 76 | return CompletableFuture.supplyAsync(() -> buildSync(type, query)); 77 | } 78 | 79 | public T buildSync(Class type, String query) { 80 | Constructor constructor = type.getConstructors()[0]; 81 | List data = querySync(query); 82 | 83 | try { 84 | return (T) constructor.newInstance(data.toArray()); 85 | } catch (Exception e) { 86 | e.printStackTrace(); 87 | } 88 | 89 | return null; 90 | } 91 | 92 | public void shutdown() { 93 | try { 94 | if (!connection.isClosed()) connection.close(); 95 | } catch (Exception e) { 96 | e.printStackTrace(); 97 | } 98 | } 99 | 100 | } 101 | -------------------------------------------------------------------------------- /src/main/java/io/github/eikefs/sql/provider/orm/Model.java: -------------------------------------------------------------------------------- 1 | package io.github.eikefs.sql.provider.orm; 2 | 3 | import io.github.eikefs.sql.provider.database.Database; 4 | import io.github.eikefs.sql.provider.query.Query; 5 | 6 | import java.util.Map; 7 | import java.util.concurrent.CompletableFuture; 8 | import java.util.stream.Collectors; 9 | 10 | /** 11 | * This class provides an easy mode to create models with methods create, 12 | * update and delete that will make the binding with Provider. 13 | */ 14 | public abstract class Model { 15 | 16 | private final String tableName; 17 | private long id; 18 | 19 | protected Model(String tableName) { 20 | this.tableName = tableName; 21 | } 22 | 23 | /** 24 | * This will update the model data, filtering if the values aren't null. 25 | * That will make easier to update data in database. 26 | * 27 | * Example: 28 | * 29 | * ```java 30 | * 31 | * Database database; // your database instance. 32 | * Model model; // your model instance. 33 | * 34 | * // ImmutableMap in this example is from google's guava. 35 | * // That will only update the name, that isn't null. 36 | * model.update(database, ImmutableMap.builder() 37 | * .put("name", "New name") 38 | * .put("email", null) 39 | * .build()) 40 | * 41 | * ``` 42 | * 43 | * @param database The database connection 44 | * @param data The data that you want to change 45 | * @return the result of query 46 | */ 47 | public CompletableFuture update(final Database database, final Map data) { 48 | final Map filteredData = data.entrySet() 49 | .stream() 50 | .filter(entry -> entry.getValue() != null) 51 | .collect(Collectors.toMap( 52 | Map.Entry::getKey, 53 | Map.Entry::getValue 54 | )); 55 | 56 | return database.update(new Query() 57 | .from(tableName) 58 | .update() 59 | .where("id", id) 60 | .sets(filteredData)); 61 | } 62 | 63 | /** 64 | * This will delete the current model using id as the searcher 65 | * 66 | * @param database The database connection 67 | * @return the result of query 68 | */ 69 | public CompletableFuture delete(final Database database) { 70 | return database.update(new Query() 71 | .from(tableName) 72 | .delete() 73 | .where("id", id)); 74 | } 75 | 76 | /** 77 | * @return The model's id 78 | */ 79 | public long getId() { 80 | return id; 81 | } 82 | 83 | /** 84 | * Sets the model's id 85 | * 86 | * @param id New id 87 | */ 88 | public void setId(final long id) { 89 | this.id = id; 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/main/java/io/github/eikefs/sql/provider/orm/ORM.java: -------------------------------------------------------------------------------- 1 | package io.github.eikefs.sql.provider.orm; 2 | 3 | import io.github.eikefs.sql.provider.orm.annotations.Field; 4 | import io.github.eikefs.sql.provider.orm.annotations.Table; 5 | 6 | import java.util.Arrays; 7 | 8 | public class ORM { 9 | 10 | public static String create(Class tableClass) { 11 | StringBuilder sb = new StringBuilder(); 12 | 13 | if (!tableClass.isAnnotationPresent(Table.class)) { 14 | throw new IllegalArgumentException("Cannot create a create query from a not-queryable class."); 15 | } 16 | 17 | Table table = tableClass.getAnnotation(Table.class); 18 | 19 | sb.append("create table "); 20 | 21 | if (table.unique()) sb.append("if not exists "); 22 | 23 | sb.append("`") 24 | .append(table.name()) 25 | .append("` ("); 26 | 27 | Arrays.stream(tableClass.getDeclaredFields()) 28 | .filter((field) -> field.isAnnotationPresent(Field.class)) 29 | .forEach((field) -> { 30 | Field tableField = field.getAnnotation(Field.class); 31 | 32 | sb.append("`") 33 | .append(field.getName()) 34 | .append("` "); 35 | 36 | sb.append(tableField.type()); 37 | 38 | if (tableField.size() > 0) { 39 | sb.append(" (").append(tableField.size()).append(") "); 40 | } 41 | 42 | if (tableField.unique()) sb.append("unique "); 43 | if (!tableField.nullable()) sb.append("not null "); 44 | if (tableField.autoIncrement()) sb.append("auto_increment "); 45 | 46 | sb.append(","); 47 | }); 48 | 49 | sb.append("primary key (`" + table.primary() + "`)"); 50 | 51 | return sb.toString().trim() + ");"; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/io/github/eikefs/sql/provider/orm/annotations/Field.java: -------------------------------------------------------------------------------- 1 | package io.github.eikefs.sql.provider.orm.annotations; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | @Retention(RetentionPolicy.RUNTIME) 9 | @Target(ElementType.FIELD) 10 | public @interface Field { 11 | 12 | boolean nullable() default true; 13 | boolean unique() default false; 14 | boolean autoIncrement() default false; 15 | String type() default "varchar"; 16 | int size() default -1; 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/io/github/eikefs/sql/provider/orm/annotations/Table.java: -------------------------------------------------------------------------------- 1 | package io.github.eikefs.sql.provider.orm.annotations; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | @Retention(RetentionPolicy.RUNTIME) 9 | @Target(ElementType.TYPE) 10 | public @interface Table { 11 | 12 | String name() default ""; 13 | String primary() default ""; 14 | boolean unique() default false; 15 | 16 | } 17 | 18 | -------------------------------------------------------------------------------- /src/main/java/io/github/eikefs/sql/provider/query/Query.java: -------------------------------------------------------------------------------- 1 | package io.github.eikefs.sql.provider.query; 2 | 3 | import io.github.eikefs.sql.provider.query.type.OrderType; 4 | import io.github.eikefs.sql.provider.query.type.WhereType; 5 | 6 | import java.util.Map; 7 | import java.util.Map.Entry; 8 | 9 | public class Query { 10 | 11 | private final StringBuilder stringBuilder; 12 | 13 | public Query() { 14 | this.stringBuilder = new StringBuilder(); 15 | } 16 | 17 | public Query(String s) { 18 | this.stringBuilder = new StringBuilder(s); 19 | } 20 | 21 | public String raw() { 22 | return stringBuilder.toString().trim() + ";"; 23 | } 24 | 25 | public Query select(String... fields) { 26 | stringBuilder.append("select "); 27 | 28 | for (int index = 0; index < fields.length; index++) { 29 | String field = '`' + fields[index] + '`'; 30 | 31 | if (index + 1 < fields.length) field += ','; 32 | 33 | stringBuilder.append(field); 34 | } 35 | 36 | stringBuilder 37 | .append(" ") 38 | .append("from"); 39 | 40 | return this; 41 | } 42 | 43 | public Query selectAll() { 44 | stringBuilder.append("select * from"); 45 | 46 | return this; 47 | } 48 | 49 | public Query from(String table) { 50 | table = '`' + table + '`'; 51 | 52 | stringBuilder.append(" ").append(table).append(" "); 53 | 54 | return this; 55 | } 56 | 57 | public Query update() { 58 | stringBuilder.append("update"); 59 | 60 | return this; 61 | } 62 | 63 | public Query sets(Map sets) { 64 | stringBuilder.append("set "); 65 | 66 | for (Entry entry : sets.entrySet()) { 67 | String set = '`' + entry.getKey() + '`'; 68 | 69 | stringBuilder.append(set).append("=").append(entry.getValue()); 70 | } 71 | 72 | stringBuilder.append(" "); 73 | 74 | return this; 75 | } 76 | 77 | public Query where(String key, Object value) { 78 | return where(key, WhereType.EQUALS, value); 79 | } 80 | 81 | public Query where(String key, String type, Object value) { 82 | key = " `" + key + "` "; 83 | String val = " '" + value + "'"; 84 | 85 | stringBuilder 86 | .append("where") 87 | .append(key) 88 | .append(type) 89 | .append(val); 90 | 91 | return this; 92 | } 93 | 94 | public Query where(String key, WhereType type, Object value) { 95 | return where(key, type.getContext(), value); 96 | } 97 | 98 | public Query and() { 99 | stringBuilder 100 | .append(" ") 101 | .append("and") 102 | .append(" "); 103 | 104 | return this; 105 | } 106 | 107 | public Query orderBy(String field, OrderType orderType) { 108 | field = " `" + field + "` "; 109 | 110 | stringBuilder 111 | .append("order by ") 112 | .append(field) 113 | .append(" ") 114 | .append(orderType.getContext()); 115 | 116 | return this; 117 | } 118 | 119 | public Query insert(String into, Object... values) { 120 | stringBuilder 121 | .append("insert into ") 122 | .append(into) 123 | .append(" values ("); 124 | 125 | for (int index = 0; index < values.length; index++) { 126 | String value = "'" + values[index] + "'"; 127 | 128 | if (index + 1 < values.length) value += ','; 129 | 130 | stringBuilder.append(value); 131 | } 132 | 133 | stringBuilder.append(")"); 134 | 135 | return this; 136 | } 137 | 138 | public Query limit(int limit) { 139 | stringBuilder 140 | .append("limit") 141 | .append(" ") 142 | .append(limit); 143 | 144 | return this; 145 | } 146 | 147 | public Query drop(String name, String type) { 148 | stringBuilder 149 | .append("drop ") 150 | .append(type) 151 | .append(" ") 152 | .append(name); 153 | 154 | return this; 155 | } 156 | 157 | public Query delete(String... sets) { 158 | stringBuilder.append("delete "); 159 | 160 | for (int index = 0; index < sets.length; index++) { 161 | String set = "`" + sets[index] + "`"; 162 | 163 | if (index + 1 < sets.length) set += ", "; 164 | 165 | stringBuilder.append(set); 166 | } 167 | 168 | return this; 169 | } 170 | 171 | public StringBuilder getStringBuilder() { 172 | return stringBuilder; 173 | } 174 | 175 | } 176 | -------------------------------------------------------------------------------- /src/main/java/io/github/eikefs/sql/provider/query/TableQuery.java: -------------------------------------------------------------------------------- 1 | package io.github.eikefs.sql.provider.query; 2 | 3 | import io.github.eikefs.sql.provider.query.field.TableField; 4 | 5 | public class TableQuery extends Query { 6 | 7 | private final StringBuilder builder = getStringBuilder(); 8 | 9 | public TableQuery fields(TableField... fields) { 10 | builder.append(" ("); 11 | 12 | for (int index = 0; index < fields.length; index++) { 13 | builder.append(fields[index].build()); 14 | 15 | if (index + 1 < fields.length) builder.append(", "); 16 | } 17 | 18 | return this; 19 | } 20 | 21 | public TableQuery primary(String fieldName) { 22 | builder.append(",") 23 | .append("primary key (") 24 | .append(fieldName) 25 | .append(")"); 26 | 27 | return this; 28 | } 29 | 30 | public TableQuery foreign(String fieldName, String reference, String tableReference) { 31 | fieldName = '`' + fieldName + '`'; 32 | reference = '`' + reference + '`'; 33 | 34 | builder.append("foreign key ") 35 | .append(fieldName) 36 | .append(" references ") 37 | .append(tableReference) 38 | .append(" (") 39 | .append(reference) 40 | .append(")"); 41 | 42 | return this; 43 | } 44 | 45 | public TableQuery name(String name, boolean unique) { 46 | builder.append("create table "); 47 | 48 | if (unique) builder.append("if not exists "); 49 | 50 | builder.append(name); 51 | 52 | return this; 53 | } 54 | 55 | public TableQuery name(String name) { 56 | return name(name, false); 57 | } 58 | 59 | @Override 60 | public String raw() { 61 | return builder.toString().trim() + ");"; 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/io/github/eikefs/sql/provider/query/field/TableField.java: -------------------------------------------------------------------------------- 1 | package io.github.eikefs.sql.provider.query.field; 2 | 3 | public class TableField { 4 | 5 | private boolean nullable = false; 6 | private boolean unique = false; 7 | private boolean autoIncrement = false; 8 | private String name; 9 | private String type; 10 | private int size; 11 | 12 | public static TableField get() { 13 | return new TableField(); 14 | } 15 | 16 | public TableField nullable() { 17 | this.nullable = true; 18 | 19 | return this; 20 | } 21 | 22 | public TableField name(String name) { 23 | this.name = name; 24 | 25 | return this; 26 | } 27 | 28 | public TableField type(String type) { 29 | this.type = type; 30 | 31 | return this; 32 | } 33 | 34 | public TableField size(int size) { 35 | this.size = size; 36 | 37 | return this; 38 | } 39 | 40 | public TableField unique() { 41 | this.unique = true; 42 | 43 | return this; 44 | } 45 | 46 | public TableField autoIncrement() { 47 | this.autoIncrement = true; 48 | 49 | return this; 50 | } 51 | 52 | public String build() { 53 | StringBuilder sb = new StringBuilder(); 54 | name = "`" + name + "` "; 55 | 56 | sb.append(name) 57 | .append(type) 58 | .append("(") 59 | .append(size) 60 | .append(") "); 61 | 62 | if (autoIncrement) sb.append("auto_increment "); 63 | if (unique) sb.append("unique "); 64 | if (!nullable) sb.append("not null "); 65 | 66 | return sb.toString().trim(); 67 | } 68 | 69 | 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/io/github/eikefs/sql/provider/query/type/OrderType.java: -------------------------------------------------------------------------------- 1 | package io.github.eikefs.sql.provider.query.type; 2 | 3 | public enum OrderType { 4 | 5 | ASCENDING(0, "asc"), 6 | DESCENDING(1, "desc"); 7 | 8 | private final int index; 9 | private final String context; 10 | 11 | OrderType(int index, String context) { 12 | this.index = index; 13 | this.context = context; 14 | } 15 | 16 | public int getIndex() { 17 | return index; 18 | } 19 | 20 | public String getContext() { 21 | return context; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/io/github/eikefs/sql/provider/query/type/WhereType.java: -------------------------------------------------------------------------------- 1 | package io.github.eikefs.sql.provider.query.type; 2 | 3 | public enum WhereType { 4 | 5 | EQUALS(0, "="), 6 | LIKE(1, "like"), 7 | BIGGEST_THAN(2, ">"), 8 | LOWER_THAN(3, "<"), 9 | BIG_EQUALS(4, ">="), 10 | LOW_EQUALS(5, "<="); 11 | 12 | private final int index; 13 | private final String context; 14 | 15 | WhereType(int index, String context) { 16 | this.index = index; 17 | this.context = context; 18 | } 19 | 20 | public String getContext() { 21 | return context; 22 | } 23 | 24 | public int getIndex() { 25 | return index; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/test/java/io/github/eikefs/sql/provider/test/QueriesTest.java: -------------------------------------------------------------------------------- 1 | package io.github.eikefs.sql.provider.test; 2 | 3 | import io.github.eikefs.sql.provider.Provider; 4 | import io.github.eikefs.sql.provider.database.Database; 5 | import io.github.eikefs.sql.provider.query.Query; 6 | import io.github.eikefs.sql.provider.query.TableQuery; 7 | import io.github.eikefs.sql.provider.query.field.TableField; 8 | import io.github.eikefs.sql.provider.test.orm.User; 9 | 10 | import java.util.List; 11 | import java.util.concurrent.ExecutionException; 12 | 13 | public class QueriesTest { 14 | 15 | public static void main(String[] args) throws ExecutionException, InterruptedException { 16 | Database database = Provider.getInstance().submit("memory"); 17 | 18 | // Creating tables 19 | database.updateSync( 20 | new TableQuery() 21 | .name("users") 22 | .fields(TableField.get() 23 | .name("id") 24 | .type("int") 25 | .size(8), 26 | TableField.get() 27 | .name("name") 28 | .type("varchar") 29 | .size(255)) 30 | .primary("id")); 31 | 32 | // Inserting data 33 | database.updateSync(new Query().insert("users", 1, "eike")); 34 | 35 | // Getting the data 36 | List data = database.querySync(new Query() 37 | .select("id", "name") 38 | .from("users")); 39 | 40 | for (Object o : data) { 41 | System.out.println(o); 42 | } 43 | 44 | User user = database.build(User.class, new Query() 45 | .selectAll() 46 | .from("users") 47 | .where("id", 1) 48 | .raw()).get(); 49 | 50 | System.out.println("ID: " + user.getId() + ", Name: " + user.getName()); 51 | 52 | database.updateSync(new Query().drop("users", "table")); 53 | 54 | database.shutdown(); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/test/java/io/github/eikefs/sql/provider/test/orm/ModelTests.java: -------------------------------------------------------------------------------- 1 | package io.github.eikefs.sql.provider.test.orm; 2 | 3 | import io.github.eikefs.sql.provider.database.Database; 4 | import io.github.eikefs.sql.provider.orm.Model; 5 | import io.github.eikefs.sql.provider.query.Query; 6 | import junit.framework.TestCase; 7 | 8 | import java.util.HashMap; 9 | import java.util.Map; 10 | 11 | import static java.util.concurrent.CompletableFuture.runAsync; 12 | import static org.mockito.Matchers.*; 13 | import static org.mockito.Mockito.*; 14 | 15 | public final class ModelTests extends TestCase { 16 | 17 | // Dummy class 18 | private static class User extends Model { 19 | public User() { 20 | super("users"); 21 | } 22 | } 23 | 24 | public void testShouldExecuteQueryCorrectlyWhenUpdateModel() { 25 | final Database databaseMock = mock(Database.class); 26 | final Model model = new User(); 27 | 28 | when(databaseMock.update(any(Query.class))).thenReturn(runAsync(doNothing())); 29 | 30 | final Map fakeData = new HashMap<>(); 31 | fakeData.put("name", "New name"); 32 | fakeData.put("email", null); 33 | 34 | model.update(databaseMock, fakeData); 35 | 36 | verify(databaseMock, times(1)).update(any(Query.class)); 37 | } 38 | 39 | public void testShouldExecuteQueryCorrectlyWhenDeleteModel() { 40 | final Database databaseMock = mock(Database.class); 41 | final Model model = new User(); 42 | 43 | when(databaseMock.update(any(Query.class))).thenReturn(runAsync(doNothing())); 44 | 45 | model.delete(databaseMock); 46 | 47 | verify(databaseMock, times(1)).update(any(Query.class)); 48 | } 49 | 50 | private static Runnable doNothing() { 51 | return () -> { 52 | // do nothing 53 | }; 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /src/test/java/io/github/eikefs/sql/provider/test/orm/ORMTest.java: -------------------------------------------------------------------------------- 1 | package io.github.eikefs.sql.provider.test.orm; 2 | 3 | public class ORMTest { 4 | 5 | public static void main(String[] args) { 6 | long current = System.currentTimeMillis(); 7 | 8 | System.out.println(User.create()); 9 | 10 | System.out.println(System.currentTimeMillis() - current); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/io/github/eikefs/sql/provider/test/orm/User.java: -------------------------------------------------------------------------------- 1 | package io.github.eikefs.sql.provider.test.orm; 2 | 3 | import io.github.eikefs.sql.provider.orm.ORM; 4 | import io.github.eikefs.sql.provider.orm.annotations.Field; 5 | import io.github.eikefs.sql.provider.orm.annotations.Table; 6 | 7 | @Table(name = "user", primary = "id") 8 | public class User extends ORM { 9 | 10 | @Field(type = "int", size = 8, nullable = false) 11 | private int id; 12 | 13 | @Field(size = 32, nullable = false) 14 | private String name; 15 | 16 | public User(int id, String name) { 17 | this.id = id; 18 | this.name = name; 19 | } 20 | 21 | public int getId() { 22 | return id; 23 | } 24 | 25 | public String getName() { 26 | return name; 27 | } 28 | 29 | public static String create() { 30 | return create(User.class); 31 | } 32 | 33 | } 34 | --------------------------------------------------------------------------------