This repository was archived by the owner on May 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpg.js
More file actions
99 lines (78 loc) · 2.89 KB
/
pg.js
File metadata and controls
99 lines (78 loc) · 2.89 KB
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
var Client = require('pg-native');
var Q = require("q");
var settings = require('./settings.js').pg;
// PostGIS Connection String
var conString = "postgres://" +
settings.user + ":" +
settings.password + "@" +
settings.server + ":" +
settings.port + "/" +
settings.database;
var pg = new Client();
//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) {
done();
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) {
if(err) {
console.error('error fetching client from pool', err);
deferred.reject(err);
}
pg.query(sqlStr.text, sqlParams, function(queryerr, result) {
//done();
if(queryerr) {
console.error('ERROR RUNNING QUERY:', sqlStr, queryerr);
deferred.reject(queryerr);
} else {
deferred.resolve(result ? result : []);
}
});
});
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);
};