?? cb.sql.upgrader.php
字號:
} foreach ( $row->children() as $field ) { if ( $field->name() == 'field' ) { $strictField = $field->attributes( 'strict' ); if ( $strictField == 'updatewithfield' ) { // if field should be updated same time than another field: check if the field is in the list to be upgraded: $strictSameAsField = $field->attributes( 'strictsameasfield' ); if ( isset( $mismatchingFields[$strictSameAsField] ) ) { $fieldName = $this->_prefixedName( $field, $colNamePrefix ); $fieldValue = $field->attributes( 'value' ); $fieldValueType = $field->attributes( 'valuetype' ); if ( ( ! array_key_exists( $fieldName, $rowData ) ) || ( $this->_adjustToStrictType( $rowData->$fieldName, $fieldValueType ) !== $this->_phpCleanQuote( $fieldValue, $fieldValueType ) ) ) { $mismatchingFields[$fieldName] = $this->_sqlCleanQuote( $fieldValue, $fieldValueType ); } } } } } } if ( count( $mismatchingFields ) > 0 ) { if ( $change === true ) { return $this->setFields( $tableName, $row, $mismatchingFields, $colNamePrefix ); } else { $texts = array(); foreach ($mismatchingFields as $name => $val ) { $texts[] = sprintf( 'Field %s = %s instead of %s', $name, ( isset( $rowData->$name ) ? $rowData->$name : '""' ), $val ); } $this->_setError( sprintf( 'Table %s Rows %s = %s : %s', $tableName, $indexName, $indexValue, implode( ', ', $texts ) ) ); return false; } } else { if ( $change === false ) { $this->_setLog( sprintf( 'Table %s Rows %s = %s are up-to-date.', $tableName, $indexName, $this->_sqlCleanQuote( $indexValue, $indexValueType ) ), null, 'ok' ); } return true; } } else { if ( $change === true ) { return $this->insertRow( $tableName, $row, $colNamePrefix ); } else { $this->_setError( sprintf( 'Table %s Rows %s = %s do not exist', $tableName, $indexName, $this->_sqlCleanQuote( $indexValue, $indexValueType ) ), null ); } return false; } } /** * Load rows from table $tableName * @access private * * @param string $tableName Name of table (for error strings) * @param string $indexName * @param string $indexValue * @param string $indexValueType * @return boolean */ function loadRows( $tableName, $indexName, $indexValue, $indexValueType ) { $sql = 'SELECT * FROM ' . $this->_db->NameQuote( $tableName ); if ( $indexName ) { $sql .= "\n WHERE " . $this->_db->NameQuote( $indexName ) . ' = ' . $this->_sqlCleanQuote( $indexValue, $indexValueType ) ; } $this->_db->setQuery( $sql ); $result = $this->_db->loadObjectList(); if ( $this->_db->getErrorMsg() ) { $this->_setError( sprintf( '%s::loadRows of Table %s Rows %s = %s failed with SQL error: %s', get_class( $this ), $tableName, $columnName, $this->_sqlCleanQuote( $indexValue, $indexValueType ), $this->_db->getErrorMsg() ), $sql ); return false; } else { // $this->_setLog( sprintf( 'Table %s Rows %s = %s successfully loaded', $tableName, $columnName, $this->_sqlCleanQuote( $indexValue, $indexValueType ) ), $sql, 'change' ); return $result; } } /** * Drop rows from table $tableName matching $selection * @access private * * @param string $tableName * @param array $selection array( 'columnName' => array( 'columnValue' => 'columnValueType' ) ) * @param boolean $positiveSelect TRUE: select corresponding to selection, FALSE: Select NOT the selection * @return boolean TRUE: no error, FALSE: error (logged) */ function dropRows( $tableName, &$selection, $positiveSelect ) { $where = $this->_sqlBuiildSelectionWhere( $selection, $positiveSelect ); $sql = 'DELETE FROM ' . $this->_db->NameQuote( $tableName ) . "\n WHERE " . $where ; if ( ! $this->_doQuery( $sql ) ) { $this->_setError( sprintf( '%s::dropRows of Table %s Row(s) %s failed with SQL error: %s', get_class( $this ), $tableName, $where, $this->_db->getErrorMsg() ), $sql ); return false; } else { $this->_setLog( sprintf( 'Table %s Row(s) %s successfully dropped', $tableName, $where ), $sql, 'change' ); return true; } } /** * Counts rows from table $tableName matching $selection * @access private * * @param string $tableName * @param array $selection array( 'columnName' => array( 'columnValue' => 'columnValueType' ) ) * @param boolean $positiveSelect TRUE: select corresponding to selection, FALSE: Select NOT the selection * @return boolean TRUE: no error, FALSE: error (logged) */ function countRows( $tableName, &$selection, $positiveSelect ) { $where = $this->_sqlBuiildSelectionWhere( $selection, $positiveSelect ); $sql = 'SELECT COUNT(*) FROM ' . $this->_db->NameQuote( $tableName ) . "\n WHERE " . $where ; $this->_db->setQuery( $sql ); $result = $this->_db->loadResult(); if ( $result === null ) { $this->_setError( sprintf( '%s::countRows of Table %s Row(s) %s failed with SQL error: %s', get_class( $this ), $tableName, $where, $this->_db->getErrorMsg() ), $sql ); } return $result; } /** * Counts rows from table $tableName matching $selection * @access private * * @param string $tableName * @param CBSimpleXMLElement $row <row index="columnname" indextype="prefixname" value="123" valuetype="sql:int" /> to delete * @param array $mismatchingFields array( 'columnName' => 'SQL-safe value' ) * @param string $colNamePrefix Prefix to add to all column names * @return boolean TRUE: no error, FALSE: error (logged) */ function setFields( $tableName, &$row, &$mismatchingFields, $colNamePrefix ) { $indexName = $this->_prefixedName( $row, $colNamePrefix, 'index', 'indextype' ); $indexValue = $row->attributes( 'value' ); $indexValueType = $row->attributes( 'valuetype' ); $selection = array( $indexName => array( $indexValue => $indexValueType ) ); $where = $this->_sqlBuiildSelectionWhere( $selection, true ); $setFields = array(); foreach ( $mismatchingFields as $name => $quotedValue ) { $setFields[] = $this->_db->NameQuote( $name ) . ' = ' . $quotedValue; } $setFieldsText = implode( ', ', $setFields ); $sql = 'UPDATE ' . $this->_db->NameQuote( $tableName ) . "\n SET " . $setFieldsText . "\n WHERE " . $where ; if ( ! $this->_doQuery( $sql ) ) { $this->_setError( sprintf( '%s::setFields of Table %s Row %s Fields %s failed with SQL error: %s', get_class( $this ), $tableName, $where, $setFieldsText, $this->_db->getErrorMsg() ), $sql ); return false; } else { $this->_setLog( sprintf( 'Table %s Row %s successfully updated', $tableName, $where ), $sql, 'change' ); return true; } } /** * Checks if an index exists and has the type of the parameters below: * @access private * * @param string $tableName Name of table (for error strings) * @param CBSimpleXMLElement $row <row> to change * @param string $colNamePrefix Prefix to add to all column names * @return boolean TRUE: success, FALSE: errors are in $this->getErrors() */ function insertRow( $tableName, &$row, $colNamePrefix ) { $indexName = $this->_prefixedName( $row, $colNamePrefix, 'index', 'indextype' ); $indexValue = $row->attributes( 'value' ); $indexValueType = $row->attributes( 'valuetype' ); if ( $row->name() == 'row' ) { $sqlFieldNames = array(); $sqlFieldValues = array(); foreach ( $row->children() as $field ) { if ( $field->name() == 'field' ) { $fieldName = $this->_prefixedName( $field, $colNamePrefix ); $fieldValue = $field->attributes( 'value' ); $fieldValueType = $field->attributes( 'valuetype' ); if ( ( $fieldName == $indexName ) && ( ( $fieldValue != $indexValue ) || ( $fieldValueType != $indexValueType ) ) ) { $this->_setError( sprintf( '%s::insertRow Error in XML: Table %s Row %s = %s (type %s) trying to insert different Field Value or type: %s = %s (type %s)', get_class( $this ), $tableName, $indexName, $indexValue, $indexValueType, $fieldName, $fieldValue, $fieldValueType ), null ); return false; } if ( isset( $sqlFieldNames[$fieldName] ) ) { $this->_setError( sprintf( '%s::insertRow Error in XML: Table %s Row %s = %s : Field %s is defined twice in XML', get_class( $this ), $tableName, $indexName, $indexValue, $fieldName ), null ); return false; } $sqlFieldNames[$fieldName] = $this->_db->NameQuote( $fieldName ); $sqlFieldValues[$fieldName] = $this->_sqlCleanQuote( $fieldValue, $fieldValueType ); } } if ( ! isset( $sqlFieldNames[$indexName] ) ) { $sqlFieldNames[$indexName] = $this->_db->NameQuote( $indexName ); $sqlFieldValues[$indexName] = $this->_sqlCleanQuote( $indexValue, $indexValueType ); } if ( count( $sqlFieldNames ) > 0 ) { $sqlColumnsText = '(' . implode( ',', $sqlFieldNames ) . ')'; $sqlColumnsValues = array(); $sqlColumnsValues[] = '(' . implode( ',', $sqlFieldValues ) . ')'; } elseif ( $indexName ) { $sqlColumnsText = '(' . $this->_db->NameQuote( $indexName ) . ')'; $sqlColumnsValues = '(' . $this->_sqlCleanQuote( $indexValue, $indexValueType ); } else { $sqlColumnsText = null; } if ( $sqlColumnsText != null ) { $sql = 'INSERT INTO ' . $this->_db->NameQuote( $tableName ) . "\n " . $sqlColumnsText . "\n VALUES " . implode( ",\n ", $sqlColumnsValues ) ; if ( ! $this->_doQuery( $sql ) ) { $this->_setError( sprintf( '%s::insertRow of Table %s Row %s = %s Fields %s = %s failed with SQL error: %s', get_class( $this ), $tableName, $indexName, $indexValue, $sqlColumnsText, $sqlColumnsValues, $this->_db->getErrorMsg() ), $sql ); return false; } else { $this->_setLog( sprintf( 'Table %s Row %s = %s successfully updated', $tableName, $indexName, $indexValue ), $sql, 'change' ); return true; } } } $this->_setError( sprintf( '%s::insertRow : Error in SQL: No values to insert Row %s = %s (type %s)', $tableName, $indexName, $indexValue, $indexValueType ), $sql ); return true; } /** * Builds SQL WHERE statement (without WHERE) based on array $selection * @access private * * @param array $selection array( 'columnName' => array( 'columnValue' => 'columnValueType' ) ) * @param boolean $positiveSelect TRUE: select corresponding to selection, FALSE: Select NOT the selection * @return boolean True: no error, False: error (logged) */ function _sqlBuiildSelectionWhere( &$selection, $positiveSelect ) { $where = array(); foreach ( $selection as $colName => $valuesArray ) { $values = array(); foreach ( $valuesArray as $colValue => $colValueType ) { $values[] = $this->_sqlCleanQuote( $colValue, $colValueType ); } if ( count( $values ) > 0 ) { if ( count( $values ) > 1 ) { $where[] = $this->_db->NameQuote( $colName ) . ' IN (' .implode( ',', $values ) . ')'; } else { $where[] = $this->_db->NameQuote( $colName ) . ' = ' . $values[0]; } } } $positiveWhere = '(' . implode( ') OR (', $where ) . ')'; if ( $positiveSelect ) { return $positiveWhere; } else { return 'NOT(' . $positiveWhere . ')'; } } /** * Drops column $ColumnName from table $tableName * @access private * * @param string $tableName Name of table (for error strings) * @param string $columnName Old name of column to change * @return boolean TRUE: no error, FALSE: errors are in $this->getErrors() */ function dropColumn( $tableName, $columnName ) { $sql = 'ALTER TABLE ' . $this->_db->NameQuote( $tableName ) . "\n DROP COLUMN " . $this->_db->NameQuote( $columnName ) ; if ( ! $this->_doQuery( $sql ) ) { $this->_setError( sprintf( '%s::dropColumn of Table %s Column %s failed with SQL error: %s', get_class( $this ), $tableName, $columnName, $this->_db->getErrorMsg() ), $sql ); return false; } else { $this->_setLog( sprintf( 'Table %s Column %s successfully dropped', $tableName, $columnName ), $sql, 'change' ); return true; } } /** * Drops INDEX $indexName from table $tableName * @access private * * @param string $tableName Name of table (for error strings) * @param string $indexName Old name of column to change * @return boolean TRUE: no error, FALSE: errors are in $this->getErrors() */ function dropIndex( $tableName, $indexName ) { $sql = 'ALTER TABLE ' . $this->_db->NameQuote( $tableName ); if ( $indexName == 'PRIMARY' ) { $sql .= "\n DROP PRIMARY KEY"; } else { $sql .= "\n DROP KEY " . $this->_db->NameQuote( $indexName ); } if ( ! $this->_doQuery( $sql ) ) { $this->_setError( sprintf( '%s::dropIndex of Table %s Index %s failed with SQL error: %s', get_class( $this ), $tableName, $indexName, $this->_db->getErrorMsg() ), $sql ); return false; } else { $this->_setLog( sprintf( 'Table %s Index %s successfully dropped', $tableName, $indexName ), $sql, 'change' ); return true; } } /** * Drops table $tableName * @access private * * @param string $tableName Name of table (for error strings) * @return boolean TRUE: no error, FALSE: errors are in $this->getErrors() */ function dropTable( $tableName ) { $sql = 'DROP TABLE ' . $this->_db->NameQuote( $tableName ) ; if ( ! $this->_doQuery( $sql ) ) { $this->_setError( sprintf( '%s::dropTable of Table %s failed with SQL error: %s', get_class( $this ), $tableName, $this->_db->getErrorMsg() ), $sql ); return false; } else { $this->_setLog( sprintf( 'Table %s successfully dropped', $tableName ), $sql, 'change' ); return true; } } /** * Creates a new table * @access private * * @param CBSimpleXMLElement $table Table * @param string $colNamePrefix Prefix to add to all column names * @return boolean True: success, False: failure */ function createTable( &$table, $colNamePrefix ) { if ( $table->name() == 'table' ) { $tableName = $this->_prefixedName( $table, $colNamePrefix ); $columns =& $table->getElementByPath( 'columns' ); if ( $tableName && ( $columns !== false ) ) {
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -