Breaking news: Magento starts running on multiple RDBMS

advertisement
Ibiza, June 4th – 7th 2011
Magento & Multiple
RDBMS
by Andrey Tserkus,
Magento Core Team
Do We Want Multiple RDBMS
Support?
Do We Want Multiple RDBMS Support?
Store owners: reduced cost, increased
store launch speed
• Common infrastructure
• No additional licensing
• Trained staff
Do We Want Multiple RDBMS Support?
Developers: more money and clients,
more fun
•
•
•
•
Wider area of customization clients
Bigger market to sell extensions
Automatic cross-DB support
Working with new technologies
We Are Not Alone
Percentage of Companies, Owning Concrete RDBMS
Microsoft SQL Server
83%
Oracle
77%
Sybase
43%
MySQL
PostgreSQL
Embarcadero Technologies survey, Dec 2010
38%
9%
D&D: Databases Differences
Each Vendor Has Its Own Vision
• All RDBMS use common language –
SQL. But each of them has its own
dialect.
• Different function names.
• Internal architecture differs
substantially.
Identifiers Length
Identifier
MySQL
Database name
Table name
Column name
Index name
64
64
64
64
SQL
Server
128
128
128
128
Oracle
30
30
30
30
Identifiers Length
Magento Core uses identifiers up to 30
symbols
• Follow this practice to make your
module cross-DB compatible
• Do not follow this practice to feel free
and target only specific RDBMS
Limits
There are no “LIMIT” constructions
outside MySQL.
At all.
Limits in Magento
column1
column2
n
…
…
1
…
…
…
…
2
3
…
…
…
…
4
5
…
…
…
…
6
…
Main
SELECT
Limiting
SELECT
Limits in Magento
Magento automatically wraps SELECT and
fetches records by number
SELECT * FROM (
, ROW_NUMBER() AS n
SELECT *
*,
FROM table
)
WHERE n >= 5 AND n <= 6
Identifiers Quoting
Quoting
MySQL
SQL
Server
Oracle
`table`
[table]
"table"
Identifiers Quoting
Magento has abstract adapter method to
quote identifiers
$query = 'UPDATE ' .
$adapter->quoteIdentifier('table') .
' SET ' .
$adapter->quoteIdentifier('col') .
' = 1';
Identifiers Quoting
Reserved
Words
MySQL
SQL
Server
Oracle
233
180
481
Identifiers Quoting
Magento automatically quotes data in
DDL/DML routines
$columns = array('col' => 1);
$adapter->update('table', $columns);
Empty Strings
Empty string in Oracle is NULL
Query
Result
SELECT '' FROM dual
NULL
UPDATE … WHERE a = ''NULL
0 rows
UPDATE … WHERE a is NULL
Identifiers Length
Magento uses NULLs instead of empty
strings
• Change logic to use NULLs
• Form conditions, using adapter
Other MySQL Specific Things
Adapter and resource helpers emulate specifics
• INSERT … ON DUPLICATE
$adapter->insertOnDuplicate();
• SELECT a, b … GROUP BY a
$helper->getQueryUsingAnalyticFunction();
• and many more...
DB Abstraction in Magento
DB Abstraction in Magento CE 1.5 / EE 1.10
Model
Mysql4 (resource)
model
MySQL adapter
MySQL
Support for other RDBMS (predicted)
Model
Mysql4 (resource)
model
Oracle (resource)
model
MySQL adapter
Oracle adapter
MySQL
Oracle
Support for Other RDBMS (Predicted)
• Worked out with Magento 1.0 in 2008
• Requires implementation of resource
models for 70 CE + 30 EE modules
(total 500 resource models)
• Lot of code/logic duplication
DB Abstraction in Magento CE 1.6+ / EE 1.11+
Model
Mysql4
resource helper
Resource
model
MySQL adapter
MySQL
Resource Helper
• Holds module’s specifics for concrete DB,
too rare to be covered by adapter
• Magento automatically chooses resource
helper for current DB:
$where = Mage::getResourceHelper('catalogsearch')
->chooseFulltext(...);
Support for Other RDBMS
Model
Mysql4
resource helper
Resource
model
Oracle
resource helper
MySQL adapter
Oracle adapter
MySQL
Oracle
Support for Other RDBMS
• Requires moving resource models to
MMDB-rails only once
• No code duplication. Just specific
queries syntax is placed in resource
helpers (actually, 13 were created)
Magento Adapters
Concrete
Zend_Db_Adapter_*
Concrete
Varien_Db_Adapter_*
Varien_Db_Adapter
Varien_Db_Adapter_*:
• Abstracts DDL & DML for Magento
• Follows PDO style
• Works with query placeholders
('WHERE a = ? AND b = :val') and
BLOBs
Creating Tables
Varien_Db_Ddl_Table
Varien_Db_Adapter_*
+addColumn()
+addIndex()
+addForeignKey()
CREATE TABLE …
RDBMS
Module File Structure
Module Files Structure
/app/code/core/Mage/Module/
… (Block, Helper, controllers etc.)
Model
Mysql4
Resource
Helper
sql
deprecated
Resource Models
/app/code/core/Mage/Cms/Model/
Mysql4
Page
Service.php
Block.php
Resource
Page
Service.php
Block.php
Resource Models – Backwards Compatibility
class Mage_Cms_Model_Mysql4_Page_Service extends
Mage_Cms_Model_Resource_Page_Service
{
// Empty class
}
Resource Helpers
/app/code/core/Mage/Module/Model/
…
Resource
Helper
Mysql4.php
Oracle.php
Mssql.php
…
SQL Installs & Upgrades
/app/code/core/Mage/Module/
…
sql
install-1.0.php
upgrade-1.0-1.1.php
… oracle-upgrade-1.0-1.1.php
…
Setting Up
MySQL Preparations
• MySQL 4.1+
• PHP_PDO library
• PHP_PDO_MYSQL library
Microsoft SQL Server Preparations
• SQL Server 2008+
• PHP_PDO library
• Windows:
• Microsoft SQL Server Native Client
• Microsoft PHP PDO Driver for SQL Sever
• Linux:
• FreeTDS library
• PHP_PDO_DBLIB
Oracle Preparations
• Oracle 10g r2+ (Standard+)
• Oracle Instant Client
• PHP_OCI8 library
Frequently Asked Questions
FAQ
Does it work?
Yes!
• QA tests
• Tests of public extensions
• Real big merchant’s store is coming
FAQ
Will it be backwards compatible?
Yes!
• Major work: models and methods are the same
• The rest:
• Compatibility mode for MySQL
• Instant query modifications
• Manual overwalks
FAQ
Can I work as before?
Yes!
• Direct queries can be used, when cross-DB
support is not required
• Same developers functionality is left
FAQ
Will I get cross-DB support by following
new development style?
Yes!
• Set of routines to access abstract RDBMS
• Adapters and helpers do all low-level work
FAQ
Does it have future?
Definitely!
• All new Magento releases, starting with CE
1.6 / EE 1.11
• Magento 2
Questions
&
Answers
Thank You!
Andrey Tserkus
Email: andrey.tserkus@magento.com
Twitter: Zerkella
Magento Inc.
Download