null+****@clear*****
null+****@clear*****
2012年 8月 2日 (木) 13:12:51 JST
SHIMODA Hiroshi 2012-08-02 13:12:51 +0900 (Thu, 02 Aug 2012) New Revision: 8409fd46650e211a27f1bac3c3ec5d3bb52d5230 https://github.com/groonga/gcs/commit/8409fd46650e211a27f1bac3c3ec5d3bb52d5230 Log: Add "Domain#exists()" to know the table exists or not Modified files: lib/database/domain.js test/database-domain.test.js Modified: lib/database/domain.js (+12 -4) =================================================================== --- lib/database/domain.js 2012-08-02 12:34:18 +0900 (12b1e19) +++ lib/database/domain.js 2012-08-02 13:12:51 +0900 (15d4f2a) @@ -68,13 +68,13 @@ Domain.prototype = { initializeNameAndId: function(source) { if (typeof source == 'string') { this.name = source; - this.id = this.getIdForTable(this.toTableNamePart(this.name)); + this.initializeId(); return; } if (source.query && source.query.DomainName) { this.name = source.query.DomainName; - this.id = this.getIdForTable(this.toTableNamePart(this.name)); + this.initializeId(); return; } @@ -99,8 +99,12 @@ Domain.prototype = { throw new Error('no domain name'); }, - getIdForTable: function(tableName) { + initializeId: function() { + this.id = this.getIdFromDatabase() || this.createNewId(); + }, + getIdFromDatabase: function() { if (this.context) { + var tableName = this.toTableNamePart(this.name); var tables = this.context.tableListSync(); var tableIdMatcher = new RegExp('^' + tableName + '_([^_]+)$'); var id; @@ -114,7 +118,7 @@ Domain.prototype = { }, this)) return id; } - return this.createNewId(); + return null; }, createNewId: function() { var size = 26; @@ -278,6 +282,10 @@ Domain.prototype = { return tables.some(function(table) { return table.name === self.synonymTableName; }); + }, + + exists: function() { + return !!this.getIdFromDatabase(); } }; Modified: test/database-domain.test.js (+8 -5) =================================================================== --- test/database-domain.test.js 2012-08-02 12:34:18 +0900 (fcc732d) +++ test/database-domain.test.js 2012-08-02 13:12:51 +0900 (2ade44f) @@ -216,16 +216,19 @@ suite('database', function() { }); test('id for database (known table)', function() { - assert.equal(domain.id, 'id0123'); + assert.equal({ id: domain.id, exists: domain.exists() }, + { id: 'id0123', exists: true }); }); test('id for database (unknown, new table)', function() { domain = new Domain('unknown', context); assert.equal(typeof domain.id, 'string'); - assert.deepEqual({ length: domain.id.length, - normalized: domain.id.replace(/[1-9a-z]/g, '0') }, - { length: 26, - normalized: Domain.DEFAULT_ID }); + assert.deepEqual({ idLength: domain.id.length, + normalizedId: domain.id.replace(/[1-9a-z]/g, '0'), + exists: domain.exists() }, + { idLength: 26, + normalizedId: Domain.DEFAULT_ID, + exists: false }); }); test('indexFields', function() { -------------- next part -------------- HTML$B$NE:IU%U%!%$%k$rJ]4I$7$^$7$?(B... Download