31
IBM DB2 Embedded SQL for PHP Александр Веремьев [email protected]

ZFConf 2012: Реализация доступа к СУБД IBM DB2 посредством встраиваемого SQL (Александр Веремьев)

Embed Size (px)

DESCRIPTION

http://www.zfconf.ru/2012/talks/ibm-db2-embedded-sql-for-php-implementation/

Citation preview

Page 1: ZFConf 2012: Реализация доступа к СУБД IBM DB2 посредством встраиваемого SQL (Александр Веремьев)

IBM DB2 Embedded SQL for PHP

Александр Веремьев

[email protected]

Page 2: ZFConf 2012: Реализация доступа к СУБД IBM DB2 посредством встраиваемого SQL (Александр Веремьев)

Что такое встраиваемый SQL

?????

Page 3: ZFConf 2012: Реализация доступа к СУБД IBM DB2 посредством встраиваемого SQL (Александр Веремьев)

$dbname = 'db_name';$dbuser = 'username';$dbpwd = 'password'; EXEC SQL connect to :$dbname user :$dbuser using :$dbpwd;

...

$userid = $_POST['userid'];$password = $_POST['password'];

EXEC SQL select firstname, lastname into :$firstname, :$lastname from users where login = :$userid AND password = :$password;

if ($SQLCODE == 100 /* NOT FOUND */) { echo "Wrong login/password combination.\n";} else { echo $firstname . "\n" . $lastname . "\n";}

Page 4: ZFConf 2012: Реализация доступа к СУБД IBM DB2 посредством встраиваемого SQL (Александр Веремьев)

EXEC SQL DECLARE c1 CURSOR for select id, time, subject, message from emails where userid = :$userid AND status = 'NEW';

EXEC SQL BEGIN DECLARE SECTION; INTEGER $email_id; TMESTAMP $time; VARCHAR(256) $subject; CLOB(16M) $message_body;EXEC SQL END DECLARE SECTION;

EXEC SQL OPEN c1; EXEC SQL FETCH c1 into :$email_id, :$time, :$subject, :$message_body;

while ($SQLCODE != 100 /* NOT FOUND */) { // process message ... EXEC SQL FETCH c1 into :$email_id, :$time, :$subject, :$message_body;}

Page 5: ZFConf 2012: Реализация доступа к СУБД IBM DB2 посредством встраиваемого SQL (Александр Веремьев)

Непосредственная поддержка со стороны СУБД в тех или иных языках:

- IBM DB2 (C/C++, Java, COBOL, FORTRAN, REXX)- Oracle (Ada, C/C++, COBOL, FORTRAN, Pascal, PL/I)- PostgreSQL (C/C++, COBOL)- Microsof SQL Server (COBOL)- MySQL (COBOL)- Sybase- …

Часть SQL-92 стандарта

Page 6: ZFConf 2012: Реализация доступа к СУБД IBM DB2 посредством встраиваемого SQL (Александр Веремьев)

IBM DB2 Embedded SQL for PHP support:

PHP extension db2_embsql

db2_embsql_precompile($input_file, $dbname, $options_string);

db2_embsql_precompile_string($php_code, $dbname, $options_string);

Page 7: ZFConf 2012: Реализация доступа к СУБД IBM DB2 посредством встраиваемого SQL (Александр Веремьев)

Особенности реализации встроенного SQL в IBM DB2.Static SQL.

<?php/** * Zend Framework * * LICENSE * * This source file is subject to the new BSD license that is bundled * with this package in the file LICENSE.txt. * It is also available through the world-wide-web at this URL: * http://framework.zend.com/license/new-bsd * If you did not receive a copy of the license and are unable to * obtain it through the world-wide-web, please send an email * to [email protected] so we can send you a copy immediately. * * @category Zend * @package Zend_Pdf * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License * @version $Id: Pdf.php 22908 2010-08-25 20:52:47Z alexander $ *//** User land classes and interfaces turned on by Zend/Pdf.php file inclusion. *//** @todo Section should be removed with ZF 2.0 release as obsolete *//** Zend_Pdf_Page */require_once 'Zend/Pdf/Page.php';/** Zend_Pdf_Canvas */require_once 'Zend/Pdf/Canvas.php';/** Internally used classes */require_once 'Zend/Pdf/Element/Dictionary.php';require_once 'Zend/Pdf/Element/Name.php';require_once 'Zend/Pdf/Element/Null.php';require_once 'Zend/Pdf/Element/Numeric.php';require_once 'Zend/Pdf/Element/String.php';/** * General entity which describes PDF document. * It implements document abstraction with a document level operations. * * Class is used to create new PDF document or load existing document. * See details in a class constructor description * * Class agregates document level properties and entities (pages, bookmarks, * document level actions, attachments, form object, etc) * * @category Zend * @package Zend_Pdf * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */class Zend_Pdf{ /**** Class Constants ****/ /** * Version number of generated PDF documents. */ const PDF_VERSION = '1.4'; /** * PDF file header. */ const PDF_HEADER = "%PDF-1.4\n%\xE2\xE3\xCF\xD3\n"; /** * Pages collection * * @todo implement it as a class, which supports ArrayAccess and Iterator interfaces, * to provide incremental parsing and pages tree updating. * That will give good performance and memory (PDF size) benefits. * * @var array - array of Zend_Pdf_Page object */ public $pages = array(); /** * List of inheritable attributesfor pages tree * * @var array */ protected static $_inheritableAttributes = array('Resources', 'MediaBox', 'CropBox', 'Rotate'); /** * Request used memory manager * * @return Zend_Memory_Manager */ static public function getMemoryManager() { if (self::$_memoryManager === null) { require_once 'Zend/Memory.php'; self::$_memoryManager = Zend_Memory::factory('none'); } return self::$_memoryManager; } /** * Set user defined memory manager * * @param Zend_Memory_Manager $memoryManager */ static public function setMemoryManager(Zend_Memory_Manager $memoryManager) { self::$_memoryManager = $memoryManager; } /** * Set the document-level JavaScript * * @param string $javascript */ public function setJavaScript($javascript) { $this->_javaScript = $javascript; } /** * Convert date to PDF format (it's close to ASN.1 (Abstract Syntax Notation * One) defined in ISO/IEC 8824). * * @todo This really isn't the best location for this method. It should * probably actually exist as Zend_Pdf_Element_Date or something like that. * * @todo Address the following E_STRICT issue: * PHP Strict Standards: date(): It is not safe to rely on the system's * timezone settings. Please use the date.timezone setting, the TZ * environment variable or the date_default_timezone_set() function. In * case you used any of those methods and you are still getting this * warning, you most likely misspelled the timezone identifier. * * @param integer $timestamp (optional) If omitted, uses the current time. * @return string */ public static function pdfDate($timestamp = null) { if ($timestamp === null) { $date = date('\D\:YmdHisO'); } else { $date = date('\D\:YmdHisO', $timestamp); } return substr_replace($date, '\'', -2, 0) . '\''; }}

<?php/** * Zend Framework * * LICENSE * * This source file is subject to the new BSD license that is bundled * with this package in the file LICENSE.txt. * It is also available through the world-wide-web at this URL: * http://framework.zend.com/license/new-bsd * If you did not receive a copy of the license and are unable to * obtain it through the world-wide-web, please send an email * to [email protected] so we can send you a copy immediately. * * @category Zend * @package Zend_Pdf * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License * @version $Id: Pdf.php 22908 2010-08-25 20:52:47Z alexander $ *//** User land classes and interfaces turned on by Zend/Pdf.php file inclusion. *//** @todo Section should be removed with ZF 2.0 release as obsolete *//** Zend_Pdf_Page */require_once 'Zend/Pdf/Page.php';/** Zend_Pdf_Canvas */require_once 'Zend/Pdf/Canvas.php';/** Internally used classes */require_once 'Zend/Pdf/Element/Dictionary.php';require_once 'Zend/Pdf/Element/Name.php';require_once 'Zend/Pdf/Element/Null.php';require_once 'Zend/Pdf/Element/Numeric.php';require_once 'Zend/Pdf/Element/String.php';/** * General entity which describes PDF document. * It implements document abstraction with a document level operations. * * Class is used to create new PDF document or load existing document. * See details in a class constructor description * * Class agregates document level properties and entities (pages, bookmarks, * document level actions, attachments, form object, etc) * * @category Zend * @package Zend_Pdf * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */class Zend_Pdf{ /**** Class Constants ****/ /** * Version number of generated PDF documents. */ const PDF_VERSION = '1.4'; /** * PDF file header. */

* * *

/** * Set user defined memory manager * * @param Zend_Memory_Manager $memoryManager */ static public function setMemoryManager(Zend_Memory_Manager $memoryManager) { self::$_memoryManager = $memoryManager; }}

.BND

DB2

Page 8: ZFConf 2012: Реализация доступа к СУБД IBM DB2 посредством встраиваемого SQL (Александр Веремьев)

<?php/** * Zend Framework * * LICENSE * * This source file is subject to the new BSD license that is bundled * with this package in the file LICENSE.txt. * It is also available through the world-wide-web at this URL: * http://framework.zend.com/license/new-bsd * If you did not receive a copy of the license and are unable to * obtain it through the world-wide-web, please send an email * to [email protected] so we can send you a copy immediately. * * @category Zend * @package Zend_Pdf * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License * @version $Id: Pdf.php 22908 2010-08-25 20:52:47Z alexander $ *//** User land classes and interfaces turned on by Zend/Pdf.php file inclusion. *//** @todo Section should be removed with ZF 2.0 release as obsolete *//** Zend_Pdf_Page */require_once 'Zend/Pdf/Page.php';/** Zend_Pdf_Canvas */require_once 'Zend/Pdf/Canvas.php';/** Internally used classes */require_once 'Zend/Pdf/Element/Dictionary.php';require_once 'Zend/Pdf/Element/Name.php';require_once 'Zend/Pdf/Element/Null.php';require_once 'Zend/Pdf/Element/Numeric.php';require_once 'Zend/Pdf/Element/String.php';/** * General entity which describes PDF document. * It implements document abstraction with a document level operations. * * Class is used to create new PDF document or load existing document. * See details in a class constructor description * * Class agregates document level properties and entities (pages, bookmarks, * document level actions, attachments, form object, etc) * * @category Zend * @package Zend_Pdf * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */class Zend_Pdf{ /**** Class Constants ****/ /** * Version number of generated PDF documents. */ const PDF_VERSION = '1.4'; /** * PDF file header. */ const PDF_HEADER = "%PDF-1.4\n%\xE2\xE3\xCF\xD3\n"; /** * Pages collection * * @todo implement it as a class, which supports ArrayAccess and Iterator interfaces, * to provide incremental parsing and pages tree updating. * That will give good performance and memory (PDF size) benefits. * * @var array - array of Zend_Pdf_Page object */ public $pages = array(); /** * List of inheritable attributesfor pages tree * * @var array */ protected static $_inheritableAttributes = array('Resources', 'MediaBox', 'CropBox', 'Rotate'); /** * Request used memory manager * * @return Zend_Memory_Manager */ static public function getMemoryManager() { if (self::$_memoryManager === null) { require_once 'Zend/Memory.php'; self::$_memoryManager = Zend_Memory::factory('none'); } return self::$_memoryManager; } /** * Set user defined memory manager * * @param Zend_Memory_Manager $memoryManager */ static public function setMemoryManager(Zend_Memory_Manager $memoryManager) { self::$_memoryManager = $memoryManager; } /** * Set the document-level JavaScript * * @param string $javascript */ public function setJavaScript($javascript) { $this->_javaScript = $javascript; } /** * Convert date to PDF format (it's close to ASN.1 (Abstract Syntax Notation * One) defined in ISO/IEC 8824). * * @todo This really isn't the best location for this method. It should * probably actually exist as Zend_Pdf_Element_Date or something like that. * * @todo Address the following E_STRICT issue: * PHP Strict Standards: date(): It is not safe to rely on the system's * timezone settings. Please use the date.timezone setting, the TZ * environment variable or the date_default_timezone_set() function. In * case you used any of those methods and you are still getting this * warning, you most likely misspelled the timezone identifier. * * @param integer $timestamp (optional) If omitted, uses the current time. * @return string */ public static function pdfDate($timestamp = null) { if ($timestamp === null) { $date = date('\D\:YmdHisO'); } else { $date = date('\D\:YmdHisO', $timestamp); } return substr_replace($date, '\'', -2, 0) . '\''; }}

PHP код

SQLКомпиляция

PHP код

Page 9: ZFConf 2012: Реализация доступа к СУБД IBM DB2 посредством встраиваемого SQL (Александр Веремьев)

Статический SQL.

- отсутствие повторной компиляции, использование готового плана выполнения (отличные результаты на OLTP нагрузке);

- защита от SQL injection;

- улучшенная модель security.

Page 10: ZFConf 2012: Реализация доступа к СУБД IBM DB2 посредством встраиваемого SQL (Александр Веремьев)

PHP

- динамическая типизация

- слабая типизация

SQL

- статическая типизация

- строгая типизация1

Связывание типов.

1 поддерживается неявный кастинг для некоторого подмножества типов.

Page 11: ZFConf 2012: Реализация доступа к СУБД IBM DB2 посредством встраиваемого SQL (Александр Веремьев)

Связывание типов.

PHP

$var1 (integer)

$var1 (string)

$var2 (float)

$var1 (string)

SELECT … INTO :$var1 …

INSERT INTO … VALUES(:$var1, …)

SELECT … WHERE COL1 > :$var2

SQL

Page 12: ZFConf 2012: Реализация доступа к СУБД IBM DB2 посредством встраиваемого SQL (Александр Веремьев)

Связывание типов, C/C++:

EXEC SQL BEGIN DECLARE SECTION; char dbName[16]; char userid[30]; char passwd[30];

SQL TYPE IS CLOB(2M) img_data;EXEC SQL END DECLARE SECTION;

...

EXEC SQL CONNECT TO :dbName USER :userid USING :passwd;

Page 13: ZFConf 2012: Реализация доступа к СУБД IBM DB2 посредством встраиваемого SQL (Александр Веремьев)

db2_embsql extension:

1. Декларативность секцииEXEC SQL BEGIN DECLARE SECTION;…EXEC SQL END DECLARE SECTION;

2. Физическое размещение HV в модуле extension’а.

Page 14: ZFConf 2012: Реализация доступа к СУБД IBM DB2 посредством встраиваемого SQL (Александр Веремьев)

3. Маппинг имён host variables (и производных lvalue конструкций) на внутренние имена HV в SQL выражениях, отправляемых на прекомпиляцию.

$this->myCoolVariable$values[$index]->val$options['my_cool_option']…

Page 15: ZFConf 2012: Реализация доступа к СУБД IBM DB2 посредством встраиваемого SQL (Александр Веремьев)

Маппинг типов:

SMALLINT (16-bit integer) - integrerINTEGER (32-bit integer) - integrerBIGINT (64-bit integer) - integrer

REAL - floatDOUBLE - float

DECIMAL - STRING

CHAR(n) - STRINGCHAR(n) FOR BIT DATA - STRINGVARCHAR(n) - STRINGCLOB(n) - STRINGBLOB(n) - STRING

DATE - STRINGTIME - STRINGTIMESTAMP - STRING

...

Page 16: ZFConf 2012: Реализация доступа к СУБД IBM DB2 посредством встраиваемого SQL (Александр Веремьев)

Решение проблемы предугадывания типов, input/output variables:

EXEC SQL SELECT LOGIN, FIRSTNAME, LASTNAME INTO :$login, :$firstname, :$lastname FROM USERS WHERE ID = :$user_id;

vs

EXEC SQL INSERT INTO USERSVALUES( :$login, :$firstname, :$lastname);

Page 17: ZFConf 2012: Реализация доступа к СУБД IBM DB2 посредством встраиваемого SQL (Александр Веремьев)

Декларация “базовых” переменных:

EXEC SQL BEGIN DECLARE SECTION;VARCHAR(32) $login;VARCHAR(64) $firstname,VARCHAR(64) $lastname;

EXEC SQL BEGIN DECLARE SECTION;

...

$login = $this->login;$firstname = $this->firstname;$lastname = $this->lastname;

EXEC SQL INSERT INTO USERSVALUES(:$login, :$firstname, :$lastname);

Page 18: ZFConf 2012: Реализация доступа к СУБД IBM DB2 посредством встраиваемого SQL (Александр Веремьев)

Декларация “производных” переменных:

EXEC SQL BEGIN DECLARE SECTION;VARCHAR(32) $this->login;VARCHAR(64) $this->firstname,VARCHAR(64) $this->lastname;

EXEC SQL BEGIN DECLARE SECTION;

...

EXEC SQL INSERT INTO USERSVALUES( :$this->login,

:$this->firstname,:$this->lastname);

Page 19: ZFConf 2012: Реализация доступа к СУБД IBM DB2 посредством встраиваемого SQL (Александр Веремьев)

PackagesPackagesPackages

Как работают вызовы SQL statements:

<?php/** * Zend Framework * * LICENSE * * This source file is subject to the new BSD license that is bundled * with this package in the file LICENSE.txt. * It is also available through the world-wide-web at this URL: * http://framework.zend.com/license/new-bsd * If you did not receive a copy of the license and are unable to * obtain it through the world-wide-web, please send an email * to [email protected] so we can send you a copy immediately. * * @category Zend * @package Zend_Pdf * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License * @version $Id: Pdf.php 22908 2010-08-25 20:52:47Z alexander $ *//** User land classes and interfaces turned on by Zend/Pdf.php file inclusion. *//** @todo Section should be removed with ZF 2.0 release as obsolete *//** Zend_Pdf_Page */require_once 'Zend/Pdf/Page.php';/** Zend_Pdf_Canvas */require_once 'Zend/Pdf/Canvas.php';/** Internally used classes */require_once 'Zend/Pdf/Element/Dictionary.php';require_once 'Zend/Pdf/Element/Name.php';require_once 'Zend/Pdf/Element/Null.php';require_once 'Zend/Pdf/Element/Numeric.php';require_once 'Zend/Pdf/Element/String.php';/** * General entity which describes PDF document. * It implements document abstraction with a document level operations. * * Class is used to create new PDF document or load existing document. * See details in a class constructor description * * Class agregates document level properties and entities (pages, bookmarks, * document level actions, attachments, form object, etc) * * @category Zend * @package Zend_Pdf * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */class Zend_Pdf{ /**** Class Constants ****/ /** * Version number of generated PDF documents. */ const PDF_VERSION = '1.4'; /** * PDF file header. */ const PDF_HEADER = "%PDF-1.4\n%\xE2\xE3\xCF\xD3\n"; /** * Pages collection * * @todo implement it as a class, which supports ArrayAccess and Iterator interfaces, * to provide incremental parsing and pages tree updating. * That will give good performance and memory (PDF size) benefits. * * @var array - array of Zend_Pdf_Page object */ public $pages = array(); /** * List of inheritable attributesfor pages tree * * @var array */ protected static $_inheritableAttributes = array('Resources', 'MediaBox', 'CropBox', 'Rotate'); /** * Request used memory manager * * @return Zend_Memory_Manager */ static public function getMemoryManager() { if (self::$_memoryManager === null) { require_once 'Zend/Memory.php'; self::$_memoryManager = Zend_Memory::factory('none'); } return self::$_memoryManager; } /** * Set user defined memory manager * * @param Zend_Memory_Manager $memoryManager */ static public function setMemoryManager(Zend_Memory_Manager $memoryManager) { self::$_memoryManager = $memoryManager; } /** * Set the document-level JavaScript * * @param string $javascript */ public function setJavaScript($javascript) { $this->_javaScript = $javascript; } /** * Convert date to PDF format (it's close to ASN.1 (Abstract Syntax Notation * One) defined in ISO/IEC 8824). * * @todo This really isn't the best location for this method. It should * probably actually exist as Zend_Pdf_Element_Date or something like that. * * @todo Address the following E_STRICT issue: * PHP Strict Standards: date(): It is not safe to rely on the system's * timezone settings. Please use the date.timezone setting, the TZ * environment variable or the date_default_timezone_set() function. In * case you used any of those methods and you are still getting this * warning, you most likely misspelled the timezone identifier. * * @param integer $timestamp (optional) If omitted, uses the current time. * @return string */ public static function pdfDate($timestamp = null) { if ($timestamp === null) { $date = date('\D\:YmdHisO'); } else { $date = date('\D\:YmdHisO', $timestamp); } return substr_replace($date, '\'', -2, 0) . '\''; }}

SELECT …

DB2 Run Time Services

:HV1

:HV2

DatabaseManager

CALL

Result:HV3

PackagesPackagesData

Page 20: ZFConf 2012: Реализация доступа к СУБД IBM DB2 посредством встраиваемого SQL (Александр Веремьев)

пример прекомпилированного С кода:

/*EXEC SQL select count(*) into :tbl_cnt from syscat.tables;*/

{#line 39 "dbconn.sqc" sqlastrt(sqla_program_id, &sqla_rtinfo, &sqlca);#line 39 "dbconn.sqc" sqlaaloc(3,1,2,0L); { struct sqla_setdata_list sql_setdlist[1];#line 39 "dbconn.sqc" sql_setdlist[0].sqltype = 496; sql_setdlist[0].sqllen = 4;#line 39 "dbconn.sqc" sql_setdlist[0].sqldata = (void*)&tbl_cnt;#line 39 "dbconn.sqc" sql_setdlist[0].sqlind = 0L;#line 39 "dbconn.sqc" sqlasetdata(3,0,1,sql_setdlist,0L,0L); }

Page 21: ZFConf 2012: Реализация доступа к СУБД IBM DB2 посредством встраиваемого SQL (Александр Веремьев)

#line 39 "dbconn.sqc" sqlacall((unsigned short)24,1,0,3,0L);#line 39 "dbconn.sqc" sqlastop(0L);}

#line 39 "dbconn.sqc"

EMB_SQL_CHECK("Count tables");

printf("select count(*) from syscat.tables;\n--------\n %d\n", tbl_cnt);

Page 22: ZFConf 2012: Реализация доступа к СУБД IBM DB2 посредством встраиваемого SQL (Александр Веремьев)

PHP engine

Архитектура extension’а как магазинного автомата:

<?php/** * Zend Framework * * LICENSE * * This source file is subject to the new BSD license that is bundled * with this package in the file LICENSE.txt. * It is also available through the world-wide-web at this URL: * http://framework.zend.com/license/new-bsd * If you did not receive a copy of the license and are unable to * obtain it through the world-wide-web, please send an email * to [email protected] so we can send you a copy immediately. * * @category Zend * @package Zend_Pdf * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License * @version $Id: Pdf.php 22908 2010-08-25 20:52:47Z alexander $ *//** User land classes and interfaces turned on by Zend/Pdf.php file inclusion. *//** @todo Section should be removed with ZF 2.0 release as obsolete *//** Zend_Pdf_Page */require_once 'Zend/Pdf/Page.php';/** Zend_Pdf_Canvas */require_once 'Zend/Pdf/Canvas.php';/** Internally used classes */require_once 'Zend/Pdf/Element/Dictionary.php';/** * General entity which describes PDF document. * It implements document abstraction with a document level operations. * * Class is used to create new PDF document or load existing document. * See details in a class constructor description * * * @category Zend * @package Zend_Pdf * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */class Zend_Pdf{ /**** Class Constants ****/ /** * Version number of generated PDF documents. */ const PDF_VERSION = '1.4'; /** * PDF file header. */ const PDF_HEADER = "%PDF-1.4\n%\xE2\xE3\xCF\xD3\n"; /**/** * List of inheritable attributesfor pages tree * * @var array */ protected static $_inheritableAttributes = array('Resources', 'MediaBox', 'CropBox', 'Rotate'); /** * Request used memory manager * * @return Zend_Memory_Manager */ static public function getMemoryManager() { if (self::$_memoryManager === null) { require_once 'Zend/Memory.php'; self::$_memoryManager = Zend_Memory::factory('none'); } return self::$_memoryManager; } /** * Set user defined memory manager * * @param Zend_Memory_Manager $memoryManager */ static public function setMemoryManager(Zend_Memory_Manager $memoryManager) { self::$_memoryManager = $memoryManager; } /** * Set the document-level JavaScript * * @param string $javascript */ public function setJavaScript($javascript) { $this->_javaScript = $javascript; } /** * Convert date to PDF format (it's close to ASN.1 (Abstract Syntax Notation * One) defined in ISO/IEC 8824). * * @todo This really isn't the best location for this method. It should * probably actually exist as Zend_Pdf_Element_Date or something like that. * * @todo Address the following E_STRICT issue: * PHP Strict Standards: date(): It is not safe to rely on the system's * timezone settings. Please use the date.timezone setting, the TZ * environment variable or the date_default_timezone_set() function. In * case you used any of those methods and you are still getting this * warning, you most likely misspelled the timezone identifier. * * @param integer $timestamp (optional) If omitted, uses the current time. * @return string */ public static function pdfDate($timestamp = null) { if ($timestamp === null) { $date = date('\D\:YmdHisO'); } else { $date = date('\D\:YmdHisO', $timestamp); } return substr_replace($date, '\'', -2, 0) . '\''; }}

DB2_ESQL extension

sqlaaloc(...) sqlacall(...) sqlacmpd(...) sqladloc(...) sqlastls(...) sqlastlv(...) sqlastlva(...) sqlasetdata(...) sqlastop(...) sqlastrt(...) sqlausda(...)

НV area

Очередь вызовов

Page 23: ZFConf 2012: Реализация доступа к СУБД IBM DB2 посредством встраиваемого SQL (Александр Веремьев)

Плюсы:

- cтатичность SQL;

- отсутствие фазы компиляции запроса на этапе выполнения;

- отсутствие фазы построения плана запроса на этапе выполнения;

- проверка SQL на этапе прекомпиляции;

- защищённость от вымывания кэшей SQL запросов;

- управляемость планов выполнения, возможность вести сравнительную статистику по изменяемости планов по модулям приложений, пакет как measurement объёмов и характера данных в проекции приложения;

- модель security – права на выполнение на уровне пакета.

Page 24: ZFConf 2012: Реализация доступа к СУБД IBM DB2 посредством встраиваемого SQL (Александр Веремьев)

Минусы:

- cтатичность SQL;

- дополнительная фаза - прекомпиляция;

- необходимость существования соответствующих объектов базы на момент компиляции;

- прирост производительности незаметен на длительных запросах (10 и более секунд);

- слабая ориентированность на “ad hoc” запросы:имеется поддержка динамического SQL, но она ничемне лучше native подходов с помощью odbc/cli;

- отсутствие выигрыша для редко выполняемых запросов.

Page 25: ZFConf 2012: Реализация доступа к СУБД IBM DB2 посредством встраиваемого SQL (Александр Веремьев)

Ограничения embedded SQL:

- невозможность использования Host variables в именах объектов имён таблиц, функций, процедур, …, кроме как при использовании динамического SQL (PREPAPRE, EXECUTE);

- невозможность использования scrollable курсоров;

Page 26: ZFConf 2012: Реализация доступа к СУБД IBM DB2 посредством встраиваемого SQL (Александр Веремьев)

IBM DB2 и динамический SQL для PHP:http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.swg.im.dbclient.php.doc/doc/c0021523.html

1. ibm_db2 extensionhttp://www.php.net/manual/en/book.ibm-db2.php

2. pdo_ibm driverhttp://www.php.net/manual/en/book.pdo.php

3. Unified odbc extensionhttp://www.php.net/manual/en/book.uodbc.php

Page 27: ZFConf 2012: Реализация доступа к СУБД IBM DB2 посредством встраиваемого SQL (Александр Веремьев)

Способы увеличения производительности динамического SQL:

- увеличение размера STATEMENT CACHE – pckacheszимеется некоторая точка насыщения, предмет для мониторинга:* pkg_cache_lookups (package cache lookups)* pkg_cache_inserts (package cache inserts)* pkg_cache_size_top (package cache high water mark)* pkg_cache_num_overflows (package cache overflows)

- использование параметризованных запросовесть “Но” – план выполнения будет строиться без учёта реального значения параметра

- StmtConcentrator CLI/ODBC параметр

Page 28: ZFConf 2012: Реализация доступа к СУБД IBM DB2 посредством встраиваемого SQL (Александр Веремьев)

- конвертирование динамического SQL в статический:

1. Коллектор запросов (CLI/ODBC параметры): STATICMODE=CAPTURE STATICCAPFILE=<path> STATICPACKAGE=<package_schema>.<package_name>

2. Работа приложения

3. db2cap bind …

4. STATICMODE=MATCH

Page 29: ZFConf 2012: Реализация доступа к СУБД IBM DB2 посредством встраиваемого SQL (Александр Веремьев)

Планы

- Пре-релиз конец мая – начало июня.

- Релиз, передача в Open Source – конец июня.

Page 30: ZFConf 2012: Реализация доступа к СУБД IBM DB2 посредством встраиваемого SQL (Александр Веремьев)

Embedded SQL

vs

ORM frameworks/ActiveRecord

???

Page 31: ZFConf 2012: Реализация доступа к СУБД IBM DB2 посредством встраиваемого SQL (Александр Веремьев)

Вопросы

Александр Веремьев

[email protected]