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

Is it possible to pass a query as parameter? #80

Closed
felipefac opened this issue Oct 7, 2013 · 6 comments
Closed

Is it possible to pass a query as parameter? #80

felipefac opened this issue Oct 7, 2013 · 6 comments
Labels

Comments

@felipefac
Copy link

No description provided.

@felipefac felipefac reopened this Oct 7, 2013
@felipefac
Copy link
Author

Just found out that there is using knex.

Bookshelf.knex.raw(query).then(callback);

@tgriesser
Copy link
Member

@felipefac - that will create a raw query... can you give me a little more about the situation where you're looking to pass a query as a parameter? There might be a better way to do it.

@felipefac
Copy link
Author

I was trying to pass a db function as a parameter like:
model.query("where", "DATE(field)", "=", "2013-01-01").fetch....

The problem is the DATE(field) was going quoted to the database and i could not find a way to remove them.

Then i tried

model.query("where", Bookshelf.knex.raw("DATE(field)"I, "=", "2013-01-01").then .....

But the select ignored the last two parameters after the raw function was called and
was returning only this:
'Select * from table where Date(field)'

After some time i decided to use a raw SQL. I should have reported a issue yesterday.
Is it a issue right?

BTW great work on bookshelf congratulations.

@tgriesser
Copy link
Member

Try this:

model.query("where", Bookshelf.knex.raw("DATE(field)"), "=", "2013-01-01")).then(...

that way you're only defining the DATE portion as raw, and I believe it should give you what you're looking for... additional arguments in a raw statement are for any bound parameters in the statement.

Also thanks! - glad you're enjoying it!

@felipefac
Copy link
Author

I tried the method you described. The thing is the query returned was incomplete.
For the command that you gave it returns:

'Select * from table where Date(field)' and nothing else.

@tgriesser
Copy link
Member

Oh, that's right - it needs to be a full raw statement:

model.query("where", Bookshelf.knex.raw("DATE(field) = ?"), "2013-01-01"))

That's the syntax you'll want to use, and in that case you can dynamically pass a value like a Date to the "bindings" spot where the "2013-01-01" is and ensure it's properly escaped into the query fragment... I think I need to better document that in knex.raw.

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

No branches or pull requests

2 participants