database abstraction layer database abstraction layers in PHP Lukas Smith BackendMedia smith@backendmedia.com



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

Q&A for Zend Framework Database Access

Database Migration from MySQL to RDM Server

php tek 2006 in Orlando Florida Lukas Kahwe Smith

A Brief Introduction to MySQL

Database Administration with MySQL

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

Instant SQL Programming

SQL Server for developers. murach's TRAINING & REFERENCE. Bryan Syverson. Mike Murach & Associates, Inc. Joel Murach

DBMS Project. COP Spring Final Submission Report

D61830GC30. MySQL for Developers. Summary. Introduction. Prerequisites. At Course completion After completing this course, students will be able to:

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

INFORMATION BROCHURE Certificate Course in Web Design Using PHP/MySQL

How To Create A Table In Sql (Ahem)

PHP Language Binding Guide For The Connection Cloud Web Services

Database SQL messages and codes

ODBC Client Driver Help Kepware, Inc.

Database Programming with PL/SQL: Learning Objectives

Overview of Databases On MacOS. Karl Kuehn Automation Engineer RethinkDB

ORACLE 9I / 10G / 11G / PL/SQL COURSE CONTENT

Database Driven Websites Using PHP with Informix

Geodatabase Programming with SQL

Real SQL Programming 1

Zend Framework Database Access

PHP Data Objects Layer (PDO) Ilia Alshanetsky

Advanced SQL. Jim Mason. Web solutions for iseries engineer, build, deploy, support, train

Database Schema Deployment. Lukas Smith - lukas@liip.ch CodeWorks PHP on the ROAD

XEP-0043: Jabber Database Access

An Oracle White Paper June Migrating Applications and Databases with Oracle Database 12c

Migrating Non-Oracle Databases and their Applications to Oracle Database 12c O R A C L E W H I T E P A P E R D E C E M B E R

Cloud Powered Mobile Apps with Azure

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

Financial Data Access with SQL, Excel & VBA

SQL Databases Course. by Applied Technology Research Center. This course provides training for MySQL, Oracle, SQL Server and PostgreSQL databases.

Object Relational Database Mapping. Alex Boughton Spring 2011

Physical Design. Meeting the needs of the users is the gold standard against which we measure our success in creating a database.

SQL Simple Queries. Chapter 3.1 V3.0. Napier University Dr Gordon Russell

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

SAP Business Objects Business Intelligence platform Document Version: 4.1 Support Package Data Federation Administration Tool Guide

A basic create statement for a simple student table would look like the following.

SQL. Short introduction

Oracle Database 12c Enables Quad Graphics to Quickly Migrate from Sybase to Oracle Exadata

sqlite driver manual

Extracting META information from Interbase/Firebird SQL (INFORMATION_SCHEMA)

The Relational Model. Why Study the Relational Model? Relational Database: Definitions

Using Database Metadata and its Semantics to Generate Automatic and Dynamic Web Entry Forms

MySQL Command Syntax

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

EMBL-EBI. Database Replication - Distribution

WEB DEVELOPMENT COURSE (PHP/ MYSQL)

SQL Server Training Course Content

Database migration. from Sybase ASE to PostgreSQL. Achim Eisele and Jens Wilke. 1&1 Internet AG

DBMS / Business Intelligence, SQL Server

Partitioning under the hood in MySQL 5.5

Oracle9i Database and MySQL Database Server are

Using Object Database db4o as Storage Provider in Voldemort

The Relational Model. Why Study the Relational Model?

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

New Features in MySQL 5.0, 5.1, and Beyond

Security Test s i t ng Eileen Donlon CMSC 737 Spring 2008

Linas Virbalas Continuent, Inc.

Oracle Database: SQL and PL/SQL Fundamentals NEW

Full Text Search with Sphinx

Achieving Database Interoperability Across Data Access APIs through SQL Up-leveling

types, but key declarations and constraints Similar CREATE X commands for other schema ëdrop X name" deletes the created element of beer VARCHARè20è,

Using MySQL for Big Data Advantage Integrate for Insight Sastry Vedantam

Programming Database lectures for mathema

Oracle BI EE Implementation on Netezza. Prepared by SureShot Strategies, Inc.

DIPLOMA IN WEBDEVELOPMENT

Facebook Twitter YouTube Google Plus Website

Database Concepts for a new generation of CMS

INSTALLING, CONFIGURING, AND DEVELOPING WITH XAMPP

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

Information Systems SQL. Nikolaj Popov

5.1 Database Schema Schema Generation in SQL

RNDr. Michal Kopecký, Ph.D. Department of Software Engineering, Faculty of Mathematics and Physics, Charles University in Prague

FileMaker 12. ODBC and JDBC Guide

MySQL és Hadoop mint Big Data platform (SQL + NoSQL = MySQL Cluster?!)

Short notes on webpage programming languages

OBJECTSTUDIO. Database User's Guide P

Introduction to Triggers using SQL

1. INTRODUCTION TO RDBMS

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

Relational Databases. Christopher Simpkins

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

JDBC (Java / SQL Programming) CS 377: Database Systems

Database Extension 1.5 ez Publish Extension Manual

Introduction to SQL for Data Scientists

Top 10 Oracle SQL Developer Tips and Tricks

Databases 2011 The Relational Model and SQL

Pervasive Data Integrator. Oracle CRM On Demand Connector Guide

ANDROID APPS DEVELOPMENT FOR MOBILE GAME

Comparing SQL and NOSQL databases

DataFlex Connectivity Kit For ODBC User's Guide. Version 2.2

Oracle Database: Program with PL/SQL

Oracle Database: Develop PL/SQL Program Units

COSC344 Database Theory and Applications. Java and SQL. Lecture 12

Complex Data and Object-Oriented. Databases

Package sjdbc. R topics documented: February 20, 2015

Transcription:

Lukas Smith database abstraction layers in PHP BackendMedia 1

Overview Introduction Motivation PDO extension PEAR::MDB2 Client API SQL syntax SQL concepts Result sets Error handling High level features 2

Motivation Support for multiple RDBMS Forward compatibility new RDBMS versions new PHP (extension) versions (MySQLi/PDO) Vendor Lock in Train costs Pushing your own preference to the client Higher level API 3

PDO Extension C Extension OO Interface Driver model Client API Unification Planned for PHP 5.1 Iterators Marked with! 4

PEAR::MDB2 (1/2) OO Interface Driver model Compatible with PHP4 and PHP5 PDO API compatible (eventually) On demand loading of advanced features Portability mode optional PEAR_Error and debug capabilities Iterators 5

PEAR::MDB2 (2/2) Installation: $> pear install MDB2-beta $> pear install MDB2_Schema-beta Already in production (planet-php.org) MDB was a merge of Metabase and DB MDB2 consolidates this merge MDB2 is much faster than as Metabase, faster than DB, and as fast as ADODB (*) Marked with!» (*) all benchmarks lie! 6

Client API Differences between RDBMS extensions Example: connect to RDBMS (!!): ibase_pconnect()/ibase_connect() OCIPLogon()/OCILogon() sqlite_popen()/sqlite_open() Example: sending SQL to the server (!!): OCIExecute() mysql_query()/mysql_unbuffered_query() pg_exec() Preference for an OO API (!!) 7

SQL syntax (1/5) SELECT LIMIT (!) Missing in some RDBMS (like Oracle) Syntax differences // set limit $db->setlimit(10, 20); REPLACE (!) Only exists in MySQL and SQLite TRANSACTIONS Missing in some RDBMS (MySQL with MyISAM) Syntax differences (!!) 8

SQL syntax (2/5) SUBSELECT (!) Missing in some RDBMS (z.b. MySQL < 4.x) $sql = 'SELECT name FROM foo WHERE id ='. $db->quote(mdb2_date::now(), 'time'); // subselect $subselect = $db->subselect($sql, 'text'); // sql string $sql = 'SELECT is_active FROM bar WHERE id IN ('.$subselect.')'; // send off query $res = $db->query($sql, 'boolean'); 9

SQL syntax (3/5) Data types (!) Date (!) Boolean (!) Decimal (!) Float (!) LOB (!!) $db->quote($text, text'); $db->quote($date, timestamp'); $db->quote($bool, boolean'); $db->quote($fp, blob'); 10

SQL syntax (4/5) Schema Management (!) CREATE Database Table Field Index Sequence ALTER TABLE SHOW Databases Tables Indexes Sequences Views Functions 11

SQL syntax (5/5) SQL functions Function names: SUBSTR() vs. SUBSTRING() Parameter order Offset count starts at 0 or 1 Stored Procedures Client API Syntax Very hard to do Requires using a meta programming language (MetaL) 12

SQL concepts (1/5) Sequences vs. auto increment Sequences can emulate auto increment (!) Auto increment can only emulate sequences with a query builder $id = $db->getbeforeid($table); $sql = INSERT INTO $table (.$db- >quote($id, 'integer').', 'foo')'; $db->query($sql); $id = $mdb2->getafterid($id, $table); 13

SQL concepts (2/5) Prepared statements:? placeholder (!!) // create sql string $sql = 'INSERT INTO foo (?,?,?)'; // send off query $types = array('text', 'decimal', 'blob') $stmt = $db->prepare($sql, $types); // bind values $stmt->bindparam(0, $text); $stmt->bindparam(1, $decimal); $stmt->bindparam(2, $fp); 14

SQL concepts (3/5) Prepared statements: named parameters (!!) // create sql string $sql = 'INSERT INTO foo (:url, :dm, :file)'; // send off query $types = array('text', 'decimal', 'blob') $stmt = $db->prepare($sql, $types); // bind values $stmt->bindparam( url', $text); $stmt->bindparam( dm', $decimal); $stmt->bindparam( file', $fp); 15

SQL concepts (4/5) Execute statement (!!) // send off query $result = $stmt->execute(); // bind next set of data $text = 'foo'; $decimal = 23453/8; $fp = 'bar.pdf'; // send off query $result = $stmt->execute(); 16

SQL concepts (5/5) Database (!) Foreign keys / Trigger Only possible with a query builder VIEWS Only possible with a query builder User management Some what doable 17

Result sets (Un-)Buffered (!!) NULL vs. empty strings (!!) Associative fetches (!!) Padded strings (!!) Introspection (!!) Data types (!) // create sql string $sql = 'SELECT datebirth, decmoney FROM foo'; // send off query $types = array( date', 'float') $res = $db->query($sql, $types); 18

Error handling Different error codes or no support for error codes Error codes (!!) Connection errors Syntactical errors Semantic errors Access violations etc. 19

High level features (1/3) XML schema (!) CREATE and ALTER statement generation User defined data types (!) Serialize PEAR Error (!) PEAR::expectError() PEAR::setErrorHandling() Exceptions Native (option) (!) Via callback (!) 20

High level features (2/3) // expect no such table error $db->expecterror(mdb2_error_nosuchtable); // send off query, fetch all data and rekey $res = $db->queryall($sql, $types, MDB2_FETCHMODE_ASSOC, true); $db->popexpect(); // table didnt exist? if (MDB2::isError($res, MDB2_ERROR_NOSUCHTABLE) {..} 21

High level features (3/3) fetchall() (!!) Read all rows of a result set into a multi dimensional array all at once Result buffering Rekey (!!) Use the first column as the key for the first array dimension instead of the row number Grouping (!!) All rows with the same value in the first column are grouped inside another array 22

Thank you for your attention! Questions? Feedback? 23