2009年2月5日木曜日

GWT 1.5.3 + Gears 1.1 Library で作る PropertiesManager

GWTプロジェクトに'Gears 1.1 Library'をclass pathに追加して利用した.
(Gears 1.1 Library はGoogle Gearsを利用するためのライブラリ.)

もっとまともなSQLがかける人のを参考にするべきかもしれませんよ♪.

PropertiesManager.java
import java.util.HashMap;

import com.google.gwt.gears.client.Factory;
import com.google.gwt.gears.client.database.Database;
import com.google.gwt.gears.client.database.DatabaseException;
import com.google.gwt.gears.client.database.ResultSet;

public class PropertiesManager {
private Database _database = null;

private String _tableName = null;

private HashMap<String, String> _hash = new HashMap<String, String>();

public PropertiesManager(String dbName, String tableName) {
if (dbName != null && dbName.length() > 0) {
_database = PropertiesManager.checkDatabase(dbName);
if (_database != null) {
if (tableName != null && tableName.length() > 0) {
_tableName = tableName;
if (PropertiesManager.checkTable(_database, _tableName) == false) {
PropertiesManager.createTable(_database, _tableName);
}
}
}
}
}

public Database get_database() {
return _database;
}

public String getProperty(String key) {
String ret = null;
if (_database != null)
ret = PropertiesManager.selectProp(_database, _tableName, key);
else
ret = _hash.get(key);
return ret;
}

public void setProperty(String key, String value) {
if (_database != null) {
String oldValue = PropertiesManager
.selectProp(_database, _tableName, key);
if (oldValue == null) {
PropertiesManager.insertProp(_database, _tableName, key, value);
} else {
PropertiesManager.updateProp(_database, _tableName, key, value);
}
}
_hash.put(key, value);
}

public static Database checkDatabase(String dbName) {
Database db = null;
try {
Factory factory = Factory.getInstance();
if (factory != null) {
db = factory.createDatabase();
db.open(dbName);
}
} catch (Exception e) {
}
return db;
}

public static boolean createTable(Database db, String tableName) {
if (db == null || tableName == null || tableName.length() == 0) {
return false;
}
boolean ret = false;
try {
db.execute("CREATE TABLE IF NOT EXISTS " + tableName
+ " (id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ " key TEXT, value TEXT)", new String[] {});
ret = true;
} catch (DatabaseException e) {
}
return ret;
}

public static boolean checkTable(Database db, String tableName) {
if (db == null || tableName == null || tableName.length() == 0) {
return false;
}
boolean ret = false;
try {
ResultSet rs = db.execute("SELECT COUNT(*) FROM " + tableName,
new String[] {});
if (rs != null) {
ret = true;
}
} catch (DatabaseException e) {
}
return ret;
}

public static boolean removeProp(Database db, String tableName, String key) {
if (db == null || tableName == null || tableName.length() == 0) {
return false;
}
boolean ret = false;
try {
db.execute("DELETE FROM " + tableName + " WHERE key = ?",
new String[] { key });
ret = true;
} catch (DatabaseException e) {
}
return ret;
}

public static boolean insertProp(Database db, String tableName, String key,
String value) {
if (db == null || tableName == null || tableName.length() == 0) {
return false;
}
boolean ret = false;
try {
db.execute("INSERT INTO " + tableName + " VALUES(NULL, ?, ?)",
new String[] { key, value });
ret = true;
} catch (DatabaseException e) {
}
return ret;
}

public static boolean updateProp(Database db, String tableName, String key,
String value) {
if (db == null || tableName == null || tableName.length() == 0) {
return false;
}
boolean ret = false;
try {
db.execute("UPDATE " + tableName + " SET value=? WHERE key = ?",
new String[] { value, key });

ret = true;
} catch (DatabaseException e) {
}
return ret;
}

public static String selectProp(Database db, String tableName, String key) {
String ret = null;
if (db == null || tableName == null || tableName.length() == 0) {
return null;
}
try {
ResultSet rs = db.execute("SELECT value FROM " + tableName
+ " WHERE key = ?", new String[] { key });
if (rs != null && rs.isValidRow()) {
ret = rs.getFieldAsString(0);
//
rs.next();
if (rs.isValidRow()) {
PropertiesManager.removeProp(db, tableName, key);
PropertiesManager.insertProp(db, tableName, key, ret);
}
}
} catch (DatabaseException e) {
}
return ret;
}
}


  • Factory.getInstance();の戻り値がからの場合はGearsが入っていない(んだと思う).
  • 結果を参照する時は以下のようにする作法(?)らしい?.
        ResultSet rs = db.execute("SELECT 句", new String[] { ... });
    for(;rs.isValidRow();rs.next())
    {
    ...
    }


db, table, hashの用意
PropertiesManager _propMgr = new PropertiesManager("DBNAME", "TABLENAME");

getter(dbが無かった時はhashからget)
String PROPERTY_VALUE = _propMgr.getProperty("PROPERTY_KEY");

setter(dbが無かった時はhashにset)
_propMgr.setProperty("PROPERTY_KEY", "PROPERTY_VALUE");


繰り返しますが,もっとまともなSQLがかける人のを参考にするべきかもしれませんよ♪.

一応,GWTFusenで利用中.

0 件のコメント: