Search in sources :

Example 1 with SQLiteConnectionFactory

use of me.lucko.luckperms.common.storage.dao.sql.connection.file.SQLiteConnectionFactory in project LuckPerms by lucko.

the class SqlDao method init.

@Override
public void init() {
    try {
        this.provider.init();
        // Init tables
        if (!tableExists(this.prefix.apply("{prefix}user_permissions"))) {
            String schemaFileName = "schema/" + this.provider.getName().toLowerCase() + ".sql";
            try (InputStream is = this.plugin.getBootstrap().getResourceStream(schemaFileName)) {
                if (is == null) {
                    throw new Exception("Couldn't locate schema file for " + this.provider.getName());
                }
                try (BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
                    try (Connection connection = this.provider.getConnection()) {
                        try (Statement s = connection.createStatement()) {
                            StringBuilder sb = new StringBuilder();
                            String line;
                            while ((line = reader.readLine()) != null) {
                                if (line.startsWith("--") || line.startsWith("#"))
                                    continue;
                                sb.append(line);
                                // check for end of declaration
                                if (line.endsWith(";")) {
                                    sb.deleteCharAt(sb.length() - 1);
                                    String result = this.prefix.apply(sb.toString().trim());
                                    if (!result.isEmpty())
                                        s.addBatch(result);
                                    // reset
                                    sb = new StringBuilder();
                                }
                            }
                            s.executeBatch();
                        }
                    }
                }
            }
        }
        // migrations
        try {
            if (!(this.provider instanceof SQLiteConnectionFactory) && !(this.provider instanceof PostgreConnectionFactory)) {
                try (Connection connection = this.provider.getConnection()) {
                    try (Statement s = connection.createStatement()) {
                        s.execute(this.prefix.apply("ALTER TABLE {prefix}actions MODIFY COLUMN actor_name VARCHAR(100)"));
                        s.execute(this.prefix.apply("ALTER TABLE {prefix}actions MODIFY COLUMN action VARCHAR(300)"));
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    } catch (Exception e) {
        this.plugin.getLogger().severe("Error occurred whilst initialising the database.");
        e.printStackTrace();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) BufferedReader(java.io.BufferedReader) Connection(java.sql.Connection) PostgreConnectionFactory(me.lucko.luckperms.common.storage.dao.sql.connection.hikari.PostgreConnectionFactory) SQLException(java.sql.SQLException) SQLiteConnectionFactory(me.lucko.luckperms.common.storage.dao.sql.connection.file.SQLiteConnectionFactory)

Aggregations

BufferedReader (java.io.BufferedReader)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 SQLiteConnectionFactory (me.lucko.luckperms.common.storage.dao.sql.connection.file.SQLiteConnectionFactory)1 PostgreConnectionFactory (me.lucko.luckperms.common.storage.dao.sql.connection.hikari.PostgreConnectionFactory)1