Skip to content
This repository was archived by the owner on May 20, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ module.exports = pgMetadata
module.exports.createQuery = createQuery
module.exports.createMetadataObject = createMetadataObject
var informationSchemaFields = module.exports.informationSchemaFields = [
'column_name',
'udt_name',
'data_type',
'column_name',
'udt_name',
'data_type',
'character_maximum_length',
'table_name',
'table_schema',
Expand Down Expand Up @@ -49,11 +49,11 @@ function pgMetadata(connection, options, callback) {
connection.query(query, function (err, result) {
if (err) {
callback(err)
} else {
} else {
var schema = createMetadataObject(result.rows)
debug(schema)
callback(null, schema)
}
callback(null, schema)
}
})
}

Expand All @@ -73,7 +73,7 @@ function createQuery(opts) {
if (opts.schema) {
whereClause.push('table_schema=%L')
values.push(opts.schema)
}
}

if (opts.database) {
whereClause.push('table_catalog=%L')
Expand All @@ -92,7 +92,7 @@ function createMetadataObject(resultSet) {

for (var i = 0; i < resultSet.length; i++) {
var row = resultSet[i]

var key = row.column_name
var table = row.table_name
var schema = row.table_schema
Expand All @@ -106,18 +106,18 @@ function createMetadataObject(resultSet) {
var schema = database[row.table_schema]

if (!schema) {
database[row.table_schema] = schema = {}
database[row.table_schema] = schema = {}
}

var table = schema[row.table_name]

if (!table) {
schema[row.table_name] = table = {}
}

var s = table[key] = {
type: row.udt_name,
required: row.is_nullable ? true : false
required: row.is_nullable === 'NO' || row.is_nullable === false
}

if (s.type.indexOf('char')>=0 || s.type.indexOf('text')>=0) {
Expand Down
22 changes: 12 additions & 10 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ describe('pg-metadata', function () {
table_name: 'atable',
table_schema: 'aschema',
table_catalog: 'adb',
is_nullable: true,
is_nullable: false,
numeric_precision: null,
numeric_scale: null,
numeric_precision_radix: null,
datetime_precision: null,
interval_type: null,
interval_precision: null
},
{
{
character_maximum_length: 244,
column_name: 'b',
udt_name: 'varchar',
Expand Down Expand Up @@ -71,6 +71,7 @@ describe('pg-metadata', function () {
table_name: 'ctable',
table_schema: 'cschema',
table_catalog: 'cdb',
is_nullable: 'NO',
numeric_precision: 24,
numeric_scale: null,
numeric_precision_radix: 2,
Expand All @@ -84,6 +85,7 @@ describe('pg-metadata', function () {
table_name: 'ctable',
table_schema: 'cschema',
table_catalog: 'cdb',
is_nullable: 'YES',
numeric_precision: 12,
numeric_scale: 2,
numeric_precision_radix: 10,
Expand Down Expand Up @@ -182,7 +184,7 @@ describe('pg-metadata', function () {
describe('creates a metadata object from a query result set that contains all', function () {
it('the databases', function () {
var actual = pgMetadata.createMetadataObject(resultSet)

actual.should.have.property('adb')
actual.should.have.property('bdb')
actual.should.have.property('cdb')
Expand All @@ -192,7 +194,7 @@ describe('pg-metadata', function () {

it('schemas in a database', function () {
var actual = pgMetadata.createMetadataObject(resultSet)

actual.adb.should.have.property('aschema')
actual.bdb.should.have.property('bschema')
actual.cdb.should.have.property('cschema')
Expand All @@ -202,7 +204,7 @@ describe('pg-metadata', function () {

it('tables in each schema', function () {
var actual = pgMetadata.createMetadataObject(resultSet)

actual.adb.aschema.should.have.property('atable')
actual.adb.aschema.should.have.property('btable')

Expand All @@ -211,15 +213,15 @@ describe('pg-metadata', function () {

it('columns in each table', function () {
var actual = pgMetadata.createMetadataObject(resultSet)

actual.adb.aschema.atable.should.have.property('a')
actual.adb.aschema.atable.should.have.property('b')
actual.bdb.bschema.btable.should.have.property('a')
})

it('types of data and length for each columns', function () {
var actual = pgMetadata.createMetadataObject(resultSet)

actual.adb.aschema.atable.a.should.have.property('type', 'varchar')
actual.adb.aschema.atable.a.should.have.property('length', 244)
actual.adb.aschema.atable.a.should.not.have.property('scale')
Expand All @@ -236,7 +238,7 @@ describe('pg-metadata', function () {
actual.cdb.should.be.eql({
cschema: {
ctable: {
a: { type: 'float4', required: false, precision: 24, scale: null, precision_radix: 2 },
a: { type: 'float4', required: true, precision: 24, scale: null, precision_radix: 2 },
b: { type: 'numeric', required: false, precision: 12, scale: 2, precision_radix: 10 }
}
}
Expand Down Expand Up @@ -269,7 +271,7 @@ describe('pg-metadata', function () {
.which.have.property('atable')
.which.have.property('a')
.which.have.property('type', 'varchar')

done()
})
})
Expand All @@ -285,7 +287,7 @@ MockConnection.prototype.query = function(q, cb) {
setImmediate(function () {
if (self.error)
cb(new Error('lalala'))
else
else
cb(null, { rows: self.resultSet })
})
}