Skip to content

Commit

Permalink
Merge pull request #91 from mehdirande/master
Browse files Browse the repository at this point in the history
WebSQL : When dropping a store, also drop multi-entry indexes.
  • Loading branch information
yathit committed Dec 25, 2015
2 parents 2ac3631 + 99b3752 commit 39b5784
Showing 1 changed file with 45 additions and 10 deletions.
55 changes: 45 additions & 10 deletions src/ydn/db/conn/websql.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,17 @@ ydn.db.con.WebSql.prototype.connect = function(dbname, schema) {
var edited_schema = schema;
edited_schema.addStore(info_store);
} else {
var sql = 'DROP TABLE ' + info_store.getQuotedName();
goog.log.finer(me.logger, sql);
tx.executeSql(sql, [],
function(tr) {
// ok
}, function(tx, e) {
throw e;
});
var sqls = me.prepareDropTable_(info_store);
for (var i = 0; i < sqls.length; i++) {
var sql = sqls[i];
goog.log.finer(me.logger, sql);
tx.executeSql(sql, [],
function(tr) {
// ok
}, function(tx, e) {
throw e;
});
}
}
}
}
Expand Down Expand Up @@ -429,6 +432,39 @@ ydn.db.con.WebSql.prototype.onError = function(e) {};
ydn.db.con.WebSql.CREATE_INDEX = false;


/**
* Prepare SQL statement for dropping the table and all columns.
*
* @private
* @param {ydn.db.schema.Store} table table schema.
* @return {!Array.<string>} SQL statement for dropping the table.
*/
ydn.db.con.WebSql.prototype.prepareDropTable_ = function(table) {

var drop_statement = 'DROP TABLE IF EXISTS ';

var sqls = [];
sqls.push(drop_statement + +goog.string.quote(table.getName()));

for (var i = 0, n = table.countIndex(); i < n; i++) {
/**
* @type {ydn.db.schema.Index}
*/
var index = table.index(i);
if (index.isMultiEntry()) {
// create separate table for multiEntry
var idx_name = ydn.db.base.PREFIX_MULTIENTRY + table.getName() + ':' +
index.getName();
var multi_entry_sql = drop_statement + goog.string.quote(idx_name);
sqls.push(multi_entry_sql);
}

}

return sqls;
};


/**
* Initialize variable to the schema and prepare SQL statement for creating
* the table.
Expand Down Expand Up @@ -833,8 +869,7 @@ ydn.db.con.WebSql.prototype.update_store_with_info_ = function(trans,
goog.log.warning(this.logger,
'table: ' + table_schema.getName() + ' has changed by ' + msg +
' ALTER TABLE cannot run in WebSql, dropping old table.');
sqls.unshift('DROP TABLE IF EXISTS ' +
goog.string.quote(table_schema.getName()));
sqls.unshift.apply(sqls, this.prepareDropTable_(existing_table_schema));
}
}

Expand Down

0 comments on commit 39b5784

Please sign in to comment.