This repository has been archived by the owner on May 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
138 lines (109 loc) · 2.88 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
var pgEscape = require('pg-escape')
var debug = require('debug')('pg-metadata')
module.exports = pgMetadata
module.exports.createQuery = createQuery
module.exports.createMetadataObject = createMetadataObject
var informationSchemaFields = module.exports.informationSchemaFields = [
'column_name',
'udt_name',
'data_type',
'character_maximum_length',
'table_name',
'table_schema',
'table_catalog',
'is_nullable',
'numeric_precision',
'numeric_scale',
'numeric_precision_radix',
'datetime_precision',
'interval_type',
'interval_precision'
]
var datetimePrecisionTypes = [
'timestamp',
'timestamptz',
'date',
'time',
'timetz'
]
function pgMetadata(connection, options, callback) {
if (!connection)
throw new Error('must provide a connection object')
if (typeof (options) === 'function') {
callback = options
options = undefined
}
if (!callback)
throw new Error('must provide a callback')
var query = pgEscape(createQuery(options))
debug(query)
connection.query(query, function (err, result) {
if (err) {
callback(err)
} else {
var schema = createMetadataObject(result.rows)
debug(schema)
callback(null, schema)
}
})
}
function createQuery(opts) {
opts = opts || {}
var sql = 'SELECT ' + informationSchemaFields.join(',') + ' FROM information_schema.columns'
var whereClause = []
var values = []
if (opts.table) {
whereClause.push('table_name=%L')
values.push(opts.table)
}
if (opts.schema) {
whereClause.push('table_schema=%L')
values.push(opts.schema)
}
if (opts.database) {
whereClause.push('table_catalog=%L')
values.push(opts.database)
}
if (whereClause.length > 0) {
sql += ' WHERE ' + whereClause.join(' AND ')
}
return pgEscape.apply(null, [sql].concat(values))
}
function createMetadataObject(resultSet) {
var metadata = {}
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
var database = metadata[row.table_catalog]
if (!database) {
metadata[row.table_catalog] = database = {}
}
var schema = database[row.table_schema]
if (!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
}
if (s.type.indexOf('char')>=0 || s.type.indexOf('text')>=0) {
s.length = row.character_maximum_length
} else if (row.numeric_precision_radix != null) {
s.precision = row.numeric_precision
s.scale = row.numeric_scale
s.precision_radix = row.numeric_precision_radix
} else if (datetimePrecisionTypes.indexOf(s.type) >= 0) {
s.precision = row.datetime_precision
} else if (s.type === 'interval') {
s.precision = row.interval_precision
s.interval_type = row.interval_type
}
}
return metadata
}