CODE STYLE class.pmTable.php

This commit is contained in:
Fernando Ontiveros
2012-10-09 13:00:37 -04:00
parent edf40d2490
commit adf671539a

View File

@@ -2,7 +2,8 @@
/** /**
* class.case.php * class.case.php
* @package workflow.engine.classes *
* @package workflow.engine.classes
* *
* ProcessMaker Open Source Edition * ProcessMaker Open Source Edition
* Copyright (C) 2004 - 2008 Colosa Inc.23 * Copyright (C) 2004 - 2008 Colosa Inc.23
@@ -14,11 +15,11 @@
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details. * GNU Affero General Public License for more details.
* *
* You should have received a copy of the GNU Affero General Public License * You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
* *
* For more information, contact Colosa Inc, 2566 Le Jeune Rd., * For more information, contact Colosa Inc, 2566 Le Jeune Rd.,
* Coral Gables, FL, 33134, USA, or email info@colosa.com. * Coral Gables, FL, 33134, USA, or email info@colosa.com.
@@ -29,6 +30,7 @@ require_once 'classes/model/AdditionalTables.php';
/** /**
* PmTable Class * PmTable Class
* New class to handle pmTable in native form invoking to Phing & Propel * New class to handle pmTable in native form invoking to Phing & Propel
*
* @author Erik Amaru Ortiz <erik@colosa.com> * @author Erik Amaru Ortiz <erik@colosa.com>
*/ */
class PmTable class PmTable
@@ -50,11 +52,11 @@ class PmTable
private $db; private $db;
private $alterTable = true; private $alterTable = true;
public function __construct($tableName=null) public function __construct ($tableName = null)
{ {
if (isset($tableName)) { if (isset( $tableName )) {
$this->tableName = $tableName; $this->tableName = $tableName;
$this->className = $this->toCamelCase($tableName); $this->className = $this->toCamelCase( $tableName );
} }
$this->dbConfig = new StdClass(); $this->dbConfig = new StdClass();
@@ -62,21 +64,23 @@ class PmTable
/** /**
* Set columns to pmTable * Set columns to pmTable
*
* @param array $columns contains a array of abjects * @param array $columns contains a array of abjects
* array(StdClass->field_name, field_type, field_size, field_null, field_key, field_autoincrement,...) * array(StdClass->field_name, field_type, field_size, field_null, field_key, field_autoincrement,...)
*/ */
public function setColumns($columns) public function setColumns ($columns)
{ {
$this->columns = $columns; $this->columns = $columns;
} }
/** /**
* Set a data source * Set a data source
*
* @param string $dbsUid DBS_UID to relate the pmTable to phisical table * @param string $dbsUid DBS_UID to relate the pmTable to phisical table
*/ */
public function setDataSource($dbsUid) public function setDataSource ($dbsUid)
{ {
$this->dataSource = self::resolveDbSource($dbsUid); $this->dataSource = self::resolveDbSource( $dbsUid );
switch ($this->dataSource) { switch ($this->dataSource) {
case 'workflow': case 'workflow':
@@ -98,10 +102,10 @@ class PmTable
default: default:
require_once 'classes/model/DbSource.php'; require_once 'classes/model/DbSource.php';
$oDBSource = new DbSource(); $oDBSource = new DbSource();
$proUid = $oDBSource->getValProUid($this->dataSource); $proUid = $oDBSource->getValProUid( $this->dataSource );
$dbSource = $oDBSource->load($this->dataSource, $proUid); $dbSource = $oDBSource->load( $this->dataSource, $proUid );
if (is_object($dbSource)) { if (is_object( $dbSource )) {
$this->dbConfig->adapter = $dbSource->getDbsType(); $this->dbConfig->adapter = $dbSource->getDbsType();
$this->dbConfig->host = $dbSource->getDbsServer(); $this->dbConfig->host = $dbSource->getDbsServer();
$this->dbConfig->name = $dbSource->getDbsDatabaseName(); $this->dbConfig->name = $dbSource->getDbsDatabaseName();
@@ -109,7 +113,7 @@ class PmTable
$this->dbConfig->passwd = $dbSource->getDbsPassword(); $this->dbConfig->passwd = $dbSource->getDbsPassword();
$this->dbConfig->port = $dbSource->getDbsPort(); $this->dbConfig->port = $dbSource->getDbsPort();
} }
if (is_array($dbSource)) { if (is_array( $dbSource )) {
$this->dbConfig->adapter = $dbSource['DBS_TYPE']; $this->dbConfig->adapter = $dbSource['DBS_TYPE'];
$this->dbConfig->host = $dbSource['DBS_SERVER']; $this->dbConfig->host = $dbSource['DBS_SERVER'];
$this->dbConfig->name = $dbSource['DBS_DATABASE_NAME']; $this->dbConfig->name = $dbSource['DBS_DATABASE_NAME'];
@@ -117,7 +121,7 @@ class PmTable
$this->dbConfig->passwd = $dbSource['DBS_PASSWORD']; $this->dbConfig->passwd = $dbSource['DBS_PASSWORD'];
$this->dbConfig->port = $dbSource['DBS_PORT']; $this->dbConfig->port = $dbSource['DBS_PORT'];
} else { } else {
throw new Exception("Db source with id $dbsUid does not exist!"); throw new Exception( "Db source with id $dbsUid does not exist!" );
} }
} }
} }
@@ -125,10 +129,11 @@ class PmTable
/** /**
* Backward compatibility function * Backward compatibility function
* Resolve a propel data source * Resolve a propel data source
*
* @param string $dbsUid corresponding to DBS_UID key * @param string $dbsUid corresponding to DBS_UID key
* @return string contains resolved DBS_UID * @return string contains resolved DBS_UID
*/ */
public function resolveDbSource($dbsUid) public function resolveDbSource ($dbsUid)
{ {
switch ($dbsUid) { switch ($dbsUid) {
case 'workflow': case 'workflow':
@@ -147,21 +152,22 @@ class PmTable
return $dbsUid; return $dbsUid;
} }
public function getDataSource() public function getDataSource ()
{ {
return $this->dataSource; return $this->dataSource;
} }
/** /**
* get Data base config object * get Data base config object
*
* @return object containing dbConfig var * @return object containing dbConfig var
*/ */
public function getDbConfig() public function getDbConfig ()
{ {
return $this->dbConfig; return $this->dbConfig;
} }
public function setAlterTable($value) public function setAlterTable ($value)
{ {
$this->alterTable = $value; $this->alterTable = $value;
} }
@@ -169,7 +175,7 @@ class PmTable
/** /**
* Build the pmTable with all dependencies * Build the pmTable with all dependencies
*/ */
public function build() public function build ()
{ {
$this->prepare(); $this->prepare();
$this->preparePropelIniFile(); $this->preparePropelIniFile();
@@ -182,11 +188,11 @@ class PmTable
} }
} }
public function buildModelFor($dbsUid, $tablesList) public function buildModelFor ($dbsUid, $tablesList)
{ {
$this->setDataSource($dbsUid); $this->setDataSource( $dbsUid );
$loadSchema = false; $loadSchema = false;
$this->prepare($loadSchema); $this->prepare( $loadSchema );
$this->phingbuildModel(); $this->phingbuildModel();
$this->phingbuildSql(); $this->phingbuildSql();
//$this->upgradeDatabaseFor($this->dataSource, $tablesList); //$this->upgradeDatabaseFor($this->dataSource, $tablesList);
@@ -195,10 +201,10 @@ class PmTable
/** /**
* Prepare the pmTable env * Prepare the pmTable env
*/ */
public function prepare($loadSchema=true) public function prepare ($loadSchema = true)
{ {
//prevent execute prepare() twice or more //prevent execute prepare() twice or more
if (is_object($this->dom)) { if (is_object( $this->dom )) {
return true; return true;
} }
@@ -210,94 +216,93 @@ class PmTable
$this->classesDir = $this->baseDir . 'classes' . PATH_SEP; $this->classesDir = $this->baseDir . 'classes' . PATH_SEP;
// G::mk_dir create the requested dir and the parents directories if not exists // G::mk_dir create the requested dir and the parents directories if not exists
G::mk_dir($this->configDir); G::mk_dir( $this->configDir );
G::mk_dir($this->dataDir); G::mk_dir( $this->dataDir );
if ($loadSchema) { if ($loadSchema) {
$this->loadSchema(); $this->loadSchema();
} }
} }
public function loadSchema() public function loadSchema ()
{ {
$this->dom = new DOMDocument('1.0', 'utf-8'); $this->dom = new DOMDocument( '1.0', 'utf-8' );
$this->dom->preserveWhiteSpace = false; $this->dom->preserveWhiteSpace = false;
$this->dom->formatOutput = true; $this->dom->formatOutput = true;
if (file_exists($this->configDir . $this->schemaFilename)) { if (file_exists( $this->configDir . $this->schemaFilename )) {
if (@$this->dom->load($this->configDir . $this->schemaFilename) !== true) { if (@$this->dom->load( $this->configDir . $this->schemaFilename ) !== true) {
throw new Exception('Error: ' . $this->schemaFilename . ' is a invalid xml file!'); throw new Exception( 'Error: ' . $this->schemaFilename . ' is a invalid xml file!' );
} }
$this->rootNode = $this->dom->firstChild; $this->rootNode = $this->dom->firstChild;
} else { } else {
$this->rootNode = $this->dom->createElement('database'); $this->rootNode = $this->dom->createElement( 'database' );
$this->rootNode->setAttribute('name', $this->dataSource); $this->rootNode->setAttribute( 'name', $this->dataSource );
$this->dom->appendChild($this->rootNode); $this->dom->appendChild( $this->rootNode );
} }
} }
/** /**
* Build the xml schema for propel * Build the xml schema for propel
*/ */
public function buildSchema() public function buildSchema ()
{ {
$tableNode = $this->dom->createElement('table'); $tableNode = $this->dom->createElement( 'table' );
$tableNode->setAttribute('name', $this->tableName); $tableNode->setAttribute( 'name', $this->tableName );
if ($this->hasAutoIncrementPKey()) { if ($this->hasAutoIncrementPKey()) {
$tableNode->setAttribute('idMethod', 'native'); $tableNode->setAttribute( 'idMethod', 'native' );
} }
// specifying collation // specifying collation
switch ($this->dbConfig->adapter) { switch ($this->dbConfig->adapter) {
case 'mysql': case 'mysql':
$vendorNode = $this->dom->createElement('vendor'); $vendorNode = $this->dom->createElement( 'vendor' );
$vendorNode->setAttribute('type', $this->dbConfig->adapter); $vendorNode->setAttribute( 'type', $this->dbConfig->adapter );
$parameterNode = $this->dom->createElement('parameter'); $parameterNode = $this->dom->createElement( 'parameter' );
$parameterNode->setAttribute('name', 'Collation'); $parameterNode->setAttribute( 'name', 'Collation' );
$parameterNode->setAttribute('value', 'utf8_general_ci'); $parameterNode->setAttribute( 'value', 'utf8_general_ci' );
$vendorNode->appendChild($parameterNode); $vendorNode->appendChild( $parameterNode );
$tableNode->appendChild($vendorNode); $tableNode->appendChild( $vendorNode );
break; break;
} }
foreach ($this->columns as $column) { foreach ($this->columns as $column) {
// create the column node // create the column node
$columnNode = $this->dom->createElement('column'); $columnNode = $this->dom->createElement( 'column' );
// setting column node attributes // setting column node attributes
$columnNode->setAttribute('name', $column->field_name); $columnNode->setAttribute( 'name', $column->field_name );
$columnNode->setAttribute('type', $column->field_type); $columnNode->setAttribute( 'type', $column->field_type );
if ($column->field_size != '' && $column->field_size != 0) { if ($column->field_size != '' && $column->field_size != 0) {
$columnNode->setAttribute('size', $column->field_size); $columnNode->setAttribute( 'size', $column->field_size );
} }
$columnNode->setAttribute('required', ($column->field_null ? 'false' : 'true')); $columnNode->setAttribute( 'required', ($column->field_null ? 'false' : 'true') );
// only define the primaryKey attribute if it is defined // only define the primaryKey attribute if it is defined
if ($column->field_key) { if ($column->field_key) {
$columnNode->setAttribute('primaryKey', "true"); $columnNode->setAttribute( 'primaryKey', "true" );
} }
// only define the autoIncrement attribute if it is defined // only define the autoIncrement attribute if it is defined
if ($column->field_autoincrement) { if ($column->field_autoincrement) {
$columnNode->setAttribute('autoIncrement', "true"); $columnNode->setAttribute( 'autoIncrement', "true" );
} }
$tableNode->appendChild($columnNode); $tableNode->appendChild( $columnNode );
} }
$xpath = new DOMXPath($this->dom); $xpath = new DOMXPath( $this->dom );
$xtable = $xpath->query('/database/table[@name="' . $this->tableName . '"]'); $xtable = $xpath->query( '/database/table[@name="' . $this->tableName . '"]' );
if ($xtable->length == 0) { if ($xtable->length == 0) {
//the table definition does not exist, then just append the new node //the table definition does not exist, then just append the new node
$this->rootNode->appendChild($tableNode); $this->rootNode->appendChild( $tableNode );
} else { } else {
// the table definition already exist, then replace the node // the table definition already exist, then replace the node
$replacedNode = $xtable->item(0); $replacedNode = $xtable->item( 0 );
$this->rootNode->replaceChild($tableNode, $replacedNode); $this->rootNode->replaceChild( $tableNode, $replacedNode );
} }
// saving the xml result file // saving the xml result file
@@ -307,7 +312,7 @@ class PmTable
/** /**
* Remove the pmTable and all related objects, files and others * Remove the pmTable and all related objects, files and others
*/ */
public function remove() public function remove ()
{ {
$this->prepare(); $this->prepare();
$this->removeFromSchema(); $this->removeFromSchema();
@@ -318,16 +323,16 @@ class PmTable
/** /**
* Remove the target pmTable from schema of propel * Remove the target pmTable from schema of propel
*/ */
public function removeFromSchema() public function removeFromSchema ()
{ {
$xpath = new DOMXPath($this->dom); $xpath = new DOMXPath( $this->dom );
// locate the node // locate the node
$xtable = $xpath->query('/database/table[@name="' . $this->tableName . '"]'); $xtable = $xpath->query( '/database/table[@name="' . $this->tableName . '"]' );
if ($xtable->length == 0) { if ($xtable->length == 0) {
return false; return false;
} }
$this->rootNode->removeChild($xtable->item(0)); $this->rootNode->removeChild( $xtable->item( 0 ) );
// saving the xml result file // saving the xml result file
$this->saveSchema(); $this->saveSchema();
} }
@@ -335,29 +340,29 @@ class PmTable
/** /**
* Remove the model related classes files * Remove the model related classes files
*/ */
public function removeModelFiles() public function removeModelFiles ()
{ {
@unlink($this->classesDir . $this->className . '.php'); @unlink( $this->classesDir . $this->className . '.php' );
@unlink($this->classesDir . $this->className . 'Peer.php'); @unlink( $this->classesDir . $this->className . 'Peer.php' );
@unlink($this->classesDir . 'map' . PATH_SEP . $this->className . 'MapBuilder.php'); @unlink( $this->classesDir . 'map' . PATH_SEP . $this->className . 'MapBuilder.php' );
@unlink($this->classesDir . 'om' . PATH_SEP . 'Base' . $this->className . '.php'); @unlink( $this->classesDir . 'om' . PATH_SEP . 'Base' . $this->className . '.php' );
@unlink($this->classesDir . 'om' . PATH_SEP . 'Base' . $this->className . 'Peer.php'); @unlink( $this->classesDir . 'om' . PATH_SEP . 'Base' . $this->className . 'Peer.php' );
} }
/** /**
* Drop the phisical table of target pmTable or any specified as parameter * Drop the phisical table of target pmTable or any specified as parameter
*/ */
public function dropTable($tableName=null) public function dropTable ($tableName = null)
{ {
$tableName = isset($tableName) ? $tableName : $this->tableName; $tableName = isset( $tableName ) ? $tableName : $this->tableName;
$con = Propel::getConnection($this->dataSource); $con = Propel::getConnection( $this->dataSource );
$stmt = $con->createStatement(); $stmt = $con->createStatement();
if (is_object($con)) { if (is_object( $con )) {
try { try {
$stmt->executeQuery("DROP TABLE {$tableName}"); $stmt->executeQuery( "DROP TABLE {$tableName}" );
} catch (Exception $e) { } catch (Exception $e) {
throw new Exception("Physical table '$tableName' does not exist!"); throw new Exception( "Physical table '$tableName' does not exist!" );
} }
} }
} }
@@ -365,28 +370,27 @@ class PmTable
/** /**
* Save the xml schema for propel * Save the xml schema for propel
*/ */
public function saveSchema() public function saveSchema ()
{ {
$this->dom->save($this->configDir . $this->schemaFilename); $this->dom->save( $this->configDir . $this->schemaFilename );
} }
/** /**
* Prepare and create if not exists the propel ini file * Prepare and create if not exists the propel ini file
*/ */
public function preparePropelIniFile() public function preparePropelIniFile ()
{ {
$adapter = $this->dbConfig->adapter; $adapter = $this->dbConfig->adapter;
if (file_exists($this->configDir . "propel.$adapter.ini")) { if (file_exists( $this->configDir . "propel.$adapter.ini" )) {
return true; return true;
} }
if (!file_exists(PATH_CORE . PATH_SEP . 'config' . PATH_SEP . "propel.$adapter.ini")) { if (! file_exists( PATH_CORE . PATH_SEP . 'config' . PATH_SEP . "propel.$adapter.ini" )) {
throw new Exception("Invalid or not supported engine '$adapter'!"); throw new Exception( "Invalid or not supported engine '$adapter'!" );
} }
@copy(PATH_CORE . PATH_SEP . 'config' . PATH_SEP . "propel.$adapter.ini", @copy( PATH_CORE . PATH_SEP . 'config' . PATH_SEP . "propel.$adapter.ini", $this->configDir . "propel.$adapter.ini" );
$this->configDir . "propel.$adapter.ini");
} }
/** /**
@@ -395,34 +399,35 @@ class PmTable
* for the related table * for the related table
* - this function is not executing other sentenses like 'SET FOREIGN_KEY_CHECKS = 0;' for mysql, and others * - this function is not executing other sentenses like 'SET FOREIGN_KEY_CHECKS = 0;' for mysql, and others
*/ */
public function upgradeDatabase() public function upgradeDatabase ()
{ {
$con = Propel::getConnection($this->dataSource); $con = Propel::getConnection( $this->dataSource );
$stmt = $con->createStatement(); $stmt = $con->createStatement();
$lines = file($this->dataDir . $this->dbConfig->adapter . PATH_SEP . 'schema.sql'); $lines = file( $this->dataDir . $this->dbConfig->adapter . PATH_SEP . 'schema.sql' );
$previous = null; $previous = null;
$queryStack = array(); $queryStack = array ();
$aDNS = $con->getDSN(); $aDNS = $con->getDSN();
$dbEngine = $aDNS["phptype"]; $dbEngine = $aDNS["phptype"];
foreach ($lines as $j => $line) { foreach ($lines as $j => $line) {
switch ($dbEngine) { switch ($dbEngine) {
case 'mysql': case 'mysql':
$line = trim($line); // Remove comments from the script $line = trim( $line ); // Remove comments from the script
if (strpos($line, "--") === 0) {
$line = substr($line, 0, strpos($line, "--")); if (strpos( $line, "--" ) === 0) {
$line = substr( $line, 0, strpos( $line, "--" ) );
} }
if (empty($line)) { if (empty( $line )) {
continue; continue;
} }
if (strpos($line, "#") === 0) { if (strpos( $line, "#" ) === 0) {
$line = substr($line, 0, strpos($line, "#")); $line = substr( $line, 0, strpos( $line, "#" ) );
} }
if (empty($line)) { if (empty( $line )) {
continue; continue;
} }
@@ -434,21 +439,19 @@ class PmTable
// If the current line doesnt end with ; then put this line together // If the current line doesnt end with ; then put this line together
// with the next one, thus supporting multi-line statements. // with the next one, thus supporting multi-line statements.
if (strrpos($line, ";") != strlen($line) - 1) { if (strrpos( $line, ";" ) != strlen( $line ) - 1) {
$previous = $line; $previous = $line;
continue; continue;
} }
$line = substr($line, 0, strrpos($line, ";")); $line = substr( $line, 0, strrpos( $line, ";" ) );
// just execute the drop and create table for target table nad not for others // just execute the drop and create table for target table nad not for others
if (stripos($line, 'CREATE TABLE') !== false || stripos($line, 'DROP TABLE') !== false) { if (stripos( $line, 'CREATE TABLE' ) !== false || stripos( $line, 'DROP TABLE' ) !== false) {
$isCreateForCurrentTable = preg_match('/CREATE\sTABLE\s[\[\'\"\`]{1}' . $this->tableName $isCreateForCurrentTable = preg_match( '/CREATE\sTABLE\s[\[\'\"\`]{1}' . $this->tableName . '[\]\'\"\`]{1}/i', $line, $match );
. '[\]\'\"\`]{1}/i', $line, $match);
if ($isCreateForCurrentTable) { if ($isCreateForCurrentTable) {
$queryStack['create'] = $line; $queryStack['create'] = $line;
} else { } else {
$isDropForCurrentTable = preg_match('/DROP TABLE.*[\[\'\"\`]{1}' . $this->tableName $isDropForCurrentTable = preg_match( '/DROP TABLE.*[\[\'\"\`]{1}' . $this->tableName . '[\]\'\"\`]{1}/i', $line, $match );
. '[\]\'\"\`]{1}/i', $line, $match);
if ($isDropForCurrentTable) { if ($isDropForCurrentTable) {
$queryStack['drop'] = $line; $queryStack['drop'] = $line;
} }
@@ -456,21 +459,22 @@ class PmTable
} }
break; break;
case 'mssql': case 'mssql':
$line = trim($line); // Remove comments from the script $line = trim( $line ); // Remove comments from the script
if (strpos($line, "--") === 0) {
$line = substr($line, 0, strpos($line, "--")); if (strpos( $line, "--" ) === 0) {
$line = substr( $line, 0, strpos( $line, "--" ) );
} }
if (empty($line)) { if (empty( $line )) {
continue; continue;
} }
if (strpos($line, "#") === 0) { if (strpos( $line, "#" ) === 0) {
$line = substr($line, 0, strpos($line, "#")); $line = substr( $line, 0, strpos( $line, "#" ) );
} }
if (empty($line)) { if (empty( $line )) {
continue; continue;
} }
@@ -482,47 +486,47 @@ class PmTable
// If the current line doesnt end with ; then put this line together // If the current line doesnt end with ; then put this line together
// with the next one, thus supporting multi-line statements. // with the next one, thus supporting multi-line statements.
if (strrpos($line, ";") != strlen($line) - 1) { if (strrpos( $line, ";" ) != strlen( $line ) - 1) {
$previous = $line; $previous = $line;
continue; continue;
} }
$line = substr($line, 0, strrpos($line, ";")); $line = substr( $line, 0, strrpos( $line, ";" ) );
if (strpos($line, $this->tableName) == false) { if (strpos( $line, $this->tableName ) == false) {
continue; continue;
} }
$auxCreate = explode('CREATE', $line); $auxCreate = explode( 'CREATE', $line );
$auxDrop = explode('IF EXISTS', $auxCreate['0']); $auxDrop = explode( 'IF EXISTS', $auxCreate['0'] );
$queryStack['drop'] = 'IF EXISTS' . $auxDrop['1']; $queryStack['drop'] = 'IF EXISTS' . $auxDrop['1'];
$queryStack['create'] = 'CREATE' . $auxCreate['1']; $queryStack['create'] = 'CREATE' . $auxCreate['1'];
break; break;
case 'oracle': case 'oracle':
$line = trim($line); // Remove comments from the script $line = trim( $line ); // Remove comments from the script
if (empty($line)) { if (empty( $line )) {
continue; continue;
} }
switch (true) { switch (true) {
case preg_match("/^CREATE TABLE\s/i", $line): case preg_match( "/^CREATE TABLE\s/i", $line ):
if (strpos($line, $this->tableName) == true) { if (strpos( $line, $this->tableName ) == true) {
$inCreate = true; $inCreate = true;
$lineCreate .= $line . ' '; $lineCreate .= $line . ' ';
} }
break; break;
case preg_match("/ALTER TABLE\s/i", $line): case preg_match( "/ALTER TABLE\s/i", $line ):
if (strpos($line, $this->tableName) == true) { if (strpos( $line, $this->tableName ) == true) {
$inAlter = true; $inAlter = true;
$lineAlter .= $line . ' '; $lineAlter .= $line . ' ';
} }
break; break;
case preg_match("/^DROP TABLE\s/i", $line): case preg_match( "/^DROP TABLE\s/i", $line ):
if (strpos($line, $this->tableName) == true) { if (strpos( $line, $this->tableName ) == true) {
$inDrop = true; $inDrop = true;
$lineDrop .= $line . ' '; $lineDrop .= $line . ' ';
if (strrpos($line, ";") > 0) { if (strrpos( $line, ";" ) > 0) {
$queryStack['drop'] = $lineDrop; $queryStack['drop'] = $lineDrop;
$inDrop = false; $inDrop = false;
} }
@@ -531,21 +535,21 @@ class PmTable
default: default:
if ($inCreate) { if ($inCreate) {
$lineCreate .= $line . ' '; $lineCreate .= $line . ' ';
if (strrpos($line, ";") > 0) { if (strrpos( $line, ";" ) > 0) {
$queryStack['create'] = $lineCreate; $queryStack['create'] = $lineCreate;
$inCreate = false; $inCreate = false;
} }
} }
if ($inAlter) { if ($inAlter) {
$lineAlter .= $line . ' '; $lineAlter .= $line . ' ';
if (strrpos($line, ";") > 0) { if (strrpos( $line, ";" ) > 0) {
$queryStack['alter'] = $lineAlter; $queryStack['alter'] = $lineAlter;
$inAlter = false; $inAlter = false;
} }
} }
if ($inDrop) { if ($inDrop) {
$lineDrop .= $line . ' '; $lineDrop .= $line . ' ';
if (strrpos($line, ";") > 0) { if (strrpos( $line, ";" ) > 0) {
$queryStack['drop'] = $lineDrop; $queryStack['drop'] = $lineDrop;
$inDrop = false; $inDrop = false;
} }
@@ -556,64 +560,65 @@ class PmTable
} }
if ($dbEngine == 'oracle') { if ($dbEngine == 'oracle') {
$queryStack['drop'] = substr($queryStack['drop'], 0, strrpos($queryStack['drop'], ";")); $queryStack['drop'] = substr( $queryStack['drop'], 0, strrpos( $queryStack['drop'], ";" ) );
$queryStack['create'] = substr($queryStack['create'], 0, strrpos($queryStack['create'], ";")); $queryStack['create'] = substr( $queryStack['create'], 0, strrpos( $queryStack['create'], ";" ) );
$queryStack['alter'] = substr($queryStack['alter'], 0, strrpos($queryStack['alter'], ";")); $queryStack['alter'] = substr( $queryStack['alter'], 0, strrpos( $queryStack['alter'], ";" ) );
$queryIfExistTable = "SELECT TABLE_NAME FROM USER_TABLES WHERE TABLE_NAME = '" . $this->tableName . "'"; $queryIfExistTable = "SELECT TABLE_NAME FROM USER_TABLES WHERE TABLE_NAME = '" . $this->tableName . "'";
$rs = $stmt->executeQuery($queryIfExistTable); $rs = $stmt->executeQuery( $queryIfExistTable );
if ($rs->next()) { if ($rs->next()) {
$stmt->executeQuery($queryStack['drop']); $stmt->executeQuery( $queryStack['drop'] );
} }
$stmt->executeQuery($queryStack['create']); $stmt->executeQuery( $queryStack['create'] );
$stmt->executeQuery($queryStack['alter']); $stmt->executeQuery( $queryStack['alter'] );
} else { } else {
if (isset($queryStack['create'])) { if (isset( $queryStack['create'] )) {
// first at all we need to verify if we have a valid schema defined, // first at all we need to verify if we have a valid schema defined,
// so we verify that creating a dummy table // so we verify that creating a dummy table
$swapQuery = str_replace($this->tableName, $this->tableName . '_TMP', $queryStack['create']); $swapQuery = str_replace( $this->tableName, $this->tableName . '_TMP', $queryStack['create'] );
// if there is a problem with user defined table schema executeQuery() will throw a sql exception // if there is a problem with user defined table schema executeQuery() will throw a sql exception
$stmt->executeQuery($swapQuery); $stmt->executeQuery( $swapQuery );
// if there was not problem above proceced deleting the dummy table and drop and create the target table // if there was not problem above proceced deleting the dummy table and drop and create the target table
$stmt->executeQuery("DROP TABLE {$this->tableName}_TMP"); $stmt->executeQuery( "DROP TABLE {$this->tableName}_TMP" );
if (!isset($queryStack['drop'])) { if (! isset( $queryStack['drop'] )) {
$queryStack['drop'] = "DROP TABLE {$this->tableName}"; $queryStack['drop'] = "DROP TABLE {$this->tableName}";
} }
if (!isset($queryStack['create'])) { if (! isset( $queryStack['create'] )) {
throw new Exception('A problem occurred resolving the schema to update for this table'); throw new Exception( 'A problem occurred resolving the schema to update for this table' );
} }
$stmt->executeQuery($queryStack['drop']); $stmt->executeQuery( $queryStack['drop'] );
$stmt->executeQuery($queryStack['create']); $stmt->executeQuery( $queryStack['create'] );
} }
} }
} }
public function upgradeDatabaseFor($dataSource, $tablesList=array()) public function upgradeDatabaseFor ($dataSource, $tablesList = array())
{ {
$con = Propel::getConnection($dataSource); $con = Propel::getConnection( $dataSource );
$stmt = $con->createStatement(); $stmt = $con->createStatement();
$lines = file($this->dataDir . $this->dbConfig->adapter . PATH_SEP . 'schema.sql'); $lines = file( $this->dataDir . $this->dbConfig->adapter . PATH_SEP . 'schema.sql' );
$previous = null; $previous = null;
$errors = ''; $errors = '';
foreach ($lines as $j => $line) { foreach ($lines as $j => $line) {
$line = trim($line); // Remove comments from the script $line = trim( $line ); // Remove comments from the script
if (strpos($line, "--") === 0) {
$line = substr($line, 0, strpos($line, "--")); if (strpos( $line, "--" ) === 0) {
$line = substr( $line, 0, strpos( $line, "--" ) );
} }
if (empty($line)) { if (empty( $line )) {
continue; continue;
} }
if (strpos($line, "#") === 0) { if (strpos( $line, "#" ) === 0) {
$line = substr($line, 0, strpos($line, "#")); $line = substr( $line, 0, strpos( $line, "#" ) );
} }
if (empty($line)) { if (empty( $line )) {
continue; continue;
} }
@@ -625,23 +630,23 @@ class PmTable
// If the current line doesnt end with ; then put this line together // If the current line doesnt end with ; then put this line together
// with the next one, thus supporting multi-line statements. // with the next one, thus supporting multi-line statements.
if (strrpos($line, ";") != strlen($line) - 1) { if (strrpos( $line, ";" ) != strlen( $line ) - 1) {
$previous = $line; $previous = $line;
continue; continue;
} }
$line = substr($line, 0, strrpos($line, ";")); $line = substr( $line, 0, strrpos( $line, ";" ) );
// execute // execute
$isCreate = stripos($line, 'CREATE TABLE') !== false; $isCreate = stripos( $line, 'CREATE TABLE' ) !== false;
$isDrop = stripos($line, 'DROP TABLE') !== false; $isDrop = stripos( $line, 'DROP TABLE' ) !== false;
if ($isCreate || $isDrop) { if ($isCreate || $isDrop) {
if (preg_match('/TABLE\s[\'\"\`]+(\w+)[\'\"\`]+/i', $line, $match)) { if (preg_match( '/TABLE\s[\'\"\`]+(\w+)[\'\"\`]+/i', $line, $match )) {
if (in_array($match[1], $tablesList)) { if (in_array( $match[1], $tablesList )) {
//error_log($line); //error_log($line);
try { try {
$stmt->executeQuery($line); $stmt->executeQuery( $line );
} catch (Exception $e) { } catch (Exception $e) {
$errors .= $e->getMessage() . "\n"; $errors .= $e->getMessage() . "\n";
continue; continue;
@@ -656,9 +661,10 @@ class PmTable
/** /**
* verify if on the columns list was set a column as primary key * verify if on the columns list was set a column as primary key
*
* @return boolean to affirm if was defined a column as pk. * @return boolean to affirm if was defined a column as pk.
*/ */
public function hasAutoIncrementPKey() public function hasAutoIncrementPKey ()
{ {
foreach ($this->columns as $column) { foreach ($this->columns as $column) {
if ($column->field_autoincrement) { if ($column->field_autoincrement) {
@@ -673,14 +679,14 @@ class PmTable
* *
* @return array contains all supported columns types provided by propel * @return array contains all supported columns types provided by propel
*/ */
public function getPropelSupportedColumnTypes() public function getPropelSupportedColumnTypes ()
{ {
/** /**
* http://www.propelorm.org/wiki/Documentation/1.2/Schema * http://www.propelorm.org/wiki/Documentation/1.2/Schema
* [type = "BOOLEAN|TINYINT|SMALLINT|INTEGER|BIGINT|DOUBLE|FLOAT|REAL|DECIMAL|CHAR|{VARCHAR} * [type = "BOOLEAN|TINYINT|SMALLINT|INTEGER|BIGINT|DOUBLE|FLOAT|REAL|DECIMAL|CHAR|{VARCHAR}
* |LONGVARCHAR|DATE|TIME|TIMESTAMP|BLOB|CLOB"] * |LONGVARCHAR|DATE|TIME|TIMESTAMP|BLOB|CLOB"]
*/ */
$types = array(); $types = array ();
$types['BOOLEAN'] = 'BOOLEAN'; $types['BOOLEAN'] = 'BOOLEAN';
$types['TINYINT'] = 'TINYINT'; $types['TINYINT'] = 'TINYINT';
@@ -700,96 +706,96 @@ class PmTable
//$types['BLOB'] = 'BLOB'; <- disabled //$types['BLOB'] = 'BLOB'; <- disabled
//$types['CLOB'] = 'CLOB'; <- disabled //$types['CLOB'] = 'CLOB'; <- disabled
return $types; return $types;
} }
/** /**
*
* @param string $name any string witha name separated by underscore * @param string $name any string witha name separated by underscore
* @return string contains a camelcase expresion for $name * @return string contains a camelcase expresion for $name
*/ */
public function toCamelCase($name) public function toCamelCase ($name)
{ {
$tmp = explode('_', trim($name)); $tmp = explode( '_', trim( $name ) );
foreach ($tmp as $i => $part) { foreach ($tmp as $i => $part) {
$tmp[$i] = ucFirst(strtolower($part)); $tmp[$i] = ucFirst( strtolower( $part ) );
} }
return implode('', $tmp); return implode( '', $tmp );
} }
/** /**
* Run om task for phing to build all mdoel classes * Run om task for phing to build all mdoel classes
*/ */
public function phingbuildModel() public function phingbuildModel ()
{ {
$this->_callPhing('om'); $this->_callPhing( 'om' );
} }
/** /**
* Run sql task for phing to generate the sql schema * Run sql task for phing to generate the sql schema
*/ */
public function phingbuildSql() public function phingbuildSql ()
{ {
$this->_callPhing('sql'); $this->_callPhing( 'sql' );
} }
/** /**
* call phing to execute a determinated task * call phing to execute a determinated task
*
* @param string $taskName [om|sql] * @param string $taskName [om|sql]
*/ */
private function _callPhing($taskName) private function _callPhing ($taskName)
{ {
$options = array( $options = array ('project.dir' => $this->configDir,'build.properties' => "propel.{$this->dbConfig->adapter}.ini",'propel.targetPackage' => 'classes','propel.output.dir' => $this->targetDir,'propel.php.dir' => $this->baseDir
'project.dir' => $this->configDir,
'build.properties' => "propel.{$this->dbConfig->adapter}.ini",
'propel.targetPackage' => 'classes',
'propel.output.dir' => $this->targetDir,
'propel.php.dir' => $this->baseDir
); );
self::callPhing(array($taskName), PATH_THIRDPARTY . 'propel-generator/build.xml', $options, false); self::callPhing( array ($taskName
), PATH_THIRDPARTY . 'propel-generator/build.xml', $options, false );
} }
/** /**
* @param string $target - task name to execute *
* @param string $target - task name to execute
* @param string $buildFile - build file path * @param string $buildFile - build file path
* @param array $options - array options to override the options on .ini file * @param array $options - array options to override the options on .ini file
* @param bool $verbose - to show a verbose output * @param bool $verbose - to show a verbose output
*/ */
public static function callPhing($target, $buildFile='', $options=array(), $verbose=true) public static function callPhing ($target, $buildFile = '', $options = array(), $verbose = true)
{ {
G::loadClass('pmPhing'); G::loadClass( 'pmPhing' );
$args = array(); $args = array ();
foreach ($options as $key => $value) { foreach ($options as $key => $value) {
$args[] = "-D$key=$value"; $args[] = "-D$key=$value";
} }
if ($buildFile) { if ($buildFile) {
$args[] = '-f'; $args[] = '-f';
$args[] = realpath($buildFile); $args[] = realpath( $buildFile );
} }
if (!$verbose) { if (! $verbose) {
$args[] = '-q'; $args[] = '-q';
} }
if (is_array($target)) { if (is_array( $target )) {
$args = array_merge($args, $target); $args = array_merge( $args, $target );
} else { } else {
$args[] = $target; $args[] = $target;
} }
if (DIRECTORY_SEPARATOR != '\\' && (function_exists('posix_isatty') && @posix_isatty(STDOUT))) { if (DIRECTORY_SEPARATOR != '\\' && (function_exists( 'posix_isatty' ) && @posix_isatty( STDOUT ))) {
$args[] = '-logger'; $args[] = '-logger';
$args[] = 'phing.listener.AnsiColorLogger'; $args[] = 'phing.listener.AnsiColorLogger';
} }
Phing::startup(); Phing::startup();
Phing::setProperty('phing.home', getenv('PHING_HOME')); Phing::setProperty( 'phing.home', getenv( 'PHING_HOME' ) );
$m = new pmPhing(); $m = new pmPhing();
$m->execute($args); $m->execute( $args );
$m->runBuild(); $m->runBuild();
} }
} }