Building a database driver with the Zend Framework

fishhookFladgeInternet και Εφαρμογές Web

13 Δεκ 2013 (πριν από 3 χρόνια και 8 μήνες)

120 εμφανίσεις

Building a database driver with
the Zend FrameworkIntroduction?
• I’m Michel Sijmons
• Founder of the company Nibble
• Professional PHP programmer for 7 years
• Writer of the product PHPGenerator
• PHP enthusiastAgenda
• Why build your own driver
• What should you think of
• Implementing the driver
• Testing the driver
• QuestionsWhy build your own driver
• New DBMS
• Missing functionality in an existing driver
• New features
• No more support for an existing driver
• Take full advantage of a DBMS
• Sync behavior of different driversCurrent drivers
• IBM DB2 pdo_ibm, ibm_db2 extension
• MySQL, pdo_mysql, mysqli extension
• Microsoft SQL Server, pdo_mssql extension
• Oracle, pdo_oci, oci8 extension
• PostgreSQL, pdo_pgsql extension
• SQLite, pdo_sqlite extension
• Firebird/Interbase, php_interbase extensionHow standard is standard
• Zend Framework does not make use of a database
transparent
• You still need to know which database is
underneathHow standard is standard
• Use of sequences (lastInsertId, lastSequenceId)
• Case folding
• Delimited identifiers
• Autocommit status
• Isolation levels
• Meta schema information
• Error Information
• Date & Time handlingImplementing your own driver
What should you think of?What you should think of
• Meta Schema Information
• Incrementing values
• Isolation levels
• Delimited identifiers
• Error Handling
• Transactions
• Case Folding
• Limiting datasets
• Prepare statement
• DBMS Specific informationMeta Schema Information
• INFORMATION_SCHEMA
First try to make system tables standard
• System tables
Each DBMS has his own set of System tablesIncrementing values
• Use of sequences
• Use of an incrementing data type
• Custom implementationIsolation levels
• Default isolation level
• READ UNCOMMITTED
• READ COMMITTED
• REPEATABLE READ
• SERIALIZABLE
• Concurrency
• Lock types
• Lock conversion and deadlocksDelimited identifiers
• SQL Standard
• Special characters
• Reserved words
Select * from “my table”
Select * from “table”
Select “data type” from “column”Error Handling
• DBMS Specific
• SQLCode and SQLState (ANSI SQL)Transactions
• Autocommit On or Off
• Starting a transaction
set autocommit off
• Ending a transaction
commit
rollback
close connection
end script
• Error Handling on commit or rollbackCase Folding
• Objects names in lower, upper or mixed case
• DBMS with mixed case is a challengeLimiting datasets
• Only selecting the first (n) number of rows
• Paging in application
• Be careful with “order by”, “distinct”, “group by”
• DBMS specific syntax
Select top (n) …
Select first (n) …
Select * from tablename limit (n)…..Prepare statement
• Parse and Optimize once, Execute many
• Thing about Performance
• Only consider if more than 5 times executedDBMS Specific information
• System functions
• Examples
@@
dbmsinfo
sp_autostats
dualImplementing the driver
• Keep your code separate from Zend code
• extend Zend_Db_Adapter_Abstract
• Zend_Db_Adapter_ExceptionNecessary methods
• Connection
object|resource|null getConnection ()
void closeConnection ()
• Transaction
bool beginTransaction ()
bool commit ()
bool rollBack () Necessary methods
• Select
array fetchAll (..)
string fetchAssoc (..)
array fetchCol (..)
string fetchOne (..)
string fetchPairs (..)
array fetchRow (..)
string limit (..) Necessary methods
• Mutation
int delete (..)
int insert (..)
int update (..)
• Incremental keys
string lastInsertId (..)
string lastSequenceId (..)
string nextSequenceId (..) Necessary methods
• Meta Information
array describeTable (..)
array listTables () Testing the driver
• Automate your testing
• Use logging information
• Test also the exceptions
• Test transaction boundaries
• Use PHPUnitFor more information
• PHP http://www.php.net
• Zend Framework: http://framework.zend.com/
• Zend Developer Zone: http://devzone.zend.com/Questions ?