Skip to content

Commit 8445348

Browse files
committed
Added SQLDriverFactory
1 parent efa2f1e commit 8445348

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.javawebstack.orm.wrapper;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.function.Supplier;
6+
7+
public class SQLDriverFactory {
8+
private Map<String, Supplier<SQL>> suppliers = new HashMap<>();
9+
private Map<String, String> properties;
10+
11+
public SQLDriverFactory(Map<String, String> properties) {
12+
this.properties = properties;
13+
addDefaultDrivers();
14+
}
15+
16+
public void registerDriver (String name, Supplier<SQL> supplier) {
17+
suppliers.put(name, supplier);
18+
}
19+
20+
private void addDefaultDrivers() {
21+
registerDriver("sqlite", () -> new SQLite(properties.get("file")));
22+
registerDriver("mysql", () -> new MySQL(
23+
properties.get("host"),
24+
Integer.parseInt(properties.get("port")),
25+
properties.get("name"),
26+
properties.get("user"),
27+
properties.get("password")
28+
));
29+
}
30+
31+
public SQL getDriver(String name) throws SQLDriverNotFoundException {
32+
Supplier<SQL> supplier = suppliers.get(name);
33+
if (supplier == null)
34+
throw new SQLDriverNotFoundException(name);
35+
return supplier.get();
36+
}
37+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.javawebstack.orm.wrapper;
2+
3+
public class SQLDriverNotFoundException extends Exception {
4+
private String name;
5+
6+
public SQLDriverNotFoundException (String name) {
7+
this.name = name;
8+
}
9+
10+
11+
public String getMessage() {
12+
return "SQL Driver " + name + " not found!";
13+
}
14+
}

0 commit comments

Comments
 (0)