Navigation Menu

Skip to content

Commit

Permalink
Detect domain id from the related table automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Aug 1, 2012
1 parent d4112b3 commit 03bf24a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
15 changes: 15 additions & 0 deletions lib/database/domain.js
Expand Up @@ -68,6 +68,7 @@ Domain.prototype = {
initializeNameAndId: function(source) {
if (typeof source == 'string') {
this.name = source;
this.id = this.getIdFromTable(this.toTableNamePart(this.name));
return;
}

Expand Down Expand Up @@ -99,6 +100,20 @@ Domain.prototype = {
throw new Error('no domain name');
},
getIdFromTable: function(tableName) {
if (this.context) {
var tables = this.context.tableListSync();
var tableIdMatcher = new RegExp('^' + tableName + '_([^_]+)$');
var id;
if (tables.some(function(table) {
var match = table.name.match(tableIdMatcher);
if (match) {
id = match[1];
return true;
}
return false;
}, this))
return id;
}
return this.id;
},

Expand Down
5 changes: 5 additions & 0 deletions lib/wrapped-nroonga.js
Expand Up @@ -80,6 +80,11 @@ Context.prototype = {
return deferred;
},

tableListSync: function(tableName) {
var result = this.commandSync('table_list');
return formatResults(result);
},

columnListSync: function(tableName) {
var result = this.commandSync('column_list', { table: tableName });
return formatResults(result);
Expand Down
11 changes: 10 additions & 1 deletion test/database-domain.test.js
Expand Up @@ -202,7 +202,7 @@ suite('database', function() {
setup(function() {
temporaryDatabase = utils.createTemporaryDatabase();
context = temporaryDatabase.get();
utils.loadDumpFile(context, __dirname + '/fixture/companies/ddl.grn');
utils.loadDumpFile(context, __dirname + '/fixture/companies/ddl-custom-id.grn');
domain = new Domain('companies', context);
});

Expand All @@ -212,6 +212,15 @@ suite('database', function() {
temporaryDatabase = undefined;
});

test('id from database (known table)', function() {
assert.equal(domain.id, 'id0123');
});

test('id from database (unknown table)', function() {
domain = new Domain('unknown', context);
assert.equal(domain.id, Domain.DEFAULT_DOMAIN_ID);
});

test('indexFields', function() {
var fields = domain.indexFields;
fields = fields.map(function(field) {
Expand Down
16 changes: 16 additions & 0 deletions test/fixture/companies/ddl-custom-id.grn
@@ -0,0 +1,16 @@
table_create companies_id0123_product TABLE_HASH_KEY ShortText
table_create companies_id0123_age TABLE_HASH_KEY UInt32
table_create companies_id0123_BigramTerms TABLE_PAT_KEY|KEY_NORMALIZE ShortText --default_tokenizer TokenBigram
table_create companies_id0123 TABLE_HASH_KEY ShortText
column_create companies_id0123 address COLUMN_SCALAR ShortText
column_create companies_id0123 age COLUMN_SCALAR UInt32
column_create companies_id0123 description COLUMN_SCALAR ShortText
column_create companies_id0123 email_address COLUMN_SCALAR ShortText
column_create companies_id0123 name COLUMN_SCALAR ShortText
column_create companies_id0123 product COLUMN_SCALAR companies_id0123_product
column_create companies_id0123_BigramTerms companies_name COLUMN_INDEX|WITH_POSITION companies_id0123 name
column_create companies_id0123_BigramTerms companies_email_address COLUMN_INDEX|WITH_POSITION companies_id0123 email_address
column_create companies_id0123_BigramTerms companies_description COLUMN_INDEX|WITH_POSITION companies_id0123 description
column_create companies_id0123_BigramTerms companies_address COLUMN_INDEX|WITH_POSITION companies_id0123 address
column_create companies_id0123_age companies_age COLUMN_INDEX|WITH_POSITION companies_id0123 age
column_create companies_id0123_product companies_product COLUMN_INDEX|WITH_POSITION companies_id0123 product

0 comments on commit 03bf24a

Please sign in to comment.