├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── composer.json └── src ├── Adapter ├── Adapter.php ├── AdapterAbstractServiceFactory.php ├── AdapterAwareInterface.php ├── AdapterAwareTrait.php ├── AdapterInterface.php ├── AdapterServiceFactory.php ├── Driver │ ├── AbstractConnection.php │ ├── ConnectionInterface.php │ ├── DriverInterface.php │ ├── Feature │ │ ├── AbstractFeature.php │ │ └── DriverFeatureInterface.php │ ├── IbmDb2 │ │ ├── Connection.php │ │ ├── IbmDb2.php │ │ ├── Result.php │ │ └── Statement.php │ ├── Mysqli │ │ ├── Connection.php │ │ ├── Mysqli.php │ │ ├── Result.php │ │ └── Statement.php │ ├── Oci8 │ │ ├── Connection.php │ │ ├── Feature │ │ │ └── RowCounter.php │ │ ├── Oci8.php │ │ ├── Result.php │ │ └── Statement.php │ ├── Pdo │ │ ├── Connection.php │ │ ├── Feature │ │ │ ├── OracleRowCounter.php │ │ │ └── SqliteRowCounter.php │ │ ├── Pdo.php │ │ ├── Result.php │ │ └── Statement.php │ ├── Pgsql │ │ ├── Connection.php │ │ ├── Pgsql.php │ │ ├── Result.php │ │ └── Statement.php │ ├── ResultInterface.php │ ├── Sqlsrv │ │ ├── Connection.php │ │ ├── Exception │ │ │ ├── ErrorException.php │ │ │ └── ExceptionInterface.php │ │ ├── Result.php │ │ ├── Sqlsrv.php │ │ └── Statement.php │ └── StatementInterface.php ├── Exception │ ├── ErrorException.php │ ├── ExceptionInterface.php │ ├── InvalidArgumentException.php │ ├── InvalidConnectionParametersException.php │ ├── InvalidQueryException.php │ ├── RuntimeException.php │ └── UnexpectedValueException.php ├── ParameterContainer.php ├── Platform │ ├── AbstractPlatform.php │ ├── IbmDb2.php │ ├── Mysql.php │ ├── Oracle.php │ ├── PlatformInterface.php │ ├── Postgresql.php │ ├── Sql92.php │ ├── SqlServer.php │ └── Sqlite.php ├── Profiler │ ├── Profiler.php │ ├── ProfilerAwareInterface.php │ └── ProfilerInterface.php ├── StatementContainer.php └── StatementContainerInterface.php ├── ConfigProvider.php ├── Exception ├── ErrorException.php ├── ExceptionInterface.php ├── InvalidArgumentException.php ├── RuntimeException.php └── UnexpectedValueException.php ├── Metadata ├── Metadata.php ├── MetadataInterface.php ├── Object │ ├── AbstractTableObject.php │ ├── ColumnObject.php │ ├── ConstraintKeyObject.php │ ├── ConstraintObject.php │ ├── TableObject.php │ ├── TriggerObject.php │ └── ViewObject.php └── Source │ ├── AbstractSource.php │ ├── Factory.php │ ├── MysqlMetadata.php │ ├── OracleMetadata.php │ ├── PostgresqlMetadata.php │ ├── SqlServerMetadata.php │ └── SqliteMetadata.php ├── Module.php ├── ResultSet ├── AbstractResultSet.php ├── Exception │ ├── ExceptionInterface.php │ ├── InvalidArgumentException.php │ └── RuntimeException.php ├── HydratingResultSet.php ├── ResultSet.php └── ResultSetInterface.php ├── RowGateway ├── AbstractRowGateway.php ├── Exception │ ├── ExceptionInterface.php │ ├── InvalidArgumentException.php │ └── RuntimeException.php ├── Feature │ ├── AbstractFeature.php │ └── FeatureSet.php ├── RowGateway.php └── RowGatewayInterface.php ├── Sql ├── AbstractExpression.php ├── AbstractPreparableSql.php ├── AbstractSql.php ├── Combine.php ├── Ddl │ ├── AlterTable.php │ ├── Column │ │ ├── AbstractLengthColumn.php │ │ ├── AbstractPrecisionColumn.php │ │ ├── AbstractTimestampColumn.php │ │ ├── BigInteger.php │ │ ├── Binary.php │ │ ├── Blob.php │ │ ├── Boolean.php │ │ ├── Char.php │ │ ├── Column.php │ │ ├── ColumnInterface.php │ │ ├── Date.php │ │ ├── Datetime.php │ │ ├── Decimal.php │ │ ├── Float.php │ │ ├── Floating.php │ │ ├── Integer.php │ │ ├── Text.php │ │ ├── Time.php │ │ ├── Timestamp.php │ │ ├── Varbinary.php │ │ └── Varchar.php │ ├── Constraint │ │ ├── AbstractConstraint.php │ │ ├── Check.php │ │ ├── ConstraintInterface.php │ │ ├── ForeignKey.php │ │ ├── PrimaryKey.php │ │ └── UniqueKey.php │ ├── CreateTable.php │ ├── DropTable.php │ ├── Index │ │ ├── AbstractIndex.php │ │ └── Index.php │ └── SqlInterface.php ├── Delete.php ├── Exception │ ├── ExceptionInterface.php │ ├── InvalidArgumentException.php │ └── RuntimeException.php ├── Expression.php ├── ExpressionInterface.php ├── Having.php ├── Insert.php ├── InsertIgnore.php ├── Join.php ├── Literal.php ├── Platform │ ├── AbstractPlatform.php │ ├── IbmDb2 │ │ ├── IbmDb2.php │ │ └── SelectDecorator.php │ ├── Mysql │ │ ├── Ddl │ │ │ ├── AlterTableDecorator.php │ │ │ └── CreateTableDecorator.php │ │ ├── Mysql.php │ │ └── SelectDecorator.php │ ├── Oracle │ │ ├── Oracle.php │ │ └── SelectDecorator.php │ ├── Platform.php │ ├── PlatformDecoratorInterface.php │ ├── SqlServer │ │ ├── Ddl │ │ │ └── CreateTableDecorator.php │ │ ├── SelectDecorator.php │ │ └── SqlServer.php │ └── Sqlite │ │ ├── SelectDecorator.php │ │ └── Sqlite.php ├── Predicate │ ├── Between.php │ ├── Expression.php │ ├── In.php │ ├── IsNotNull.php │ ├── IsNull.php │ ├── Like.php │ ├── Literal.php │ ├── NotBetween.php │ ├── NotIn.php │ ├── NotLike.php │ ├── Operator.php │ ├── Predicate.php │ ├── PredicateInterface.php │ └── PredicateSet.php ├── PreparableSqlInterface.php ├── Select.php ├── Sql.php ├── SqlInterface.php ├── TableIdentifier.php ├── Update.php └── Where.php └── TableGateway ├── AbstractTableGateway.php ├── Exception ├── ExceptionInterface.php ├── InvalidArgumentException.php └── RuntimeException.php ├── Feature ├── AbstractFeature.php ├── EventFeature.php ├── EventFeature │ └── TableGatewayEvent.php ├── EventFeatureEventsInterface.php ├── FeatureSet.php ├── GlobalAdapterFeature.php ├── MasterSlaveFeature.php ├── MetadataFeature.php ├── RowGatewayFeature.php └── SequenceFeature.php ├── TableGateway.php └── TableGatewayInterface.php /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2005-2019, Zend Technologies USA, Inc. 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, 5 | are permitted provided that the following conditions are met: 6 | 7 | - Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | - Redistributions in binary form must reproduce the above copyright notice, this 11 | list of conditions and the following disclaimer in the documentation and/or 12 | other materials provided with the distribution. 13 | 14 | - Neither the name of Zend Technologies USA, Inc. nor the names of its 15 | contributors may be used to endorse or promote products derived from this 16 | software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 19 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 22 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 25 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 27 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # zend-db 2 | 3 | > ## Repository abandoned 2019-12-31 4 | > 5 | > This repository has moved to [laminas/laminas-db](https://github.com/laminas/laminas-db). 6 | 7 | [![Build Status](https://secure.travis-ci.org/zendframework/zend-db.svg?branch=master)](https://secure.travis-ci.org/zendframework/zend-db) 8 | [![Coverage Status](https://coveralls.io/repos/github/zendframework/zend-db/badge.svg?branch=master)](https://coveralls.io/github/zendframework/zend-db?branch=master) 9 | 10 | `Zend\Db` is a component that abstract the access to a Database using an object 11 | oriented API to build the queries. `Zend\Db` consumes different storage adapters 12 | to access different database vendors such as MySQL, PostgreSQL, Oracle, IBM DB2, 13 | Microsoft Sql Server, PDO, etc. 14 | 15 | - File issues at https://github.com/zendframework/zend-db/issues 16 | - Documentation is at https://docs.zendframework.com/zend-db/ 17 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "zendframework/zend-db", 3 | "description": "Database abstraction layer, SQL abstraction, result set abstraction, and RowDataGateway and TableDataGateway implementations", 4 | "license": "BSD-3-Clause", 5 | "keywords": [ 6 | "zf", 7 | "zendframework", 8 | "db" 9 | ], 10 | "support": { 11 | "docs": "https://docs.zendframework.com/zend-db/", 12 | "issues": "https://github.com/zendframework/zend-db/issues", 13 | "source": "https://github.com/zendframework/zend-db", 14 | "rss": "https://github.com/zendframework/zend-db/releases.atom", 15 | "chat": "https://zendframework-slack.herokuapp.com", 16 | "forum": "https://discourse.zendframework.com/c/questions/components" 17 | }, 18 | "require": { 19 | "php": "^5.6 || ^7.0", 20 | "zendframework/zend-stdlib": "^2.7 || ^3.0" 21 | }, 22 | "require-dev": { 23 | "phpunit/phpunit": "^5.7.27 || ^6.5.14", 24 | "zendframework/zend-coding-standard": "~1.0.0", 25 | "zendframework/zend-eventmanager": "^2.6.2 || ^3.0", 26 | "zendframework/zend-hydrator": "^1.1 || ^2.1 || ^3.0", 27 | "zendframework/zend-servicemanager": "^2.7.5 || ^3.0.3" 28 | }, 29 | "suggest": { 30 | "zendframework/zend-eventmanager": "Zend\\EventManager component", 31 | "zendframework/zend-hydrator": "Zend\\Hydrator component for using HydratingResultSets", 32 | "zendframework/zend-servicemanager": "Zend\\ServiceManager component" 33 | }, 34 | "autoload": { 35 | "psr-4": { 36 | "Zend\\Db\\": "src/" 37 | } 38 | }, 39 | "autoload-dev": { 40 | "files": [ 41 | "test/autoload.php" 42 | ], 43 | "psr-4": { 44 | "ZendTest\\Db\\": "test/unit", 45 | "ZendIntegrationTest\\Db\\": "test/integration" 46 | } 47 | }, 48 | "config": { 49 | "sort-packages": true 50 | }, 51 | "extra": { 52 | "branch-alias": { 53 | "dev-master": "2.11.x-dev", 54 | "dev-develop": "2.12.x-dev" 55 | }, 56 | "zf": { 57 | "component": "Zend\\Db", 58 | "config-provider": "Zend\\Db\\ConfigProvider" 59 | } 60 | }, 61 | "scripts": { 62 | "check": [ 63 | "@cs-check", 64 | "@test" 65 | ], 66 | "cs-check": "phpcs", 67 | "cs-fix": "phpcbf", 68 | "test": "phpunit --colors=always --testsuite \"unit test\"", 69 | "test-coverage": "phpunit --colors=always --coverage-clover clover.xml", 70 | "test-integration": "phpunit --colors=always --testsuite \"integration test\"", 71 | "upload-coverage": "coveralls -v" 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/Adapter/AdapterAbstractServiceFactory.php: -------------------------------------------------------------------------------- 1 | getConfig($container); 38 | if (empty($config)) { 39 | return false; 40 | } 41 | 42 | return ( 43 | isset($config[$requestedName]) 44 | && is_array($config[$requestedName]) 45 | && ! empty($config[$requestedName]) 46 | ); 47 | } 48 | 49 | /** 50 | * Determine if we can create a service with name (SM v2 compatibility) 51 | * 52 | * @param ServiceLocatorInterface $serviceLocator 53 | * @param string $name 54 | * @param string $requestedName 55 | * @return bool 56 | */ 57 | public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) 58 | { 59 | return $this->canCreate($serviceLocator, $requestedName); 60 | } 61 | 62 | /** 63 | * Create a DB adapter 64 | * 65 | * @param ContainerInterface $container 66 | * @param string $requestedName 67 | * @param array $options 68 | * @return Adapter 69 | */ 70 | public function __invoke(ContainerInterface $container, $requestedName, array $options = null) 71 | { 72 | $config = $this->getConfig($container); 73 | return new Adapter($config[$requestedName]); 74 | } 75 | 76 | /** 77 | * Create service with name 78 | * 79 | * @param ServiceLocatorInterface $serviceLocator 80 | * @param string $name 81 | * @param string $requestedName 82 | * @return Adapter 83 | */ 84 | public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) 85 | { 86 | return $this($serviceLocator, $requestedName); 87 | } 88 | 89 | /** 90 | * Get db configuration, if any 91 | * 92 | * @param ContainerInterface $container 93 | * @return array 94 | */ 95 | protected function getConfig(ContainerInterface $container) 96 | { 97 | if ($this->config !== null) { 98 | return $this->config; 99 | } 100 | 101 | if (! $container->has('config')) { 102 | $this->config = []; 103 | return $this->config; 104 | } 105 | 106 | $config = $container->get('config'); 107 | if (! isset($config['db']) 108 | || ! is_array($config['db']) 109 | ) { 110 | $this->config = []; 111 | return $this->config; 112 | } 113 | 114 | $config = $config['db']; 115 | if (! isset($config['adapters']) 116 | || ! is_array($config['adapters']) 117 | ) { 118 | $this->config = []; 119 | return $this->config; 120 | } 121 | 122 | $this->config = $config['adapters']; 123 | return $this->config; 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /src/Adapter/AdapterAwareInterface.php: -------------------------------------------------------------------------------- 1 | adapter = $adapter; 28 | 29 | return $this; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/Adapter/AdapterInterface.php: -------------------------------------------------------------------------------- 1 | get('config'); 29 | return new Adapter($config['db']); 30 | } 31 | 32 | /** 33 | * Create db adapter service (v2) 34 | * 35 | * @param ServiceLocatorInterface $container 36 | * @return Adapter 37 | */ 38 | public function createService(ServiceLocatorInterface $container) 39 | { 40 | return $this($container, Adapter::class); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/Adapter/Driver/AbstractConnection.php: -------------------------------------------------------------------------------- 1 | isConnected()) { 55 | $this->resource = null; 56 | } 57 | 58 | return $this; 59 | } 60 | 61 | /** 62 | * Get connection parameters 63 | * 64 | * @return array 65 | */ 66 | public function getConnectionParameters() 67 | { 68 | return $this->connectionParameters; 69 | } 70 | 71 | /** 72 | * Get driver name 73 | * 74 | * @return null|string 75 | */ 76 | public function getDriverName() 77 | { 78 | return $this->driverName; 79 | } 80 | 81 | /** 82 | * @return null|ProfilerInterface 83 | */ 84 | public function getProfiler() 85 | { 86 | return $this->profiler; 87 | } 88 | 89 | /** 90 | * {@inheritDoc} 91 | * 92 | * @return resource 93 | */ 94 | public function getResource() 95 | { 96 | if (! $this->isConnected()) { 97 | $this->connect(); 98 | } 99 | 100 | return $this->resource; 101 | } 102 | 103 | /** 104 | * Checks whether the connection is in transaction state. 105 | * 106 | * @return boolean 107 | */ 108 | public function inTransaction() 109 | { 110 | return $this->inTransaction; 111 | } 112 | 113 | /** 114 | * @param array $connectionParameters 115 | * @return self Provides a fluent interface 116 | */ 117 | public function setConnectionParameters(array $connectionParameters) 118 | { 119 | $this->connectionParameters = $connectionParameters; 120 | 121 | return $this; 122 | } 123 | 124 | /** 125 | * {@inheritDoc} 126 | * 127 | * @return self Provides a fluent interface 128 | */ 129 | public function setProfiler(ProfilerInterface $profiler) 130 | { 131 | $this->profiler = $profiler; 132 | 133 | return $this; 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /src/Adapter/Driver/ConnectionInterface.php: -------------------------------------------------------------------------------- 1 | driver = $driver; 30 | } 31 | 32 | /** 33 | * Get name 34 | * 35 | * @return string 36 | */ 37 | abstract public function getName(); 38 | } 39 | -------------------------------------------------------------------------------- /src/Adapter/Driver/Feature/DriverFeatureInterface.php: -------------------------------------------------------------------------------- 1 | getSql(); 36 | if ($sql == '' || stripos(strtolower($sql), 'select') === false) { 37 | return; 38 | } 39 | $countSql = 'SELECT COUNT(*) as "count" FROM (' . $sql . ')'; 40 | $countStmt->prepare($countSql); 41 | $result = $countStmt->execute(); 42 | $countRow = $result->current(); 43 | return $countRow['count']; 44 | } 45 | 46 | /** 47 | * @param string $sql 48 | * @return null|int 49 | */ 50 | public function getCountForSql($sql) 51 | { 52 | if (stripos(strtolower($sql), 'select') === false) { 53 | return; 54 | } 55 | $countSql = 'SELECT COUNT(*) as "count" FROM (' . $sql . ')'; 56 | $result = $this->driver->getConnection()->execute($countSql); 57 | $countRow = $result->current(); 58 | return $countRow['count']; 59 | } 60 | 61 | /** 62 | * @param \Zend\Db\Adapter\Driver\Oci8\Statement|string $context 63 | * @return callable 64 | */ 65 | public function getRowCountClosure($context) 66 | { 67 | $rowCounter = $this; 68 | return function () use ($rowCounter, $context) { 69 | /** @var $rowCounter RowCounter */ 70 | return ($context instanceof Statement) 71 | ? $rowCounter->getCountForStatement($context) 72 | : $rowCounter->getCountForSql($context); 73 | }; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/Adapter/Driver/Pdo/Feature/OracleRowCounter.php: -------------------------------------------------------------------------------- 1 | getSql(); 36 | if ($sql == '' || stripos($sql, 'select') === false) { 37 | return; 38 | } 39 | $countSql = 'SELECT COUNT(*) as "count" FROM (' . $sql . ')'; 40 | $countStmt->prepare($countSql); 41 | $result = $countStmt->execute(); 42 | $countRow = $result->getResource()->fetch(\PDO::FETCH_ASSOC); 43 | unset($statement, $result); 44 | return $countRow['count']; 45 | } 46 | 47 | /** 48 | * @param $sql 49 | * @return null|int 50 | */ 51 | public function getCountForSql($sql) 52 | { 53 | if (stripos($sql, 'select') === false) { 54 | return; 55 | } 56 | $countSql = 'SELECT COUNT(*) as count FROM (' . $sql . ')'; 57 | /** @var $pdo \PDO */ 58 | $pdo = $this->driver->getConnection()->getResource(); 59 | $result = $pdo->query($countSql); 60 | $countRow = $result->fetch(\PDO::FETCH_ASSOC); 61 | return $countRow['count']; 62 | } 63 | 64 | /** 65 | * @param $context 66 | * @return \Closure 67 | */ 68 | public function getRowCountClosure($context) 69 | { 70 | return function () use ($context) { 71 | return ($context instanceof Pdo\Statement) 72 | ? $this->getCountForStatement($context) 73 | : $this->getCountForSql($context); 74 | }; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/Adapter/Driver/Pdo/Feature/SqliteRowCounter.php: -------------------------------------------------------------------------------- 1 | getSql(); 36 | if ($sql == '' || stripos($sql, 'select') === false) { 37 | return; 38 | } 39 | $countSql = 'SELECT COUNT(*) as "count" FROM (' . $sql . ')'; 40 | $countStmt->prepare($countSql); 41 | $result = $countStmt->execute(); 42 | $countRow = $result->getResource()->fetch(\PDO::FETCH_ASSOC); 43 | unset($statement, $result); 44 | return $countRow['count']; 45 | } 46 | 47 | /** 48 | * @param $sql 49 | * @return null|int 50 | */ 51 | public function getCountForSql($sql) 52 | { 53 | if (stripos($sql, 'select') === false) { 54 | return; 55 | } 56 | $countSql = 'SELECT COUNT(*) as count FROM (' . $sql . ')'; 57 | /** @var $pdo \PDO */ 58 | $pdo = $this->driver->getConnection()->getResource(); 59 | $result = $pdo->query($countSql); 60 | $countRow = $result->fetch(\PDO::FETCH_ASSOC); 61 | return $countRow['count']; 62 | } 63 | 64 | /** 65 | * @param $context 66 | * @return \Closure 67 | */ 68 | public function getRowCountClosure($context) 69 | { 70 | return function () use ($context) { 71 | return ($context instanceof Pdo\Statement) 72 | ? $this->getCountForStatement($context) 73 | : $this->getCountForSql($context); 74 | }; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/Adapter/Driver/ResultInterface.php: -------------------------------------------------------------------------------- 1 | errors = ($errors === false) ? sqlsrv_errors() : $errors; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Adapter/Driver/Sqlsrv/Exception/ExceptionInterface.php: -------------------------------------------------------------------------------- 1 | parameters = $parameters; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Adapter/Exception/InvalidQueryException.php: -------------------------------------------------------------------------------- 1 | quoteIdentifiers) { 40 | return $identifier; 41 | } 42 | 43 | $safeWordsInt = ['*' => true, ' ' => true, '.' => true, 'as' => true]; 44 | 45 | foreach ($safeWords as $sWord) { 46 | $safeWordsInt[strtolower($sWord)] = true; 47 | } 48 | 49 | $parts = preg_split( 50 | $this->quoteIdentifierFragmentPattern, 51 | $identifier, 52 | -1, 53 | PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY 54 | ); 55 | 56 | $identifier = ''; 57 | 58 | foreach ($parts as $part) { 59 | $identifier .= isset($safeWordsInt[strtolower($part)]) 60 | ? $part 61 | : $this->quoteIdentifier[0] 62 | . str_replace($this->quoteIdentifier[0], $this->quoteIdentifierTo, $part) 63 | . $this->quoteIdentifier[1]; 64 | } 65 | 66 | return $identifier; 67 | } 68 | 69 | /** 70 | * {@inheritDoc} 71 | */ 72 | public function quoteIdentifier($identifier) 73 | { 74 | if (! $this->quoteIdentifiers) { 75 | return $identifier; 76 | } 77 | 78 | return $this->quoteIdentifier[0] 79 | . str_replace($this->quoteIdentifier[0], $this->quoteIdentifierTo, $identifier) 80 | . $this->quoteIdentifier[1]; 81 | } 82 | 83 | /** 84 | * {@inheritDoc} 85 | */ 86 | public function quoteIdentifierChain($identifierChain) 87 | { 88 | return '"' . implode('"."', (array) str_replace('"', '\\"', $identifierChain)) . '"'; 89 | } 90 | 91 | /** 92 | * {@inheritDoc} 93 | */ 94 | public function getQuoteIdentifierSymbol() 95 | { 96 | return $this->quoteIdentifier[0]; 97 | } 98 | 99 | /** 100 | * {@inheritDoc} 101 | */ 102 | public function getQuoteValueSymbol() 103 | { 104 | return '\''; 105 | } 106 | 107 | /** 108 | * {@inheritDoc} 109 | */ 110 | public function quoteValue($value) 111 | { 112 | trigger_error( 113 | 'Attempting to quote a value in ' . get_class($this) . 114 | ' without extension/driver support can introduce security vulnerabilities in a production environment' 115 | ); 116 | return '\'' . addcslashes((string) $value, "\x00\n\r\\'\"\x1a") . '\''; 117 | } 118 | 119 | /** 120 | * {@inheritDoc} 121 | */ 122 | public function quoteTrustedValue($value) 123 | { 124 | return '\'' . addcslashes((string) $value, "\x00\n\r\\'\"\x1a") . '\''; 125 | } 126 | 127 | /** 128 | * {@inheritDoc} 129 | */ 130 | public function quoteValueList($valueList) 131 | { 132 | return implode(', ', array_map([$this, 'quoteValue'], (array) $valueList)); 133 | } 134 | 135 | /** 136 | * {@inheritDoc} 137 | */ 138 | public function getIdentifierSeparator() 139 | { 140 | return '.'; 141 | } 142 | } 143 | -------------------------------------------------------------------------------- /src/Adapter/Platform/IbmDb2.php: -------------------------------------------------------------------------------- 1 | quoteIdentifiers = false; 29 | } 30 | 31 | if (isset($options['identifier_separator'])) { 32 | $this->identifierSeparator = $options['identifier_separator']; 33 | } 34 | } 35 | 36 | /** 37 | * {@inheritDoc} 38 | */ 39 | public function getName() 40 | { 41 | return 'IBM DB2'; 42 | } 43 | 44 | /** 45 | * {@inheritDoc} 46 | */ 47 | public function quoteIdentifierInFragment($identifier, array $safeWords = []) 48 | { 49 | if (! $this->quoteIdentifiers) { 50 | return $identifier; 51 | } 52 | $safeWordsInt = ['*' => true, ' ' => true, '.' => true, 'as' => true]; 53 | foreach ($safeWords as $sWord) { 54 | $safeWordsInt[strtolower($sWord)] = true; 55 | } 56 | $parts = preg_split( 57 | '/([^0-9,a-z,A-Z$#_:])/i', 58 | $identifier, 59 | -1, 60 | PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY 61 | ); 62 | $identifier = ''; 63 | foreach ($parts as $part) { 64 | $identifier .= isset($safeWordsInt[strtolower($part)]) 65 | ? $part 66 | : $this->quoteIdentifier[0] 67 | . str_replace($this->quoteIdentifier[0], $this->quoteIdentifierTo, $part) 68 | . $this->quoteIdentifier[1]; 69 | } 70 | return $identifier; 71 | } 72 | 73 | /** 74 | * {@inheritDoc} 75 | */ 76 | public function quoteIdentifierChain($identifierChain) 77 | { 78 | if ($this->quoteIdentifiers === false) { 79 | if (is_array($identifierChain)) { 80 | return implode($this->identifierSeparator, $identifierChain); 81 | } else { 82 | return $identifierChain; 83 | } 84 | } 85 | $identifierChain = str_replace('"', '\\"', $identifierChain); 86 | if (is_array($identifierChain)) { 87 | $identifierChain = implode('"' . $this->identifierSeparator . '"', $identifierChain); 88 | } 89 | return '"' . $identifierChain . '"'; 90 | } 91 | 92 | /** 93 | * {@inheritDoc} 94 | */ 95 | public function quoteValue($value) 96 | { 97 | if (function_exists('db2_escape_string')) { 98 | return '\'' . db2_escape_string($value) . '\''; 99 | } 100 | trigger_error( 101 | 'Attempting to quote a value in ' . __CLASS__ . ' without extension/driver support ' 102 | . 'can introduce security vulnerabilities in a production environment.' 103 | ); 104 | return '\'' . str_replace("'", "''", $value) . '\''; 105 | } 106 | 107 | /** 108 | * {@inheritDoc} 109 | */ 110 | public function quoteTrustedValue($value) 111 | { 112 | if (function_exists('db2_escape_string')) { 113 | return '\'' . db2_escape_string($value) . '\''; 114 | } 115 | return '\'' . str_replace("'", "''", $value) . '\''; 116 | } 117 | 118 | /** 119 | * {@inheritDoc} 120 | */ 121 | public function getIdentifierSeparator() 122 | { 123 | return $this->identifierSeparator; 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /src/Adapter/Platform/Mysql.php: -------------------------------------------------------------------------------- 1 | setDriver($driver); 48 | } 49 | } 50 | 51 | /** 52 | * @param \Zend\Db\Adapter\Driver\Mysqli\Mysqli|\Zend\Db\Adapter\Driver\Pdo\Pdo|\mysqli|\PDO $driver 53 | * @return self Provides a fluent interface 54 | * @throws \Zend\Db\Adapter\Exception\InvalidArgumentException 55 | */ 56 | public function setDriver($driver) 57 | { 58 | // handle Zend\Db drivers 59 | if ($driver instanceof Mysqli\Mysqli 60 | || ($driver instanceof Pdo\Pdo && $driver->getDatabasePlatformName() == 'Mysql') 61 | || ($driver instanceof \mysqli) 62 | || ($driver instanceof \PDO && $driver->getAttribute(\PDO::ATTR_DRIVER_NAME) == 'mysql') 63 | ) { 64 | $this->resource = $driver; 65 | return $this; 66 | } 67 | 68 | throw new Exception\InvalidArgumentException( 69 | '$driver must be a Mysqli or Mysql PDO Zend\Db\Adapter\Driver, Mysqli instance or MySQL PDO instance' 70 | ); 71 | } 72 | 73 | /** 74 | * {@inheritDoc} 75 | */ 76 | public function getName() 77 | { 78 | return 'MySQL'; 79 | } 80 | 81 | /** 82 | * {@inheritDoc} 83 | */ 84 | public function quoteIdentifierChain($identifierChain) 85 | { 86 | return '`' . implode('`.`', (array) str_replace('`', '``', $identifierChain)) . '`'; 87 | } 88 | 89 | /** 90 | * {@inheritDoc} 91 | */ 92 | public function quoteValue($value) 93 | { 94 | if ($this->resource instanceof DriverInterface) { 95 | $this->resource = $this->resource->getConnection()->getResource(); 96 | } 97 | if ($this->resource instanceof \mysqli) { 98 | return '\'' . $this->resource->real_escape_string($value) . '\''; 99 | } 100 | if ($this->resource instanceof \PDO) { 101 | return $this->resource->quote($value); 102 | } 103 | return parent::quoteValue($value); 104 | } 105 | 106 | /** 107 | * {@inheritDoc} 108 | */ 109 | public function quoteTrustedValue($value) 110 | { 111 | if ($this->resource instanceof DriverInterface) { 112 | $this->resource = $this->resource->getConnection()->getResource(); 113 | } 114 | if ($this->resource instanceof \mysqli) { 115 | return '\'' . $this->resource->real_escape_string($value) . '\''; 116 | } 117 | if ($this->resource instanceof \PDO) { 118 | return $this->resource->quote($value); 119 | } 120 | return parent::quoteTrustedValue($value); 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /src/Adapter/Platform/Oracle.php: -------------------------------------------------------------------------------- 1 | quoteIdentifiers = false; 35 | } 36 | 37 | if ($driver) { 38 | $this->setDriver($driver); 39 | } 40 | } 41 | 42 | /** 43 | * @param Pdo|Oci8 $driver 44 | * @return self Provides a fluent interface 45 | * @throws InvalidArgumentException 46 | */ 47 | public function setDriver($driver) 48 | { 49 | if ($driver instanceof Oci8 50 | || ($driver instanceof Pdo && $driver->getDatabasePlatformName() == 'Oracle') 51 | || ($driver instanceof Pdo && $driver->getDatabasePlatformName() == 'Sqlite') 52 | || ($driver instanceof \oci8) 53 | || ($driver instanceof PDO && $driver->getAttribute(PDO::ATTR_DRIVER_NAME) == 'oci') 54 | ) { 55 | $this->resource = $driver; 56 | return $this; 57 | } 58 | 59 | throw new InvalidArgumentException( 60 | '$driver must be a Oci8 or Oracle PDO Zend\Db\Adapter\Driver, ' 61 | . 'Oci8 instance, or Oci PDO instance' 62 | ); 63 | } 64 | 65 | /** 66 | * @return null|Pdo|Oci8 67 | */ 68 | public function getDriver() 69 | { 70 | return $this->resource; 71 | } 72 | 73 | /** 74 | * {@inheritDoc} 75 | */ 76 | public function getName() 77 | { 78 | return 'Oracle'; 79 | } 80 | 81 | /** 82 | * {@inheritDoc} 83 | */ 84 | public function quoteIdentifierChain($identifierChain) 85 | { 86 | if ($this->quoteIdentifiers === false) { 87 | return implode('.', (array) $identifierChain); 88 | } 89 | 90 | return '"' . implode('"."', (array) str_replace('"', '\\"', $identifierChain)) . '"'; 91 | } 92 | 93 | /** 94 | * {@inheritDoc} 95 | */ 96 | public function quoteValue($value) 97 | { 98 | if ($this->resource instanceof DriverInterface) { 99 | $this->resource = $this->resource->getConnection()->getResource(); 100 | } 101 | 102 | if ($this->resource) { 103 | if ($this->resource instanceof PDO) { 104 | return $this->resource->quote($value); 105 | } 106 | 107 | if (get_resource_type($this->resource) == 'oci8 connection' 108 | || get_resource_type($this->resource) == 'oci8 persistent connection' 109 | ) { 110 | return "'" . addcslashes(str_replace("'", "''", $value), "\x00\n\r\"\x1a") . "'"; 111 | } 112 | } 113 | 114 | trigger_error( 115 | 'Attempting to quote a value in ' . __CLASS__ . ' without extension/driver support ' 116 | . 'can introduce security vulnerabilities in a production environment.' 117 | ); 118 | 119 | return "'" . addcslashes(str_replace("'", "''", $value), "\x00\n\r\"\x1a") . "'"; 120 | } 121 | 122 | /** 123 | * {@inheritDoc} 124 | */ 125 | public function quoteTrustedValue($value) 126 | { 127 | return "'" . addcslashes(str_replace('\'', '\'\'', $value), "\x00\n\r\"\x1a") . "'"; 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /src/Adapter/Platform/PlatformInterface.php: -------------------------------------------------------------------------------- 1 | setDriver($driver); 38 | } 39 | } 40 | 41 | /** 42 | * @param \Zend\Db\Adapter\Driver\Pgsql\Pgsql|\Zend\Db\Adapter\Driver\Pdo\Pdo|resource|\PDO $driver 43 | * @return self Provides a fluent interface 44 | * @throws \Zend\Db\Adapter\Exception\InvalidArgumentException 45 | */ 46 | public function setDriver($driver) 47 | { 48 | if ($driver instanceof Pgsql\Pgsql 49 | || ($driver instanceof Pdo\Pdo && $driver->getDatabasePlatformName() == 'Postgresql') 50 | || (is_resource($driver) && (in_array(get_resource_type($driver), ['pgsql link', 'pgsql link persistent']))) 51 | || ($driver instanceof \PDO && $driver->getAttribute(\PDO::ATTR_DRIVER_NAME) == 'pgsql') 52 | ) { 53 | $this->resource = $driver; 54 | return $this; 55 | } 56 | 57 | throw new Exception\InvalidArgumentException( 58 | '$driver must be a Pgsql or Postgresql PDO Zend\Db\Adapter\Driver, pgsql link resource or Postgresql PDO ' 59 | . 'instance' 60 | ); 61 | } 62 | 63 | /** 64 | * {@inheritDoc} 65 | */ 66 | public function getName() 67 | { 68 | return 'PostgreSQL'; 69 | } 70 | 71 | /** 72 | * {@inheritDoc} 73 | */ 74 | public function quoteIdentifierChain($identifierChain) 75 | { 76 | return '"' . implode('"."', (array) str_replace('"', '""', $identifierChain)) . '"'; 77 | } 78 | 79 | /** 80 | * {@inheritDoc} 81 | */ 82 | public function quoteValue($value) 83 | { 84 | if ($this->resource instanceof DriverInterface) { 85 | $this->resource = $this->resource->getConnection()->getResource(); 86 | } 87 | if (is_resource($this->resource)) { 88 | return '\'' . pg_escape_string($this->resource, $value) . '\''; 89 | } 90 | if ($this->resource instanceof \PDO) { 91 | return $this->resource->quote($value); 92 | } 93 | return 'E' . parent::quoteValue($value); 94 | } 95 | 96 | /** 97 | * {@inheritDoc} 98 | */ 99 | public function quoteTrustedValue($value) 100 | { 101 | if ($this->resource instanceof DriverInterface) { 102 | $this->resource = $this->resource->getConnection()->getResource(); 103 | } 104 | if (is_resource($this->resource)) { 105 | return '\'' . pg_escape_string($this->resource, $value) . '\''; 106 | } 107 | if ($this->resource instanceof \PDO) { 108 | return $this->resource->quote($value); 109 | } 110 | return 'E' . parent::quoteTrustedValue($value); 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /src/Adapter/Platform/Sql92.php: -------------------------------------------------------------------------------- 1 | setDriver($driver); 40 | } 41 | } 42 | 43 | /** 44 | * @param \Zend\Db\Adapter\Driver\Sqlsrv\Sqlsrv|\Zend\Db\Adapter\Driver\Pdo\Pdo|resource|\PDO $driver 45 | * @return self Provides a fluent interface 46 | * @throws \Zend\Db\Adapter\Exception\InvalidArgumentException 47 | */ 48 | public function setDriver($driver) 49 | { 50 | // handle Zend\Db drivers 51 | if (($driver instanceof Pdo\Pdo && in_array($driver->getDatabasePlatformName(), ['SqlServer', 'Dblib'])) 52 | || ($driver instanceof \PDO && in_array($driver->getAttribute(\PDO::ATTR_DRIVER_NAME), ['sqlsrv', 'dblib'])) 53 | ) { 54 | $this->resource = $driver; 55 | return $this; 56 | } 57 | 58 | throw new Exception\InvalidArgumentException( 59 | '$driver must be a Sqlsrv PDO Zend\Db\Adapter\Driver or Sqlsrv PDO instance' 60 | ); 61 | } 62 | 63 | /** 64 | * {@inheritDoc} 65 | */ 66 | public function getName() 67 | { 68 | return 'SQLServer'; 69 | } 70 | 71 | /** 72 | * {@inheritDoc} 73 | */ 74 | public function getQuoteIdentifierSymbol() 75 | { 76 | return $this->quoteIdentifier; 77 | } 78 | 79 | /** 80 | * {@inheritDoc} 81 | */ 82 | public function quoteIdentifierChain($identifierChain) 83 | { 84 | return '[' . implode('].[', (array) $identifierChain) . ']'; 85 | } 86 | 87 | /** 88 | * {@inheritDoc} 89 | */ 90 | public function quoteValue($value) 91 | { 92 | if ($this->resource instanceof DriverInterface) { 93 | $this->resource = $this->resource->getConnection()->getResource(); 94 | } 95 | if ($this->resource instanceof \PDO) { 96 | return $this->resource->quote($value); 97 | } 98 | trigger_error( 99 | 'Attempting to quote a value in ' . __CLASS__ . ' without extension/driver support ' 100 | . 'can introduce security vulnerabilities in a production environment.' 101 | ); 102 | 103 | return '\'' . str_replace('\'', '\'\'', addcslashes($value, "\000\032")) . '\''; 104 | } 105 | 106 | /** 107 | * {@inheritDoc} 108 | */ 109 | public function quoteTrustedValue($value) 110 | { 111 | if ($this->resource instanceof DriverInterface) { 112 | $this->resource = $this->resource->getConnection()->getResource(); 113 | } 114 | if ($this->resource instanceof \PDO) { 115 | return $this->resource->quote($value); 116 | } 117 | return '\'' . str_replace('\'', '\'\'', $value) . '\''; 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /src/Adapter/Platform/Sqlite.php: -------------------------------------------------------------------------------- 1 | setDriver($driver); 40 | } 41 | } 42 | 43 | /** 44 | * @param \Zend\Db\Adapter\Driver\Pdo\Pdo|\PDO $driver 45 | * @return self Provides a fluent interface 46 | * @throws \Zend\Db\Adapter\Exception\InvalidArgumentException 47 | */ 48 | public function setDriver($driver) 49 | { 50 | if (($driver instanceof \PDO && $driver->getAttribute(\PDO::ATTR_DRIVER_NAME) == 'sqlite') 51 | || ($driver instanceof Pdo\Pdo && $driver->getDatabasePlatformName() == 'Sqlite') 52 | ) { 53 | $this->resource = $driver; 54 | return $this; 55 | } 56 | 57 | throw new Exception\InvalidArgumentException( 58 | '$driver must be a Sqlite PDO Zend\Db\Adapter\Driver, Sqlite PDO instance' 59 | ); 60 | } 61 | 62 | /** 63 | * {@inheritDoc} 64 | */ 65 | public function getName() 66 | { 67 | return 'SQLite'; 68 | } 69 | 70 | /** 71 | * {@inheritDoc} 72 | */ 73 | public function quoteValue($value) 74 | { 75 | $resource = $this->resource; 76 | 77 | if ($resource instanceof DriverInterface) { 78 | $resource = $resource->getConnection()->getResource(); 79 | } 80 | 81 | if ($resource instanceof \PDO) { 82 | return $resource->quote($value); 83 | } 84 | 85 | return parent::quoteValue($value); 86 | } 87 | 88 | /** 89 | * {@inheritDoc} 90 | */ 91 | public function quoteTrustedValue($value) 92 | { 93 | $resource = $this->resource; 94 | 95 | if ($resource instanceof DriverInterface) { 96 | $resource = $resource->getConnection()->getResource(); 97 | } 98 | 99 | if ($resource instanceof \PDO) { 100 | return $resource->quote($value); 101 | } 102 | 103 | return parent::quoteTrustedValue($value); 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /src/Adapter/Profiler/Profiler.php: -------------------------------------------------------------------------------- 1 | '', 36 | 'parameters' => null, 37 | 'start' => microtime(true), 38 | 'end' => null, 39 | 'elapse' => null 40 | ]; 41 | if ($target instanceof StatementContainerInterface) { 42 | $profileInformation['sql'] = $target->getSql(); 43 | $profileInformation['parameters'] = clone $target->getParameterContainer(); 44 | } elseif (is_string($target)) { 45 | $profileInformation['sql'] = $target; 46 | } else { 47 | throw new Exception\InvalidArgumentException( 48 | __FUNCTION__ . ' takes either a StatementContainer or a string' 49 | ); 50 | } 51 | 52 | $this->profiles[$this->currentIndex] = $profileInformation; 53 | 54 | return $this; 55 | } 56 | 57 | /** 58 | * @return self Provides a fluent interface 59 | */ 60 | public function profilerFinish() 61 | { 62 | if (! isset($this->profiles[$this->currentIndex])) { 63 | throw new Exception\RuntimeException( 64 | 'A profile must be started before ' . __FUNCTION__ . ' can be called.' 65 | ); 66 | } 67 | $current = &$this->profiles[$this->currentIndex]; 68 | $current['end'] = microtime(true); 69 | $current['elapse'] = $current['end'] - $current['start']; 70 | $this->currentIndex++; 71 | return $this; 72 | } 73 | 74 | /** 75 | * @return array|null 76 | */ 77 | public function getLastProfile() 78 | { 79 | return end($this->profiles); 80 | } 81 | 82 | /** 83 | * @return array 84 | */ 85 | public function getProfiles() 86 | { 87 | return $this->profiles; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/Adapter/Profiler/ProfilerAwareInterface.php: -------------------------------------------------------------------------------- 1 | setSql($sql); 32 | } 33 | $this->parameterContainer = ($parameterContainer) ?: new ParameterContainer; 34 | } 35 | 36 | /** 37 | * @param $sql 38 | * @return self Provides a fluent interface 39 | */ 40 | public function setSql($sql) 41 | { 42 | $this->sql = $sql; 43 | return $this; 44 | } 45 | 46 | /** 47 | * @return string 48 | */ 49 | public function getSql() 50 | { 51 | return $this->sql; 52 | } 53 | 54 | /** 55 | * @param ParameterContainer $parameterContainer 56 | * @return self Provides a fluent interface 57 | */ 58 | public function setParameterContainer(ParameterContainer $parameterContainer) 59 | { 60 | $this->parameterContainer = $parameterContainer; 61 | return $this; 62 | } 63 | 64 | /** 65 | * @return null|ParameterContainer 66 | */ 67 | public function getParameterContainer() 68 | { 69 | return $this->parameterContainer; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/Adapter/StatementContainerInterface.php: -------------------------------------------------------------------------------- 1 | $this->getDependencyConfig(), 21 | ]; 22 | } 23 | 24 | /** 25 | * Retrieve zend-db default dependency configuration. 26 | * 27 | * @return array 28 | */ 29 | public function getDependencyConfig() 30 | { 31 | return [ 32 | 'abstract_factories' => [ 33 | Adapter\AdapterAbstractServiceFactory::class, 34 | ], 35 | 'factories' => [ 36 | Adapter\AdapterInterface::class => Adapter\AdapterServiceFactory::class, 37 | ], 38 | 'aliases' => [ 39 | Adapter\Adapter::class => Adapter\AdapterInterface::class, 40 | ], 41 | ]; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Exception/ErrorException.php: -------------------------------------------------------------------------------- 1 | source = Source\Factory::createSourceFromAdapter($adapter); 32 | } 33 | 34 | /** 35 | * {@inheritdoc} 36 | */ 37 | public function getTables($schema = null, $includeViews = false) 38 | { 39 | return $this->source->getTables($schema, $includeViews); 40 | } 41 | 42 | /** 43 | * {@inheritdoc} 44 | */ 45 | public function getViews($schema = null) 46 | { 47 | return $this->source->getViews($schema); 48 | } 49 | 50 | /** 51 | * {@inheritdoc} 52 | */ 53 | public function getTriggers($schema = null) 54 | { 55 | return $this->source->getTriggers($schema); 56 | } 57 | 58 | /** 59 | * {@inheritdoc} 60 | */ 61 | public function getConstraints($table, $schema = null) 62 | { 63 | return $this->source->getConstraints($table, $schema); 64 | } 65 | 66 | /** 67 | * {@inheritdoc} 68 | */ 69 | public function getColumns($table, $schema = null) 70 | { 71 | return $this->source->getColumns($table, $schema); 72 | } 73 | 74 | /** 75 | * {@inheritdoc} 76 | */ 77 | public function getConstraintKeys($constraint, $table, $schema = null) 78 | { 79 | return $this->source->getConstraintKeys($constraint, $table, $schema); 80 | } 81 | 82 | /** 83 | * {@inheritdoc} 84 | */ 85 | public function getConstraint($constraintName, $table, $schema = null) 86 | { 87 | return $this->source->getConstraint($constraintName, $table, $schema); 88 | } 89 | 90 | /** 91 | * {@inheritdoc} 92 | */ 93 | public function getSchemas() 94 | { 95 | return $this->source->getSchemas(); 96 | } 97 | 98 | /** 99 | * {@inheritdoc} 100 | */ 101 | public function getTableNames($schema = null, $includeViews = false) 102 | { 103 | return $this->source->getTableNames($schema, $includeViews); 104 | } 105 | 106 | /** 107 | * {@inheritdoc} 108 | */ 109 | public function getTable($tableName, $schema = null) 110 | { 111 | return $this->source->getTable($tableName, $schema); 112 | } 113 | 114 | /** 115 | * {@inheritdoc} 116 | */ 117 | public function getViewNames($schema = null) 118 | { 119 | return $this->source->getViewNames($schema); 120 | } 121 | 122 | /** 123 | * {@inheritdoc} 124 | */ 125 | public function getView($viewName, $schema = null) 126 | { 127 | return $this->source->getView($viewName, $schema); 128 | } 129 | 130 | /** 131 | * {@inheritdoc} 132 | */ 133 | public function getTriggerNames($schema = null) 134 | { 135 | return $this->source->getTriggerNames($schema); 136 | } 137 | 138 | /** 139 | * {@inheritdoc} 140 | */ 141 | public function getTrigger($triggerName, $schema = null) 142 | { 143 | return $this->source->getTrigger($triggerName, $schema); 144 | } 145 | 146 | /** 147 | * {@inheritdoc} 148 | */ 149 | public function getColumnNames($table, $schema = null) 150 | { 151 | return $this->source->getColumnNames($table, $schema); 152 | } 153 | 154 | /** 155 | * {@inheritdoc} 156 | */ 157 | public function getColumn($columnName, $table, $schema = null) 158 | { 159 | return $this->source->getColumn($columnName, $table, $schema); 160 | } 161 | } 162 | -------------------------------------------------------------------------------- /src/Metadata/MetadataInterface.php: -------------------------------------------------------------------------------- 1 | setName($name); 52 | } 53 | } 54 | 55 | /** 56 | * Set columns 57 | * 58 | * @param array $columns 59 | */ 60 | public function setColumns(array $columns) 61 | { 62 | $this->columns = $columns; 63 | } 64 | 65 | /** 66 | * Get columns 67 | * 68 | * @return array 69 | */ 70 | public function getColumns() 71 | { 72 | return $this->columns; 73 | } 74 | 75 | /** 76 | * Set constraints 77 | * 78 | * @param array $constraints 79 | */ 80 | public function setConstraints($constraints) 81 | { 82 | $this->constraints = $constraints; 83 | } 84 | 85 | /** 86 | * Get constraints 87 | * 88 | * @return array 89 | */ 90 | public function getConstraints() 91 | { 92 | return $this->constraints; 93 | } 94 | 95 | /** 96 | * Set name 97 | * 98 | * @param string $name 99 | */ 100 | public function setName($name) 101 | { 102 | $this->name = $name; 103 | } 104 | 105 | /** 106 | * Get name 107 | * 108 | * @return string 109 | */ 110 | public function getName() 111 | { 112 | return $this->name; 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /src/Metadata/Object/TableObject.php: -------------------------------------------------------------------------------- 1 | viewDefinition; 24 | } 25 | 26 | /** 27 | * @param string $viewDefinition to set 28 | * @return self Provides a fluent interface 29 | */ 30 | public function setViewDefinition($viewDefinition) 31 | { 32 | $this->viewDefinition = $viewDefinition; 33 | return $this; 34 | } 35 | 36 | /** 37 | * @return string $checkOption 38 | */ 39 | public function getCheckOption() 40 | { 41 | return $this->checkOption; 42 | } 43 | 44 | /** 45 | * @param string $checkOption to set 46 | * @return self Provides a fluent interface 47 | */ 48 | public function setCheckOption($checkOption) 49 | { 50 | $this->checkOption = $checkOption; 51 | return $this; 52 | } 53 | 54 | /** 55 | * @return bool $isUpdatable 56 | */ 57 | public function getIsUpdatable() 58 | { 59 | return $this->isUpdatable; 60 | } 61 | 62 | /** 63 | * @param bool $isUpdatable to set 64 | * @return self Provides a fluent interface 65 | */ 66 | public function setIsUpdatable($isUpdatable) 67 | { 68 | $this->isUpdatable = $isUpdatable; 69 | return $this; 70 | } 71 | 72 | public function isUpdatable() 73 | { 74 | return $this->isUpdatable; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/Metadata/Source/Factory.php: -------------------------------------------------------------------------------- 1 | getPlatform()->getName(); 31 | 32 | switch ($platformName) { 33 | case 'MySQL': 34 | return new MysqlMetadata($adapter); 35 | case 'SQLServer': 36 | return new SqlServerMetadata($adapter); 37 | case 'SQLite': 38 | return new SqliteMetadata($adapter); 39 | case 'PostgreSQL': 40 | return new PostgresqlMetadata($adapter); 41 | case 'Oracle': 42 | return new OracleMetadata($adapter); 43 | default: 44 | throw new InvalidArgumentException("Unknown adapter platform '{$platformName}'"); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/Module.php: -------------------------------------------------------------------------------- 1 | $provider->getDependencyConfig(), 22 | ]; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/ResultSet/Exception/ExceptionInterface.php: -------------------------------------------------------------------------------- 1 | setHydrator($hydrator ?: new $defaultHydratorClass()); 41 | $this->setObjectPrototype(($objectPrototype) ?: new ArrayObject); 42 | } 43 | 44 | /** 45 | * Set the row object prototype 46 | * 47 | * @param object $objectPrototype 48 | * @return self Provides a fluent interface 49 | * @throws Exception\InvalidArgumentException 50 | */ 51 | public function setObjectPrototype($objectPrototype) 52 | { 53 | if (! is_object($objectPrototype)) { 54 | throw new Exception\InvalidArgumentException( 55 | 'An object must be set as the object prototype, a ' . gettype($objectPrototype) . ' was provided.' 56 | ); 57 | } 58 | $this->objectPrototype = $objectPrototype; 59 | return $this; 60 | } 61 | 62 | /** 63 | * Get the row object prototype 64 | * 65 | * @return object 66 | */ 67 | public function getObjectPrototype() 68 | { 69 | return $this->objectPrototype; 70 | } 71 | 72 | /** 73 | * Set the hydrator to use for each row object 74 | * 75 | * @param HydratorInterface $hydrator 76 | * @return self Provides a fluent interface 77 | */ 78 | public function setHydrator(HydratorInterface $hydrator) 79 | { 80 | $this->hydrator = $hydrator; 81 | return $this; 82 | } 83 | 84 | /** 85 | * Get the hydrator to use for each row object 86 | * 87 | * @return HydratorInterface 88 | */ 89 | public function getHydrator() 90 | { 91 | return $this->hydrator; 92 | } 93 | 94 | /** 95 | * Iterator: get current item 96 | * 97 | * @return object 98 | */ 99 | public function current() 100 | { 101 | if ($this->buffer === null) { 102 | $this->buffer = -2; // implicitly disable buffering from here on 103 | } elseif (is_array($this->buffer) && isset($this->buffer[$this->position])) { 104 | return $this->buffer[$this->position]; 105 | } 106 | $data = $this->dataSource->current(); 107 | $object = is_array($data) ? $this->hydrator->hydrate($data, clone $this->objectPrototype) : false; 108 | 109 | if (is_array($this->buffer)) { 110 | $this->buffer[$this->position] = $object; 111 | } 112 | 113 | return $object; 114 | } 115 | 116 | /** 117 | * Cast result set to array of arrays 118 | * 119 | * @return array 120 | * @throws Exception\RuntimeException if any row is not castable to an array 121 | */ 122 | public function toArray() 123 | { 124 | $return = []; 125 | foreach ($this as $row) { 126 | $return[] = $this->hydrator->extract($row); 127 | } 128 | return $return; 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /src/ResultSet/ResultSet.php: -------------------------------------------------------------------------------- 1 | allowedReturnTypes, true)) { 50 | $this->returnType = $returnType; 51 | } else { 52 | $this->returnType = self::TYPE_ARRAYOBJECT; 53 | } 54 | if ($this->returnType === self::TYPE_ARRAYOBJECT) { 55 | $this->setArrayObjectPrototype(($arrayObjectPrototype) ?: new ArrayObject([], ArrayObject::ARRAY_AS_PROPS)); 56 | } 57 | } 58 | 59 | /** 60 | * Set the row object prototype 61 | * 62 | * @param ArrayObject $arrayObjectPrototype 63 | * @return self Provides a fluent interface 64 | * @throws Exception\InvalidArgumentException 65 | */ 66 | public function setArrayObjectPrototype($arrayObjectPrototype) 67 | { 68 | if (! is_object($arrayObjectPrototype) 69 | || ( 70 | ! $arrayObjectPrototype instanceof ArrayObject 71 | && ! method_exists($arrayObjectPrototype, 'exchangeArray') 72 | ) 73 | ) { 74 | throw new Exception\InvalidArgumentException( 75 | 'Object must be of type ArrayObject, or at least implement exchangeArray' 76 | ); 77 | } 78 | $this->arrayObjectPrototype = $arrayObjectPrototype; 79 | return $this; 80 | } 81 | 82 | /** 83 | * Get the row object prototype 84 | * 85 | * @return ArrayObject 86 | */ 87 | public function getArrayObjectPrototype() 88 | { 89 | return $this->arrayObjectPrototype; 90 | } 91 | 92 | /** 93 | * Get the return type to use when returning objects from the set 94 | * 95 | * @return string 96 | */ 97 | public function getReturnType() 98 | { 99 | return $this->returnType; 100 | } 101 | 102 | /** 103 | * @return array|\ArrayObject|null 104 | */ 105 | public function current() 106 | { 107 | $data = parent::current(); 108 | 109 | if ($this->returnType === self::TYPE_ARRAYOBJECT && is_array($data)) { 110 | /** @var $ao ArrayObject */ 111 | $ao = clone $this->arrayObjectPrototype; 112 | if ($ao instanceof ArrayObject || method_exists($ao, 'exchangeArray')) { 113 | $ao->exchangeArray($data); 114 | } 115 | return $ao; 116 | } 117 | 118 | return $data; 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /src/ResultSet/ResultSetInterface.php: -------------------------------------------------------------------------------- 1 | rowGateway = $rowGateway; 41 | } 42 | 43 | /** 44 | * @throws \Zend\Db\RowGateway\Exception\RuntimeException 45 | */ 46 | public function initialize() 47 | { 48 | throw new Exception\RuntimeException('This method is not intended to be called on this object.'); 49 | } 50 | 51 | /** 52 | * @return array 53 | */ 54 | public function getMagicMethodSpecifications() 55 | { 56 | return []; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/RowGateway/Feature/FeatureSet.php: -------------------------------------------------------------------------------- 1 | addFeatures($features); 40 | } 41 | } 42 | 43 | /** 44 | * @param AbstractRowGateway $rowGateway 45 | * @return self Provides a fluent interface 46 | */ 47 | public function setRowGateway(AbstractRowGateway $rowGateway) 48 | { 49 | $this->rowGateway = $rowGateway; 50 | foreach ($this->features as $feature) { 51 | $feature->setRowGateway($this->rowGateway); 52 | } 53 | return $this; 54 | } 55 | 56 | public function getFeatureByClassName($featureClassName) 57 | { 58 | $feature = false; 59 | foreach ($this->features as $potentialFeature) { 60 | if ($potentialFeature instanceof $featureClassName) { 61 | $feature = $potentialFeature; 62 | break; 63 | } 64 | } 65 | return $feature; 66 | } 67 | 68 | /** 69 | * @param array $features 70 | * @return self Provides a fluent interface 71 | */ 72 | public function addFeatures(array $features) 73 | { 74 | foreach ($features as $feature) { 75 | $this->addFeature($feature); 76 | } 77 | return $this; 78 | } 79 | 80 | /** 81 | * @param AbstractFeature $feature 82 | * @return self Provides a fluent interface 83 | */ 84 | public function addFeature(AbstractFeature $feature) 85 | { 86 | $this->features[] = $feature; 87 | $feature->setRowGateway($feature); 88 | return $this; 89 | } 90 | 91 | public function apply($method, $args) 92 | { 93 | foreach ($this->features as $feature) { 94 | if (method_exists($feature, $method)) { 95 | $return = call_user_func_array([$feature, $method], $args); 96 | if ($return === self::APPLY_HALT) { 97 | break; 98 | } 99 | } 100 | } 101 | } 102 | 103 | /** 104 | * @param string $property 105 | * @return bool 106 | */ 107 | public function canCallMagicGet($property) 108 | { 109 | return false; 110 | } 111 | 112 | /** 113 | * @param string $property 114 | * @return mixed 115 | */ 116 | public function callMagicGet($property) 117 | { 118 | $return = null; 119 | return $return; 120 | } 121 | 122 | /** 123 | * @param string $property 124 | * @return bool 125 | */ 126 | public function canCallMagicSet($property) 127 | { 128 | return false; 129 | } 130 | 131 | /** 132 | * @param $property 133 | * @param $value 134 | * @return mixed 135 | */ 136 | public function callMagicSet($property, $value) 137 | { 138 | $return = null; 139 | return $return; 140 | } 141 | 142 | /** 143 | * @param string $method 144 | * @return bool 145 | */ 146 | public function canCallMagicCall($method) 147 | { 148 | return false; 149 | } 150 | 151 | /** 152 | * @param string $method 153 | * @param array $arguments 154 | * @return mixed 155 | */ 156 | public function callMagicCall($method, $arguments) 157 | { 158 | $return = null; 159 | return $return; 160 | } 161 | } 162 | -------------------------------------------------------------------------------- /src/RowGateway/RowGateway.php: -------------------------------------------------------------------------------- 1 | primaryKeyColumn = empty($primaryKeyColumn) ? null : (array) $primaryKeyColumn; 29 | 30 | // set table 31 | $this->table = $table; 32 | 33 | // set Sql object 34 | if ($adapterOrSql instanceof Sql) { 35 | $this->sql = $adapterOrSql; 36 | } elseif ($adapterOrSql instanceof AdapterInterface) { 37 | $this->sql = new Sql($adapterOrSql, $this->table); 38 | } else { 39 | throw new Exception\InvalidArgumentException('A valid Sql object was not provided.'); 40 | } 41 | 42 | if ($this->sql->getTable() !== $this->table) { 43 | throw new Exception\InvalidArgumentException( 44 | 'The Sql object provided does not have a table that matches this row object' 45 | ); 46 | } 47 | 48 | $this->initialize(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/RowGateway/RowGatewayInterface.php: -------------------------------------------------------------------------------- 1 | buildNormalizedArgument($argument, self::TYPE_VALUE); 38 | } 39 | 40 | if (is_scalar($argument) || $argument === null) { 41 | return $this->buildNormalizedArgument($argument, $defaultType); 42 | } 43 | 44 | if (is_array($argument)) { 45 | $value = current($argument); 46 | 47 | if ($value instanceof ExpressionInterface || $value instanceof SqlInterface) { 48 | return $this->buildNormalizedArgument($value, self::TYPE_VALUE); 49 | } 50 | 51 | $key = key($argument); 52 | 53 | if (is_integer($key) && ! in_array($value, $this->allowedTypes)) { 54 | return $this->buildNormalizedArgument($value, $defaultType); 55 | } 56 | 57 | return $this->buildNormalizedArgument($key, $value); 58 | } 59 | 60 | throw new Exception\InvalidArgumentException(sprintf( 61 | '$argument should be %s or %s or %s or %s or %s, "%s" given', 62 | 'null', 63 | 'scalar', 64 | 'array', 65 | 'Zend\Db\Sql\ExpressionInterface', 66 | 'Zend\Db\Sql\SqlInterface', 67 | is_object($argument) ? get_class($argument) : gettype($argument) 68 | )); 69 | } 70 | 71 | /** 72 | * @param mixed $argument 73 | * @param string $argumentType 74 | * 75 | * @return array 76 | * 77 | * @throws Exception\InvalidArgumentException 78 | */ 79 | private function buildNormalizedArgument($argument, $argumentType) 80 | { 81 | if (! in_array($argumentType, $this->allowedTypes)) { 82 | throw new Exception\InvalidArgumentException(sprintf( 83 | 'Argument type should be in array(%s)', 84 | implode(',', $this->allowedTypes) 85 | )); 86 | } 87 | 88 | return [ 89 | $argument, 90 | $argumentType, 91 | ]; 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /src/Sql/AbstractPreparableSql.php: -------------------------------------------------------------------------------- 1 | getParameterContainer(); 26 | 27 | if (! $parameterContainer instanceof ParameterContainer) { 28 | $parameterContainer = new ParameterContainer(); 29 | 30 | $statementContainer->setParameterContainer($parameterContainer); 31 | } 32 | 33 | $statementContainer->setSql( 34 | $this->buildSqlString($adapter->getPlatform(), $adapter->getDriver(), $parameterContainer) 35 | ); 36 | 37 | return $statementContainer; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/Sql/Ddl/Column/AbstractLengthColumn.php: -------------------------------------------------------------------------------- 1 | setLength($length); 27 | 28 | parent::__construct($name, $nullable, $default, $options); 29 | } 30 | 31 | /** 32 | * @param int $length 33 | * @return self Provides a fluent interface 34 | */ 35 | public function setLength($length) 36 | { 37 | $this->length = (int) $length; 38 | 39 | return $this; 40 | } 41 | 42 | /** 43 | * @return int 44 | */ 45 | public function getLength() 46 | { 47 | return $this->length; 48 | } 49 | 50 | /** 51 | * @return string 52 | */ 53 | protected function getLengthExpression() 54 | { 55 | return (string) $this->length; 56 | } 57 | 58 | /** 59 | * @return array 60 | */ 61 | public function getExpressionData() 62 | { 63 | $data = parent::getExpressionData(); 64 | 65 | if ($this->getLengthExpression()) { 66 | $data[0][1][1] .= '(' . $this->getLengthExpression() . ')'; 67 | } 68 | 69 | return $data; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/Sql/Ddl/Column/AbstractPrecisionColumn.php: -------------------------------------------------------------------------------- 1 | setDecimal($decimal); 34 | 35 | parent::__construct($name, $digits, $nullable, $default, $options); 36 | } 37 | 38 | /** 39 | * @param int $digits 40 | * 41 | * @return self 42 | */ 43 | public function setDigits($digits) 44 | { 45 | return $this->setLength($digits); 46 | } 47 | 48 | /** 49 | * @return int 50 | */ 51 | public function getDigits() 52 | { 53 | return $this->getLength(); 54 | } 55 | 56 | /** 57 | * @param int|null $decimal 58 | * @return self Provides a fluent interface 59 | */ 60 | public function setDecimal($decimal) 61 | { 62 | $this->decimal = null === $decimal ? null : (int) $decimal; 63 | 64 | return $this; 65 | } 66 | 67 | /** 68 | * @return int|null 69 | */ 70 | public function getDecimal() 71 | { 72 | return $this->decimal; 73 | } 74 | 75 | /** 76 | * {@inheritDoc} 77 | */ 78 | protected function getLengthExpression() 79 | { 80 | if ($this->decimal !== null) { 81 | return $this->length . ',' . $this->decimal; 82 | } 83 | 84 | return $this->length; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/Sql/Ddl/Column/AbstractTimestampColumn.php: -------------------------------------------------------------------------------- 1 | specification; 25 | 26 | $params = []; 27 | $params[] = $this->name; 28 | $params[] = $this->type; 29 | 30 | $types = [self::TYPE_IDENTIFIER, self::TYPE_LITERAL]; 31 | 32 | if (! $this->isNullable) { 33 | $spec .= ' NOT NULL'; 34 | } 35 | 36 | if ($this->default !== null) { 37 | $spec .= ' DEFAULT %s'; 38 | $params[] = $this->default; 39 | $types[] = self::TYPE_VALUE; 40 | } 41 | 42 | $options = $this->getOptions(); 43 | 44 | if (isset($options['on_update'])) { 45 | $spec .= ' %s'; 46 | $params[] = 'ON UPDATE CURRENT_TIMESTAMP'; 47 | $types[] = self::TYPE_LITERAL; 48 | } 49 | 50 | $data = [[ 51 | $spec, 52 | $params, 53 | $types, 54 | ]]; 55 | 56 | foreach ($this->constraints as $constraint) { 57 | $data[] = ' '; 58 | $data = array_merge($data, $constraint->getExpressionData()); 59 | } 60 | 61 | return $data; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/Sql/Ddl/Column/BigInteger.php: -------------------------------------------------------------------------------- 1 | getOptions(); 21 | 22 | if (isset($options['length'])) { 23 | $data[0][1][1] .= '(' . $options['length'] . ')'; 24 | } 25 | 26 | return $data; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Sql/Ddl/Column/Text.php: -------------------------------------------------------------------------------- 1 | setColumns($columns); 47 | } 48 | 49 | $this->setName($name); 50 | } 51 | 52 | /** 53 | * @param string $name 54 | * @return self Provides a fluent interface 55 | */ 56 | public function setName($name) 57 | { 58 | $this->name = (string) $name; 59 | return $this; 60 | } 61 | 62 | /** 63 | * @return string 64 | */ 65 | public function getName() 66 | { 67 | return $this->name; 68 | } 69 | 70 | /** 71 | * @param null|string|array $columns 72 | * @return self Provides a fluent interface 73 | */ 74 | public function setColumns($columns) 75 | { 76 | $this->columns = (array) $columns; 77 | 78 | return $this; 79 | } 80 | 81 | /** 82 | * @param string $column 83 | * @return self Provides a fluent interface 84 | */ 85 | public function addColumn($column) 86 | { 87 | $this->columns[] = $column; 88 | return $this; 89 | } 90 | 91 | /** 92 | * {@inheritDoc} 93 | */ 94 | public function getColumns() 95 | { 96 | return $this->columns; 97 | } 98 | 99 | /** 100 | * {@inheritDoc} 101 | */ 102 | public function getExpressionData() 103 | { 104 | $colCount = count($this->columns); 105 | $newSpecTypes = []; 106 | $values = []; 107 | $newSpec = ''; 108 | 109 | if ($this->name) { 110 | $newSpec .= $this->namedSpecification; 111 | $values[] = $this->name; 112 | $newSpecTypes[] = self::TYPE_IDENTIFIER; 113 | } 114 | 115 | $newSpec .= $this->specification; 116 | 117 | if ($colCount) { 118 | $values = array_merge($values, $this->columns); 119 | $newSpecParts = array_fill(0, $colCount, '%s'); 120 | $newSpecTypes = array_merge($newSpecTypes, array_fill(0, $colCount, self::TYPE_IDENTIFIER)); 121 | $newSpec .= sprintf($this->columnSpecification, implode(', ', $newSpecParts)); 122 | } 123 | 124 | return [[ 125 | $newSpec, 126 | $values, 127 | $newSpecTypes, 128 | ]]; 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /src/Sql/Ddl/Constraint/Check.php: -------------------------------------------------------------------------------- 1 | expression = $expression; 31 | $this->name = $name; 32 | } 33 | 34 | /** 35 | * {@inheritDoc} 36 | */ 37 | public function getExpressionData() 38 | { 39 | $newSpecTypes = [self::TYPE_LITERAL]; 40 | $values = [$this->expression]; 41 | $newSpec = ''; 42 | 43 | if ($this->name) { 44 | $newSpec .= $this->namedSpecification; 45 | 46 | array_unshift($values, $this->name); 47 | array_unshift($newSpecTypes, self::TYPE_IDENTIFIER); 48 | } 49 | 50 | return [[ 51 | $newSpec . $this->specification, 52 | $values, 53 | $newSpecTypes, 54 | ]]; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/Sql/Ddl/Constraint/ConstraintInterface.php: -------------------------------------------------------------------------------- 1 | 'DROP TABLE %1$s' 25 | ]; 26 | 27 | /** 28 | * @var string 29 | */ 30 | protected $table = ''; 31 | 32 | /** 33 | * @param string|TableIdentifier $table 34 | */ 35 | public function __construct($table = '') 36 | { 37 | $this->table = $table; 38 | } 39 | 40 | protected function processTable(PlatformInterface $adapterPlatform = null) 41 | { 42 | return [$this->resolveTable($this->table, $adapterPlatform)]; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/Sql/Ddl/Index/AbstractIndex.php: -------------------------------------------------------------------------------- 1 | setColumns($columns); 32 | 33 | $this->name = null === $name ? null : (string) $name; 34 | $this->lengths = $lengths; 35 | } 36 | 37 | /** 38 | * 39 | * @return array of array|string should return an array in the format: 40 | * 41 | * array ( 42 | * // a sprintf formatted string 43 | * string $specification, 44 | * 45 | * // the values for the above sprintf formatted string 46 | * array $values, 47 | * 48 | * // an array of equal length of the $values array, with either TYPE_IDENTIFIER or TYPE_VALUE for each value 49 | * array $types, 50 | * ) 51 | * 52 | */ 53 | public function getExpressionData() 54 | { 55 | $colCount = count($this->columns); 56 | $values = []; 57 | $values[] = $this->name ?: ''; 58 | $newSpecTypes = [self::TYPE_IDENTIFIER]; 59 | $newSpecParts = []; 60 | 61 | for ($i = 0; $i < $colCount; $i++) { 62 | $specPart = '%s'; 63 | 64 | if (isset($this->lengths[$i])) { 65 | $specPart .= "({$this->lengths[$i]})"; 66 | } 67 | 68 | $newSpecParts[] = $specPart; 69 | $newSpecTypes[] = self::TYPE_IDENTIFIER; 70 | } 71 | 72 | $newSpec = str_replace('...', implode(', ', $newSpecParts), $this->specification); 73 | 74 | return [[ 75 | $newSpec, 76 | array_merge($values, $this->columns), 77 | $newSpecTypes, 78 | ]]; 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/Sql/Ddl/SqlInterface.php: -------------------------------------------------------------------------------- 1 | 'INSERT IGNORE INTO %1$s (%2$s) VALUES (%3$s)', 17 | self::SPECIFICATION_SELECT => 'INSERT IGNORE INTO %1$s %2$s %3$s', 18 | ]; 19 | } 20 | -------------------------------------------------------------------------------- /src/Sql/Literal.php: -------------------------------------------------------------------------------- 1 | literal = $literal; 25 | } 26 | 27 | /** 28 | * @param string $literal 29 | * @return self Provides a fluent interface 30 | */ 31 | public function setLiteral($literal) 32 | { 33 | $this->literal = $literal; 34 | return $this; 35 | } 36 | 37 | /** 38 | * @return string 39 | */ 40 | public function getLiteral() 41 | { 42 | return $this->literal; 43 | } 44 | 45 | /** 46 | * @return array 47 | */ 48 | public function getExpressionData() 49 | { 50 | return [[ 51 | str_replace('%', '%%', $this->literal), 52 | [], 53 | [] 54 | ]]; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/Sql/Platform/AbstractPlatform.php: -------------------------------------------------------------------------------- 1 | subject = $subject; 37 | 38 | return $this; 39 | } 40 | 41 | /** 42 | * @param string $type 43 | * @param PlatformDecoratorInterface $decorator 44 | * 45 | * @return void 46 | */ 47 | public function setTypeDecorator($type, PlatformDecoratorInterface $decorator) 48 | { 49 | $this->decorators[$type] = $decorator; 50 | } 51 | 52 | /** 53 | * @param PreparableSqlInterface|SqlInterface $subject 54 | * @return PlatformDecoratorInterface|PreparableSqlInterface|SqlInterface 55 | */ 56 | public function getTypeDecorator($subject) 57 | { 58 | foreach ($this->decorators as $type => $decorator) { 59 | if ($subject instanceof $type) { 60 | $decorator->setSubject($subject); 61 | 62 | return $decorator; 63 | } 64 | } 65 | 66 | return $subject; 67 | } 68 | 69 | /** 70 | * @return array|PlatformDecoratorInterface[] 71 | */ 72 | public function getDecorators() 73 | { 74 | return $this->decorators; 75 | } 76 | 77 | /** 78 | * {@inheritDoc} 79 | * 80 | * @throws Exception\RuntimeException 81 | */ 82 | public function prepareStatement(AdapterInterface $adapter, StatementContainerInterface $statementContainer) 83 | { 84 | if (! $this->subject instanceof PreparableSqlInterface) { 85 | throw new Exception\RuntimeException( 86 | 'The subject does not appear to implement Zend\Db\Sql\PreparableSqlInterface, thus calling ' 87 | . 'prepareStatement() has no effect' 88 | ); 89 | } 90 | 91 | $this->getTypeDecorator($this->subject)->prepareStatement($adapter, $statementContainer); 92 | 93 | return $statementContainer; 94 | } 95 | 96 | /** 97 | * {@inheritDoc} 98 | * 99 | * @throws Exception\RuntimeException 100 | */ 101 | public function getSqlString(PlatformInterface $adapterPlatform = null) 102 | { 103 | if (! $this->subject instanceof SqlInterface) { 104 | throw new Exception\RuntimeException( 105 | 'The subject does not appear to implement Zend\Db\Sql\SqlInterface, thus calling ' 106 | . 'prepareStatement() has no effect' 107 | ); 108 | } 109 | 110 | return $this->getTypeDecorator($this->subject)->getSqlString($adapterPlatform); 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /src/Sql/Platform/IbmDb2/IbmDb2.php: -------------------------------------------------------------------------------- 1 | setTypeDecorator('Zend\Db\Sql\Select', ($selectDecorator) ?: new SelectDecorator()); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/Sql/Platform/Mysql/Mysql.php: -------------------------------------------------------------------------------- 1 | setTypeDecorator('Zend\Db\Sql\Select', new SelectDecorator()); 19 | $this->setTypeDecorator('Zend\Db\Sql\Ddl\CreateTable', new Ddl\CreateTableDecorator()); 20 | $this->setTypeDecorator('Zend\Db\Sql\Ddl\AlterTable', new Ddl\AlterTableDecorator()); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/Sql/Platform/Mysql/SelectDecorator.php: -------------------------------------------------------------------------------- 1 | subject = $select; 31 | } 32 | 33 | protected function localizeVariables() 34 | { 35 | parent::localizeVariables(); 36 | if ($this->limit === null && $this->offset !== null) { 37 | $this->specifications[self::LIMIT] = 'LIMIT 18446744073709551615'; 38 | } 39 | } 40 | 41 | protected function processLimit( 42 | PlatformInterface $platform, 43 | DriverInterface $driver = null, 44 | ParameterContainer $parameterContainer = null 45 | ) { 46 | if ($this->limit === null && $this->offset !== null) { 47 | return ['']; 48 | } 49 | if ($this->limit === null) { 50 | return; 51 | } 52 | if ($parameterContainer) { 53 | $paramPrefix = $this->processInfo['paramPrefix']; 54 | $parameterContainer->offsetSet($paramPrefix . 'limit', $this->limit, ParameterContainer::TYPE_INTEGER); 55 | return [$driver->formatParameterName($paramPrefix . 'limit')]; 56 | } 57 | 58 | return [$this->limit]; 59 | } 60 | 61 | protected function processOffset( 62 | PlatformInterface $platform, 63 | DriverInterface $driver = null, 64 | ParameterContainer $parameterContainer = null 65 | ) { 66 | if ($this->offset === null) { 67 | return; 68 | } 69 | if ($parameterContainer) { 70 | $paramPrefix = $this->processInfo['paramPrefix']; 71 | $parameterContainer->offsetSet($paramPrefix . 'offset', $this->offset, ParameterContainer::TYPE_INTEGER); 72 | return [$driver->formatParameterName($paramPrefix . 'offset')]; 73 | } 74 | 75 | return [$this->offset]; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/Sql/Platform/Oracle/Oracle.php: -------------------------------------------------------------------------------- 1 | setTypeDecorator('Zend\Db\Sql\Select', ($selectDecorator) ?: new SelectDecorator()); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/Sql/Platform/PlatformDecoratorInterface.php: -------------------------------------------------------------------------------- 1 | subject = $subject; 30 | return $this; 31 | } 32 | 33 | /** 34 | * @param PlatformInterface $adapterPlatform 35 | * @return array 36 | */ 37 | protected function processTable(PlatformInterface $adapterPlatform = null) 38 | { 39 | $table = ($this->isTemporary ? '#' : '') . ltrim($this->table, '#'); 40 | return [ 41 | '', 42 | $adapterPlatform->quoteIdentifier($table), 43 | ]; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/Sql/Platform/SqlServer/SqlServer.php: -------------------------------------------------------------------------------- 1 | setTypeDecorator('Zend\Db\Sql\Select', ($selectDecorator) ?: new SelectDecorator()); 19 | $this->setTypeDecorator('Zend\Db\Sql\Ddl\CreateTable', new Ddl\CreateTableDecorator()); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/Sql/Platform/Sqlite/SelectDecorator.php: -------------------------------------------------------------------------------- 1 | subject = $select; 34 | 35 | return $this; 36 | } 37 | 38 | /** 39 | * {@inheritDoc} 40 | */ 41 | protected function localizeVariables() 42 | { 43 | parent::localizeVariables(); 44 | $this->specifications[self::COMBINE] = '%1$s %2$s'; 45 | } 46 | 47 | /** 48 | * {@inheritDoc} 49 | */ 50 | protected function processStatementStart( 51 | PlatformInterface $platform, 52 | DriverInterface $driver = null, 53 | ParameterContainer $parameterContainer = null 54 | ) { 55 | return ''; 56 | } 57 | 58 | protected function processLimit( 59 | PlatformInterface $platform, 60 | DriverInterface $driver = null, 61 | ParameterContainer $parameterContainer = null 62 | ) { 63 | if ($this->limit === null && $this->offset !== null) { 64 | return ['']; 65 | } 66 | if ($this->limit === null) { 67 | return; 68 | } 69 | if ($parameterContainer) { 70 | $paramPrefix = $this->processInfo['paramPrefix']; 71 | $parameterContainer->offsetSet($paramPrefix . 'limit', $this->limit, ParameterContainer::TYPE_INTEGER); 72 | return [$driver->formatParameterName('limit')]; 73 | } 74 | 75 | return [$this->limit]; 76 | } 77 | 78 | protected function processOffset( 79 | PlatformInterface $platform, 80 | DriverInterface $driver = null, 81 | ParameterContainer $parameterContainer = null 82 | ) { 83 | if ($this->offset === null) { 84 | return; 85 | } 86 | if ($parameterContainer) { 87 | $paramPrefix = $this->processInfo['paramPrefix']; 88 | $parameterContainer->offsetSet($paramPrefix . 'offset', $this->offset, ParameterContainer::TYPE_INTEGER); 89 | return [$driver->formatParameterName('offset')]; 90 | } 91 | 92 | return [$this->offset]; 93 | } 94 | 95 | /** 96 | * {@inheritDoc} 97 | */ 98 | protected function processStatementEnd( 99 | PlatformInterface $platform, 100 | DriverInterface $driver = null, 101 | ParameterContainer $parameterContainer = null 102 | ) { 103 | return ''; 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /src/Sql/Platform/Sqlite/Sqlite.php: -------------------------------------------------------------------------------- 1 | setTypeDecorator('Zend\Db\Sql\Select', new SelectDecorator()); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Sql/Predicate/Between.php: -------------------------------------------------------------------------------- 1 | setIdentifier($identifier); 32 | } 33 | if ($minValue !== null) { 34 | $this->setMinValue($minValue); 35 | } 36 | if ($maxValue !== null) { 37 | $this->setMaxValue($maxValue); 38 | } 39 | } 40 | 41 | /** 42 | * Set identifier for comparison 43 | * 44 | * @param string $identifier 45 | * @return self Provides a fluent interface 46 | */ 47 | public function setIdentifier($identifier) 48 | { 49 | $this->identifier = $identifier; 50 | return $this; 51 | } 52 | 53 | /** 54 | * Get identifier of comparison 55 | * 56 | * @return null|string 57 | */ 58 | public function getIdentifier() 59 | { 60 | return $this->identifier; 61 | } 62 | 63 | /** 64 | * Set minimum boundary for comparison 65 | * 66 | * @param int|float|string $minValue 67 | * @return self Provides a fluent interface 68 | */ 69 | public function setMinValue($minValue) 70 | { 71 | $this->minValue = $minValue; 72 | return $this; 73 | } 74 | 75 | /** 76 | * Get minimum boundary for comparison 77 | * 78 | * @return null|int|float|string 79 | */ 80 | public function getMinValue() 81 | { 82 | return $this->minValue; 83 | } 84 | 85 | /** 86 | * Set maximum boundary for comparison 87 | * 88 | * @param int|float|string $maxValue 89 | * @return self Provides a fluent interface 90 | */ 91 | public function setMaxValue($maxValue) 92 | { 93 | $this->maxValue = $maxValue; 94 | return $this; 95 | } 96 | 97 | /** 98 | * Get maximum boundary for comparison 99 | * 100 | * @return null|int|float|string 101 | */ 102 | public function getMaxValue() 103 | { 104 | return $this->maxValue; 105 | } 106 | 107 | /** 108 | * Set specification string to use in forming SQL predicate 109 | * 110 | * @param string $specification 111 | * @return self Provides a fluent interface 112 | */ 113 | public function setSpecification($specification) 114 | { 115 | $this->specification = $specification; 116 | return $this; 117 | } 118 | 119 | /** 120 | * Get specification string to use in forming SQL predicate 121 | * 122 | * @return string 123 | */ 124 | public function getSpecification() 125 | { 126 | return $this->specification; 127 | } 128 | 129 | /** 130 | * Return "where" parts 131 | * 132 | * @return array 133 | */ 134 | public function getExpressionData() 135 | { 136 | list($values[], $types[]) = $this->normalizeArgument($this->identifier, self::TYPE_IDENTIFIER); 137 | list($values[], $types[]) = $this->normalizeArgument($this->minValue, self::TYPE_VALUE); 138 | list($values[], $types[]) = $this->normalizeArgument($this->maxValue, self::TYPE_VALUE); 139 | return [ 140 | [ 141 | $this->getSpecification(), 142 | $values, 143 | $types, 144 | ], 145 | ]; 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /src/Sql/Predicate/Expression.php: -------------------------------------------------------------------------------- 1 | setExpression($expression); 26 | } 27 | 28 | $this->setParameters(is_array($valueParameter) ? $valueParameter : array_slice(func_get_args(), 1)); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/Sql/Predicate/IsNotNull.php: -------------------------------------------------------------------------------- 1 | setIdentifier($identifier); 35 | } 36 | } 37 | 38 | /** 39 | * Set identifier for comparison 40 | * 41 | * @param string $identifier 42 | * @return self Provides a fluent interface 43 | */ 44 | public function setIdentifier($identifier) 45 | { 46 | $this->identifier = $identifier; 47 | return $this; 48 | } 49 | 50 | /** 51 | * Get identifier of comparison 52 | * 53 | * @return null|string 54 | */ 55 | public function getIdentifier() 56 | { 57 | return $this->identifier; 58 | } 59 | 60 | /** 61 | * Set specification string to use in forming SQL predicate 62 | * 63 | * @param string $specification 64 | * @return self Provides a fluent interface 65 | */ 66 | public function setSpecification($specification) 67 | { 68 | $this->specification = $specification; 69 | return $this; 70 | } 71 | 72 | /** 73 | * Get specification string to use in forming SQL predicate 74 | * 75 | * @return string 76 | */ 77 | public function getSpecification() 78 | { 79 | return $this->specification; 80 | } 81 | 82 | /** 83 | * Get parts for where statement 84 | * 85 | * @return array 86 | */ 87 | public function getExpressionData() 88 | { 89 | $identifier = $this->normalizeArgument($this->identifier, self::TYPE_IDENTIFIER); 90 | return [[ 91 | $this->getSpecification(), 92 | [$identifier[0]], 93 | [$identifier[1]], 94 | ]]; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/Sql/Predicate/Like.php: -------------------------------------------------------------------------------- 1 | setIdentifier($identifier); 39 | } 40 | if ($like) { 41 | $this->setLike($like); 42 | } 43 | } 44 | 45 | /** 46 | * @param string $identifier 47 | * @return self Provides a fluent interface 48 | */ 49 | public function setIdentifier($identifier) 50 | { 51 | $this->identifier = $identifier; 52 | return $this; 53 | } 54 | 55 | /** 56 | * @return string 57 | */ 58 | public function getIdentifier() 59 | { 60 | return $this->identifier; 61 | } 62 | 63 | /** 64 | * @param string $like 65 | * @return self Provides a fluent interface 66 | */ 67 | public function setLike($like) 68 | { 69 | $this->like = $like; 70 | return $this; 71 | } 72 | 73 | /** 74 | * @return string 75 | */ 76 | public function getLike() 77 | { 78 | return $this->like; 79 | } 80 | 81 | /** 82 | * @param string $specification 83 | * @return self Provides a fluent interface 84 | */ 85 | public function setSpecification($specification) 86 | { 87 | $this->specification = $specification; 88 | return $this; 89 | } 90 | 91 | /** 92 | * @return string 93 | */ 94 | public function getSpecification() 95 | { 96 | return $this->specification; 97 | } 98 | 99 | /** 100 | * @return array 101 | */ 102 | public function getExpressionData() 103 | { 104 | list($values[], $types[]) = $this->normalizeArgument($this->identifier, self::TYPE_IDENTIFIER); 105 | list($values[], $types[]) = $this->normalizeArgument($this->like, self::TYPE_VALUE); 106 | return [ 107 | [ 108 | $this->specification, 109 | $values, 110 | $types, 111 | ] 112 | ]; 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /src/Sql/Predicate/Literal.php: -------------------------------------------------------------------------------- 1 | table = (string) $table; 40 | 41 | if ('' === $this->table) { 42 | throw new Exception\InvalidArgumentException('$table must be a valid table name, empty string given'); 43 | } 44 | 45 | if (null === $schema) { 46 | $this->schema = null; 47 | } else { 48 | if (! (is_string($schema) || is_callable([$schema, '__toString']))) { 49 | throw new Exception\InvalidArgumentException(sprintf( 50 | '$schema must be a valid schema name, parameter of type %s given', 51 | is_object($schema) ? get_class($schema) : gettype($schema) 52 | )); 53 | } 54 | 55 | $this->schema = (string) $schema; 56 | 57 | if ('' === $this->schema) { 58 | throw new Exception\InvalidArgumentException( 59 | '$schema must be a valid schema name or null, empty string given' 60 | ); 61 | } 62 | } 63 | } 64 | 65 | /** 66 | * @param string $table 67 | * 68 | * @deprecated please use the constructor and build a new {@see TableIdentifier} instead 69 | */ 70 | public function setTable($table) 71 | { 72 | $this->table = $table; 73 | } 74 | 75 | /** 76 | * @return string 77 | */ 78 | public function getTable() 79 | { 80 | return $this->table; 81 | } 82 | 83 | /** 84 | * @return bool 85 | */ 86 | public function hasSchema() 87 | { 88 | return ($this->schema !== null); 89 | } 90 | 91 | /** 92 | * @param $schema 93 | * 94 | * @deprecated please use the constructor and build a new {@see TableIdentifier} instead 95 | */ 96 | public function setSchema($schema) 97 | { 98 | $this->schema = $schema; 99 | } 100 | 101 | /** 102 | * @return null|string 103 | */ 104 | public function getSchema() 105 | { 106 | return $this->schema; 107 | } 108 | 109 | public function getTableAndSchema() 110 | { 111 | return [$this->table, $this->schema]; 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /src/Sql/Where.php: -------------------------------------------------------------------------------- 1 | tableGateway = $tableGateway; 32 | } 33 | 34 | public function initialize() 35 | { 36 | throw new Exception\RuntimeException('This method is not intended to be called on this object.'); 37 | } 38 | 39 | public function getMagicMethodSpecifications() 40 | { 41 | return []; 42 | } 43 | 44 | 45 | /* 46 | public function preInitialize(); 47 | public function postInitialize(); 48 | public function preSelect(Select $select); 49 | public function postSelect(StatementInterface $statement, ResultInterface $result, ResultSetInterface $resultSet); 50 | public function preInsert(Insert $insert); 51 | public function postInsert(StatementInterface $statement, ResultInterface $result); 52 | public function preUpdate(Update $update); 53 | public function postUpdate(StatementInterface $statement, ResultInterface $result); 54 | public function preDelete(Delete $delete); 55 | public function postDelete(StatementInterface $statement, ResultInterface $result); 56 | */ 57 | } 58 | -------------------------------------------------------------------------------- /src/TableGateway/Feature/EventFeature/TableGatewayEvent.php: -------------------------------------------------------------------------------- 1 | name; 40 | } 41 | 42 | /** 43 | * Get target/context from which event was triggered 44 | * 45 | * @return null|string|object 46 | */ 47 | public function getTarget() 48 | { 49 | return $this->target; 50 | } 51 | 52 | /** 53 | * Get parameters passed to the event 54 | * 55 | * @return array|\ArrayAccess 56 | */ 57 | public function getParams() 58 | { 59 | return $this->params; 60 | } 61 | 62 | /** 63 | * Get a single parameter by name 64 | * 65 | * @param string $name 66 | * @param mixed $default Default value to return if parameter does not exist 67 | * @return mixed 68 | */ 69 | public function getParam($name, $default = null) 70 | { 71 | return (isset($this->params[$name]) ? $this->params[$name] : $default); 72 | } 73 | 74 | /** 75 | * Set the event name 76 | * 77 | * @param string $name 78 | * @return void 79 | */ 80 | public function setName($name) 81 | { 82 | $this->name = $name; 83 | } 84 | 85 | /** 86 | * Set the event target/context 87 | * 88 | * @param null|string|object $target 89 | * @return void 90 | */ 91 | public function setTarget($target) 92 | { 93 | $this->target = $target; 94 | } 95 | 96 | /** 97 | * Set event parameters 98 | * 99 | * @param string $params 100 | * @return void 101 | */ 102 | public function setParams($params) 103 | { 104 | $this->params = $params; 105 | } 106 | 107 | /** 108 | * Set a single parameter by key 109 | * 110 | * @param string $name 111 | * @param mixed $value 112 | * @return void 113 | */ 114 | public function setParam($name, $value) 115 | { 116 | $this->params[$name] = $value; 117 | } 118 | 119 | /** 120 | * Indicate whether or not the parent EventManagerInterface should stop propagating events 121 | * 122 | * @param bool $flag 123 | * @return void 124 | */ 125 | public function stopPropagation($flag = true) 126 | { 127 | return; 128 | } 129 | 130 | /** 131 | * Has this event indicated event propagation should stop? 132 | * 133 | * @return bool 134 | */ 135 | public function propagationIsStopped() 136 | { 137 | return false; 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /src/TableGateway/Feature/EventFeatureEventsInterface.php: -------------------------------------------------------------------------------- 1 | tableGateway->adapter = self::getStaticAdapter(); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/TableGateway/Feature/MasterSlaveFeature.php: -------------------------------------------------------------------------------- 1 | slaveAdapter = $slaveAdapter; 41 | if ($slaveSql) { 42 | $this->slaveSql = $slaveSql; 43 | } 44 | } 45 | 46 | public function getSlaveAdapter() 47 | { 48 | return $this->slaveAdapter; 49 | } 50 | 51 | /** 52 | * @return Sql 53 | */ 54 | public function getSlaveSql() 55 | { 56 | return $this->slaveSql; 57 | } 58 | 59 | /** 60 | * after initialization, retrieve the original adapter as "master" 61 | */ 62 | public function postInitialize() 63 | { 64 | $this->masterSql = $this->tableGateway->sql; 65 | if ($this->slaveSql === null) { 66 | $this->slaveSql = new Sql( 67 | $this->slaveAdapter, 68 | $this->tableGateway->sql->getTable(), 69 | $this->tableGateway->sql->getSqlPlatform() 70 | ); 71 | } 72 | } 73 | 74 | /** 75 | * preSelect() 76 | * Replace adapter with slave temporarily 77 | */ 78 | public function preSelect() 79 | { 80 | $this->tableGateway->sql = $this->slaveSql; 81 | } 82 | 83 | /** 84 | * postSelect() 85 | * Ensure to return to the master adapter 86 | */ 87 | public function postSelect() 88 | { 89 | $this->tableGateway->sql = $this->masterSql; 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/TableGateway/Feature/MetadataFeature.php: -------------------------------------------------------------------------------- 1 | metadata = $metadata; 34 | } 35 | $this->sharedData['metadata'] = [ 36 | 'primaryKey' => null, 37 | 'columns' => [] 38 | ]; 39 | } 40 | 41 | public function postInitialize() 42 | { 43 | if ($this->metadata === null) { 44 | $this->metadata = SourceFactory::createSourceFromAdapter($this->tableGateway->adapter); 45 | } 46 | 47 | // localize variable for brevity 48 | $t = $this->tableGateway; 49 | $m = $this->metadata; 50 | 51 | $tableGatewayTable = is_array($t->table) ? current($t->table) : $t->table; 52 | 53 | if ($tableGatewayTable instanceof TableIdentifier) { 54 | $table = $tableGatewayTable->getTable(); 55 | $schema = $tableGatewayTable->getSchema(); 56 | } else { 57 | $table = $tableGatewayTable; 58 | $schema = null; 59 | } 60 | 61 | // get column named 62 | $columns = $m->getColumnNames($table, $schema); 63 | $t->columns = $columns; 64 | 65 | // set locally 66 | $this->sharedData['metadata']['columns'] = $columns; 67 | 68 | // process primary key only if table is a table; there are no PK constraints on views 69 | if (! ($m->getTable($table, $schema) instanceof TableObject)) { 70 | return; 71 | } 72 | 73 | $pkc = null; 74 | 75 | foreach ($m->getConstraints($table, $schema) as $constraint) { 76 | /** @var $constraint \Zend\Db\Metadata\Object\ConstraintObject */ 77 | if ($constraint->getType() == 'PRIMARY KEY') { 78 | $pkc = $constraint; 79 | break; 80 | } 81 | } 82 | 83 | if ($pkc === null) { 84 | throw new Exception\RuntimeException('A primary key for this column could not be found in the metadata.'); 85 | } 86 | 87 | $pkcColumns = $pkc->getColumns(); 88 | if (count($pkcColumns) === 1) { 89 | $primaryKey = $pkcColumns[0]; 90 | } else { 91 | $primaryKey = $pkcColumns; 92 | } 93 | 94 | $this->sharedData['metadata']['primaryKey'] = $primaryKey; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/TableGateway/Feature/RowGatewayFeature.php: -------------------------------------------------------------------------------- 1 | constructorArguments = func_get_args(); 30 | } 31 | 32 | public function postInitialize() 33 | { 34 | $args = $this->constructorArguments; 35 | 36 | /** @var $resultSetPrototype ResultSet */ 37 | $resultSetPrototype = $this->tableGateway->resultSetPrototype; 38 | 39 | if (! $this->tableGateway->resultSetPrototype instanceof ResultSet) { 40 | throw new Exception\RuntimeException( 41 | 'This feature ' . __CLASS__ . ' expects the ResultSet to be an instance of Zend\Db\ResultSet\ResultSet' 42 | ); 43 | } 44 | 45 | if (isset($args[0])) { 46 | if (is_string($args[0])) { 47 | $primaryKey = $args[0]; 48 | $rowGatewayPrototype = new RowGateway( 49 | $primaryKey, 50 | $this->tableGateway->table, 51 | $this->tableGateway->adapter 52 | ); 53 | $resultSetPrototype->setArrayObjectPrototype($rowGatewayPrototype); 54 | } elseif ($args[0] instanceof RowGatewayInterface) { 55 | $rowGatewayPrototype = $args[0]; 56 | $resultSetPrototype->setArrayObjectPrototype($rowGatewayPrototype); 57 | } 58 | } else { 59 | // get from metadata feature 60 | $metadata = $this->tableGateway->featureSet->getFeatureByClassName( 61 | 'Zend\Db\TableGateway\Feature\MetadataFeature' 62 | ); 63 | if ($metadata === false || ! isset($metadata->sharedData['metadata'])) { 64 | throw new Exception\RuntimeException( 65 | 'No information was provided to the RowGatewayFeature and/or no MetadataFeature could be consulted ' 66 | . 'to find the primary key necessary for RowGateway object creation.' 67 | ); 68 | } 69 | $primaryKey = $metadata->sharedData['metadata']['primaryKey']; 70 | $rowGatewayPrototype = new RowGateway( 71 | $primaryKey, 72 | $this->tableGateway->table, 73 | $this->tableGateway->adapter 74 | ); 75 | $resultSetPrototype->setArrayObjectPrototype($rowGatewayPrototype); 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/TableGateway/TableGateway.php: -------------------------------------------------------------------------------- 1 | table = $table; 45 | 46 | // adapter 47 | $this->adapter = $adapter; 48 | 49 | // process features 50 | if ($features !== null) { 51 | if ($features instanceof Feature\AbstractFeature) { 52 | $features = [$features]; 53 | } 54 | if (is_array($features)) { 55 | $this->featureSet = new Feature\FeatureSet($features); 56 | } elseif ($features instanceof Feature\FeatureSet) { 57 | $this->featureSet = $features; 58 | } else { 59 | throw new Exception\InvalidArgumentException( 60 | 'TableGateway expects $feature to be an instance of an AbstractFeature or a FeatureSet, or an ' 61 | . 'array of AbstractFeatures' 62 | ); 63 | } 64 | } else { 65 | $this->featureSet = new Feature\FeatureSet(); 66 | } 67 | 68 | // result prototype 69 | $this->resultSetPrototype = ($resultSetPrototype) ?: new ResultSet; 70 | 71 | // Sql object (factory for select, insert, update, delete) 72 | $this->sql = ($sql) ?: new Sql($this->adapter, $this->table); 73 | 74 | // check sql object bound to same table 75 | if ($this->sql->getTable() != $this->table) { 76 | throw new Exception\InvalidArgumentException( 77 | 'The table inside the provided Sql object must match the table of this TableGateway' 78 | ); 79 | } 80 | 81 | $this->initialize(); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/TableGateway/TableGatewayInterface.php: -------------------------------------------------------------------------------- 1 |