forked from anneb/pgserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayer_columns.js
91 lines (86 loc) · 2.38 KB
/
layer_columns.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
const infoFromSld = require('./sldtable.js');
const sql = (params) => {
return `
SELECT
attname as field_name,
typname as field_type
FROM
pg_namespace, pg_attribute, pg_type, pg_class
WHERE
pg_type.oid = atttypid AND
pg_class.oid = attrelid AND
relnamespace = pg_namespace.oid AND
attnum >= 1 AND
relname = '${params.table}'
${params.schema?` AND nspname= '${params.schema}'`:''}
`
}
module.exports = function (app, pool) {
/**
* @swagger
*
* /data/layer_columns/{table}:
* get:
* description: Returns a list of columns in the specified table.
* tags: ['meta']
* summary: 'list table columns'
* produces:
* - application/json
* parameters:
* - name: table
* description: The name of the table
* in: path
* required: true
* type: string
* responses:
* 200:
* description: list of columns (names and types)
* 422:
* description: table not found or not accessible
*/
app.get('/data/layer_columns/:table', async (req, res)=> {
let tableName, schemaName;
let table = req.params.table;
if (req.query.sldlayer) {
let sldInfo = await infoFromSld(pool, table, req.query.sldlayer);
if (!sldInfo) {
return res.status(500).json({"error": "no sld info found, sld table or layer name not found?"});
}
table = sldInfo.table;
}
if (table) {
const parts = table.split('.');
if (parts.length === 1) {
tableName = parts[0];
schemaName = null;
} else {
schemaName = parts[0];
tableName = parts[1];
}
req.params.table = tableName;
req.params.schema = schemaName;
const sqlString = sql(req.params, req.query);
try {
let result = await pool.query(sqlString);
res.json(result);
} catch (err) {
console.log(err);
let status = 500;
switch (err.code) {
case '42P01':
// table does not exist
status = 422;
break;
case '42703':
// column does not exist
status = 422;
break;
default:
}
res.status(status).json({error:err.message})
}
} else {
res.status(422).json({error:"missing parameter 'table'"})
}
});
}