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

implemented emulate prepare and added tests #371

Merged
merged 6 commits into from
May 2, 2017

Conversation

yukiwongky
Copy link
Contributor

@yukiwongky yukiwongky commented Apr 21, 2017

Fix for #92 and #140 .

Implement emulate prepare by:

  1. When a statement is prepared with emulate prepare, parse out placeholders and store it in a placeholders hashtable in stmt
  2. Get statement information in the quote function
  3. Going through the placeholders in order, get the encoding from stmt->bound_params
  4. Set the encoding to param level encoding, stmt level encoding, or connection level encoding; param encoding dominate, then stmt level, then connection level. If one level's encoding is invalid, then go to the higher level encoding.
  5. "quote" the parameter base on the encoding
  6. Replace placeholders in the sql string with proper quoted/encoded parameters

Note: Emulate prepare only works for input bound parameters.
When user specify PDO::PARAM_INPUT_OUTPUT in PDO::bindParam when emulate prepare is on, the driver throws a PDO exception.
When user binds an output parameter, the sql query should be a placeholder preceding an '=' sign (e.g., "SELECT ? = COUNT(*) FROM Employee WHERE Department='Human Resources'"). When while parsing out the placeholders out from the sql string, it also checks if the sql query has an '=' immediately after a placeholder. If it does, throws an exception saying output parameter with emulate prepared statement is not supported. If user binds an output parameter to a stored procedure, an error would not be raise.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.4%) to 61.878% when pulling 7aaa16d on v-kaywon:emulate_prepare into 012b16a on Microsoft:dev.

@david-garcia-garcia
Copy link

david-garcia-garcia commented Apr 21, 2017

Thaaaaanks!! Just a question... What happens with duplicate placeholders in the updated implementation? On the previous driver you could not have a placeholder twice, yet on other PDO drivers such as MySQL they will let you re-use parameteres by allowing a placeholder to be repeated.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.4%) to 61.868% when pulling fa38f7d on v-kaywon:emulate_prepare into 012b16a on Microsoft:dev.

@yukiwongky
Copy link
Contributor Author

@david-garcia-garcia ,

Can you clarify on what you mean by duplicated placeholders?

Do you mean having two placeholders with the same name in a sql query?
For example.

$statement = $connection->prepare("SELECT * FROM Table1 WHERE uid=:id AND pid=:id");
$statement->bindParam(':id', 1234);

And both ':id' placeholders in the sql query is replaced with 1234.

Or do you mean binding to the same placeholder twice?
For example,

$statement = $connection->prepare("SELECT * FROM Table1 WHERE uid=:id");
$statement->bindParam(':id', 1234);
$statement->bindParam(':id', 6789);

@coveralls
Copy link

Coverage Status

Coverage increased (+0.5%) to 61.967% when pulling 2aec3b4 on v-kaywon:emulate_prepare into 012b16a on Microsoft:dev.

@coveralls
Copy link

coveralls commented Apr 21, 2017

Coverage Status

Coverage increased (+0.5%) to 61.995% when pulling 6d7cddf on v-kaywon:emulate_prepare into 012b16a on Microsoft:dev.

@david-garcia-garcia
Copy link

I'm talking about your first example werwe the placeholder is present twice in the query itself.

@yukiwongky
Copy link
Contributor Author

I did some preliminary testing. Seems like duplicated placeholders works for parameters of any encoding types that's not binary.

…ses through each acceptable characters to using if else statement that covers a range of characters
@coveralls
Copy link

Coverage Status

Coverage increased (+0.5%) to 61.995% when pulling b7d4a9b on v-kaywon:emulate_prepare into 012b16a on Microsoft:dev.

@yukiwongky yukiwongky requested a review from yitam April 24, 2017 19:17
Copy link

@Hadis-Knj Hadis-Knj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job!


// get the current object in PHP; this distinguishes pdo_sqlsrv_dbh_quote being called from:
// 1. PDO::quote() - object name is PDO
// 2. PDO::execute() - object name is PDOStatement

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you meant PDOStatement::execute 😉

@coveralls
Copy link

Coverage Status

Coverage increased (+0.5%) to 61.995% when pulling c0707f6 on v-kaywon:emulate_prepare into 012b16a on Microsoft:dev.

@yukiwongky yukiwongky merged commit fb3663b into microsoft:dev May 2, 2017
@yukiwongky yukiwongky deleted the emulate_prepare branch June 30, 2017 21:14
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.

4 participants