-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
T14733 Data Mapper PDO Connection #14824
Changes from 25 commits
861f130
6891ed4
b44a54e
f6d85ca
107206c
15437ff
66dc40b
001fba0
6564c93
91bb717
b0b63a0
3814350
1038ce4
82cab68
02a91a6
2e7c5f3
4527227
174af25
122543b
d6b6e1f
503e4f8
6982ddf
e80add5
094ffea
fbace94
6073fe3
32222d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
/** | ||
* This file is part of the Phalcon Framework. | ||
* | ||
* (c) Phalcon Team <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE.txt | ||
* file that was distributed with this source code. | ||
* | ||
* Implementation of this file has been influenced by AtlasPHP | ||
* | ||
* @link https://github.com/atlasphp/Atlas.Pdo | ||
* @license https://github.com/atlasphp/Atlas.Pdo/blob/1.x/LICENSE.md | ||
*/ | ||
|
||
namespace Phalcon\DM\Pdo; | ||
|
||
use InvalidArgumentException; | ||
use Phalcon\DM\Pdo\Connection\AbstractConnection; | ||
use Phalcon\DM\Pdo\Profiler\Profiler; | ||
use Phalcon\DM\Pdo\Profiler\ProfilerInterface; | ||
|
||
/** | ||
* Provides array quoting, profiling, a new `perform()` method, new `fetch*()` | ||
* methods | ||
* | ||
* @property array $arguments | ||
* @property PDO $pdo | ||
* @property ProfilerInterface $profiler | ||
*/ | ||
class Connection extends AbstractConnection | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected arguments = []; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* This overrides the parent so that it can take connection attributes as a | ||
* constructor parameter, and set them after connection. | ||
* | ||
* @param string $dsn | ||
* @param string $username | ||
* @param string $password | ||
* @param array $options | ||
* @param array $queries | ||
* @param ProfilerInterface $profiler | ||
*/ | ||
public function __construct( | ||
string dsn, | ||
string username = null, | ||
string password = null, | ||
array options = [], | ||
array queries = [], | ||
<ProfilerInterface> profiler = null | ||
) { | ||
var parts; | ||
array available; | ||
|
||
let parts = explode(":", dsn), | ||
available = [ | ||
"mysql" : true, | ||
"pgsql" : true, | ||
"sqlite" : true, | ||
"mssql" : true | ||
]; | ||
|
||
if !isset available[parts[0]] { | ||
throw new InvalidArgumentException( | ||
"Driver not supported [" . parts[0] . "]" | ||
); | ||
} | ||
|
||
|
||
// if no error mode is specified, use exceptions | ||
if !isset options[\PDO::ATTR_ERRMODE] { | ||
let options[\PDO::ATTR_ERRMODE] = \PDO::ERRMODE_EXCEPTION; | ||
} | ||
|
||
// Arguments store | ||
let this->arguments = [ | ||
dsn, | ||
username, | ||
password, | ||
options, | ||
queries | ||
]; | ||
|
||
// Create a new profiler if none has been passed | ||
if profiler === null { | ||
let profiler = new Profiler(); | ||
} | ||
this->setProfiler(profiler); | ||
} | ||
|
||
/** | ||
* The purpose of this method is to hide sensitive data from stack traces. | ||
* | ||
* @return array | ||
*/ | ||
public function __debugInfo() -> array | ||
{ | ||
return [ | ||
"arguments" : [ | ||
this->arguments[0], | ||
"****", | ||
"****", | ||
this->arguments[3], | ||
this->arguments[4] | ||
] | ||
]; | ||
} | ||
|
||
/** | ||
* Connects to the database. | ||
*/ | ||
public function connect() -> void | ||
{ | ||
var dsn, options, password, query, queries, username; | ||
|
||
if !this->pdo { | ||
// connect | ||
this->profiler->start(__FUNCTION__); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we make the profiler optional for perfomance? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
||
let dsn = this->arguments[0], | ||
username = this->arguments[1], | ||
password = this->arguments[2], | ||
options = this->arguments[3], | ||
queries = this->arguments[4]; | ||
|
||
let this->pdo = new \PDO(dsn, username, password, options); | ||
|
||
this->profiler->finish(); | ||
|
||
// connection-time queries | ||
for query in queries { | ||
this->exec(query); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Disconnects from the database. | ||
*/ | ||
public function disconnect() -> void | ||
{ | ||
this->profiler->start(__FUNCTION__); | ||
|
||
let this->pdo = null; | ||
|
||
this->profiler->finish(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the the DM can be a bit confusing. This could also be Device management, Device mapper, Distributed Mama's ;) etc. Can we just name it DataMapper? It isn't super long, and I don't see the advantages of abbreviation here.