Skip to content

Conversation

bigpresh
Copy link
Owner

This feature allows you to pass a scalarref when you want a value untouched by the normal parameterisation, so you could say e.g.:

$handle->quick_update($table, { id => 42 }, { foo => 'One', timestamp => \'NOW()' });

(Obviously, if you do that, it's included in the SQL as you provide it, so only you can prevent wildfires^WSQL injection attacks - be careful.)

This also includes refactoring out of the SQL & bind values generation into its own method, _generate_sql() for testability, and a variety of tests exercising it (testing both previous expected functionality, and the new functionality).

This was prompted by a question from @b100s on Freenode/#perl earlier.

(Previously you had to provide a WHERE clause as {})

... not 100% sure I want this; it could make it easy to accidentally select all
rows when you didn't mean to?

Also, don't have a spurious extra space at the end of a SELECT query generated
without a WHERE clause (or a double space if there is one)
Call _generate_sql() with a variety of inputs and test that the resulting SQL is
as we expect.
This lets you say e.g.:

```perl
$handle->quick_insert('tablename', { foo => 'bar', timestamp => \'NOW()' });
```

... and the scalarref will go into the query as-is (so be careful about SQL
injection - if you've asked the module to put it in as-is, it will, and can't
protect you...)
If you've supplied a scalarref to be interpolated into the VALUES() as-is, it
shouldn't also get added to the @bind_params.
This passes now, showing the feature was implemented right \o/
Define the sqlgen tests along with the others, then declare our plan, then start
testing.
@ambs
Copy link
Collaborator

ambs commented Jan 30, 2015

+1 :-)

@bigpresh
Copy link
Owner Author

ta :) One thing I'm concerned about is that some of the tests may be relying on the semi-random ordering of hash iteration - so could fail randomly - will probably need to look at the places we iterate over keys/values and sort by key or something.

@ambs
Copy link
Collaborator

ambs commented Jan 30, 2015

Just a note, +1 on the idea, did not review the code. Can do, though, it you like.

@bigpresh
Copy link
Owner Author

If you have the time, I'd certainly appreciate code review :) Some of the code in there was pretty crufty to begin with - it's not my proudest work, but it does the job.

bigpresh added a commit that referenced this pull request Feb 7, 2015
…alls

Scalarrefs for verbatim values (for SQL function calls etc)
@bigpresh bigpresh merged commit fe2e8ae into master Feb 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants