Skip to content

Commit 8ec0bfe

Browse files
committed
1. Added support for PostgreSQL drivers.
2. Fix some bugs.
1 parent 424d641 commit 8ec0bfe

10 files changed

Lines changed: 195 additions & 158 deletions

File tree

README.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
## ORM-Java
7-
``ORM-Java`` is an ORM used for SQLite or MySQL databases. Using ``JDBC`` as the driver at the bottom level. It provides a simple and efficient API without the need to write a large number of SQL statements. You only need to know the basics of SQL to get started.
7+
``ORM-Java`` is an ORM used for SQLite, MySQL, PostgreSQL databases. Using ``JDBC`` as the driver at the bottom level. It provides a simple and efficient API without the need to write a large number of SQL statements. You only need to know the basics of SQL to get started.
88

99

1010
## Features
@@ -22,7 +22,7 @@ repositories {
2222
}
2323
2424
dependencies {
25-
implementation 'com.github.artbits:orm-java:2.0.0'
25+
implementation 'com.github.artbits:orm-java:2.1.0'
2626
}
2727
```
2828
Maven:
@@ -35,7 +35,7 @@ Maven:
3535
<dependency>
3636
<groupId>com.github.artbits</groupId>
3737
<artifactId>orm-java</artifactId>
38-
<version>2.0.0</version>
38+
<version>2.1.0</version>
3939
</dependency>
4040
```
4141

@@ -71,10 +71,7 @@ public class Book {
7171

7272
Connect to the database and load tables (automatically add tables, columns and index).
7373
```java
74-
Config config = Config.of(c -> {
75-
c.driver = Config.Driver.SQLITE;
76-
c.url = "jdbc:sqlite:example.db";
77-
});
74+
Config config = Config.of(c -> c.url = "jdbc:sqlite:example.db");
7875
DB db = DB.connect(config);
7976
db.tables(User.class, Book.class);
8077
```
@@ -173,7 +170,8 @@ int min2 = db.min(User.class, "age", "vip = ?", true).intValue();
173170

174171

175172
## Links
176-
+ Thanks:
173+
+ Thanks:
174+
+ [PostgreSQL JDBC Driver](https://github.com/pgjdbc/pgjdbc)
177175
+ [MySQL Connector/J](https://github.com/mysql/mysql-connector-j)
178176
+ [SQLite-JDBC](https://github.com/xerial/sqlite-jdbc)
179177
+ [QuickIO](https://github.com/artbits/quickio)

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
}
55

66
group = 'com.github.artbits.orm-java'
7-
version = '2.0.0'
7+
version = '2.1.0'
88

99
repositories {
1010
maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
@@ -14,6 +14,7 @@ repositories {
1414
dependencies {
1515
implementation 'org.apache.commons:commons-dbcp2:2.13.0'
1616
implementation 'com.mysql:mysql-connector-j:9.4.0'
17+
implementation 'org.postgresql:postgresql:42.7.11'
1718
implementation 'org.xerial:sqlite-jdbc:3.50.3.0'
1819

1920
testImplementation platform('org.junit:junit-bom:5.10.0')

src/main/java/com/github/artbits/orm/Config.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
import java.util.function.Consumer;
2020

2121
public final class Config {
22+
2223
public String url;
23-
public String driver;
2424
public String username;
2525
public String password;
2626
public int initSize = 30;
@@ -29,16 +29,6 @@ public final class Config {
2929
public int maxIdle = 20;
3030

3131

32-
public interface Driver {
33-
String SQLITE = "org.sqlite.JDBC";
34-
String MYSQL = "com.mysql.cj.jdbc.Driver";
35-
// String POSTGRESQL = "org.postgresql.Driver";
36-
// String SQLSERVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
37-
// String ORACLE = "oracle.jdbc.driver.OracleDriver";
38-
// String H2 = "org.h2.Driver";
39-
}
40-
41-
4232
private Config() { }
4333

4434

@@ -47,4 +37,25 @@ public static Config of(Consumer<Config> consumer) {
4737
consumer.accept(config);
4838
return config;
4939
}
40+
41+
42+
int type() {
43+
if (url.contains("sqlite")) {
44+
return Type.SQLite;
45+
} else if (url.contains("mysql")) {
46+
return Type.MySQL;
47+
} else if (url.contains("postgresql")) {
48+
return Type.PostgreSQL;
49+
} else {
50+
return 0;
51+
}
52+
}
53+
54+
55+
interface Type {
56+
int SQLite = 1;
57+
int MySQL = 2;
58+
int PostgreSQL = 3;
59+
}
60+
5061
}

src/main/java/com/github/artbits/orm/Core.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
final class Core implements DB {
2323

2424
private final Runner runner;
25-
private final Config config;
25+
public static Config config;
2626

2727

2828
Core(Config config) {
29-
this.config = config;
30-
runner = new Runner(config);
29+
Core.config = config;
30+
runner = new Runner();
3131
Runtime.getRuntime().addShutdownHook(new Thread(this::close));
3232
}
3333

@@ -40,7 +40,7 @@ public void close() {
4040

4141
@Override
4242
public void tables(Class<?>... classes) {
43-
TableManager.init(runner, config, classes);
43+
TableManager.init(runner, classes);
4444
}
4545

4646

src/main/java/com/github/artbits/orm/Options.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,7 @@ public Options where(String predicate, Object... objects) {
5656
if (o instanceof String || o instanceof Character) {
5757
return String.format("'%s'", o);
5858
} else {
59-
String s = String.valueOf(o);
60-
switch (s) {
61-
case "true": return "1";
62-
case "false": return "0";
63-
default: return s;
64-
}
59+
return String.valueOf(o);
6560
}
6661
}).toArray());
6762
}

src/main/java/com/github/artbits/orm/Reflect.java

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.HashMap;
2323
import java.util.LinkedHashMap;
2424
import java.util.Map;
25-
import java.util.Objects;
2625
import java.util.function.BiConsumer;
2726
import java.util.function.Consumer;
2827

@@ -87,34 +86,65 @@ Class<?> getType(String fieldName) {
8786

8887

8988
String getDatabaseType(String fieldName) {
90-
switch (getType(fieldName).getSimpleName().toLowerCase()) {
91-
case "int":
92-
case "integer":
93-
case "byte":
94-
case "short":
95-
case "long": return "integer";
96-
case "float":
97-
case "double": return "real";
98-
case "char":
99-
case "character":
100-
case "string": return "text";
101-
case "boolean" : return "blob";
102-
default: throw new NullPointerException();
89+
String type = getType(fieldName).getSimpleName().toLowerCase();
90+
if (Core.config.type() == Config.Type.SQLite) {
91+
switch (type) {
92+
case "byte":
93+
case "short":
94+
case "int":
95+
case "integer":
96+
case "long":
97+
case "boolean": return "integer";
98+
case "float":
99+
case "double": return "real";
100+
case "char":
101+
case "character":
102+
case "string": return "text";
103+
default: throw new NullPointerException();
104+
}
105+
} else if (Core.config.type() == Config.Type.MySQL) {
106+
switch (type) {
107+
case "byte": return "tinyint";
108+
case "short": return "smallint";
109+
case "int":
110+
case "integer": return "int";
111+
case "long": return "bigint";
112+
case "float": return "float";
113+
case "double": return "double";
114+
case "char":
115+
case "character":
116+
case "string": return "text";
117+
case "boolean": return "boolean";
118+
default: throw new NullPointerException();
119+
}
120+
} else if (Core.config.type() == Config.Type.PostgreSQL) {
121+
switch (type) {
122+
case "byte":
123+
case "short": return "smallint";
124+
case "int":
125+
case "integer": return "int";
126+
case "long": return "bigint";
127+
case "float": return "real";
128+
case "double": return "double precision";
129+
case "char":
130+
case "character":
131+
case "string": return "text";
132+
case "boolean": return "boolean";
133+
default: throw new NullPointerException();
134+
}
135+
} else {
136+
throw new RuntimeException("Unsupported database type.");
103137
}
104138
}
105139

106140

107-
Object getDBValue(Field field) {
141+
Object getDatabaseValue(Field field) {
108142
try {
109143
String fieldName = field.getName();
110144
Field dbField = fieldMap.getOrDefault(fieldName, null);
111145
Object dbValue = (dbField != null) ? dbField.get(t) : null;
112146
if (dbField != null && dbValue != null) {
113-
switch (getDatabaseType(fieldName)) {
114-
case "text": return String.format("'%s'", dbValue);
115-
case "blob": return (Objects.equals(dbValue, true)) ? 1 : 0;
116-
default: return dbValue;
117-
}
147+
return getDatabaseType(fieldName).equals("text") ? String.format("'%s'", dbValue) : dbValue;
118148
}
119149
return null;
120150
} catch (IllegalAccessException e) {
@@ -123,14 +153,14 @@ Object getDBValue(Field field) {
123153
}
124154

125155

126-
void getDBColumnsWithValue(BiConsumer<String, Object> consumer) {
156+
void getDatabaseColumnsWithValue(BiConsumer<String, Object> consumer) {
127157
for (Field field : fieldMap.values()) {
128-
consumer.accept(field.getName(), getDBValue(field));
158+
consumer.accept(field.getName(), getDatabaseValue(field));
129159
}
130160
}
131161

132162

133-
void getDBColumnsWithType(BiConsumer<String, String> consumer) {
163+
void getDatabaseColumnsWithType(BiConsumer<String, String> consumer) {
134164
for (Field field : fieldMap.values()) {
135165
consumer.accept(field.getName(), getDatabaseType(field.getName()));
136166
}

src/main/java/com/github/artbits/orm/Runner.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,22 @@
2121
import java.sql.*;
2222
import java.util.ArrayList;
2323
import java.util.List;
24-
import java.util.Objects;
2524
import java.util.Optional;
2625

2726
class Runner {
2827

2928
private final BasicDataSource source = new BasicDataSource();
30-
private final Config config;
3129

3230

33-
Runner(Config config) {
31+
Runner() {
3432
try {
35-
this.config = config;
36-
Class.forName(config.driver);
37-
Optional.ofNullable(config.url).ifPresent(source::setUrl);
38-
Optional.ofNullable(config.username).ifPresent(source::setUsername);
39-
Optional.ofNullable(config.password).ifPresent(source::setPassword);
40-
source.setInitialSize(config.initSize);
41-
source.setMaxTotal(config.maxSize);
42-
source.setMinIdle(config.minIdle);
43-
source.setMaxIdle(config.maxIdle);
33+
Optional.ofNullable(Core.config.url).ifPresent(source::setUrl);
34+
Optional.ofNullable(Core.config.username).ifPresent(source::setUsername);
35+
Optional.ofNullable(Core.config.password).ifPresent(source::setPassword);
36+
source.setInitialSize(Core.config.initSize);
37+
source.setMaxTotal(Core.config.maxSize);
38+
source.setMinIdle(Core.config.minIdle);
39+
source.setMaxIdle(Core.config.maxIdle);
4440
} catch (Exception e) {
4541
throw new RuntimeException(e);
4642
}
@@ -70,7 +66,7 @@ long insert(String sql) {
7066
if (statement.executeUpdate() == 0) {
7167
return -1;
7268
}
73-
if (Objects.equals(config.driver, Config.Driver.SQLITE)) {
69+
if (Core.config.type() == Config.Type.SQLite) {
7470
try (PreparedStatement statement1 = connection.prepareStatement("select last_insert_rowid()");
7571
ResultSet set = statement1.executeQuery()) {
7672
return set.next() ? set.getLong(1) : -1;

src/main/java/com/github/artbits/orm/SQLTemplate.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,18 @@
2121

2222
final class SQLTemplate {
2323

24-
static <T> String create(Class<T> tClass, Config config) {
24+
static <T> String create(Class<T> tClass) {
2525
StringBuffer columnsString = new StringBuffer();
26-
if (Objects.equals(config.driver, Config.Driver.SQLITE)) {
26+
if (Core.config.type() == Config.Type.SQLite) {
2727
columnsString.append("id integer primary key autoincrement,");
2828
}
29-
if (Objects.equals(config.driver, Config.Driver.MYSQL)) {
29+
if (Core.config.type() == Config.Type.MySQL) {
3030
columnsString.append("id int primary key auto_increment,");
3131
}
32-
new Reflect<>(tClass).getDBColumnsWithType((column, type) -> {
32+
if (Core.config.type() == Config.Type.PostgreSQL) {
33+
columnsString.append("id bigserial primary key,");
34+
}
35+
new Reflect<>(tClass).getDatabaseColumnsWithType((column, type) -> {
3336
if (!Objects.equals(column, "id")) {
3437
columnsString.append(column).append(" ").append(type).append(",");
3538
}
@@ -54,7 +57,7 @@ static <T> String drop(Class<T> tClass) {
5457
static <T> String insert(T t) {
5558
StringBuffer columnsString = new StringBuffer();
5659
StringBuffer valueString = new StringBuffer();
57-
new Reflect<>(t).getDBColumnsWithValue((column, value) -> {
60+
new Reflect<>(t).getDatabaseColumnsWithValue((column, value) -> {
5861
if (!Objects.equals(column, "id")) {
5962
columnsString.append(column).append(",");
6063
valueString.append(value).append(",");
@@ -71,7 +74,7 @@ static <T> String update(T t, Options options) {
7174
String tableName = t.getClass().getSimpleName().toLowerCase();
7275
String whereString = (options.wherePredicate != null) ? $("where %s ", options.wherePredicate) : "";
7376
StringBuffer setString = new StringBuffer();
74-
new Reflect<>(t).getDBColumnsWithValue((column, value) -> {
77+
new Reflect<>(t).getDatabaseColumnsWithValue((column, value) -> {
7578
if (value != null && !Objects.equals(column, "id")) {
7679
setString.append(column).append(" = ").append(value).append(",");
7780
}
@@ -134,7 +137,7 @@ static <T> String query(Class<T> tClass, Options options) {
134137
static <T> String createIndex(Class<T> tClass, String column) {
135138
String table = tClass.getSimpleName().toLowerCase();
136139
String index = $("idx_%s_%s", table, column);
137-
return $("create index %s on %s(%s)", index, table, column);
140+
return $("create unique index %s on %s(%s)", index, table, column);
138141
}
139142

140143

0 commit comments

Comments
 (0)