Commit 32ec4e3a by Lee Jaebin

DB_type 削除(CipherSQL,JDBCSQL)

parent b33f949d
package jp.agentec.abook.abv.bl.common.db.impl;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import jp.agentec.abook.abv.bl.common.db.Cursor;
/**
* JDBC用カーソルクラス
*
* JDBCのResultSetをラップしている。
* 大きな違いは、AndroidのCursorがcolumnIndexが0から始まるのに対して、
* ResultSetは1から始まること。
* そのため、Cursorに合わせるためにcolumnIndexに1を加算している。
*
*
* @author tsukada
*
*/
public class JDBCCursor implements Cursor {
private ResultSet rs;
public JDBCCursor(ResultSet cursor) {
this.rs = cursor;
}
@Override
public void close() {
try {
rs.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public byte[] getBlob(int columnIndex) {
try {
return rs.getBytes(columnIndex + 1);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public int getColumnCount() {
try {
ResultSetMetaData rsmd = rs.getMetaData();
return rsmd.getColumnCount();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public int getColumnIndex(String columnName) {
try {
return rs.findColumn(columnName) - 1;
} catch (SQLException e) {
return 0;
// throw new RuntimeException(e);
}
}
@Override
public int getColumnIndexOrThrow(String columnName) {
return getColumnIndex(columnName) - 1;
}
@Override
public String getColumnName(int columnIndex) {
try {
ResultSetMetaData rsmd = rs.getMetaData();
return rsmd.getColumnLabel(columnIndex + 1);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public String[] getColumnNames() {
try {
ResultSetMetaData rsmd = rs.getMetaData();
int cols = rsmd.getColumnCount();
String[] ret = new String[cols];
for (int i = 0; i < cols; i++) {
ret[i] = rsmd.getColumnLabel(i+1);
}
return ret;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
@Deprecated
public int getCount() {
try {
rs.afterLast();
int count = rs.getRow();
rs.beforeFirst();
return count;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public double getDouble(int columnIndex) {
try {
return rs.getDouble(columnIndex + 1);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public float getFloat(int columnIndex) {
try {
return rs.getFloat(columnIndex + 1);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public int getInt(int columnIndex) {
try {
return rs.getInt(columnIndex + 1);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public long getLong(int columnIndex) {
try {
return rs.getLong(columnIndex + 1);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public short getShort(int columnIndex) {
try {
return rs.getShort(columnIndex + 1);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public String getString(int columnIndex) {
try {
return rs.getString(columnIndex + 1);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public int getType(int columnIndex) {
throw new RuntimeException("Not implemented.");
}
@Override
public boolean isAfterLast() {
try {
return rs.isAfterLast();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public boolean isBeforeFirst() {
try {
return rs.isBeforeFirst();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Deprecated
@Override
public boolean isClosed() {
return true;
// try {
// return rs.isClosed();
// } catch (SQLException e) {
// throw new RuntimeException(e);
// }
}
@Override
public boolean isFirst() {
try {
return rs.isFirst();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Deprecated
@Override
public boolean isLast() {
try {
return rs.isLast();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public boolean isNull(int columnIndex) {
try {
return rs.getObject(columnIndex + 1) == null;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public boolean move(int offset) {
try {
return rs.relative(offset);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public boolean moveToFirst() {
return true;
// try {
// return rs.first();
// } catch (SQLException e) {
// throw new RuntimeException(e);
// }
}
@Deprecated
@Override
public boolean moveToLast() {
try {
return rs.last();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public boolean moveToNext() {
try {
return rs.next();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
package jp.agentec.abook.abv.bl.common.db.impl;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import jp.agentec.abook.abv.bl.common.db.Cursor;
import jp.agentec.abook.abv.bl.common.db.SQLiteDatabase;
import jp.agentec.abook.abv.bl.common.db.SQLiteStatement;
import jp.agentec.adf.util.StringUtil;
public class JDBCSQLiteDatabase implements SQLiteDatabase {
private String dbpath;
private static JDBCSQLiteDatabase instance;
private Connection conn;
private boolean transactionSuccessful;
public static jp.agentec.abook.abv.bl.common.db.SQLiteDatabase getInstance() {
if (instance == null) {
synchronized (JDBCSQLiteDatabase.class) {
if (instance == null) {
instance = new JDBCSQLiteDatabase();
}
}
}
return instance;
}
private JDBCSQLiteDatabase() {
}
public void load(String dbpath) throws ClassNotFoundException, SQLException {
this.dbpath = dbpath;
Class.forName("org.sqlite.JDBC");
connect();
}
public void connect() throws SQLException {
conn = DriverManager.getConnection("jdbc:sqlite:" + dbpath);
conn.setAutoCommit(false);
}
public void open() throws SQLException {
if (conn == null || conn.isClosed()) {
connect();
}
}
@Override
public void beginTransaction() {
try {
open();
transactionSuccessful = false;
conn.setAutoCommit(false);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public void endTransaction() {
try {
if (!conn.isClosed()) {
if (transactionSuccessful) {
conn.commit();
}
else {
conn.rollback();
}
}
close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public void setTransactionSuccessful() {
transactionSuccessful = true;
}
@Override
public boolean inTransaction() {
try {
return !conn.isClosed(); // 方法がないのでcloseかどうかで判定
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public void close() {
try {
conn.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public jp.agentec.abook.abv.bl.common.db.Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy) {
return query(false, table, columns, selection, selectionArgs, groupBy, having, orderBy, null);
}
@Override
public jp.agentec.abook.abv.bl.common.db.Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) {
return query(false, table, columns, selection, selectionArgs, groupBy, having, orderBy, limit);
}
@Override
public jp.agentec.abook.abv.bl.common.db.Cursor rawQuery(String sql, String[] selectionArgs) {
try {
open();
PreparedStatement pstmt = conn.prepareStatement(sql);
bind(pstmt, (Object[])selectionArgs);
ResultSet rs = pstmt.executeQuery();
return new JDBCCursor(rs);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public Cursor query(boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) {
StringBuffer sql = new StringBuffer();
sql.append("select ");
if (distinct) {
sql.append("distinct ");
}
sql.append(StringUtil.join(",", columns));
sql.append(" from ");
sql.append(table);
if (selection != null) {
sql.append(" where ");
sql.append(selection);
}
if (groupBy != null) {
sql.append(" group by ");
sql.append(groupBy);
}
if (having != null) {
sql.append(" having ");
sql.append(having);
}
if (orderBy != null) {
sql.append(" order by ");
sql.append(orderBy);
}
if (limit != null) {
sql.append(" limit ");
sql.append(limit);
}
System.out.println(sql.toString());
return rawQuery(sql.toString(), selectionArgs);
}
@Override
public int delete(String table, String whereClause, String[] whereArgs) {
StringBuffer sql = new StringBuffer();
sql.append("delete from ");
sql.append(table);
if (whereClause != null) {
sql.append(" where ");
sql.append(whereClause);
}
System.out.println(sql.toString());
execSQL(sql.toString(), whereArgs);
Cursor cursor = rawQuery("select changes()", null);
return cursor.getInt(0);
}
@Override
public void execSQL(String sql) {
Statement stmt = null;
try {
stmt = conn.createStatement();
stmt.executeUpdate(sql);
} catch (SQLException e) {
throw new RuntimeException(e);
}
finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}
@Override
public void execSQL(String sql, Object[] bindArgs) {
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql);
bind(pstmt, bindArgs);
pstmt.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
}
finally {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}
private void bind(PreparedStatement pstmt, Object... params) {
try {
if (params == null) {
return;
}
for (int i=1; i <= params.length; i++) {
Object param = params[i-1];
if (param == null) {
pstmt.setNull(i, java.sql.Types.NULL);
}
else if (param instanceof String) {
pstmt.setString(i, (String)param);
}
else if (param instanceof Integer) {
pstmt.setLong(i, (Integer)param);
}
else if (param instanceof Long) {
pstmt.setLong(i, (Long)param);
}
else if (param instanceof Float) {
pstmt.setDouble(i, (Float)param);
}
else if (param instanceof Double) {
pstmt.setDouble(i, (Double)param);
}
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public boolean isOpen() {
try {
return !conn.isClosed();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public boolean needUpgrade(int newVersion) {
return newVersion > getVersion(); // not implemented
}
@Override
public SQLiteStatement compileStatement(String sql) {
try {
return new JDBCSQLiteStatement(sql, conn.prepareStatement(sql));
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public String getPath() {
return dbpath;
}
@Override
public int getVersion() {
return 0; // not implemented
}
}
package jp.agentec.abook.abv.bl.common.db.impl;
import java.sql.SQLException;
import jp.agentec.abook.abv.bl.common.db.SQLiteDatabase;
import jp.agentec.abook.abv.bl.common.log.Logger;
import jp.agentec.abook.abv.bl.data.ABVDataOpenHelper;
public class JDBCSQLiteOpenHelper implements jp.agentec.abook.abv.bl.common.db.SQLiteOpenHelper {
private static final String TAG = "JDBCSQLiteOpenHelper";
private String name;
private int version;
public static ABVDataOpenHelper abhelper;
/**
* コンストラクタ
*
* @param name データベースのパス(相対パスで記述するとカレントディレクトリベースとなる)
* @param version 未使用
*/
public JDBCSQLiteOpenHelper(String name, int version) {
this.name = name;
this.version = version;
}
public void onCreate(SQLiteDatabase db) {
abhelper.onCreate(db);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
abhelper.onUpgrade(db, oldVersion, newVersion);
}
public boolean isCreated() {
return abhelper.isCreated();
}
public Throwable getLastError() {
return abhelper.getLastError();
}
/**
* DB生成が必要な場合は、手動で直接以下をinitのあとに呼び出す
* ((JDBCSQLiteOpenHelper)sqlLiteOpenHelper).onCreate(conn.getDatabase());
* ((JDBCSQLiteOpenHelper)sqlLiteOpenHelper).onUpgrade(conn.getDatabase(), 1, 2);
*/
@Override
public jp.agentec.abook.abv.bl.common.db.SQLiteDatabase openDatabase() {
JDBCSQLiteOpenHelper.abhelper = new ABVDataOpenHelper();
Logger.i(TAG, "open jdbc database start.");
SQLiteDatabase db = JDBCSQLiteDatabase.getInstance();
try {
((JDBCSQLiteDatabase)db).load(name);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
}
Logger.i(TAG, "open jdbc database completed.");
// onCreate(null);
return db;
}
}
package jp.agentec.abook.abv.bl.common.db.impl;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import jp.agentec.abook.abv.bl.common.db.SQLiteStatement;
public class JDBCSQLiteStatement implements SQLiteStatement {
private Statement stmt;
private PreparedStatement pstmt;
private String sql;
private long updateCount;
public JDBCSQLiteStatement(String sql, Statement stmt) {
this.sql = sql;
this.stmt = stmt;
}
public JDBCSQLiteStatement(String sql, PreparedStatement pstmt) {
this.sql = sql;
this.pstmt = pstmt;
}
@Override
public void execute() {
try {
updateCount = 0;
if (stmt != null) {
updateCount = stmt.executeUpdate(sql);
}
else if (pstmt != null) {
updateCount = pstmt.executeUpdate();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public long executeInsert() {
try {
updateCount = 0;
if (stmt != null) {
updateCount = stmt.executeUpdate(sql);
}
else if (pstmt != null) {
updateCount = pstmt.executeUpdate();
}
return 1; // 生成されたシーケンスの値を返すがここでは固定で1を返す TODO: later 必要になった段階で考える
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public long simpleQueryForLong() {
try {
ResultSet rs = null;
if (stmt != null) {
rs = stmt.executeQuery(sql);
} else if (pstmt != null) {
rs = pstmt.executeQuery();
}
if (rs != null) {
rs.next();
return rs.getLong(1);
}
return 0;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public String simpleQueryForString() {
try {
ResultSet rs = null;
if (stmt != null) {
rs = stmt.executeQuery(sql);
} else if (pstmt != null) {
rs = pstmt.executeQuery();
}
if (rs != null) {
rs.next();
return rs.getString(1);
}
return null;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public void bindNull(int index) {
try {
pstmt.setNull(index, java.sql.Types.NULL);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public void bindLong(int index, long value) {
try {
pstmt.setLong(index, value);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public void bindDouble(int index, double value) {
try {
pstmt.setDouble(index, value);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public void bindString(int index, String value) {
try {
pstmt.setString(index, value);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public void bindBlob(int index, byte[] value) {
try {
pstmt.setBytes(index, value);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public void close() {
try {
if (pstmt != null) {
pstmt.close();
}
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public long getUpdateCount() {
return updateCount;
}
}
...@@ -21,10 +21,6 @@ public class DBConnector { ...@@ -21,10 +21,6 @@ public class DBConnector {
public static final String DatabaseName = "ABVJE"; public static final String DatabaseName = "ABVJE";
public static final int DatabaseVersion = DatabaseVersions.Plus_1_9_4_1; public static final int DatabaseVersion = DatabaseVersions.Plus_1_9_4_1;
public static final int DB_TYPE_STANDARD = 0;
public static final int DB_TYPE_CIPHER = 1;
public static final int DB_TYPE_JDBC = 2;
protected SQLiteDatabase db = null; protected SQLiteDatabase db = null;
private jp.agentec.abook.abv.bl.common.db.SQLiteOpenHelper sqlLiteOpenHelper = null; private jp.agentec.abook.abv.bl.common.db.SQLiteOpenHelper sqlLiteOpenHelper = null;
......
...@@ -22,7 +22,6 @@ import jp.agentec.abook.abv.bl.acms.type.DownloadStatusType; ...@@ -22,7 +22,6 @@ import jp.agentec.abook.abv.bl.acms.type.DownloadStatusType;
import jp.agentec.abook.abv.bl.acms.type.InstallType; import jp.agentec.abook.abv.bl.acms.type.InstallType;
import jp.agentec.abook.abv.bl.common.ABVEnvironment; import jp.agentec.abook.abv.bl.common.ABVEnvironment;
import jp.agentec.abook.abv.bl.common.db.SQLiteOpenHelper; import jp.agentec.abook.abv.bl.common.db.SQLiteOpenHelper;
import jp.agentec.abook.abv.bl.common.db.impl.JDBCSQLiteOpenHelper;
import jp.agentec.abook.abv.bl.common.exception.ABVException; import jp.agentec.abook.abv.bl.common.exception.ABVException;
import jp.agentec.abook.abv.bl.common.exception.AcmsException; import jp.agentec.abook.abv.bl.common.exception.AcmsException;
import jp.agentec.abook.abv.bl.common.exception.JSONValidationException; import jp.agentec.abook.abv.bl.common.exception.JSONValidationException;
......
package jp.agentec.abook.abv.bl.common.db.impl;
import java.sql.SQLException;
import jp.agentec.abook.abv.bl.common.db.Cursor;
import junit.framework.TestCase;
public class JDBCCursorTest extends TestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
}
public void testCursor() throws SQLException, ClassNotFoundException {
JDBCSQLiteDatabase db = (JDBCSQLiteDatabase)JDBCSQLiteDatabase.getInstance();
db.load("test.db");
db.connect();
Cursor cursor = db.rawQuery("select * from m_app_config", null);
System.out.println(cursor.getColumnCount());
System.out.println(cursor.getColumnIndex("app_config_name"));
System.out.println(cursor.getColumnName(1));
for (String name :cursor.getColumnNames()) {
System.out.println(name);
}
// System.out.println(cursor.getCount());
System.out.println(cursor.isBeforeFirst());
if (cursor.moveToNext()) {
System.out.println(cursor.getDouble(0));
System.out.println(cursor.getFloat(0));
System.out.println(cursor.getInt(0));
System.out.println(cursor.getLong(0));
System.out.println(cursor.getShort(0));
System.out.println(cursor.getBlob(0));
System.out.println(cursor.getString(0));
}
System.out.println(cursor.isBeforeFirst());
System.out.println(cursor.isAfterLast());
// cursor.moveToLast();
// System.out.println(cursor.isNull(1));
// System.out.println(cursor.isFirst() + " : " + cursor.isLast());
// System.out.println(cursor.getString(1));
// cursor.moveToFirst();
// System.out.println(cursor.isFirst() + " : " + cursor.isLast());
// System.out.println(cursor.getString(1));
// cursor.move(3);
// System.out.println(cursor.isFirst() + " : " + cursor.isLast());
// System.out.println(cursor.getString(1));
// System.out.println(cursor.isClosed());
cursor.close();
}
}
package jp.agentec.abook.abv.bl.common.db.impl;
import java.sql.SQLException;
import jp.agentec.abook.abv.bl.common.db.Cursor;
import jp.agentec.abook.abv.bl.common.db.SQLiteDatabase;
import jp.agentec.abook.abv.bl.common.db.SQLiteOpenHelper;
import jp.agentec.abook.abv.bl.common.db.SQLiteStatement;
import junit.framework.TestCase;
public class JDBCSQLiteDatabaseTest extends TestCase {
@Override
protected void setUp() throws Exception {
SQLiteOpenHelper sqlLiteOpenHelper = new JDBCSQLiteOpenHelper("test.db", 0);
SQLiteDatabase db = sqlLiteOpenHelper.openDatabase();
((JDBCSQLiteOpenHelper)sqlLiteOpenHelper).onCreate(db);
db.close();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
}
public void testDB() throws SQLException, ClassNotFoundException {
JDBCSQLiteDatabase db = (JDBCSQLiteDatabase)JDBCSQLiteDatabase.getInstance();
db.load("test.db");
db.connect();
System.out.println("isOpen: " + db.isOpen());
Cursor cursor = db.rawQuery("select * from m_app_config", null);
while (cursor.moveToNext()) {
System.out.println(cursor.getInt(0) + " : " + cursor.getString(1) + " : " + cursor.getString(2));
}
cursor = db.query("m_app_config", new String[]{"app_config_id", "app_config_name", "default_val"}, "app_config_id<?", new String[]{"7"}, null, null, "app_config_id desc");
while (cursor.moveToNext()) {
System.out.println(cursor.getInt(0) + " : " + cursor.getString(1) + " : " + cursor.getString(2));
}
cursor = db.query("m_app_config", new String[]{"app_config_id"}, "app_config_id<?", new String[]{"7"}, "app_config_id", "app_config_id > 5", "app_config_id desc");
while (cursor.moveToNext()) {
System.out.println(cursor.getInt(0));
}
cursor = db.query(true, "m_app_config", new String[]{"app_config_id", "app_config_name", "default_val"}, "app_config_id<?", new String[]{"7"}, null, null, "app_config_id desc", "3");
while (cursor.moveToNext()) {
System.out.println(cursor.getInt(0) + " : " + cursor.getString(1) + " : " + cursor.getString(2));
}
db.close();
System.out.println("isOpen: " + db.isOpen());
db.beginTransaction();
System.out.println("inTransaction: " + db.inTransaction());
SQLiteStatement stmt = db.compileStatement("update m_app_config set default_val='5' where app_config_id=1");
stmt.execute();
// db.setTransactionSuccessful();
db.endTransaction();
db.beginTransaction();
db.delete("m_app_config", "app_config_id=?", new String[]{"10"});
db.setTransactionSuccessful();
db.endTransaction();
System.out.println("inTransaction: " + db.inTransaction());
db.beginTransaction();
db.execSQL("insert into m_app_config values (10, 'hogeaaa', '223', '444')");
db.execSQL("update m_app_config set default_val=? where app_config_id=?", new String[]{"987", "3"});
db.setTransactionSuccessful();
db.endTransaction();
db.close();
System.out.println("isOpen: " + db.isOpen());
}
}
package jp.agentec.abook.abv.bl.common.db.impl;
import jp.agentec.abook.abv.bl.common.db.SQLiteDatabase;
import jp.agentec.abook.abv.bl.data.DatabaseVersions;
import junit.framework.TestCase;
public class JDBCSQLiteOpenHelperTest extends TestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
}
public void testOpenHelper() {
JDBCSQLiteOpenHelper sqlLiteOpenHelper = new JDBCSQLiteOpenHelper("test.db", DatabaseVersions.Ver1_5_4);
SQLiteDatabase db = sqlLiteOpenHelper.openDatabase();
sqlLiteOpenHelper.onCreate(db);
sqlLiteOpenHelper.onUpgrade(db, DatabaseVersions.Ver1_5_4, DatabaseVersions.Ver1_5_4);
System.out.println(sqlLiteOpenHelper.isCreated());
System.out.println(sqlLiteOpenHelper.getLastError());
}
}
package jp.agentec.abook.abv.bl.common.db.impl;
import java.sql.SQLException;
import jp.agentec.abook.abv.bl.common.db.SQLiteStatement;
import junit.framework.TestCase;
public class JDBCSQLiteStatementTest extends TestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
}
public void testSQLiteStatement() throws ClassNotFoundException, SQLException {
JDBCSQLiteDatabase db = (JDBCSQLiteDatabase)JDBCSQLiteDatabase.getInstance();
db.load("test.db");
db.connect();
db.beginTransaction();
SQLiteStatement stmt = db.compileStatement("update m_app_config set default_val=?, val=? where app_config_id=?");
stmt.bindNull(1);
stmt.bindString(2, "hoge");
stmt.bindLong(3, 5);
stmt.execute();
db.setTransactionSuccessful();
db.endTransaction();
stmt.close();
db.beginTransaction();
stmt = db.compileStatement("insert into t_content_resource values (?,?,?,?,?)");
stmt.bindLong(1, 0);
stmt.bindString(2, "aaa");
stmt.bindString(3, "bbb");
stmt.bindDouble(4, 3.333);
stmt.bindBlob(5, new byte[]{0x00, 0x11, 0x22});
stmt.executeInsert();
db.setTransactionSuccessful();
db.endTransaction();
stmt.close();
}
}
package jp.agentec.abook.abv.bl.data.dao; package jp.agentec.abook.abv.bl.data.dao;
import jp.agentec.abook.abv.bl.common.db.SQLiteOpenHelper; import jp.agentec.abook.abv.bl.common.db.SQLiteOpenHelper;
import jp.agentec.abook.abv.bl.common.db.impl.JDBCSQLiteOpenHelper;
import jp.agentec.abook.abv.bl.data.DBConnector; import jp.agentec.abook.abv.bl.data.DBConnector;
import junit.framework.TestCase; import junit.framework.TestCase;
import junit.framework.TestSuite; import junit.framework.TestSuite;
......
package jp.agentec.abook.abv.bl.logic; package jp.agentec.abook.abv.bl.logic;
import jp.agentec.abook.abv.bl.common.db.SQLiteOpenHelper; import jp.agentec.abook.abv.bl.common.db.SQLiteOpenHelper;
import jp.agentec.abook.abv.bl.common.db.impl.JDBCSQLiteOpenHelper;
import jp.agentec.abook.abv.bl.data.DBConnector; import jp.agentec.abook.abv.bl.data.DBConnector;
import jp.agentec.abook.abv.bl.dto.ContentMemoDto; import jp.agentec.abook.abv.bl.dto.ContentMemoDto;
import jp.agentec.adf.util.DateTimeUtil; import jp.agentec.adf.util.DateTimeUtil;
......
...@@ -31,7 +31,6 @@ android { ...@@ -31,7 +31,6 @@ android {
resValue("integer", "product_code", "${product_code}") resValue("integer", "product_code", "${product_code}")
resValue("bool", "use_cache", "${use_cache}") resValue("bool", "use_cache", "${use_cache}")
resValue("integer", "cache_size", "${cache_size}") resValue("integer", "cache_size", "${cache_size}")
resValue("integer", "db_type", "${db_type}")
resValue("bool", "content_protected", "${content_protected}") resValue("bool", "content_protected", "${content_protected}")
resValue("bool", "pdf_thumbnail_output", "${pdf_thumbnail_output}") resValue("bool", "pdf_thumbnail_output", "${pdf_thumbnail_output}")
resValue("bool", "pdf_image_output", "${pdf_image_output}") resValue("bool", "pdf_image_output", "${pdf_image_output}")
......
package jp.agentec.abook.abv.bl.common.db.impl;
import jp.agentec.abook.abv.bl.common.db.Cursor;
import jp.agentec.abook.abv.bl.common.db.SQLiteDatabase;
import jp.agentec.abook.abv.bl.common.db.SQLiteStatement;
public class CipherSQLiteDatabase implements SQLiteDatabase {
private net.sqlcipher.database.SQLiteDatabase db;
public CipherSQLiteDatabase(net.sqlcipher.database.SQLiteDatabase db) {
this.db = db;
}
@Override
public void beginTransaction() {
db.beginTransaction();
}
@Override
public void endTransaction() {
db.endTransaction();
}
@Override
public void setTransactionSuccessful() {
db.setTransactionSuccessful();
}
@Override
public boolean inTransaction() {
return db.inTransaction();
}
@Override
public void close() {
db.close();
}
@Override
public jp.agentec.abook.abv.bl.common.db.Cursor query(String table, String[] columns, String selection,
String[] selectionArgs, String groupBy, String having,
String orderBy) {
return new StandardCursor(db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy));
}
@Override
public jp.agentec.abook.abv.bl.common.db.Cursor query(String table, String[] columns, String selection,
String[] selectionArgs, String groupBy, String having, String orderBy, String limit) {
return new StandardCursor(db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy, limit));
}
@Override
public jp.agentec.abook.abv.bl.common.db.Cursor rawQuery(String sql, String[] selectionArgs) {
return new StandardCursor(db.rawQuery(sql, selectionArgs));
}
// @Override
// public jp.agentec.abook.abv.core.database.Cursor rawQuery(String sql, String[] selectionArgs, int initialRead, int maxRead) {
// return new StandardCursor(db.rawQuery(sql, selectionArgs, initialRead, maxRead));
// }
@Override
public int delete(String table, String whereClause, String[] whereArgs) {
return db.delete(table, whereClause, whereArgs);
}
@Override
public void execSQL(String sql) {
db.execSQL(sql);
}
@Override
public void execSQL(String sql, Object[] bindArgs) {
db.execSQL(sql, bindArgs);
}
@Override
public boolean isOpen() {
return db.isOpen();
}
@Override
public boolean needUpgrade(int newVersion) {
return db.needUpgrade(newVersion);
}
@Override
public SQLiteStatement compileStatement(String sql) {
return new CipherSQLiteStatement(db.compileStatement(sql));
}
@Override
public Cursor query(boolean distinct, String table, String[] columns,
String selection, String[] selectionArgs, String groupBy,
String having, String orderBy, String limit) {
return new StandardCursor(db.query(distinct, table, columns, selection, selectionArgs, groupBy, having, orderBy, limit));
}
@Override
public String getPath() {
return db.getPath();
}
@Override
public int getVersion() {
return db.getVersion();
}
}
package jp.agentec.abook.abv.bl.common.db.impl;
import jp.agentec.abook.abv.bl.common.exception.ABVRuntimeException;
import jp.agentec.abook.abv.bl.common.log.Logger;
import jp.agentec.abook.abv.bl.data.ABVDataOpenHelper;
import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteOpenHelper;
import android.content.Context;
public class CipherSQLiteOpenHelper extends SQLiteOpenHelper implements jp.agentec.abook.abv.bl.common.db.SQLiteOpenHelper {
private static final String TAG = "CipherSQLiteOpenHelper";
public static ABVDataOpenHelper abhelper;
private Context context;
private boolean isLibLoaded = false;
public CipherSQLiteOpenHelper(Context context, String name, int version) {
super(context, name, null, version);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
abhelper.onCreate(new CipherSQLiteDatabase(db));
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
abhelper.onUpgrade(new CipherSQLiteDatabase(db), oldVersion, newVersion);
}
public boolean isCreated() {
return abhelper.isCreated();
}
public Throwable getLastError() {
return abhelper.getLastError();
}
@Override
public jp.agentec.abook.abv.bl.common.db.SQLiteDatabase openDatabase() {
if (context == null) {
throw new ABVRuntimeException("Android context is null. You must call init()");
}
if (!isLibLoaded) {
Logger.i("ABook", "SQLCipher loadLibs !!!");
net.sqlcipher.database.SQLiteDatabase.loadLibs(context);
isLibLoaded = true;
}
CipherSQLiteOpenHelper.abhelper = new ABVDataOpenHelper();
Logger.i(TAG, "open cipher database start.");
jp.agentec.abook.abv.bl.common.db.SQLiteDatabase db = new CipherSQLiteDatabase(getWritableDatabase("pass")); // このタイミングでDBが生成され、onCreateが呼び出される
Logger.i(TAG, "open cipher database completed");
if (!isCreated()) {
throw new ABVRuntimeException("Can't open or create database.", getLastError());
}
return db;
}
}
package jp.agentec.abook.abv.bl.common.db.impl;
import jp.agentec.abook.abv.bl.common.db.SQLiteStatement;
import jp.agentec.abook.abv.bl.common.log.Logger;
public class CipherSQLiteStatement implements SQLiteStatement {
private net.sqlcipher.database.SQLiteStatement stmt;
public CipherSQLiteStatement(net.sqlcipher.database.SQLiteStatement stmt) {
this.stmt = stmt;
}
@Override
public void execute() {
stmt.execute();
}
@Override
public long executeInsert() {
return stmt.executeInsert();
}
@Override
public long simpleQueryForLong() {
return stmt.simpleQueryForLong();
}
@Override
public String simpleQueryForString() {
return stmt.simpleQueryForString();
}
@Override
public void bindNull(int index) {
stmt.bindNull(index);
}
@Override
public void bindLong(int index, long value) {
stmt.bindLong(index, value);
}
@Override
public void bindDouble(int index, double value) {
stmt.bindDouble(index, value);
}
@Override
public void bindString(int index, String value) {
stmt.bindString(index, value);
}
@Override
public void bindBlob(int index, byte[] value) {
stmt.bindBlob(index, value);
}
@Override
public void close() {
try {
stmt.close();
} catch (Exception e) { // finallyで呼ばれるため例外はスローしないようにする
Logger.e("CipherSQLiteStatement", "close error.", e);
}
}
}
...@@ -6,8 +6,6 @@ import jp.agentec.abook.abv.bl.common.Constant; ...@@ -6,8 +6,6 @@ import jp.agentec.abook.abv.bl.common.Constant;
import jp.agentec.abook.abv.bl.common.Constant.AspectType; import jp.agentec.abook.abv.bl.common.Constant.AspectType;
import jp.agentec.abook.abv.bl.common.db.SQLiteDatabase; import jp.agentec.abook.abv.bl.common.db.SQLiteDatabase;
import jp.agentec.abook.abv.bl.common.db.SQLiteOpenHelper; import jp.agentec.abook.abv.bl.common.db.SQLiteOpenHelper;
import jp.agentec.abook.abv.bl.common.db.impl.CipherSQLiteOpenHelper;
import jp.agentec.abook.abv.bl.common.db.impl.JDBCSQLiteOpenHelper;
import jp.agentec.abook.abv.bl.common.db.impl.StandardSQLiteOpenHelper; import jp.agentec.abook.abv.bl.common.db.impl.StandardSQLiteOpenHelper;
import jp.agentec.abook.abv.bl.common.exception.ABVException; import jp.agentec.abook.abv.bl.common.exception.ABVException;
import jp.agentec.abook.abv.bl.common.log.LogLevel; import jp.agentec.abook.abv.bl.common.log.LogLevel;
...@@ -159,16 +157,7 @@ public class Initializer { ...@@ -159,16 +157,7 @@ public class Initializer {
DBConnector conn = DBConnector.getInstance(); DBConnector conn = DBConnector.getInstance();
if (!conn.isOpen()) { if (!conn.isOpen()) {
Logger.i(TAG, "initDB start"); Logger.i(TAG, "initDB start");
SQLiteOpenHelper sqlLiteOpenHelper; SQLiteOpenHelper sqlLiteOpenHelper = new StandardSQLiteOpenHelper(context, DBConnector.DatabaseName, DBConnector.DatabaseVersion);
if (i(R.integer.db_type) == DBConnector.DB_TYPE_CIPHER) {
sqlLiteOpenHelper = new CipherSQLiteOpenHelper(context, DBConnector.DatabaseName, DBConnector.DatabaseVersion);
}
else if (i(R.integer.db_type) == DBConnector.DB_TYPE_JDBC) {
sqlLiteOpenHelper = new JDBCSQLiteOpenHelper(DBConnector.DatabaseName, DBConnector.DatabaseVersion);
}
else {
sqlLiteOpenHelper = new StandardSQLiteOpenHelper(context, DBConnector.DatabaseName, DBConnector.DatabaseVersion);
}
conn.setSqlLiteOpenHelper(sqlLiteOpenHelper); conn.setSqlLiteOpenHelper(sqlLiteOpenHelper);
SQLiteDatabase db = conn.getDatabase(); SQLiteDatabase db = conn.getDatabase();
db.execSQL("PRAGMA foreign_keys=ON;journal_mode=WAL;synchronous=OFF;"); // foreign keyを有効,ジャーナルモードをWAL,書き込み非同期にする db.execSQL("PRAGMA foreign_keys=ON;journal_mode=WAL;synchronous=OFF;"); // foreign keyを有効,ジャーナルモードをWAL,書き込み非同期にする
......
...@@ -66,8 +66,6 @@ use_cache=true ...@@ -66,8 +66,6 @@ use_cache=true
#コンテンツリソースをcacheフォルダに展開する数 #コンテンツリソースをcacheフォルダに展開する数
cache_size=0 cache_size=0
#0:標準SQLite, 1:SQLCipher
db_type=0
#コンテンツのHead暗号化するかしないか #コンテンツのHead暗号化するかしないか
content_protected=true content_protected=true
#use_cacheと同様にPDFサムネイルimageをファイル化するかしないか #use_cacheと同様にPDFサムネイルimageをファイル化するかしないか
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment