mirror of
https://github.com/magefree/mage.git
synced 2026-01-25 20:59:14 -08:00
LogService. Added saving game started event to DB. Some refactoring.
This commit is contained in:
parent
bdb2754847
commit
b0a1c07067
8 changed files with 249 additions and 7 deletions
|
|
@ -1,14 +1,17 @@
|
|||
package mage.db;
|
||||
|
||||
import mage.db.model.Log;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author noxx
|
||||
*/
|
||||
public enum EntityManager {
|
||||
public enum EntityManager implements Storage {
|
||||
|
||||
instance;
|
||||
|
||||
|
|
@ -16,6 +19,9 @@ public enum EntityManager {
|
|||
|
||||
private static final String MAGE_JDBC_URL = "jdbc:sqlite:db/mage.db";
|
||||
|
||||
private static String QUERY_SAVE_LOG = "insert into logs values (?, ?, ?, ?, ?, ?, ?, ?)";
|
||||
private static String QUERY_GET_ALL_LOGS = "select * from logs";
|
||||
|
||||
static {
|
||||
try {
|
||||
init();
|
||||
|
|
@ -29,6 +35,77 @@ public enum EntityManager {
|
|||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts log entry to DB.
|
||||
*
|
||||
* @param key
|
||||
* @param date
|
||||
* @param args
|
||||
* @throws Exception
|
||||
*/
|
||||
public void insertLog(String key, java.util.Date date, String... args) throws SQLException {
|
||||
Connection conn = DriverManager.getConnection(MAGE_JDBC_URL);
|
||||
|
||||
try {
|
||||
PreparedStatement prep = conn.prepareStatement(QUERY_SAVE_LOG);
|
||||
|
||||
prep.setString(1, key);
|
||||
prep.setDate(2, new java.sql.Date(date.getTime()));
|
||||
|
||||
int index = 3;
|
||||
for (String arg : args) {
|
||||
if (index > 8) break;
|
||||
prep.setString(index++, arg);
|
||||
}
|
||||
|
||||
prep.execute();
|
||||
|
||||
} finally {
|
||||
try {
|
||||
if (conn != null) conn.close();
|
||||
} catch (Exception e) {
|
||||
// swallow
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Log> getAllLogs() {
|
||||
List<Log> logs = new ArrayList<Log>();
|
||||
|
||||
try {
|
||||
Connection conn = DriverManager.getConnection(MAGE_JDBC_URL);
|
||||
try {
|
||||
Statement stat = conn.createStatement();
|
||||
ResultSet rs = stat.executeQuery(QUERY_GET_ALL_LOGS);
|
||||
while (rs.next()) {
|
||||
Log log = new Log(rs.getString(1), rs.getDate(2));
|
||||
List<String> args = new ArrayList<String>();
|
||||
for (int index = 0; index < 6; index++) {
|
||||
String arg = rs.getString(3 + index);
|
||||
if (arg == null) {
|
||||
break;
|
||||
}
|
||||
args.add(arg);
|
||||
}
|
||||
log.setArguments(args);
|
||||
logs.add(log);
|
||||
}
|
||||
rs.close();
|
||||
} finally {
|
||||
try {
|
||||
if (conn != null) conn.close();
|
||||
} catch (Exception e) {
|
||||
// swallow
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
log.fatal("SQL Exception: ", e);
|
||||
}
|
||||
|
||||
return logs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inits database. Creates tables if they don't exist.
|
||||
*
|
||||
|
|
@ -41,6 +118,7 @@ public enum EntityManager {
|
|||
try {
|
||||
Statement stat = conn.createStatement();
|
||||
stat.executeUpdate("create table if not exists users (login, password, status);");
|
||||
stat.executeUpdate("create table if not exists logs (key, created_dt, arg0, arg1, arg2, arg3, arg4, arg5);");
|
||||
} finally {
|
||||
try {
|
||||
conn.close();
|
||||
|
|
@ -63,7 +141,7 @@ public enum EntityManager {
|
|||
try {
|
||||
Statement stat = conn.createStatement();
|
||||
stat.executeUpdate("drop table users;");
|
||||
stat.executeUpdate("create table users (login, password, status);");
|
||||
init();
|
||||
} finally {
|
||||
try {
|
||||
conn.close();
|
||||
|
|
@ -117,7 +195,7 @@ public enum EntityManager {
|
|||
rs.close();
|
||||
} finally {
|
||||
try {
|
||||
conn.close();
|
||||
if (conn != null) conn.close();
|
||||
} catch (Exception e) {
|
||||
// swallow
|
||||
}
|
||||
|
|
@ -147,6 +225,7 @@ public enum EntityManager {
|
|||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
//EntityManager.getInstance().reinit();
|
||||
EntityManager.getInstance().testDB();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
33
Mage.Common/src/mage/db/EntityManagerTest.java
Normal file
33
Mage.Common/src/mage/db/EntityManagerTest.java
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
package mage.db;
|
||||
|
||||
import mage.db.model.Log;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author noxx
|
||||
*/
|
||||
public class EntityManagerTest {
|
||||
|
||||
private static DateFormat timeFormatter = SimpleDateFormat.getTimeInstance(SimpleDateFormat.FULL);
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
EntityManager.instance.testDB();
|
||||
List<Log> logs = EntityManager.instance.getAllLogs();
|
||||
System.out.println("logs found: " + logs.size());
|
||||
for (Log log : logs) {
|
||||
System.out.println(" key=" + log.getKey());
|
||||
System.out.println(" date=" + timeFormatter.format(log.getCreatedDate()));
|
||||
System.out.print(" arguments=[ ");
|
||||
if (log.getArguments() != null) {
|
||||
for (String argument : log.getArguments()) {
|
||||
System.out.print("arg=" + argument + " ");
|
||||
}
|
||||
}
|
||||
System.out.println("]");
|
||||
System.out.println(" --------------");
|
||||
}
|
||||
}
|
||||
}
|
||||
14
Mage.Common/src/mage/db/Storage.java
Normal file
14
Mage.Common/src/mage/db/Storage.java
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
package mage.db;
|
||||
|
||||
import mage.db.model.Log;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public interface Storage {
|
||||
void insertLog(String key, Date date, String... args) throws Exception;
|
||||
List<Log> getAllLogs();
|
||||
}
|
||||
45
Mage.Common/src/mage/db/model/Log.java
Normal file
45
Mage.Common/src/mage/db/model/Log.java
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
package mage.db.model;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author noxx
|
||||
*/
|
||||
public class Log {
|
||||
|
||||
private String key;
|
||||
|
||||
private Date createdDate;
|
||||
|
||||
private List<String> arguments;
|
||||
|
||||
public Log(String key, Date createdDate) {
|
||||
this.key = key;
|
||||
this.createdDate = createdDate;
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public void setKey(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public Date getCreatedDate() {
|
||||
return createdDate;
|
||||
}
|
||||
|
||||
public void setCreatedDate(Date createdDate) {
|
||||
this.createdDate = createdDate;
|
||||
}
|
||||
|
||||
public List<String> getArguments() {
|
||||
return arguments;
|
||||
}
|
||||
|
||||
public void setArguments(List<String> arguments) {
|
||||
this.arguments = arguments;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue