function testConfigure() { $config =& new Piece_Unity_Config(); $config->setConfiguration('Configurator_PieceORM', 'configDirectory', $this->_cacheDirectory); $config->setConfiguration('Configurator_PieceORM', 'cacheDirectory', $this->_cacheDirectory); $config->setConfiguration('Configurator_PieceORM', 'mapperConfigDirectory', $this->_cacheDirectory); $context =& Piece_Unity_Context::singleton(); $context->setConfiguration($config); $configurator =& new Piece_Unity_Plugin_Configurator_PieceORM(); $configurator->invoke(); $yaml = Spyc::YAMLLoad("{$this->_cacheDirectory}/piece-orm-config.yaml"); $this->assertTrue(count($yaml)); $context =& Piece_ORM_Context::singleton(); $config =& $context->getConfiguration(); foreach ($yaml as $configuration) { $this->assertEquals($configuration['dsn'], $config->getDSN($configuration['name'])); $this->assertEquals($configuration['options'], $config->getOptions($configuration['name'])); } $this->assertEquals($this->_cacheDirectory, $GLOBALS['PIECE_ORM_Mapper_ConfigDirectory']); $this->assertEquals($this->_cacheDirectory, $GLOBALS['PIECE_ORM_Mapper_CacheDirectory']); $this->assertEquals($this->_cacheDirectory, $GLOBALS['PIECE_ORM_Metadata_CacheDirectory']); }
/** * Creates a mapper object for a given mapper name. * * @param string $mapperName * @return Piece_ORM_Mapper_Common * @throws PIECE_ORM_ERROR_INVALID_MAPPER */ function &factory($mapperName) { $context =& Piece_ORM_Context::singleton(); if (!$context->getUseMapperNameAsTableName()) { $mapperName = Piece_ORM_Inflector::camelize($mapperName); } $mapperID = sha1($context->getDSN() . ".{$mapperName}." . realpath($GLOBALS['PIECE_ORM_Mapper_ConfigDirectory'])); if (!array_key_exists($mapperID, $GLOBALS['PIECE_ORM_Mapper_Instances'])) { Piece_ORM_Mapper_Factory::_load($mapperID, $mapperName); if (Piece_ORM_Error::hasErrors()) { $return = null; return $return; } $metadata =& Piece_ORM_Metadata_Factory::factory($mapperName); if (Piece_ORM_Error::hasErrors()) { $return = null; return $return; } $mapperClass = Piece_ORM_Mapper_Factory::_getMapperClass($mapperID); $mapper =& new $mapperClass($metadata); if (!is_subclass_of($mapper, 'Piece_ORM_Mapper_Common')) { Piece_ORM_Error::push(PIECE_ORM_ERROR_INVALID_MAPPER, "The mapper class for [ {$mapperName} ] is invalid."); $return = null; return $return; } $GLOBALS['PIECE_ORM_Mapper_Instances'][$mapperID] =& $mapper; } $dbh =& $context->getConnection(); if (Piece_ORM_Error::hasErrors()) { $return = null; return $return; } $GLOBALS['PIECE_ORM_Mapper_Instances'][$mapperID]->setConnection($dbh); return $GLOBALS['PIECE_ORM_Mapper_Instances'][$mapperID]; }
/** * Sets a database as the current database. * * @param string $database * @throws PIECE_ORM_ERROR_INVALID_OPERATION */ function setDatabase($database) { if (!$GLOBALS['PIECE_ORM_Configured']) { Piece_ORM_Error::push(PIECE_ORM_ERROR_INVALID_OPERATION, __FUNCTION__ . ' method must be called after calling configure().'); return; } $context =& Piece_ORM_Context::singleton(); $context->setDatabase($database); }
/** * Creates a Piece_ORM_Metadata object from a database. * * @param string $tableName * @return Piece_ORM_Metadata * @throws PIECE_ORM_ERROR_CANNOT_INVOKE * @throws PIECE_ORM_ERROR_NOT_FOUND */ function &_createMetadataFromDatabase($tableName) { $context =& Piece_ORM_Context::singleton(); $dbh =& $context->getConnection(); if (Piece_ORM_Error::hasErrors()) { $return = null; return $return; } PEAR::staticPushErrorHandling(PEAR_ERROR_RETURN); $result = $dbh->setLimit(1); PEAR::staticPopErrorHandling(); if (MDB2::isError($result)) { Piece_ORM_Error::pushPEARError($result, PIECE_ORM_ERROR_CANNOT_INVOKE, "Failed to invoke MDB2_Driver_{$dbh->phptype}::setLimit() for any reasons."); $return = null; return $return; } PEAR::staticPushErrorHandling(PEAR_ERROR_RETURN); $result = $dbh->query('SELECT 1 FROM ' . $dbh->quoteIdentifier($tableName)); PEAR::staticPopErrorHandling(); if (MDB2::isError($result)) { if ($result->getCode() != MDB2_ERROR_NOSUCHTABLE) { Piece_ORM_Error::pushPEARError($result, PIECE_ORM_ERROR_CANNOT_INVOKE, "Failed to invoke MDB2_Driver_{$dbh->phptype}::query() for any reasons."); $return = null; return $return; } Piece_ORM_Error::pushPEARError($result, PIECE_ORM_ERROR_NOT_FOUND, "Failed to invoke MDB2_Driver_{$dbh->phptype}::query() for any reasons."); $return = null; return $return; } PEAR::staticPushErrorHandling(PEAR_ERROR_RETURN); $reverse =& $dbh->loadModule('Reverse'); PEAR::staticPopErrorHandling(); if (MDB2::isError($reverse)) { Piece_ORM_Error::pushPEARError($reverse, PIECE_ORM_ERROR_CANNOT_INVOKE, 'Failed to invoke $dbh->loadModule() for any reasons.'); $return = null; return $return; } if ($dbh->phptype == 'mssql') { include_once 'Piece/ORM/MDB2/Decorator/Reverse/Mssql.php'; $reverse =& new Piece_ORM_MDB2_Decorator_Reverse_Mssql($reverse); } PEAR::staticPushErrorHandling(PEAR_ERROR_RETURN); $tableInfo = $reverse->tableInfo($tableName); PEAR::staticPopErrorHandling(); if (MDB2::isError($tableInfo)) { Piece_ORM_Error::pushPEARError($tableInfo, PIECE_ORM_ERROR_CANNOT_INVOKE, 'Failed to invoke $reverse->tableInfo() for any reasons.'); $return = null; return $return; } if ($dbh->phptype == 'mysql') { foreach (array_keys($tableInfo) as $fieldName) { if ($tableInfo[$fieldName]['nativetype'] == 'datetime' && $tableInfo[$fieldName]['notnull'] && $tableInfo[$fieldName]['default'] == '0000-00-00 00:00:00') { $tableInfo[$fieldName]['flags'] = str_replace('default_0000-00-00%2000%3A00%3A00', '', $tableInfo[$fieldName]['flags']); $tableInfo[$fieldName]['default'] = ''; } } } $metadata =& new Piece_ORM_Metadata($tableInfo); return $metadata; }
/** * Gets the table name. * * @param boolean $notQuoteIdentifier * @return string */ function getTableName($notQuoteIdentifier = false) { $context =& Piece_ORM_Context::singleton(); if (!$context->getUseMapperNameAsTableName() || $notQuoteIdentifier) { return $this->_tableName; } else { $dbh =& $context->getConnection(); return $dbh->quoteIdentifier($this->_tableName); } }