Post on 17-Dec-2015
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
Percentage of Companies, Owning Concrete RDBMS
PostgreSQL
MySQL
Sybase
Oracle
Microsoft SQL Server
9%
38%
43%
77%
83%
Embarcadero Technologies survey, Dec 2010
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 SQL Server
Oracle
Database name 64 128 30Table name 64 128 30Column name 64 128 30Index name 64 128 30
Identifiers Length
• Follow this practice to make your module cross-DB compatible
• Do not follow this practice to feel free and target only specific RDBMS
Magento Core uses identifiers up to 30 symbols
column1 column2 n… … 1… … 2… … 3… … 4… … 5… … 6… … …
column1 column2… …… …… …… …… …… …… …
Limits in Magento
Main SELECT
Limiting SELECT
SELECT * FROM ( SELECT *, ROW_NUMBER() AS n FROM table) WHERE n >= 5 AND n <= 6
SELECT * FROM table
Limits in Magento
Magento automatically wraps SELECT and fetches records by number
, ROW_NUMBER() AS n
Identifiers Quoting
Magento has abstract adapter method to quote identifiers
$query = 'UPDATE ' . $adapter-
>quoteIdentifier('table') . ' SET ' . $adapter->quoteIdentifier('col') . ' = 1';
Identifiers Quoting
Magento automatically quotes data in DDL/DML routines
$columns = array('col' => 1);$adapter->update('table', $columns);
Query Result
SELECT '' FROM dual NULL
UPDATE … WHERE a = NULL 0 rows
Query Result
SELECT '' FROM dual NULL
UPDATE … WHERE a = '' 0 rows
Empty Strings
Empty string in Oracle is NULL
Query Result
SELECT '' FROM dual NULL
UPDATE … WHERE a is NULL
Identifiers Length
•Change logic to use NULLs• Form conditions, using adapter
Magento uses NULLs instead of empty strings
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...
Support for other RDBMS (predicted)
MySQL
Model
Mysql4 (resource)model
MySQL adapter
Oracle
Oracle (resource)model
Oracle adapter
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+
MySQL
Model
Resourcemodel
MySQL adapter
Mysql4 resource helper
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
MySQL
Model
Resourcemodel
MySQL adapter
Mysql4 resource
helper
Oracle resource
helper
Oracle adapter
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)
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+addColumn()+addIndex()+addForeignKey()
Varien_Db_Adapter_*
CREATE TABLE …
RDBMS
Mysql4Mysql4
Module Files Structure
/app/code/core/Mage/Module/
Model
ResourceHelper
sql
… (Block, Helper, controllers etc.)
deprecated
Resource Models
Mysql4
ResourcePage
Service.phpBlock.php
PageService.php
Block.php
/app/code/core/Mage/Cms/Model/
Resource Models – Backwards Compatibility
class Mage_Cms_Model_Mysql4_Page_Service extends Mage_Cms_Model_Resource_Page_Service { // Empty class}
…
SQL Installs & Upgrades
sql
install-1.0.php
upgrade-1.0-1.1.php
oracle-upgrade-1.0-1.1.php…
/app/code/core/Mage/Module/…
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
Yes!• Major work: models and methods are the same• The rest: • Compatibility mode for MySQL• Instant query modifications• Manual overwalks
FAQ
Will it be backwards compatible?
Yes!•Direct queries can be used, when cross-DB
support is not required• Same developers functionality is left
FAQ
Can I work as before?
Yes!
• Set of routines to access abstract RDBMS•Adapters and helpers do all low-level work
FAQ
Will I get cross-DB support by following new development style?
Definitely!•All new Magento releases, starting with CE
1.6 / EE 1.11•Magento 2
FAQ
Does it have future?