Comments (17)
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.
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.
Are you sure that second and third query are executed?
from db.
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.
This is expected PDO behavior: https://bugs.php.net/bug.php?id=61613
from db.
Maybe we should introduce something like executeMultiple()
?
from db.
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.
Is the query only executed on calling nextRowset()
? or is just the error not reported back before calling it?
from db.
@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 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.
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.
@tomaszkane, yes, thanks, I'm looking for solution to remove delimiters from queriy now.
from db.
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.
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.
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.
Any suggestion how to detect the errors in a multiple procedure call?
Yes. See #79 (comment)
from db.
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)
- `Query::count()` can return `int|string` but always returns `int` HOT 1
- `Query::leftJoin` - a simpler way to quote join column? HOT 3
- Remove `$withColumn` parameter from `getTableNameParts()` HOT 3
- Remove method `TableSchema::compositeForeignKey()` HOT 1
- `CommandInterface::getRawSQL()` - using in production / alternative HOT 12
- `Schema::hasTableName()` method is missing HOT 1
- Allow scalars `int|float|bool` in `Query::select()` HOT 1
- Changes before release 2.0.0
- Allow use `DMLQueryBuilderInterface::batchInsert()` without `$columns` HOT 4
- Add posibility to drop table `IF EXISTS` and `CASCADE`
- Allow pass `ExpressionInterface` as alias to `QueryPartsInterface::withQuery()` HOT 1
- No usage of `AbstractCommand::requireTransaction()` HOT 3
- Needs to refactor `getRawSql()`
- Bug in `Command::insertWithReturningPks()` with empty values
- `Command::upsertWithReturningPks()` required for `ActiveRecord::upsert()`
- update irc link
- Bug when try to `Command::update()` using `Expression`s in values or in where condition
- Add ability to create like condition as `%value`, `value%` and `value` without `%`
- Add new method `SchemaInterface::getUserDefinedTypes()`
- Add ability to create `SELECT` with `FOR UPDATE`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from db.