Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Commit

Permalink
Add optional support for raw SQL queries
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornharrtell committed Sep 14, 2012
1 parent 2d2c8f5 commit 916fa29
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ expects a single JSON object with properties corresponding to column names.
* Optionally use authenticated user/password to connect to DB
* Handle ORDER BY via orderby param
* Handle PostGIS data
* Optionally allow raw SQL

## License

Expand Down
4 changes: 3 additions & 1 deletion lib/cli.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ optimist.options 'user',
default : process.env.USER
optimist.options 'password',
describe : 'PostgreSQL password'
optimist.options 'raw',
describe : 'Enable raw SQL usage'
optimist.options 'cors',
describe : 'Enable CORS support'
optimist.options 'help',
describe : 'Show this message'
argv = optimist.boolean('cors')
argv = optimist.boolean('raw').boolean('cors')
.demand(['port', 'dbhost', 'dbport', 'database', 'user'])
.argv

Expand Down
6 changes: 6 additions & 0 deletions lib/db.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ pg = require 'pg'
lexer = require './lexer'

module.exports = (log, connectionString, database) ->
###
config.sql - sql to query
config.res - response to send query results to (or eventual error)
config.values - parameter values
config.callback - callback to be called on successful query with a single argument containing the query result
###
query = (config) ->
connectionStringDb = connectionString + "/" + (config.database || database)

Expand Down
12 changes: 11 additions & 1 deletion lib/resources/database.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = (server) ->
module.exports = (server, raw) ->
log = server.log
db = server.db
app = server.app
Expand All @@ -9,3 +9,13 @@ module.exports = (server) ->
res.send
type: 'database'
children: ['schemas']

if raw
app.post '/db/:databaseName', (req, res) ->
console.log 'RAW SQL POST: ' + req.body.sql
db.query
sql: req.body.sql
res: res
database: req.params.databaseName
callback: (result) ->
res.send result.rows
11 changes: 8 additions & 3 deletions lib/server.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,23 @@ start = (argv) ->
log.info "Enable Cross-origin Resource Sharing"
app.options '/*', (req,res,next) ->
res.header 'Access-Control-Allow-Origin', '*'
res.header 'Access-Control-Allow-Headers', 'origin, x-requested-with'
res.header 'Access-Control-Allow-Headers', 'origin, x-requested-with, content-type'
next()

app.get '/*', (req,res,next) ->
res.header 'Access-Control-Allow-Origin', '*'
res.header 'Access-Control-Allow-Headers', 'origin, x-requested-with'
res.header 'Access-Control-Allow-Headers', 'origin, x-requested-with, content-type'
next()

app.post '/*', (req,res,next) ->
res.header 'Access-Control-Allow-Origin', '*'
res.header 'Access-Control-Allow-Headers', 'origin, x-requested-with, content-type'
next()

log.info "Setting up resources"
resources.root exports
resources.db exports
resources.database exports
resources.database exports, argv.raw
resources.schemas exports
resources.schema exports
resources.tables exports
Expand Down

0 comments on commit 916fa29

Please sign in to comment.