GithubHelp home page GithubHelp logo

Comments (17)

rob006 avatar rob006 commented on May 27, 2024 5

I mean rather using PDOStatement::nextRowset() to iterate through all queries passed to PDOStatement::execute() to catch exceptions and get number of rows affected by each of these queries.

See example in linked issue in PHP's bugzilla:

$pdo->beginTransaction();
try {
	$statement = $pdo->prepare($sql);
	$statement->execute();
	while ($statement->nextRowset()) {/* https://bugs.php.net/bug.php?id=61613 */};
	$pdo->commit();
} catch (\PDOException $e) {
	$pdo->rollBack();
	throw $e;
}

from db.

papalapa avatar papalapa commented on May 27, 2024 1

In continue of our discussion I have one more question. Construction:

try {
  Yii::$app->db->pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, 1);
  $statement = Yii::$app->db->pdo->prepare($sql);
  $statement->execute();
  while ($statement->nextRowset()) {
    //
  }
} catch (\Exception $e) {
  //
}

works fine and throw an exception on first failed sql query, but it is not possible to put MySQL code with 'DELIMITER' - the exception will return:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER $$ CREATE DEFINER=`root`@`localhost` PROCEDURE `new_procedure`() BEG' at line 1.

But I if use Yii::$app->db->createCommand($sql)->execute(); - no exceptions with 'DELIMITER'.
PDO doesn`t understand DELIMITER, and what solution in this case?

from db.

rob006 avatar rob006 commented on May 27, 2024

Are you sure that second and third query are executed?

from db.

cebe avatar cebe commented on May 27, 2024

dependent on the PDO driver settings it may only support one query. Passing multiple queries to createCommand() works in some cases but that is not documented to work and may not work.

from db.

rob006 avatar rob006 commented on May 27, 2024

This is expected PDO behavior: https://bugs.php.net/bug.php?id=61613

from db.

rob006 avatar rob006 commented on May 27, 2024

Maybe we should introduce something like executeMultiple()?

from db.

cebe avatar cebe commented on May 27, 2024

Maybe we should introduce something like executeMultiple()?

that would require parsing of SQL to separate statements. That is not easy to do as we need to fully understand SQL syntax of the corresponding dbms. Need to consider things like MySQL magic comments.

from db.

cebe avatar cebe commented on May 27, 2024

Is the query only executed on calling nextRowset()? or is just the error not reported back before calling it?

from db.

tomaszkane avatar tomaszkane commented on May 27, 2024

@rob006 executed is all queries before the broken one. Thats why it's tricky - if broken sql is on of the last query (example: some alter table), then when you check database (with some tool/console) and see all tables, you think: "My schema import is success".
BTW, in my case transaction is worthless because MYSQL don't support it on tables create/drop/alter.

@cebe queries are executed, nextRowset() just iterate on results and throw PDOException when first result is fail.

from db.

cebe avatar cebe commented on May 27, 2024

@cebe queries are executed, nextRowset() just iterate on results and throw PDOException when first result is fail.

if all queries are executed, we might also want to check further queries for failure, e.g. having the last two queries fail, we'd want an exception reporting all of these.

from db.

tomaszkane avatar tomaszkane commented on May 27, 2024

BTW, does delimiter is really need? See: https://stackoverflow.com/a/9053823/1829368
I have schema dump in my project - after remove delimiters near the triggers all queries was successful and triggers was created.

from db.

papalapa avatar papalapa commented on May 27, 2024

@tomaszkane, yes, thanks, I'm looking for solution to remove delimiters from queriy now.

from db.

tomaszkane avatar tomaszkane commented on May 27, 2024

My schema with delimiters:

-- some queries here

DELIMITER $$
CREATE TRIGGER `fooCounterCacheAfterDelete` AFTER DELETE ON `foo` FOR EACH ROW BEGIN
    UPDATE user SET foo_count = foo_count - 1 WHERE id = OLD.user_id;
END
$$
DELIMITER ;

and import fail.
Schema without delimiter:

-- some queries here

CREATE TRIGGER `fooCounterCacheAfterDelete` AFTER DELETE ON `foo` FOR EACH ROW BEGIN
  UPDATE user SET foo_count = foo_count - 1 WHERE id = OLD.user_id;
END;

works fine.

from db.

FantomX1 avatar FantomX1 commented on May 27, 2024

Delimiters in MySQL
https://stackoverflow.com/a/10259528/3419535

Attempting to use DELIMITER with a client that doesn't support it will cause it to be sent to the server, which will report a syntax error. For example, using PHP and MySQLi:

$mysqli = new mysqli('localhost', 'user', 'pass', 'test');
$result = $mysqli->query('DELIMITER $$');
echo $mysqli->error;
Errors with:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER $$' at line 1

from db.

thomaswruss avatar thomaswruss commented on May 27, 2024

I had the same problem if I execute a procedure that calls more than one procedure. If there is an error, the error will not be raised in Yii. But if I execute just that one procedure, the error will be raised. So this seems to be a bug of Yii. Any suggestion how to detect the errors in a multiple procedure call?

from db.

samdark avatar samdark commented on May 27, 2024

Any suggestion how to detect the errors in a multiple procedure call?

Yes. See #79 (comment)

from db.

tomaszkane avatar tomaszkane commented on May 27, 2024

I also link a solution in Additional info in this issue: https://stackoverflow.com/a/46280130/1829368
@thomaswruss read topic before ask.

from db.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.