Commit 0a9d9246 authored by Anne Blankert's avatar Anne Blankert

better cache filenames, layer_columns uses schema

parent 120af7fb
...@@ -12,7 +12,8 @@ const sql = (params) => { ...@@ -12,7 +12,8 @@ const sql = (params) => {
pg_class.oid = attrelid AND pg_class.oid = attrelid AND
relnamespace = pg_namespace.oid AND relnamespace = pg_namespace.oid AND
attnum >= 1 AND attnum >= 1 AND
relname = '${params.table}' relname = '${params.table}'
${params.schema?` AND nspname= '${params.schema}'`:''}
` `
} }
...@@ -40,25 +41,42 @@ module.exports = function (app, pool) { ...@@ -40,25 +41,42 @@ module.exports = function (app, pool) {
* description: table not found or not accessible * description: table not found or not accessible
*/ */
app.get('/data/layer_columns/:table', async (req, res)=> { app.get('/data/layer_columns/:table', async (req, res)=> {
const sqlString = sql(req.params, req.query); let tableName, schemaName;
try { const table = req.params.table;
const result = await pool.query(sqlString); if (table) {
res.json(result.rows); const parts = table.split('.');
} catch (err) { if (parts.length === 1) {
console.log(err); tableName = parts[0];
let status = 500; schemaName = null;
switch (err.code) { } else {
case '42P01': schemaName = parts[0];
// table does not exist tableName = parts[1];
status = 422;
break;
case '42703':
// column does not exist
status = 422;
break;
default:
} }
res.status(status).json({error:err.message}) req.params.table = tableName;
req.params.schema = schemaName;
const sqlString = sql(req.params, req.query);
try {
const result = await pool.query(sqlString);
res.json(result.rows);
} 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'"})
} }
}) });
} }
\ No newline at end of file
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
const sqlTableName = require('./utils/sqltablename.js'); const sqlTableName = require('./utils/sqltablename.js');
const DirCache = require('./utils/dircache.js') const DirCache = require('./utils/dircache.js')
const cache = new DirCache('./cache/mvt'); const cache = new DirCache('./cache');
const sm = require('@mapbox/sphericalmercator'); const sm = require('@mapbox/sphericalmercator');
const merc = new sm({ const merc = new sm({
...@@ -10,8 +10,9 @@ const merc = new sm({ ...@@ -10,8 +10,9 @@ const merc = new sm({
}) })
let cacheMiddleWare = async(req, res, next) => { let cacheMiddleWare = async(req, res, next) => {
const cacheDir = `${req.params.datasource}/${req.params.z}/${req.params.x}/${req.params.y}`; const cacheDir = `${req.params.datasource}/mvt/${req.params.z}/${req.params.x}/${req.params.y}`;
const key = (req.query.geom_column?req.query.geom_column:'geom') + req.query.columns?','+req.query.columns:''; const key = ((req.query.geom_column?req.query.geom_column:'geom') + (req.query.columns?','+req.query.columns:''))
.replace(/[\W]+/g, '_');
const mvt = await cache.getCachedFile(cacheDir, key); const mvt = await cache.getCachedFile(cacheDir, key);
if (mvt) { if (mvt) {
......
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
document.querySelector('#tablename').innerHTML = fullTableName; document.querySelector('#tablename').innerHTML = fullTableName;
const parts = fullTableName.split('.'); const parts = fullTableName.split('.');
const tableName = (parts.length > 1) ? parts[1] : parts[0]; const tableName = (parts.length > 1) ? parts[1] : parts[0];
fetch(`data/layer_columns/${tableName}`).then(response=>{ fetch(`data/layer_columns/${fullTableName}`).then(response=>{
const list = document.querySelector('#columns'); const list = document.querySelector('#columns');
if (response.ok) { if (response.ok) {
response.json().then(json=> { response.json().then(json=> {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment