Skip to content

Commit ff1b292

Browse files
committed
Data source
1 parent 62134d1 commit ff1b292

File tree

6 files changed

+172
-0
lines changed

6 files changed

+172
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,50 @@
11
package io.izzel.netzach;
22

3+
import io.izzel.netzach.config.ConfigSpec;
4+
import io.izzel.netzach.data.DataManager;
35
import io.izzel.taboolib.loader.Plugin;
6+
import io.izzel.taboolib.module.locale.TLocale;
7+
import org.yaml.snakeyaml.Yaml;
8+
import org.yaml.snakeyaml.constructor.CustomClassLoaderConstructor;
49

510
public final class Netzach extends Plugin {
611

12+
private ConfigSpec configSpec;
13+
private DataManager dataManager;
14+
15+
@SuppressWarnings("deprecation")
16+
@Override
17+
public void onLoading() {
18+
this.saveDefaultConfig();
19+
Yaml yaml = new Yaml(new CustomClassLoaderConstructor(ConfigSpec.class, this.getClassLoader()));
20+
this.configSpec = yaml.load(this.getConfig().saveToString());
21+
this.dataManager = new DataManager();
22+
}
23+
724
@Override
825
public void onStarting() {
26+
this.dataManager.open();
27+
TLocale.sendToConsole("load.complete");
28+
}
29+
30+
@Override
31+
public void onStopping() {
32+
this.dataManager.close();
33+
}
34+
35+
public DataManager getDataManager() {
36+
return dataManager;
37+
}
38+
39+
public ConfigSpec getConfigSpec() {
40+
return configSpec;
41+
}
42+
43+
public static Netzach instance() {
44+
return (Netzach) plugin;
45+
}
946

47+
public static ConfigSpec config() {
48+
return instance().getConfigSpec();
1049
}
1150
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.izzel.netzach.config;
2+
3+
public class ConfigSpec {
4+
5+
private DatabaseSpec database = new DatabaseSpec();
6+
7+
public DatabaseSpec database() {
8+
return database;
9+
}
10+
11+
public void setDatabase(DatabaseSpec database) {
12+
this.database = database;
13+
}
14+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package io.izzel.netzach.config;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class DatabaseSpec {
7+
8+
private String tablePrefix = "netzach_";
9+
private String jdbcUrl;
10+
private Map<String, String> properties = new HashMap<>();
11+
private Map<String, String> dataSourceProperties = new HashMap<>();
12+
13+
public String tablePrefix() {
14+
return tablePrefix;
15+
}
16+
17+
public void setTablePrefix(String tablePrefix) {
18+
this.tablePrefix = tablePrefix;
19+
}
20+
21+
public String jdbcUrl() {
22+
return jdbcUrl;
23+
}
24+
25+
public void setJdbcUrl(String jdbcUrl) {
26+
this.jdbcUrl = jdbcUrl;
27+
}
28+
29+
public Map<String, String> properties() {
30+
return properties;
31+
}
32+
33+
public void setProperties(Map<String, String> properties) {
34+
this.properties = properties;
35+
}
36+
37+
public Map<String, String> dataSourceProperties() {
38+
return dataSourceProperties;
39+
}
40+
41+
public void setDataSourceProperties(Map<String, String> dataSourceProperties) {
42+
this.dataSourceProperties = dataSourceProperties;
43+
}
44+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package io.izzel.netzach.data;
2+
3+
import com.zaxxer.hikari.HikariConfig;
4+
import com.zaxxer.hikari.HikariDataSource;
5+
import com.zaxxer.hikari.util.PropertyElf;
6+
import io.izzel.netzach.Netzach;
7+
import io.izzel.taboolib.module.locale.TLocale;
8+
9+
import java.sql.Connection;
10+
import java.sql.SQLException;
11+
import java.util.Properties;
12+
13+
public class DataManager {
14+
15+
private HikariDataSource dataSource;
16+
17+
public void open() {
18+
String jdbcUrl = Netzach.config().database().jdbcUrl();
19+
HikariConfig hikariConfig = new HikariConfig();
20+
hikariConfig.setJdbcUrl(jdbcUrl);
21+
hikariConfig.setPoolName("Netzach");
22+
23+
if (jdbcUrl.startsWith("jdbc:sqlite")) {
24+
hikariConfig.setDriverClassName("org.sqlite.JDBC");
25+
} else if (jdbcUrl.startsWith("jdbc:mysql")) {
26+
hikariConfig.setDriverClassName("com.mysql.jdbc.Driver");
27+
}
28+
29+
Properties properties = new Properties();
30+
properties.putAll(Netzach.config().database().properties());
31+
PropertyElf.setTargetFromProperties(hikariConfig, properties);
32+
33+
Properties dataSourceProp = new Properties();
34+
dataSourceProp.putAll(Netzach.config().database().dataSourceProperties());
35+
hikariConfig.setDataSourceProperties(dataSourceProp);
36+
this.dataSource = new HikariDataSource(hikariConfig);
37+
TLocale.sendToConsole("load.database");
38+
}
39+
40+
public void close() {
41+
if (this.dataSource != null) {
42+
this.dataSource.close();
43+
this.dataSource = null;
44+
}
45+
}
46+
47+
public Connection connection() throws SQLException {
48+
return this.dataSource.getConnection();
49+
}
50+
}

src/main/resources/config.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
database:
3+
tablePrefix: 'netzach_'
4+
jdbcUrl: 'jdbc:sqlite:./plugins/Netzach/data.db'
5+
#jdbcUrl: 'jdbc:mysql://localhost:3306/database'
6+
properties:
7+
#username: root
8+
#password: pass
9+
connectionTimeout: 5000
10+
maximumPoolSize: 10
11+
maxLifetime: 1800000
12+
dataSourceProperties:
13+
useUnicode: true
14+
characterEncoding: utf8
15+
cachePrepStmts: true
16+
prepStmtCacheSize: 64
17+
prepStmtCacheSqlLimit: 256
18+
#useSSL: false
19+
#verifyServerCertificate: false

src/main/resources/lang/zh_CN.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
load:
3+
database: '数据库初始化完成'
4+
complete: 'Netzach 加载完成'
5+
database:
6+
unknown_protocol: '&未知的 jdbc 协议 {0}'

0 commit comments

Comments
 (0)