Databases: The Next Generationszeged2008.drupalcon.org/files/DBTNG_Presentation.pdf · The old...
Transcript of Databases: The Next Generationszeged2008.drupalcon.org/files/DBTNG_Presentation.pdf · The old...
![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](https://reader033.fdocuments.net/reader033/viewer/2022051811/601df13ea7b2ff70f4589509/html5/thumbnails/1.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022051811/601df13ea7b2ff70f4589509/html5/thumbnails/2.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022051811/601df13ea7b2ff70f4589509/html5/thumbnails/3.jpg)
PDO
● Formal prepared statements– Typeagnostic queries
● Objectoriented 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](https://reader033.fdocuments.net/reader033/viewer/2022051811/601df13ea7b2ff70f4589509/html5/thumbnails/4.jpg)
What does that give us?
● Multiple database types at once● Improved database portability● Easier driver implementation● Databasespecific 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](https://reader033.fdocuments.net/reader033/viewer/2022051811/601df13ea7b2ff70f4589509/html5/thumbnails/5.jpg)
What can we do with it?
● Arraybased database configuration● Master/save replication● Portable typesafe 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](https://reader033.fdocuments.net/reader033/viewer/2022051811/601df13ea7b2ff70f4589509/html5/thumbnails/6.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022051811/601df13ea7b2ff70f4589509/html5/thumbnails/7.jpg)
Targets
● Alternate database connection● May be used or not● Fallback to default● Master/slave replication● Multislave 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](https://reader033.fdocuments.net/reader033/viewer/2022051811/601df13ea7b2ff70f4589509/html5/thumbnails/8.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022051811/601df13ea7b2ff70f4589509/html5/thumbnails/9.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022051811/601df13ea7b2ff70f4589509/html5/thumbnails/10.jpg)
New drivers are easy
● Subclass and god● Magic directory location● No hardcoded 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](https://reader033.fdocuments.net/reader033/viewer/2022051811/601df13ea7b2ff70f4589509/html5/thumbnails/11.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022051811/601df13ea7b2ff70f4589509/html5/thumbnails/12.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022051811/601df13ea7b2ff70f4589509/html5/thumbnails/13.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022051811/601df13ea7b2ff70f4589509/html5/thumbnails/14.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022051811/601df13ea7b2ff70f4589509/html5/thumbnails/15.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022051811/601df13ea7b2ff70f4589509/html5/thumbnails/16.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022051811/601df13ea7b2ff70f4589509/html5/thumbnails/17.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022051811/601df13ea7b2ff70f4589509/html5/thumbnails/18.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022051811/601df13ea7b2ff70f4589509/html5/thumbnails/19.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022051811/601df13ea7b2ff70f4589509/html5/thumbnails/20.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022051811/601df13ea7b2ff70f4589509/html5/thumbnails/21.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022051811/601df13ea7b2ff70f4589509/html5/thumbnails/22.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022051811/601df13ea7b2ff70f4589509/html5/thumbnails/23.jpg)
Insert, Update, Delete
● db_insert(), db_update(), db_delete()● Builder is required
– Crossdatabase 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](https://reader033.fdocuments.net/reader033/viewer/2022051811/601df13ea7b2ff70f4589509/html5/thumbnails/24.jpg)
Insert
● Multiinsert● Delayed insert● Optimized perdriver
![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](https://reader033.fdocuments.net/reader033/viewer/2022051811/601df13ea7b2ff70f4589509/html5/thumbnails/25.jpg)
Merge
● db_merge()● "INSERT, UPDATE, whichever"● Update to same value or new● Databasespecific 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](https://reader033.fdocuments.net/reader033/viewer/2022051811/601df13ea7b2ff70f4589509/html5/thumbnails/26.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022051811/601df13ea7b2ff70f4589509/html5/thumbnails/27.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022051811/601df13ea7b2ff70f4589509/html5/thumbnails/28.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022051811/601df13ea7b2ff70f4589509/html5/thumbnails/29.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022051811/601df13ea7b2ff70f4589509/html5/thumbnails/30.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022051811/601df13ea7b2ff70f4589509/html5/thumbnails/31.jpg)
For Eaton...