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

T14733 Data Mapper PDO Connection #14824

Merged
merged 27 commits into from
Feb 8, 2020
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
861f130
[#14733] - Added exceptions for the connection
niden Jan 23, 2020
6891ed4
[#14733] - Initial connectioninterface
niden Jan 23, 2020
b44a54e
[#14733] - Added the PDOInterface
niden Jan 23, 2020
f6d85ca
[#14733] - Parser, Profiler interfaces; AbstractConnection initial co…
niden Jan 23, 2020
107206c
[#14733] - Profiler implementation
niden Jan 23, 2020
15437ff
[#14733] - Connection and connection locator; Fixes for the abstract …
niden Jan 25, 2020
66dc40b
[#14733] - Added DM/PDO class
niden Jan 26, 2020
001fba0
[#14733] - Tests for the DM/Pdo
niden Jan 26, 2020
6564c93
[#14733] - Fixtures for DM
niden Jan 26, 2020
91bb717
[#14733] - Added migrations to the new classes; Corrected project_path
niden Jan 26, 2020
b0b63a0
Database tests
niden Feb 5, 2020
3814350
Adjutments to the migration and helper
niden Feb 5, 2020
1038ce4
Test corrections
niden Feb 6, 2020
82cab68
PHPCS
niden Feb 6, 2020
02a91a6
Changed the config
niden Feb 6, 2020
2e7c5f3
Trying without the connection tests
niden Feb 6, 2020
4527227
Moved more tests in
niden Feb 6, 2020
174af25
Correcting test
niden Feb 6, 2020
122543b
Trying debugging
niden Feb 6, 2020
d6b6e1f
Enabling more tests
niden Feb 6, 2020
503e4f8
More debugging
niden Feb 6, 2020
6982ddf
Added all tests enabled only mysql
niden Feb 6, 2020
e80add5
Corrected tests and connection string for sqlite
niden Feb 8, 2020
094ffea
Enabled sqlite tests
niden Feb 8, 2020
fbace94
Changelog update
niden Feb 8, 2020
6073fe3
[#14733] - Renamed DM to DataMapper (namespace)
niden Feb 8, 2020
32222d2
[#14733] - Renaming DM to DataMapper
niden Feb 8, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG-4.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
- Added `Phalcon\Helper\Arr::blackList()` to exclude elements of an array by the keys obtained from the elements of a blacklist [#14801](https://github.com/phalcon/cphalcon/issues/14801) [@TimurFlush](https://github.com/TimurFlush)
- Added `Phalcon\Debug::renderHtml()` to get a HTML representation of the exception [#14794](https://github.com/phalcon/cphalcon/issues/14794) [@TimurFlush](https://github.com/TimurFlush)
- Added `Phalcon\Mvc\Router\Annotations->setActionPreformatCallback($callback)` to set a callback which pre-formats actions to custom pattern [#14819](https://github.com/phalcon/cphalcon/pull/14819)
- Added new PDO wrapper for the Data Mapper implementation, with decorated instance, locator and profiler
- `DM\Connection`
- `DM\Connection\Decorated`
- `DM\Profiler\Profiler`
- `DM\Profiler\MemoryLogger`
- `DM\ConnectionLocator`
This component will be used in the Data Mapper implementation but can be used as a stand alone component for PDO connections. [#14733](https://github.com/phalcon/cphalcon/issues/14733)

## Changed
- Added service checks for the session. Now cookies will be saved in the session only when the `session` service is defined [#11770](https://github.com/phalcon/cphalcon/issues/11770), [#14649](https://github.com/phalcon/cphalcon/pull/14649)
Expand Down
154 changes: 154 additions & 0 deletions phalcon/DM/Pdo/Connection.zep
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;
Copy link
Member

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.


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__);
Copy link
Member

Choose a reason for hiding this comment

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

Can we make the profiler optional for perfomance?

Copy link
Member Author

Choose a reason for hiding this comment

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

The if statement is in the Profiler class (active). It was a tradeoff between putting if statements in every fetch method and not having one method call vs. having the method call and the if is in the profiler.


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();
}
}
Loading