Top Banner
 Databases: The Next Generation Larry Garfield Palantir.net Database maintainer PHP 5 evangelist
31

Databases: The Next Generationszeged2008.drupalcon.org/files/DBTNG_Presentation.pdf · The old system ext/mysql,ext/mysqli, ext/pgsql PHP 3 / MySQL 3 No prepared statements Procedural

Sep 30, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Databases: The Next Generationszeged2008.drupalcon.org/files/DBTNG_Presentation.pdf · The old system ext/mysql,ext/mysqli, ext/pgsql PHP 3 / MySQL 3 No prepared statements Procedural

   

Databases: The Next Generation

● Larry Garfield● Palantir.net● Database maintainer● PHP 5 evangelist

Page 2: Databases: The Next Generationszeged2008.drupalcon.org/files/DBTNG_Presentation.pdf · The old system ext/mysql,ext/mysqli, ext/pgsql PHP 3 / MySQL 3 No prepared statements Procedural

   

The old system

● ext/mysql,ext/mysqli, ext/pgsql

● PHP 3 / MySQL 3● No prepared 

statements● Procedural● Manual escaping

Page 3: Databases: The Next Generationszeged2008.drupalcon.org/files/DBTNG_Presentation.pdf · The old system ext/mysql,ext/mysqli, ext/pgsql PHP 3 / MySQL 3 No prepared statements Procedural

   

PDO

● Formal prepared statements– Type­agnostic queries

● Object­oriented API● Unified access API (not abstraction API)

Page 4: Databases: The Next Generationszeged2008.drupalcon.org/files/DBTNG_Presentation.pdf · The old system ext/mysql,ext/mysqli, ext/pgsql PHP 3 / MySQL 3 No prepared statements Procedural

   

What does that give us?

● Multiple database types at once● Improved database portability● Easier driver implementation● Database­specific optimizations● Transactions

Page 5: Databases: The Next Generationszeged2008.drupalcon.org/files/DBTNG_Presentation.pdf · The old system ext/mysql,ext/mysqli, ext/pgsql PHP 3 / MySQL 3 No prepared statements Procedural

   

What can we do with it?

● Array­based database configuration● Master/save replication● Portable type­safe queries● Structured query builders

Page 6: Databases: The Next Generationszeged2008.drupalcon.org/files/DBTNG_Presentation.pdf · The old system ext/mysql,ext/mysqli, ext/pgsql PHP 3 / MySQL 3 No prepared statements Procedural

   

New concepts

● Database targets● Placeholders● Fluent API

– All modifier queries– Never write INSERT again

Page 7: Databases: The Next Generationszeged2008.drupalcon.org/files/DBTNG_Presentation.pdf · The old system ext/mysql,ext/mysqli, ext/pgsql PHP 3 / MySQL 3 No prepared statements Procedural

   

Targets

● Alternate database connection● May be used or not● Fallback to default● Master/slave replication● Multi­slave configuration

Page 8: Databases: The Next Generationszeged2008.drupalcon.org/files/DBTNG_Presentation.pdf · The old system ext/mysql,ext/mysqli, ext/pgsql PHP 3 / MySQL 3 No prepared statements Procedural

   

Configuration

$db_url

Page 9: Databases: The Next Generationszeged2008.drupalcon.org/files/DBTNG_Presentation.pdf · The old system ext/mysql,ext/mysqli, ext/pgsql PHP 3 / MySQL 3 No prepared statements Procedural

   

Master/slave configuration

Page 10: Databases: The Next Generationszeged2008.drupalcon.org/files/DBTNG_Presentation.pdf · The old system ext/mysql,ext/mysqli, ext/pgsql PHP 3 / MySQL 3 No prepared statements Procedural

   

New drivers are easy

● Subclass and god● Magic directory location● No hard­coded list

Page 11: Databases: The Next Generationszeged2008.drupalcon.org/files/DBTNG_Presentation.pdf · The old system ext/mysql,ext/mysqli, ext/pgsql PHP 3 / MySQL 3 No prepared statements Procedural

   

Obligatory

Page 12: Databases: The Next Generationszeged2008.drupalcon.org/files/DBTNG_Presentation.pdf · The old system ext/mysql,ext/mysqli, ext/pgsql PHP 3 / MySQL 3 No prepared statements Procedural

   

Types of queries

● Static● Dynamic● Insert● Update● Delete● Merge

Page 13: Databases: The Next Generationszeged2008.drupalcon.org/files/DBTNG_Presentation.pdf · The old system ext/mysql,ext/mysqli, ext/pgsql PHP 3 / MySQL 3 No prepared statements Procedural

   

Static queries

● Doesn't change based on environment● Takes named or unnamed placeholders● (May remove unnamed placeholders)

Page 14: Databases: The Next Generationszeged2008.drupalcon.org/files/DBTNG_Presentation.pdf · The old system ext/mysql,ext/mysqli, ext/pgsql PHP 3 / MySQL 3 No prepared statements Procedural

   

db_query()

Page 15: Databases: The Next Generationszeged2008.drupalcon.org/files/DBTNG_Presentation.pdf · The old system ext/mysql,ext/mysqli, ext/pgsql PHP 3 / MySQL 3 No prepared statements Procedural

   

db_query*()

Page 16: Databases: The Next Generationszeged2008.drupalcon.org/files/DBTNG_Presentation.pdf · The old system ext/mysql,ext/mysqli, ext/pgsql PHP 3 / MySQL 3 No prepared statements Procedural

   

Fetching

Page 17: Databases: The Next Generationszeged2008.drupalcon.org/files/DBTNG_Presentation.pdf · The old system ext/mysql,ext/mysqli, ext/pgsql PHP 3 / MySQL 3 No prepared statements Procedural

   

Query options

Page 18: Databases: The Next Generationszeged2008.drupalcon.org/files/DBTNG_Presentation.pdf · The old system ext/mysql,ext/mysqli, ext/pgsql PHP 3 / MySQL 3 No prepared statements Procedural

   

Fetch options

Page 19: Databases: The Next Generationszeged2008.drupalcon.org/files/DBTNG_Presentation.pdf · The old system ext/mysql,ext/mysqli, ext/pgsql PHP 3 / MySQL 3 No prepared statements Procedural

   

Dynamic queries

Page 20: Databases: The Next Generationszeged2008.drupalcon.org/files/DBTNG_Presentation.pdf · The old system ext/mysql,ext/mysqli, ext/pgsql PHP 3 / MySQL 3 No prepared statements Procedural

   

Dynamic queries

Page 21: Databases: The Next Generationszeged2008.drupalcon.org/files/DBTNG_Presentation.pdf · The old system ext/mysql,ext/mysqli, ext/pgsql PHP 3 / MySQL 3 No prepared statements Procedural

   

Dynamic queries

Page 22: Databases: The Next Generationszeged2008.drupalcon.org/files/DBTNG_Presentation.pdf · The old system ext/mysql,ext/mysqli, ext/pgsql PHP 3 / MySQL 3 No prepared statements Procedural

   

hook_query_alter()

● All dynamic SELECT statements● Query tagging● Structured data is easy to alter● Access internal query structure● hook_db_rewrite_sql()

Page 23: Databases: The Next Generationszeged2008.drupalcon.org/files/DBTNG_Presentation.pdf · The old system ext/mysql,ext/mysqli, ext/pgsql PHP 3 / MySQL 3 No prepared statements Procedural

   

Insert, Update, Delete

● db_insert(), db_update(), db_delete()● Builder is required

– Cross­database BLOB and LOB

● Fully chainable● "Fluent API"

Page 24: Databases: The Next Generationszeged2008.drupalcon.org/files/DBTNG_Presentation.pdf · The old system ext/mysql,ext/mysqli, ext/pgsql PHP 3 / MySQL 3 No prepared statements Procedural

   

Insert

● Multi­insert● Delayed insert● Optimized per­driver

Page 25: Databases: The Next Generationszeged2008.drupalcon.org/files/DBTNG_Presentation.pdf · The old system ext/mysql,ext/mysqli, ext/pgsql PHP 3 / MySQL 3 No prepared statements Procedural

   

Merge

● db_merge()● "INSERT, UPDATE, whichever"● Update to same value or new● Database­specific implementation

– MySQL: INSERT ... ON DUPLICATE KEY UPDATE ...

– PostgreSQL: Needs function...

Page 26: Databases: The Next Generationszeged2008.drupalcon.org/files/DBTNG_Presentation.pdf · The old system ext/mysql,ext/mysqli, ext/pgsql PHP 3 / MySQL 3 No prepared statements Procedural

   

Next week on DB: TNG...

● Subselects in FROM● Union queries● Extenders (tablesort, pager, Views...)● Schema improvements● Exposed transactions (db_transaction())● http://groups.drupal.org/node/14222

Page 27: Databases: The Next Generationszeged2008.drupalcon.org/files/DBTNG_Presentation.pdf · The old system ext/mysql,ext/mysqli, ext/pgsql PHP 3 / MySQL 3 No prepared statements Procedural

   

Want to overhaul a core system?

Some advice...

Page 28: Databases: The Next Generationszeged2008.drupalcon.org/files/DBTNG_Presentation.pdf · The old system ext/mysql,ext/mysqli, ext/pgsql PHP 3 / MySQL 3 No prepared statements Procedural

   

Want to overhaul a core system?

Don't

Page 29: Databases: The Next Generationszeged2008.drupalcon.org/files/DBTNG_Presentation.pdf · The old system ext/mysql,ext/mysqli, ext/pgsql PHP 3 / MySQL 3 No prepared statements Procedural

   

Don't...

● Work alone● Expect others to do the work● Keep it a secret● Act big (but think big; have a roadmap)● Fear unit tests● Cut corners (Dries won't let you)● Believe that chx doesn't understand OOP

Page 30: Databases: The Next Generationszeged2008.drupalcon.org/files/DBTNG_Presentation.pdf · The old system ext/mysql,ext/mysqli, ext/pgsql PHP 3 / MySQL 3 No prepared statements Procedural

   

Don't...

Give up

Page 31: Databases: The Next Generationszeged2008.drupalcon.org/files/DBTNG_Presentation.pdf · The old system ext/mysql,ext/mysqli, ext/pgsql PHP 3 / MySQL 3 No prepared statements Procedural

   

For Eaton...