mirror of
https://github.com/correl/mage.git
synced 2025-04-13 09:11:06 -09:00
SQLite EntityManager
This commit is contained in:
parent
9a5eca433d
commit
fd25002578
2 changed files with 146 additions and 15 deletions
Mage.Common/src
8
Mage.Common/src/log4j.properties
Normal file
8
Mage.Common/src/log4j.properties
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
#default levels
|
||||||
|
log4j.rootLogger=debug, console
|
||||||
|
|
||||||
|
#console log
|
||||||
|
log4j.appender.console=org.apache.log4j.ConsoleAppender
|
||||||
|
log4j.appender.console.layout=org.apache.log4j.PatternLayout
|
||||||
|
log4j.appender.console.layout.ConversionPattern=%-5p [%d{yyyy-MM-dd HH:mm [ss:SSS]}] %C{1}[%t]: %m%n
|
||||||
|
log4j.appender.console.Threshold=debug
|
|
@ -1,5 +1,8 @@
|
||||||
package mage.db;
|
package mage.db;
|
||||||
|
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -7,29 +10,149 @@ import java.sql.*;
|
||||||
*/
|
*/
|
||||||
public class EntityManager {
|
public class EntityManager {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
private static final Logger log = Logger.getLogger(EntityManager.class);
|
||||||
|
|
||||||
|
private static final EntityManager instance;
|
||||||
|
|
||||||
|
private static final String MAGE_JDBC_URL = "jdbc:sqlite:db/mage.db";
|
||||||
|
|
||||||
|
static {
|
||||||
|
instance = new EntityManager();
|
||||||
|
try {
|
||||||
|
init();
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.fatal(e);
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static EntityManager getInstance() {
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inits database. Creates tables if they don't exist.
|
||||||
|
*
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
protected static void init() throws Exception {
|
||||||
Class.forName("org.sqlite.JDBC");
|
Class.forName("org.sqlite.JDBC");
|
||||||
Connection conn = DriverManager.getConnection("jdbc:sqlite:mage.db");
|
checkDBFolderExistance();
|
||||||
Statement stat = conn.createStatement();
|
Connection conn = DriverManager.getConnection(MAGE_JDBC_URL);
|
||||||
stat.executeUpdate("drop table if exists users;");
|
try {
|
||||||
stat.executeUpdate("create table users (login, password);");
|
Statement stat = conn.createStatement();
|
||||||
|
stat.executeUpdate("create table if not exists users (login, password, status);");
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
conn.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
// swallow
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
PreparedStatement prep = conn.prepareStatement("insert into users values (?, ?);");
|
/**
|
||||||
|
* Reinits database. Drops all tables and then creates them from scratch.
|
||||||
|
* BE CAREFUL! THIS METHOD WILL DESTROY ALL DATA.
|
||||||
|
*
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public static void reinit() throws Exception {
|
||||||
|
Class.forName("org.sqlite.JDBC");
|
||||||
|
checkDBFolderExistance();
|
||||||
|
Connection conn = DriverManager.getConnection(MAGE_JDBC_URL);
|
||||||
|
try {
|
||||||
|
Statement stat = conn.createStatement();
|
||||||
|
stat.executeUpdate("drop table users;");
|
||||||
|
stat.executeUpdate("create table users (login, password, status);");
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
conn.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
// swallow
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
prep.setString(1, "TestUser");
|
/**
|
||||||
prep.setString(2, "123");
|
* Creates folders for Mage sqlite db.
|
||||||
prep.execute();
|
*/
|
||||||
|
protected static void checkDBFolderExistance() {
|
||||||
|
File file = new File("db");
|
||||||
|
if (!file.exists()) {
|
||||||
|
file.mkdirs();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
prep.setString(1, "TestUser2");
|
/**
|
||||||
prep.setString(2, "12345");
|
* Tests DB with base operations.
|
||||||
prep.execute();
|
*
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public void testDB() throws Exception {
|
||||||
|
|
||||||
|
Connection conn = DriverManager.getConnection(MAGE_JDBC_URL);
|
||||||
|
|
||||||
|
try {
|
||||||
|
Statement stat = conn.createStatement();
|
||||||
|
|
||||||
|
ResultSet rs = stat.executeQuery("select * from users where login = 'testtest';");
|
||||||
|
|
||||||
|
if (rs.next()) {
|
||||||
|
checkTestUser(conn, stat);
|
||||||
|
} else {
|
||||||
|
log.debug("[DBTest] creating test user...");
|
||||||
|
PreparedStatement prep = conn.prepareStatement("insert into users values (?, ?, ?);");
|
||||||
|
|
||||||
|
prep.setString(1, "testtest");
|
||||||
|
prep.setString(2, "12345");
|
||||||
|
prep.setString(3, "disabled");
|
||||||
|
|
||||||
|
prep.execute();
|
||||||
|
|
||||||
|
log.debug("[DBTest] creating test user [OK]");
|
||||||
|
|
||||||
|
checkTestUser(conn, stat);
|
||||||
|
}
|
||||||
|
|
||||||
|
rs.close();
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
conn.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
// swallow
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks test user existence and its parameters.
|
||||||
|
*
|
||||||
|
* @param conn
|
||||||
|
* @param stat
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
private void checkTestUser(Connection conn, Statement stat) throws Exception {
|
||||||
|
ResultSet rs = stat.executeQuery("select * from users where login = 'testtest';");
|
||||||
|
if (rs.next()) {
|
||||||
|
log.debug("[DBTest] checking test user [OK]");
|
||||||
|
if (rs.getString("login").equals("testtest") && rs.getString("password").equals("12345") && rs.getString("status").equals("disabled")) {
|
||||||
|
log.debug("[DBTest] checking test user parameters [OK]");
|
||||||
|
} else {
|
||||||
|
log.debug("[DBTest] checking test user parameters [ERROR]");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.debug("[DBTest] checking test user [ERROR]");
|
||||||
|
log.debug("[DBTest] couldn't find test user");
|
||||||
|
}
|
||||||
|
|
||||||
ResultSet rs = stat.executeQuery("select * from users;");
|
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
System.out.println("user = " + rs.getString("login"));
|
System.out.println("user = " + rs.getString("login"));
|
||||||
System.out.println("password = " + rs.getString("password"));
|
System.out.println("password = " + rs.getString("password"));
|
||||||
}
|
}
|
||||||
rs.close();
|
}
|
||||||
conn.close();
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
EntityManager.getInstance().testDB();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue