-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpg.js
106 lines (86 loc) · 3.23 KB
/
pg.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
var pg = require('pg');
var Q = require("q");
var settings, conString;
//var settings = require('./settings.js').pg;
//pg.defaults.ssl = true;
if(!process.env.hasOwnProperty("DB_SERVER")){
settings = require('./settings.js').pg;
conString = "postgres://" + settings.user + ":" +
settings.password + "@" +
settings.server + ":" +
settings.port + "/" +
settings.database;
} else {
conString = "postgres://" + process.env.DB_USER + ":" +
process.env.DB_PASSWORD + "@" +
process.env.DB_SERVER + ":" +
process.env.DB_PORT + "/" +
process.env.DB_NAME;
}
//var conString = 'postgres://gzzmncituhhtxm:re4E13HP-uqTlwGW71xoeH25F_@ec2-54-204-30-115.compute-1.amazonaws.com:5432/d491g46d8uagv';
/**
* Main query function to execute an SQL query; callback form.
*
* @type {Function}
*/
module.exports.queryCallback = function(sql, cb, opts) {
pg.connect(conString, function(err, client, done) {
if(err) {
console.error('error fetching client from pool', err);
}
client.query(sqlStr, sqlParams, function(queryerr, result) {
pg.end();
if(queryerr) {
console.error('ERROR RUNNING QUERY:', sqlStr, queryerr);
}
cb((err || queryerr), (result && result.rows ? result.rows : result));
});
});
};
/**
* Main query function to execute an SQL query; deferred form.
*
* @type {Function}
*/
module.exports.queryDeferred = function(sqlStr, opts){
var options = opts || {};
var sqlParams = options.sqlParams || null;
var deferred = Q.defer();
pg.connect(conString, function(err, client, done) {
if(err) {
console.error('error fetching client from pool', err);
deferred.reject(err);
}
client.query(sqlStr.text, sqlStr.values, function(queryerr, result) {
done();
if(queryerr) {
console.error('ERROR RUNNING QUERY:', sqlStr, queryerr);
deferred.reject(queryerr);
} else {
deferred.resolve(result && result.rows ? result.rows : []);
}
});
});
return deferred.promise;
};
var sanitize = module.exports.sanitize = function (val) {
// we want a null to still be null, not a string
if (typeof val === 'string' && val !== 'null') {
// $nh9$ is using $$ with an arbitrary tag. $$ in pg is a safe way to quote something,
// because all escape characters are ignored inside of it.
var esc = settings.escapeStr;
return "$" + esc + "$" + val + "$" + esc + "$";
}
return val;
};
var featureCollectionSQL = module.exports.featureCollectionSQL = function(table, propertyColumns, opts){
var options = opts || {};
var whereClause = options.whereClause || '';
var sql = "SELECT row_to_json(fc) AS response "
+ "FROM ( SELECT 'FeatureCollection' As type, array_to_json(array_agg(f)) As features "
+ "FROM (SELECT 'Feature' As type "
+ ", row_to_json((SELECT l FROM (select {{columns}}) As l "
+ ")) As properties "
+ "FROM " + table + " As t {{where}}) As f ) As fc;"
return sql.replace('{{columns}}', propertyColumns).replace('{{where}}', whereClause);
};