* clean up, document data binding

* try and handle exceptions involving transactions, but fail at it.  we need to perform some kind of cleanup, apparently.
This commit is contained in:
Andrew Sutherland 2008-08-03 23:41:35 -07:00
Родитель 17eadfc612
Коммит 6157c342ab
3 изменённых файлов: 66 добавлений и 23 удалений

Просмотреть файл

@ -52,8 +52,9 @@ Cu.import("resource://gloda/modules/log4moz.js");
function GlodaDatabind(aTableDef, aDatastore) {
this._tableDef = aTableDef;
this._datastore = aDatastore;
this._log = Log4Moz.Service.getLogger("gloda.databind." + aTableDef.name);
let insertSql = "INSERT INTO " + this._tableDef.name + " (" +
let insertSql = "INSERT INTO " + this._tableDef._realName + " (" +
[coldef[0] for each
(coldef in this._tableDef.columns)].join(", ") +
") VALUES (" +
@ -69,7 +70,7 @@ function GlodaDatabind(aTableDef, aDatastore) {
GlodaDatabind.prototype = {
getHighId: function(aLessThan) {
let sql = "select MAX(id) AS m_id FROM " + this._tableDef.name;
let sql = "select MAX(id) AS m_id FROM " + this._tableDef._realName;
if (aLessThan !== undefined)
sql += " WHERE id < " + aLessThan;
dump("SQL: " + sql);
@ -89,7 +90,7 @@ GlodaDatabind.prototype = {
let stmt;
if (!(aColName in this._stmtCache)) {
stmt = this._datastore._createStatement("SELECT * FROM " +
this._tableDef.name + " WHERE " + aColName + " = :value");
this._tableDef._realName + " WHERE " + aColName + " = :value");
this._stmtCache[aColName] = stmt;
}
else
@ -112,6 +113,7 @@ GlodaDatabind.prototype = {
insert: function(aValueDict) {
let stmt = this._insertStmt;
for each (let coldef in this._tableDef.columns) {
this._log.debug("insert arg: " + coldef[0] + "=" + aValueDict[coldef[0]]);
stmt.params[coldef[0]] = aValueDict[coldef[0]];
}
stmt.execute();

Просмотреть файл

@ -288,23 +288,37 @@ let GlodaDatastore = {
* we don't want to have to parse the column names out.
*/
createTableIfNotExists: function gloda_ds_createTableIfNotExists(aTableDef) {
aTableDef._realName = "plugin_" + aTableDef.name;
// first, check if the table exists
let testTableSql = "SELECT * FROM sqlite_master WHERE type='table' AND " +
"name = '" + aTableDef.name + "'";
"name = '" + aTableDef._realName + "'";
let testTableStmt = this._createStatement(testTableSql);
if (!testTableStmt.step()) {
this.dbConnection.createTable(aTableDef.name,
[coldef.join(" ") for each
try {
this.dbConnection.createTable(aTableDef._realName,
[coldef.join(" ") for each
(coldef in aTableDef.columns)].join(", "));
for (let indexName in table.indices) {
let indexColumns = table.indices[indexName];
this.dbConnection.executeSimpleSQL(
"CREATE INDEX " + indexName + " ON " + tableName +
"(" + indexColumns.join(", ") + ")");
}
catch (ex) {
this._log.error("Problem creating table " + aTableDef.name + " " +
"because: " + ex + " at " + ex.fileName + ":" + ex.lineNumber);
return null;
}
for (let indexName in aTableDef.indices) {
let indexColumns = aTableDef.indices[indexName];
try {
let indexSql = "CREATE INDEX " + indexName + " ON " +
aTableDef._realName + " (" + indexColumns.join(", ") + ")";
this.dbConnection.executeSimpleSQL(indexSql);
}
catch (ex) {
this._log.error("Problem creating index " + indexName + " for " +
"table " + aTableDef.name + " because " + ex + " at " +
ex.fileName + ":" + ex.lineNumber);
}
}
}
testTableStmt.reset();
@ -359,10 +373,15 @@ let GlodaDatastore = {
_commitTransaction: function gloda_ds_commitTransaction() {
this._transactionDepth--;
if (this._transactionDepth == 0) {
if (this._transactionGood)
this.dbConnection.commitTransaction();
else
this.dbConnection.rollbackTransaction();
try {
if (this._transactionGood)
this.dbConnection.commitTransaction();
else
this.dbConnection.rollbackTransaction();
}
catch (ex) {
this._log.error("Commit problem: " + ex);
}
}
},
/**
@ -374,7 +393,12 @@ let GlodaDatastore = {
this._transactionDepth--;
this._transactionGood = false;
if (this._transactionDepth == 0) {
this.dbConnection.rollbackTransaction();
try {
this.dbConnection.rollbackTransaction();
}
catch (ex) {
this._log.error("Rollback problem: " + ex);
}
}
},
@ -932,6 +956,11 @@ let GlodaDatastore = {
try {
for (let iAttribute=0; iAttribute < aAttributes.length; iAttribute++) {
let attribValueTuple = aAttributes[iAttribute];
this._log.debug("inserting conv:" + aMessage.conversationID +
" message:" + aMessage.id +
" attributeID:" + attribValueTuple[0] +
" value:" + attribValueTuple[1]);
imas.params.conversationID = aMessage.conversationID;
imas.params.messageID = aMessage.id;

Просмотреть файл

@ -299,7 +299,7 @@ let Gloda = {
if (!(aSubjectType in this._nounIDToMeta))
throw Error("Invalid subject type: " + aSubjectType);
let objectCoerce = this._nounIDToMeta[aObjectType].fromParamAndValue;
let nounMeta = this._nounIDToMeta[aObjectType];
let storageName = "__" + aBindName;
let getter;
@ -311,7 +311,7 @@ let Gloda = {
let instances = this.getAttributeInstances(aAttr);
let val;
if (instances.length > 0)
val = objectCoerce(instances[0][1], instances[0][2]);
val = nounMeta.fromParamAndValue(instances[0][1], instances[0][2]);
else
val = null;
this[storageName] = val;
@ -326,7 +326,8 @@ let Gloda = {
if (instances.length > 0) {
values = [];
for (let iInst=0; iInst < instances.length; iInst++) {
values.push(objectCoerce(instances[iInst][1], instances[iInst][2]));
values.push(nounMeta.fromParamAndValue(instances[iInst][1],
instances[iInst][2]));
}
}
else {
@ -501,7 +502,18 @@ let Gloda = {
return GlodaDatastore._attributes[compoundName];
},
defineTable: function(aTableDef) {
/**
* Define a table for plug-ins. The argument should be a dictionary with
* the following keys:
* @param name The table name; don't conflict with other things!
* @param columns A list of [column name, sqlite type] tuples. You should
* always include a definition like ["id", "INTEGER PRIMARY KEY"] for
* now.
* @param indices A dictionary of lists of column names, where the key name
* becomes the index name. Ex: {foo: ["bar"]} results in an index on
* the column "bar" where the index is named "foo".
*/
defineTable: function gloda_ns_defineTable(aTableDef) {
return GlodaDatastore.createTableIfNotExists(aTableDef);
},