PHP Classes
elePHPant
Icontem

PHP Sweet PDO: Access databases using PDO

Recommend this page to a friend!
  Info   View files View files (8)   DownloadInstall with Composer Download .zip   Reputation   Support forum (2)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2011-05-03 (5 years ago) RSS 2.0 feedNot yet rated by the usersTotal: 396 All time: 6,250 This week: 935Up
Version License PHP version Categories
php-sweet-pdo 1.0Freeware5.3PHP 5, Databases
Description Author

This package can access databases using PDO.

It provides several classes that can establish database connections, execute prepared queries, retrieve query results, insert or update table records using arrays of parameters.

The classes can trigger events that can be fired and call event handler classes that registered to listen to those events for debugging purposes.

Picture of FractalizeR2
Name: FractalizeR2 <contact>
Classes: 1 package by
Country: Russian Federation Russian Federation

Details
What is phpSweetPDO?
=======================================

phpSweetPDO is a php PDO wrapper, which is:

*   Extremly simple to use
*   Phar package to ease deployment. You just need one 9Kb file to work with library (compressed phar)
*   Compact (like 400 lines of commented code which you may compress even further)
*   Optionally supports events, that allow easy profiling and debugging

Examples of use:
=======================================

```php

<?php
require_once('phpsweetpdo.phar');

//Connecting
$connection = new \phpSweetPDO\Connection('mysql:dbname=test;host=127.0.0.1', 'root', 'password');

//Executing DDL
$connection->execute("DROP TABLE IF EXISTS `phpsweetpdo`");

//Selecting only one value
$value = $this->connection->getOneValue("SELECT field2 FROM phpsweetpdo WHERE id=? AND field2 <> ?", array(1, 300));
echo $value;

//Selecing only one row
$record = $this->connection->getOneRow("SELECT * FROM phpsweetpdo WHERE id=:id1 AND field2<>:id2",
                                                 array('id1' => 1, 'id2' => 300));
echo $record->field1 . $record->field2; //Will throw exception if fields do not exist in a row

//Selecting more than 1 row
$recordset = $connection->select("SELECT * FROM phpsweetpdo ORDER BY field1 ASC");
foreach ($recordset as $currentRow) {
    echo $currentRow->id; //Will throw exception if field id does not exist in recordset
}

//Output parameters of stored procedures
$this->connection->execute("CALL phpsweetpdo_out(@test)");
$result = $this->connection->getOneValue("SELECT @test");

//INSERT and UPDATE build helpers
use phpSweetPDO\SQLHelpers\Basic as Helpers;
$sql = Helpers::insert('mytable', array('field_name' => 'field_value'));
$connection->execute($sql); // INSERT INTO mytable (field_name) VALUES (:field_name); //:field_name = 'field_value'

$sql = Helpers::update('mytable', array('field_name' => 'field_value'), "field_2=13");
$connection->execute($sql); // UPDATE test SET field_name=:field_name WHERE field_2='13'; //:field_name = 'field_value'
```

Events
=======================================
If you pass sfEventDispatcher to the constructor of Connection class, it will fire events on it's actions.

You can read more about event dispatcher here: http://components.symfony-project.org/event-dispatcher/documentation.

The following events can be tracked down:

*    phpsweetpdo.connect.started **/** phpsweetpdo.connect.finished
*    phpsweetpdo.execute.started **/** phpsweetpdo.execute.finished
*    phpsweetpdo.select.started **/** phpsweetpdo.select.finished
*    phpsweetpdo.get_one.value_started **/** phpsweetpdo.get_one_value.finished
*    phpsweetpdo.get_one.row_started **/** phpsweetpdo.get_one_row.finished
*    phpsweetpdo.begin_transaction.started **/** phpsweetpdo.begin_transaction.finished
*    phpsweetpdo.commit_transaction.started **/** phpsweetpdo.commit_transaction.finished
*    phpsweetpdo.rollback_transaction.started **/** phpsweetpdo.rollback_transaction.finished

Most events is accompanied by parameters. They are mostly _sql_ (sql query which is executing), _params_ (parameters,
passed to query), and _driver_options_ - driver options used, if any.


```php
<?php

public function onEvent(sfEvent $event) {
    echo $event->getName();
    $params = $event->getParameters();
    echo 'SQL query is ' . $params['sql'];
}

$eventDispatcher = new sfEventDispatcher();
$eventDispatcher->connect('phpsweetpdo.select.started', 'onEvent');
$eventDispatcher->connect('phpsweetpdo.select.finished', 'onEvent');

$this->_connection = new \phpSweetPDO\Connection('mysql:dbname=test;host=127.0.0.1', 'root', '', $eventDispatcher);
$this->_connection->select("SELECT * FROM phpsweetpdo ORDER BY field1 ASC");
//At this point our onEvent() function will be called twice with respected events and will print the query,
//we tried to execute.
```


Limitations
=======================================

phpSweetPDO does not support (at least yet, send pull requests):

*   Typed PDO parameters. All params are passed as is to PDOStatement->execute() (For example, MySQL deals with type
conversion itself. The same probably goes to many other database engines).
*   Explicit output parameters. You cannot mark a parameter as output in phpSweetPDO call. But you can use
a method described above in the examples section.


Requirements
=======================================

*   PHP 5.3 or higher (you can use lower versions, but the code will need to get cleaned from namespaces then).
  Files folder image Files  
File Role Description
Files folder imagephpSweetPDO (4 files, 2 directories)
Accessible without login Plain text file LICENSE.txt Lic. Licence
Accessible without login Plain text file README.markdown Doc. Readme and examples

  Files folder image Files  /  phpSweetPDO  
File Role Description
Files folder imageExceptions (1 file)
Files folder imageSQLHelpers (1 file)
  Accessible without login Plain text file Autoloader.php Class Autoloader (PSR-0 compliant)
  Accessible without login Plain text file Connection.php Class Main class for connecting to database and executing queries
  Accessible without login Plain text file Recordset.php Class Recordset class
  Accessible without login Plain text file RecordsetRow.php Class Recordset row (record) class

  Files folder image Files  /  phpSweetPDO  /  Exceptions  
File Role Description
  Accessible without login Plain text file DbException.php Class Detailed info about PDO exception

  Files folder image Files  /  phpSweetPDO  /  SQLHelpers  
File Role Description
  Accessible without login Plain text file Basic.php Class Helpers to build insert and update queries

 Version Control Unique User Downloads Download Rankings  
 0%
Total:396
This week:0
All time:6,250
This week:935Up