Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use escape function with custom query #304

Closed
SamuelBolduc opened this issue Aug 19, 2013 · 11 comments
Closed

Use escape function with custom query #304

SamuelBolduc opened this issue Aug 19, 2013 · 11 comments
Assignees

Comments

@SamuelBolduc
Copy link

I have a few custom queries in my models and I would like to use the function you use for escaping strings to me inserted. Is there a way to call it to sanitize my inputs?

@dxg
Copy link
Collaborator

dxg commented Aug 19, 2013

For partial queries:

Person.find().where("name LIKE ?", ["john"]).run(...)

For complete queries it's less nice:

var val = db.driver.query.escapeVal("john o'connor");
var sql = "SELECT * FROM person WHERE name LIKE "+val;

We could provide something nicer like:

var sql = db.driver.execQuery("SELECT * FROM person WHERE name LIKE ?", [val])

Thoughts?
I'm also wondering if there are implications of overloading execQuery but I think it should be fine.

@dresende
Copy link
Owner

Yes, it should be fine, if backwards compliant this could be nice. Maybe then Model.find().where() could use it directly.

@SamuelBolduc
Copy link
Author

This would be a really nice feature! My app is pretty complex and there are quite a few queries I can't really do with the ORM directly, so I create Model and Instance methods and put my queries there. This keeps the app structure intact (MVC).

Being able to escape my queries directly there would be very nice and more in line with my app structure.

@ghost ghost assigned dxg Aug 20, 2013
@dxg
Copy link
Collaborator

dxg commented Aug 20, 2013

I'll work on this

@SamuelBolduc
Copy link
Author

Many thanks for this!! It will save me a lot of time and uselessly long code!

@SamuelBolduc
Copy link
Author

I didn't try it until today, and here is what I get with the latest git version :

var sql = "INSERT INTO object (id, name, object_subtype_id, object_type_id, client_id) VALUES (DEFAULT, '?', ?, ?, ?) RETURNING id;";
    db.driver.execQuery(sql, [
        data.name,
        data.object_subtype_id,
        data.object_type_id,
        data.client_id
      ], function(err, returning) {...}

And here is the SQL I see in the debug console :

[SQL/postgres] INSERT INTO object (id, name, object_subtype_id, object_type_id, client_id) VALUES (DEFAULT, '?', ?, ?, ?) RETURNING id;

Followed by this error:

/var/www/project/node_modules/orm/lib/Drivers/DML/postgres.js:86
                                        cb(err);
                                        ^
TypeError: object is not a function
    at null.callback (/var/www/project/node_modules/orm/lib/Drivers/DML/postgres.js:86:6)
    at Query.handleError (/var/www/project/node_modules/pg/lib/query.js:93:10)
    at null.<anonymous> (/var/www/project/node_modules/pg/lib/client.js:178:19)
    at EventEmitter.emit (events.js:95:17)
    at Socket.<anonymous> (/var/www/project/node_modules/pg/lib/connection.js:89:12)
    at Socket.EventEmitter.emit (events.js:95:17)
    at Socket.<anonymous> (_stream_readable.js:736:14)
    at Socket.EventEmitter.emit (events.js:92:17)
    at emitReadable_ (_stream_readable.js:408:10)
    at emitReadable (_stream_readable.js:404:5)

Is there something I didn't do correctly? I followed the docs but I might still have done some error...

@SamuelBolduc SamuelBolduc reopened this Aug 22, 2013
@dxg
Copy link
Collaborator

dxg commented Aug 22, 2013

Did you run npm install ? You need sql-query version 0.1.11

@SamuelBolduc
Copy link
Author

Here is the result of npm install in the orm directory (since npm-install in my project directory did nothing) :

[email protected] node_modules/async

[email protected] node_modules/should

[email protected] node_modules/sql-query

[email protected] node_modules/mocha
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected] ([email protected])
└── [email protected] ([email protected], [email protected], [email protected])

[email protected] node_modules/pg
├── [email protected]
└── [email protected] ([email protected])

[email protected] node_modules/mysql
├── [email protected]
└── [email protected]

[email protected] node_modules/mongodb
├── [email protected]
└── [email protected]

[email protected] node_modules/sqlite3

But I still have the same issue as in my last post

@dxg
Copy link
Collaborator

dxg commented Aug 25, 2013

Hey, you have this:

var sql = "INSERT INTO object (id, name, object_subtype_id, object_type_id, client_id) VALUES (DEFAULT, '?', ?, ?, ?) RETURNING id;";

Can you provide a more complete example? Something like the test case

I'm curious about how you're calling execQuery.

@SamuelBolduc
Copy link
Author

Well the code where I call execQuery is in the example I posted... Like this :

var sql = "INSERT INTO object (id, name, object_subtype_id, object_type_id, client_id) VALUES (DEFAULT, '?', ?, ?, ?) RETURNING id;";
    db.driver.execQuery(sql, [
        data.name,
        data.object_subtype_id,
        data.object_type_id,
        data.client_id
      ], function(err, returning) {...}

It's pretty much exactly like in the test case. I don't think anything more than this could really help. I write my SQL query, I call execQuery, I pass an array of values to the query (and the values are OK when console.logged before this) and I handle the callback in a third argument. I really don't see where it fails

@SamuelBolduc
Copy link
Author

I'm sorry, I searched some more today to find what was wrong (cause your tests showed that the escaping indeed worked) and I found that when I updated to last version it wasn't properly done. Now it works... Sorry guys! All's good now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants