Depot implementation of the Config persistence - hopefully schema compatible with the old version.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4783 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Par Winzell
2007-07-26 20:04:27 +00:00
parent 363cf719ec
commit 29bfb576d3
2 changed files with 127 additions and 57 deletions
@@ -0,0 +1,113 @@
//
// $Id: PuzzleManagerDelegate.java 209 2007-02-24 00:37:33Z mdb $
//
// Vilya library - tools for developing networked games
// Copyright (C) 2002-2007 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/vilya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.admin.server.persist;
import com.samskivert.jdbc.depot.Key;
import com.samskivert.jdbc.depot.PersistentRecord;
import com.samskivert.jdbc.depot.annotation.Column;
import com.samskivert.jdbc.depot.annotation.Entity;
import com.samskivert.jdbc.depot.annotation.Id;
import com.samskivert.jdbc.depot.expression.ColumnExp;
@Entity(name="CONFIG")
public class ConfigRecord extends PersistentRecord
{
// AUTO-GENERATED: FIELDS START
/** The column identifier for the {@link #node} field. */
public static final String NODE = "node";
/** The qualified column identifier for the {@link #node} field. */
public static final ColumnExp NODE_C =
new ColumnExp(ConfigRecord.class, NODE);
/** The column identifier for the {@link #object} field. */
public static final String OBJECT = "object";
/** The qualified column identifier for the {@link #object} field. */
public static final ColumnExp OBJECT_C =
new ColumnExp(ConfigRecord.class, OBJECT);
/** The column identifier for the {@link #field} field. */
public static final String FIELD = "field";
/** The qualified column identifier for the {@link #field} field. */
public static final ColumnExp FIELD_C =
new ColumnExp(ConfigRecord.class, FIELD);
/** The column identifier for the {@link #value} field. */
public static final String VALUE = "value";
/** The qualified column identifier for the {@link #value} field. */
public static final ColumnExp VALUE_C =
new ColumnExp(ConfigRecord.class, VALUE);
// AUTO-GENERATED: FIELDS END
public static final int SCHEMA_VERSION = 2;
@Id
@Column(name="NODE", length=64)
public String node;
/** The code of the stat. */
@Id
@Column(name="OBJECT", length=128)
public String object;
@Id
@Column(name="FIELD", length=64)
public String field;
@Column(name="VALUE", type="TEXT")
public String value;
/**
* An empty constructor for unmarshalling.
*/
public ConfigRecord ()
{
super();
}
public ConfigRecord (String node, String object, String field, String value)
{
super();
this.node = node;
this.object = object;
this.field = field;
this.value = value;
}
// AUTO-GENERATED: METHODS START
/**
* Create and return a primary {@link Key} to identify a {@link #ConfigRecord}
* with the supplied key values.
*/
public static Key<ConfigRecord> getKey (String node, String object, String field)
{
return new Key<ConfigRecord>(
ConfigRecord.class,
new String[] { NODE, OBJECT, FIELD },
new Comparable[] { node, object, field });
}
// AUTO-GENERATED: METHODS END
}
@@ -21,25 +21,20 @@
package com.threerings.admin.server.persist; package com.threerings.admin.server.persist;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Set;
import com.samskivert.io.PersistenceException; import com.samskivert.io.PersistenceException;
import com.samskivert.jdbc.ConnectionProvider; import com.samskivert.jdbc.ConnectionProvider;
import com.samskivert.jdbc.DatabaseLiaison; import com.samskivert.jdbc.depot.DepotRepository;
import com.samskivert.jdbc.JDBCUtil; import com.samskivert.jdbc.depot.PersistenceContext;
import com.samskivert.jdbc.JORARepository; import com.samskivert.jdbc.depot.PersistentRecord;
import com.samskivert.jdbc.jora.Table; import com.samskivert.jdbc.depot.clause.Where;
import com.threerings.admin.Log;
/** /**
* Stores configuration information in a database table. * Stores configuration information in a database table.
*/ */
public class ConfigRepository extends JORARepository public class ConfigRepository extends DepotRepository
{ {
/** The identifier used when establishing a database connection. */ /** The identifier used when establishing a database connection. */
public static final String CONFIG_DB_IDENT = "configdb"; public static final String CONFIG_DB_IDENT = "configdb";
@@ -51,7 +46,7 @@ public class ConfigRepository extends JORARepository
public ConfigRepository (ConnectionProvider conprov) public ConfigRepository (ConnectionProvider conprov)
throws PersistenceException throws PersistenceException
{ {
super(conprov, CONFIG_DB_IDENT); super(new PersistenceContext(CONFIG_DB_IDENT, conprov));
} }
/** /**
@@ -63,13 +58,10 @@ public class ConfigRepository extends JORARepository
public HashMap<String,String> loadConfig (String node, String object) public HashMap<String,String> loadConfig (String node, String object)
throws PersistenceException throws PersistenceException
{ {
ArrayList<ConfigDatum> list = loadAll(
_ctable, "where OBJECT = " + JDBCUtil.escape(object) +
" and NODE = " + JDBCUtil.escape(node));
HashMap<String,String> data = new HashMap<String,String>(); HashMap<String,String> data = new HashMap<String,String>();
for (int ii = 0, ll = list.size(); ii < ll; ii++) { Where where = new Where(ConfigRecord.OBJECT_C, object, ConfigRecord.NODE_C, node);
ConfigDatum datum = list.get(ii); for (ConfigRecord record : findAll(ConfigRecord.class, where)) {
data.put(datum.field, datum.value); data.put(record.field, record.value.toString());
} }
return data; return data;
} }
@@ -80,47 +72,12 @@ public class ConfigRepository extends JORARepository
public void updateConfig (String node, String object, String field, String value) public void updateConfig (String node, String object, String field, String value)
throws PersistenceException throws PersistenceException
{ {
ConfigDatum datum = new ConfigDatum(); store(new ConfigRecord(node, object, field, value));
datum.node = node;
datum.object = object;
datum.field = field;
datum.value = value;
store(_ctable, datum);
} }
// documentation inherited @Override // from DepotRepository
protected void migrateSchema (Connection conn, DatabaseLiaison liaison) protected void getManagedRecords (Set<Class<? extends PersistentRecord>> classes)
throws SQLException, PersistenceException
{ {
JDBCUtil.createTableIfMissing(conn, "CONFIG", new String[] { classes.add(ConfigRecord.class);
"NODE VARCHAR(64) NOT NULL",
"OBJECT VARCHAR(128) NOT NULL",
"FIELD VARCHAR(64) NOT NULL",
"VALUE TEXT NOT NULL",
"PRIMARY KEY (NODE, OBJECT, FIELD)",
}, "");
// TEMP: add NODE column
if (!JDBCUtil.tableContainsColumn(conn, "CONFIG", "NODE")) {
JDBCUtil.addColumn(conn, "CONFIG", "NODE", "VARCHAR(64) NOT NULL FIRST", null);
Statement stmt = conn.createStatement();
try {
stmt.executeUpdate(
"alter table CONFIG drop PRIMARY KEY, add PRIMARY KEY (NODE, OBJECT, FIELD)");
} finally {
stmt.close();
}
}
// END TEMP
} }
// documentation inherited
protected void createTables ()
{
_ctable = new Table<ConfigDatum>(
ConfigDatum.class, "CONFIG",
new String[] { "NODE", "OBJECT", "FIELD" }, true);
}
protected Table<ConfigDatum> _ctable;
} }