Advanced Object Oriented Database access using PDO. Marcus Börger



Similar documents
database abstraction layer database abstraction layers in PHP Lukas Smith BackendMedia

PHP Data Objects Layer (PDO) Ilia Alshanetsky

Database Driven Websites Using PHP with Informix

Q&A for Zend Framework Database Access

Web development... the server side (of the force)

7 Web Databases. Access to Web Databases: Servlets, Applets. Java Server Pages PHP, PEAR. Languages: Java, PHP, Python,...

Zend Framework Database Access

Database Toolkit: Portable and Cost Effective Software

Web Development using PHP (WD_PHP) Duration 1.5 months

Facebook Twitter YouTube Google Plus Website

Using Object Database db4o as Storage Provider in Voldemort

Connecting to a Database Using PHP. Prof. Jim Whitehead CMPS 183, Spring 2006 May 15, 2006

Services. Relational. Databases & JDBC. Today. Relational. Databases SQL JDBC. Next Time. Services. Relational. Databases & JDBC. Today.

Object Relational Database Mapping. Alex Boughton Spring 2011

ODBC Client Driver Help Kepware, Inc.

PHP Tutorial From beginner to master

DBMS / Business Intelligence, SQL Server

Developing a Web Server Platform with SAPI Support for AJAX RPC using JSON

Writing Scripts with PHP s PEAR DB Module

The JAVA Way: JDBC and SQLJ

The release notes provide details of enhancements and features in Cloudera ODBC Driver for Impala , as well as the version history.

SQL PDO and Microsoft SQL Server

Overview of Databases On MacOS. Karl Kuehn Automation Engineer RethinkDB

Online Multimedia Winter semester 2015/16

Chapter 9, More SQL: Assertions, Views, and Programming Techniques

Using IRDB in a Dot Net Project

PHP Language Binding Guide For The Connection Cloud Web Services

Sharding with postgres_fdw

Developing Perl, PHP, Python, and Ruby on Rails Applications

A SQL Injection : Internal Investigation of Injection, Detection and Prevention of SQL Injection Attacks

Database Programming. Week *Some of the slides in this lecture are created by Prof. Ian Horrocks from University of Oxford

Oracle PL/SQL Injection

How To Use Postgresql With Foreign Data In A Foreign Server In A Multi Threaded Database (Postgres)

DBMS Project. COP Spring Final Submission Report

SQL. by Steven Holzner, Ph.D. ALPHA. A member of Penguin Group (USA) Inc.

COSC 6397 Big Data Analytics. 2 nd homework assignment Pig and Hive. Edgar Gabriel Spring 2015

1. What is SQL Injection?

Detecting (and even preventing) SQL Injection Using the Percona Toolkit and Noinject!

Database migration using Wizard, Studio and Commander. Based on migration from Oracle to PostgreSQL (Greenplum)

OBJECTSTUDIO. Database User's Guide P

Duration Vendor Audience 5 Days Oracle Developers, Technical Consultants, Database Administrators and System Analysts

Raima Database Manager Version 14.0 In-memory Database Engine

DIPLOMA IN WEBDEVELOPMENT

Exploring Microsoft Office Access Chapter 2: Relational Databases and Multi-Table Queries

RDS Migration Tool Customer FAQ Updated 7/23/2015

MANDARAX + ORYX An Open-Source Rule Platform

Assignment 3 Version 2.0 Reactive NoSQL Due April 13

Mercury Users Guide Version 1.3 February 14, 2006

Maksym Iaroshenko Co-Founder and Senior Software Engineer at Eltrino. Magento non-mysql implementations

Instant SQL Programming

Advanced PostgreSQL SQL Injection and Filter Bypass Techniques

FileMaker 11. ODBC and JDBC Guide

CS 377 Database Systems SQL Programming. Li Xiong Department of Mathematics and Computer Science Emory University

CONTACTS SYNCHRONIZER FOR IPAD USER GUIDE

"SQL Database Professional " module PRINTED MANUAL

OTN Developer Day: Oracle Big Data. Hands On Lab Manual. Introduction to Oracle NoSQL Database

ODBC Sample Application for Tandem NonStop SQL/MX

Chapter 9 Java and SQL. Wang Yang wyang@njnet.edu.cn

Intended status: Standards Track October 8, 2014 Expires: April 11, 2015

Short notes on webpage programming languages

Linas Virbalas Continuent, Inc.

Advanced Tornado TWENTYONE Advanced Tornado Accessing MySQL from Python LAB

Programming Database lectures for mathema

Real SQL Programming 1

Web Services for Management Perl Library VMware ESX Server 3.5, VMware ESX Server 3i version 3.5, and VMware VirtualCenter 2.5

INSTALLING, CONFIGURING, AND DEVELOPING WITH XAMPP

IRF2000 IWL3000 SRC1000 Application Note - Apps with OSGi - Condition Monitoring with WWH push

Database Access from a Programming Language: Database Access from a Programming Language

Database Access from a Programming Language:

Simba Apache Cassandra ODBC Driver

Package sjdbc. R topics documented: February 20, 2015

FileMaker 12. ODBC and JDBC Guide

Introduction. Introduction: Database management system. Introduction: DBS concepts & architecture. Introduction: DBS versus File system

Classes para Manipulação de BDs 5

An Introduction to Developing ez Publish Extensions

DataLogger Kepware, Inc.

Database Extension 1.5 ez Publish Extension Manual

JDBC. It is connected by the Native Module of dependent form of h/w like.dll or.so. ex) OCI driver for local connection to Oracle

Database Management System Choices. Introduction To Database Systems CSE 373 Spring 2013

Introduction: Database management system

1. INTERFACE ENHANCEMENTS 2. REPORTING ENHANCEMENTS

WHITE PAPER. Domo Advanced Architecture

Building a Multi-Threaded Web Server

CS506 Web Design and Development Solved Online Quiz No. 01

SQL Injection. SQL Injection. CSCI 4971 Secure Software Principles. Rensselaer Polytechnic Institute. Spring

Application note: Connecting the to a Database

The full setup includes the server itself, the server control panel, Firebird Database Server, and three sample applications with source code.

FileMaker 13. ODBC and JDBC Guide

Introduction to HP ArcSight ESM Web Services APIs

Database Migration from MySQL to RDM Server

ASP.NET Programming with C# and SQL Server

Data Access Guide. BusinessObjects 11. Windows and UNIX

Manage cloud infrastructures using Zend Framework

ODBC Chapter,First Edition

How to Design and Create Your Own Custom Ext Rep

Conquer the 5 Most Common Magento Coding Issues to Optimize Your Site for Performance

ADOBE AIR. Working with Data in AIR. David Tucker

MOC 20461C: Querying Microsoft SQL Server. Course Overview

SQL and programming languages

Transcription:

Advanced Object Oriented Database access using PDO Marcus Börger ApacheCon EU 2005

Marcus Börger Advanced Object Oriented Database access using PDO 2 Intro PHP and Databases PHP 5 and PDO

Marcus Börger Advanced Object Oriented Database access using PDO 3 PHP 4 and Databases PHP can connect to all important RDBMS Each RDBMS needs a separate extension Each extension has a different interface ext/dbx is an inefficient abstraction Multiple PEAR solutions Abstraction layers Query builders Data Access Objects... Nested Set support But there is no OO in PHP 4

Marcus Börger Advanced Object Oriented Database access using PDO 4 PHP 5 and Databases PHP can connect to all important RDBMS PDO provides a unified efficient abstraction PHP is ready for UML Specialized extensions allow detailed control Multiple PEAR solutions More sophisticated abstraction layers Query builders Data Access Objects... Nested Set support Multiple ways of using databases with PHP File based as ext/dba or ext/sqlite or embedded MySQL Talking SQL with embedded RDBMS Talking SQL with external RDBMS Using ODBC

Marcus Börger Advanced Object Oriented Database access using PDO 5 Dedicated Host Apache mod_php Internet Browser Browser Browser Browser Browser Browser Browser database extension SQL

Marcus Börger Advanced Object Oriented Database access using PDO 6 ISP/Shared Host Apache mod_php Internet Browser Browser Browser Browser Browser Browser Browser database extension SQL

Marcus Börger Advanced Object Oriented Database access using PDO 7 Embedded GTK /??? CLI / EMBED dba / dbase NO SQL

Marcus Börger Advanced Object Oriented Database access using PDO 8 Embedded GTK /??? CLI / EMBED database extension MySQLi SQLite (PDO) SQL

Marcus Börger Advanced Object Oriented Database access using PDO 9 PHP and Databases PHP can connect to all important RDBMs Oracle PostgreSQL MySQL Interbase/Firebird ODBC SQLite MS-SQL msql All talk some SQL dialect and All using different API DBM-style databases Support for native XML database available using XQL

Marcus Börger Advanced Object Oriented Database access using PDO 10 PHP and Databases PHP can connect to all important RDBMs Oracle PDO & native (pc) PostgreSQL PDO & native (pc) MySQL PDO & native (pc/oo) Interbase/Firebird PDO & native (pc) ODBC PDO & native (pc) SQLite PDO & native (pc/oo) MS-SQL PDO & native (pc) msql native (pc) DBM-style databases Support for native XML database available using XQL

Marcus Börger Advanced Object Oriented Database access using PDO 11 PDO at a glance Data access abstraction (API unification) Multiple database plug-in extensions Object oriented Iterator support Destructive read support All written in a tiny c layer Will be used us base layer of upcoming MDB2 Available through PECL Buildable for PHP 5.0 Built-in starting from 5.1 Windows DLLs available Already used in a few production servers ATM still marked experimental

Marcus Börger Advanced Object Oriented Database access using PDO 12 PDO at a glance Prepared statements (unified, name and index) SQL state error code Portability attributes Transaction supprt Scrollable cursors Uses normal PHP error facilities or Exceptions Plans: LOB support

Marcus Börger Advanced Object Oriented Database access using PDO 13 Connecting to the database PDO uses DSNs to connect <handler-name> ':' <native-dsn> try { $dbh = new PDO($dsn, $user, $password, $options); // // Use the database // // and close it $dbh = NULL; catch (PDOException $e) { echo "Failed to connect:". $e->getmessage();

Marcus Börger Advanced Object Oriented Database access using PDO 14 PDO DSN format odbc:odbc_dsn mysql:host=name;dbname=dbname sqlite:/path/to/db/file sqlite::memory: sqlite2:/path/to/sqlite2/file pgsql:host=localhost port=5432 dbname=test oci:dbname=dbname;charset=charset firebird:dbname=db;charset=charset;role=role

Marcus Börger Advanced Object Oriented Database access using PDO 15 Direct SQL execution PDO::exec() allows to avoid PDOStatement object Most usefull for DDL (i.e. CREATE) and INSETR, UPDATE $dbh = new PDO($dsn); $cnt = $dbh->exec($sql); if ($cnt!== false) { echo "Rows affected: ". $cnt; echo "Last inserted id: ". $dbh->lastinsertid(); else { echo "Error";

Marcus Börger Advanced Object Oriented Database access using PDO 16 Fetching data with prepare The default fetch methodology is unbuffered Uses methods prepare() and execute() Forward only Row count unknown $dbh = new PDO($dsn); $stmt = $dbh->prepare("select * FROM FOO"); $stmt->execute(); while ($row = $stmt->fetch()) { // use data in $row $stmt = null;

Marcus Börger Advanced Object Oriented Database access using PDO 17 Fetching data w/o prepare Uses method query() Forward only Row count unknown $dbh = new PDO($dsn); $stmt = $dbh->query("select * FROM FOO"); $stmt->execute(); while ($row = $stmt->fetch()) { // use data in $row $stmt = null;

Marcus Börger Advanced Object Oriented Database access using PDO 18 Fetching data from iterator Faster data access Works with and without preparation Forward only Row count not available $dbh = new PDO($dsn); $stmt = $dbh->prepare("select * FROM FOO"); $stmt->execute(); foreach ($stmt as $row) { // use data in $row $stmt = null; foreach($dbh->query("select * FROM bar") as $row) { // use data in $row

Marcus Börger Advanced Object Oriented Database access using PDO 19 Fetching data into array Data is fully buffered Works with and without preparation Randam access Row count available Usefull if database doesn't support parallel queries $dbh = new PDO($dsn); $stmt = $dbh->prepare("select * FROM FOO"); $stmt->execute(); $data = $stmt->fetchall(); foreach ($data as $row) { // use data in $row $stmt = null;

Marcus Börger Advanced Object Oriented Database access using PDO 20 How to retrieve data Fetch single dataset in default way mixed PDOStatement::fetch( int $mode = PDO_FETCH_BOTH, int $orientation = PDO_FETCH_ORI_NEXT, int $offset = 0) also controlled by void PDOStatement::setFetchMode( int $mode, // PDO_FETCH_* [mixed*$params]) // mode specific params Fetch single column value mixed PDOStatement::fetchColumn( int $column_number = 0) // zero based index

Marcus Börger Advanced Object Oriented Database access using PDO 21 How to retrieve data Fetch all rows at once array PDOStatement::fetchAll( int $mode = PDO_FETCH_BOTH, string $class_name = NULL, array $ctor_args = NULL) Fetch single row as object mixed PDOStatement::fetchObject( string $class_name = NULL, array $ctor_args = NULL)

Marcus Börger Advanced Object Oriented Database access using PDO 22 Fetch modes and flags Modes PDO_FETCH_ASSOC associative array PDO_FETCH_NUM numeric array PDO_FETCH_BOTH default (assoc/numeric) PDO_FETCH_OBJ into stdclass object PDO_FETCH_BOUND into bound variables PDO_FETCH_COLUMN single column PDO_FETCH_CLASS into new instance PDO_FETCH_INTO into existing object PDO_FETCH_FUNC through function call Flags PDO_FETCH_GROUP group by first col PDO_FETCH_UNIQUE group unique by first col PDO_FETCH_CLASSTYPE use class name in row PDO_FETCH_SERIALIZE use serialization

Marcus Börger Advanced Object Oriented Database access using PDO 23 PDO_FETCH_BOUND Fetching returns true until there is no more data Binding parameters by "?" in sql (1 based index) Binding parameters by ":name" in sql Binding columns by name and index $dbh = new PDO($dsn); $stmt = $dbh->prepare( 'SELECT url FROM urls WHERE key=:urlkey'); $stmt->bindparam(':urlkey', $urlkey); $stmt->bindcolumn('url', $href); $urlkey =...; // get url key to translate $stmt->execute(); // execute the query // fetch data $stmt->fetch(pdo_fetch_bound); // use data echo '<a href="'. $href. '">'. $urlkey. '</a>';

Marcus Börger Advanced Object Oriented Database access using PDO 24 PDO_FETCH_BOUND Fetching returns true until there is no more data Binding parameters by "?" in sql 1 based index Binding parameters by ":name" in sql Binding columns by name and index Binding can be done on execute() $dbh = new PDO($dsn); $stmt = $dbh->prepare( 'SELECT url FROM urls WHERE key=:urlkey'); $urlkey =...; // get url key to translate $stmt->execute(array(':urlkey' => $urlkey), array('url' => $href)); // fetch data $stmt->fetch(pdo_fetch_bound); // use data echo '<a href="'. $href. '">'. $urlkey. '</a>';

Marcus Börger Advanced Object Oriented Database access using PDO 25 PDO_FETCH_CLASS Lets you specify the class to instantiate PDO_FETCH_OBJ always uses stdclass Writes data before calling construct Can write private/protected members Lets you call the constructor with parameters class Person { protected $dbh, $fname, $lname; function construct($dbh) { $this->dbh = $dbh; function tostring() { return $this->fname. " ". $this->lname; $stmt = $dbh->prepare('select fname, lname FROM persons'); $stmt->setfetchmode(pdo_fetch_class, 'Person', array($dbh)); $stmt->execute(); foreach($stmt as $person) { echo $person;

Marcus Börger Advanced Object Oriented Database access using PDO 26 PDO_FETCH_CLASSTYPE Lets you fetch the class to instantiate from rows Must be used with PDO_FETCH_CLASS The class name specified in fetch mode is a fallback class Person { /*... */ class Employee extends Person { /*... */ class Manager extends Employee { /*... */ $stmt = $dbh->prepare( 'SELECT class, fname, lname FROM persons LEFT JOIN classes ON persons.kind = classes.id'); $stmt->setfetchmode(pdo_fetch_class PDO_FETCH_CLASSTYPE, 'Person', array($dbh)); $stmt->execute(); foreach($stmt as $person) { echo $person;

Marcus Börger Advanced Object Oriented Database access using PDO 27 PDO_FETCH_INTO Lets you reuse an already instantiated object Does not allow to read into protected or private Because the constructor was already executed class Person { public $dbh, $fname, $lname; function construct($dbh) { $this->dbh = $dbh; function tostring() { return $this->fname. " ". $this->lname; $stmt = $dbh->prepare('select fname, lname FROM persons'); $stmt->setfetchmode(pdo_fetch_into, new Person($dbh)); $stmt->execute(); foreach($stmt as $person) { echo $person;

Marcus Börger Advanced Object Oriented Database access using PDO 28 PDO_FETCH_FUNC Lets you specify a function to execute on each row class Person { protected $fname, $lname; static function Factory($fname, $lname) { $obj = new Person; $obj->fname = $fname; $obj->lname = $lname; function tostring() { return $this->fname. " ". $this->lname; $stmt = $dbh->prepare('select fname, lname FROM persons'); $stmt->setfetchmode(pdo_fetch_func, array('person', 'Factory')); $stmt->execute(); foreach($stmt as $person) { echo $person;

Marcus Börger Advanced Object Oriented Database access using PDO 29 PDOStatement as real iterator PDOStatement only implements Traversable Wrapper IteratorIterator takes a Traverable $it = new IteratorIterator($stmt); Now the fun begins Just plug this into any other iterator Recursion, SQL external unions, Filters, Limit, foreach(new LimitIterator($it, 10) as $data) { var_dump($data);

Marcus Börger Advanced Object Oriented Database access using PDO 30 Deriving PDOStatement prepare() allows to specify fetch attributes PDOStatement PDO::prepare( string $sql, array(pdo_attr_statement_class => array(string classname, array(mixed * ctor_args)))); class MyPDOStatement extends PDOStatement { protected $dbh; function construct($dbh) { $this->dbh = $dbh; $dbh->prepare($sql, array(pdo_attr_statement_class => array('mypdostatement', array($dbh))));

Marcus Börger Advanced Object Oriented Database access using PDO 31 Deriving PDOStatement Deriving allows to convert to real iterator class PDOStatementAggregate extends PDOStatement implements IteratorAggregate { private function construct($dbh, $classtype) { $this->dbh = $dbh; $this->setfetchmode(pdo_fetch_class, $classtype, array($this)); function getiterator() { $this->execute(); return new IteratorIterator($this, 'PDOStatement'); /* Need to be base class */ $stmt = $dbh->prepare('select * FROM Persons', array(pdo_attr_statement_class => array('pdostatementaggregate', array($dbh, 'Person')))); foreach($stmt as $person){ echo $person;

Marcus Börger Advanced Object Oriented Database access using PDO 32 PDO error modes PDO offers 3 different error modes $dbh->setattribute(pdo_attr_errmode, $mode); PDO_ERRMODE_SILENT Simply ignore any errors PDO_ERRMODE_WARNING Issue errors as standard php warnings PDO_ERRMODE_EXCEPTION Throw exception on errors Map native codes to SQLSTATE standard codes Aditionally offers native info

Marcus Börger Advanced Object Oriented Database access using PDO 33 Performance 10 times Querying 10 rows Iterators vs. Arrays Implemented as engine feature: 56% Building an Array is expensive queryarray vs. query and fetcharray: 89% Function calls are expensive

Marcus Börger Advanced Object Oriented Database access using PDO 34 Performance Buffered vs. Unbuffered: up to 60% Buffered queries need to build a hash table Buffered queries must copy data Unbuffered queries can use destructive reads Copying data is expensive buffered destructive engine extension engine extension copy data copy pointer set NULL

Marcus Börger Advanced Object Oriented Database access using PDO 35 Performance Comparing OO vs. Procedural code PC is easy to program? PC uses resources: O(n*log(n)) PC uses a single function table: 2000... 4000 OO code is little bit more to learn OO code is easy to maintain OO code uses object storage: O(n+c) OO uses small method tables: 10... 100

Marcus Börger Advanced Object Oriented Database access using PDO 36 Performance? Don't get overexcited using PDO your RDBMS is your bottlneck

Marcus Börger Advanced Object Oriented Database access using PDO 37 Links This presenatation http://talks.somabo.de Documenation on PDO http://docs.php.net/pdo The PDO Extension http://pecl.php.net/package/pdo The Windows DLLs http://snaps.php.net