atlasphp / atlas.pdo Goto Github PK
View Code? Open in Web Editor NEWProvides Connection and ConnectionLocator functionality.
License: MIT License
Provides Connection and ConnectionLocator functionality.
License: MIT License
Hi, I was looking into integrating this library with an existing application and while running a small test I found an a compatibility issue between the ConnectionLocator
class and use of PDO::ATTR_PERSISTENT
. It seems that the ConnectionLocator->newConnection()
method always calls the Connection->logQueries()
method, which in turn sets the attribute PDO::ATTR_STATEMENT_CLASS
. According to the PDO documentation (https://www.php.net/manual/en/pdo.setattribute.php), these two attribute settings cannot be used together:
PDO::ATTR_STATEMENT_CLASS: Set user-supplied statement class derived from PDOStatement. Cannot be used with persistent PDO instances. Requires array(string classname, array(mixed constructor_args)).
This would also be an issue if you instantiated a Connection
object and called it's logQueries()
method, but in the case of the ConnectionFactory
it happens automatically.
<?php
use Atlas\Pdo\ConnectionLocator;
require_once __DIR__ . '/vendor/autoload.php';
...
$connectionLocator = ConnectionLocator::new(
$dsn,
$username,
$password,
[
PDO::ATTR_PERSISTENT => true,
]
);
$connection = $connectionLocator->getDefault();
Running the above sample code will produce the following exception:
Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: PDO::ATTR_STATEMENT_CLASS cannot be used with persistent PDO instances in ...
I think this is an excellent package and would love to be able to use it in my organization. I would be happy to submit a PR for this issue or alternatively to add to the documentation so it is known that persistent connections are not compatible with this package if that is the desired approach.
Thank you,
Daniel Braff
@dbraff see current 1.x branch, in tests/LoggedStatementTest::testBindColumn().
It appears that the double-indirection-by-reference does not work when binding a variable to a column result. Although I suppose it's not critical, it does represent a BC break. Any ideas on how to get that working?
If there can be no straightforward solution, I may have to break LoggedStatement into two classes: one for "transient" connections, that looks a lot like the v1.1.2 LoggedStatement, and one for "persistent" connections, that does not support bindColumn(). (See #16 for what that would look like.)
Hi all,
While trying out the query logging I found that the definition of the overridden bindValue method in LoggedStatement.php (here:
Atlas.Pdo/src/LoggedStatement.php
Line 48 in 50bd809
\PDO::PARAM_STR
rather than null for the optional $dataType parameter.Hi,
I think it will be interesting to save the connection to the database, with a connection time.
This can be good information for debugging an unusually slow application, due to the slow PDO connection, for example.
The statement for logging can be: "CONNECTION"
Regards.
Hello .. thanks for a very useful libraries...
A question: is 2.0 production ready, recommended for production use..
Thanks
Johan
Hi,
In case of bad SQL request, a PDO exception is throwed.
But logging appears after a good execution. Possible to do perform the logging in all cases with result (true/false) ?
Example of simple implementation to do that:
public function commit() : bool
{
$executionPerformed = false;
$entry = $this->newLogEntry(__METHOD__);
try {
$result = $this->pdo->commit();
$executionPerformed = true;
} finally {
$entry['performed'] = $executionPerformed;
$this->addLogEntry($entry);
}
return $result;
}
Regards.
$connectionLocator = \Atlas\Pdo\ConnectionLocator::new(
'mysql:dbname=db;host=127.0.0.1',
'user',
'pswd'
);
$connectionLocator->logQueries(true);
$pdo = $connectionLocator->getDefault()->getPdo();
// SQLBuilder is a 3th-party open source library
// 1st argument MUST BE a PDO instance
$sqlbuilder = new SQLBuilder($pdo);
// In the query method, will prepare the SQL and execute it
$result = $sqlbuilder->query('select * from user where id=?', $id)->fetchAll();
// The SQLBuilder query method look like this
function query(string $sql, ...$bind): PDOStatement
{
$stm = $this->pdo->parepare($sql);
foreach ($bind as $k => $v) {
$stm->bindValue($k + 1, $v);
}
// LoggedStatement->execute() will throw an Error
// Function name must be a string
// at line 41
return $stm->execute();
}
Because ConectionLocator
enable the logQueries
, the PDO::ATTR_STATEMENT_CLASS
will set to the LoggedStatement
class.
In SQLBuilder
the LoggedStatement
is create from native PDO::prepare
method, so not call the LoggedStatement::setLogEntry
and LoggedStatement::setQueryLogger
. Then when run LoggedStatement->execute()
, it will throw this error.
The SQLBuilder
only required a PDO
instance, I don't think this requirement is too much. So I think it's Atlas PDO
issue.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.