?? cb.sql.upgrader.php
字號:
$indexName = $this->_prefixedName( $index, $colNamePrefix ); if ( isset( $allIndexes[$indexName] ) && isset( $allIndexes[$indexName][1] ) ) { $idxType = $allIndexes[$indexName][1]['type']; $idxUsing = $allIndexes[$indexName][1]['using']; if ( $idxType != $index->attributes( 'type' ) ) { if ( $change === false ) { $this->_setError( sprintf( 'Table %s Index %s type is %s instead of %s', $tableName, $indexName, $idxType, $index->attributes( 'type' ) ) ); } return false; } if ( $index->attributes( 'using' ) && ( $idxUsing != $index->attributes( 'using' ) ) ) { if ( $change === false ) { $indexShouldBeUsing = ( $index->attributes( 'using' ) ? $index->attributes( 'using' ) : 'btree' ); $this->_setError( sprintf( 'Table %s Index %s is using %s instead of %s', $tableName, $indexName, $idxUsing, $indexShouldBeUsing ) ); } return false; } $sequence = 1; foreach ( $index->children() as $column ) { if ( $column->name() == 'column' ) { $colName = $this->_prefixedName( $column, $colNamePrefix ); if ( ! isset( $allIndexes[$indexName][$sequence] ) ) { if ( $change === false ) { $this->_setError( sprintf( 'Table %s Index %s Column %s is missing in index', $tableName, $indexName, $colName ) ); } return false; } if ( $allIndexes[$indexName][$sequence]['name'] != $colName ) { if ( $change === false ) { $this->_setError( sprintf( 'Table %s Index %s Column %s is not the intended column, but %s', $tableName, $indexName, $colName, $allIndexes[$indexName][$sequence]['name'] ) ); } return false; } if ( $column->attributes( 'size' ) && ( $allIndexes[$indexName][$sequence]['size'] != $column->attributes( 'size' ) ) ) { if ( $change === false ) { $this->_setError( sprintf( 'Table %s Index %s Column %s Size is %d instead of %s', $tableName, $indexName, $colName, $allIndexes[$indexName][$sequence]['size'], $column->attributes( 'size' ) ) ); } return false; } // don't check ordering, as it can't be checked, and is probably irrelevant. ++$sequence; } } $this->_setLog( sprintf( 'Table %s Index %s is up-to-date.', $tableName, $indexName ), null, 'ok' ); return true; } if ( $change === false ) { $this->_setError( sprintf( 'Table %s Index %s does not exist', $tableName, $indexName ), null ); } return false; } /** * Checks if no surnumerous indexes exist * @access private * * @param string $tableName Name of table (for error strings) * @param array $allIndexes From $this->getAllTableIndexes( $table ) * @param CBSimpleXMLElement $indexes Indexes to check * @param string $colNamePrefix Prefix to add to all column names * @param boolean $drop TRUE If drops unneeded columns or not * @return boolean TRUE: no other columns exist, FALSE: errors are in $this->getErrors() */ function checkOtherIndexesExist( $tableName, &$allIndexes, &$indexes, $colNamePrefix, $drop = false ) { $isMatching = false; if ( $indexes->name() == 'indexes' ) { $isMatching = true; foreach ( array_keys( $allIndexes ) as $existingIndexName ) { if ( ! $this->_inXmlChildrenAttribute( $existingIndexName, $indexes, 'index', 'name', $colNamePrefix ) ) { if ( $drop ) { if ( ! $this->dropIndex( $tableName, $existingIndexName ) ) { $isMatching = false; } } else { $isMatching = false; $this->_setError( sprintf( 'Table %s Index %s exists but should not exist', $tableName, $existingIndexName ), null ); } } } if ( $isMatching && ! $drop ) { $this->_setLog( sprintf( 'Table %s has no unneeded indexes.', $tableName ), null, 'ok' ); } } return $isMatching; } /** * ROWS CHECKS: */ /** * Checks if no surnumerous indexes exist * @access private * * @param string $tableName Name of table (for error strings) * @param CBSimpleXMLElement $rows <rows...> * @param string $colNamePrefix Prefix to add to all column names * @param boolean $drop TRUE If drops unneeded columns or not * @return boolean TRUE: no other columns exist, FALSE: errors are in $this->getErrors() */ function checkOtherRowsExist( $tableName, &$rows, $colNamePrefix, $drop = false ) { $isMatching = false; if ( $rows->name() == 'rows' ) { $isMatching = true; $strictRows = ( ( $rows->attributes( 'strict' ) === 'true' ) ); if ( true /* $strictRows */ ) { // Build $strictRows index of indexes: $rowIndexes = array(); foreach ( $rows->children() as $row ) { if ( $row->name() == 'row' ) { $indexName = $this->_prefixedName( $row, $colNamePrefix, 'index', 'indextype' ); $indexValue = $row->attributes( 'value' ); $indexValueType = $row->attributes( 'valuetype' ); $rowIndexes[$indexName][$indexValue] = $indexValueType; } } // Count and if asked, drop rows which don't match: $otherRowsCount = $this->countRows( $tableName, $rowIndexes, false ); $isMatching = ( ( $otherRowsCount !== null ) && ( $otherRowsCount == 0 ) ); if ( ! $isMatching ) { if ( $drop ) { $isMatching = $this->dropRows( $tableName, $rowIndexes, false ); } else { $this->_setError( sprintf( 'Table %s has %s rows which should not exist', $tableName, $otherRowsCount ), null ); } } } if ( $isMatching && ! $drop ) { $this->_setLog( sprintf( 'Table %s has no unneeded rows.', $tableName ), null, 'ok' ); } } return $isMatching; } /** * Drops $row from table $tableName * @access private * * @param string $tableName Name of table (for error strings) * @param CBSimpleXMLElement $row <row index="columnname" indextype="prefixname" value="123" valuetype="sql:int" /> to delete * @param string $colNamePrefix Prefix to add to all column names * @return boolean */ function dropRow( $tableName, &$row, $colNamePrefix ) { $indexName = $this->_prefixedName( $row, $colNamePrefix, 'index', 'indextype' ); $indexValue = $row->attributes( 'value' ); $indexValueType = $row->attributes( 'valuetype' ); $selection = array( $indexName => array( $indexValue => $indexValueType ) ); return $this->dropRows( $tableName, $selection, true ); } /** * CHANGES OF TABLE STRUCTURE: */ /** * Changes if a column exists or Creates a new column * @access private * * @param string $tableName Name of table (for error strings) * @param array $allColumns From $this->getAllTableColumns( $table ) * @param CBSimpleXMLElement $column Column to check * @param string $colNamePrefix Prefix to add to all column names * @param CBSimpleXMLElement $columnNameAfter The column which should be just before this one * @return boolean TRUE: identical (no check on indexes), FALSE: errors are in $this->getErrors() */ function changeColumn( $tableName, &$allColumns, &$column, $colNamePrefix, &$columnNameAfter ) { switch ( $columnNameAfter ) { case null: $firstAfterSQL = ''; break; case 1: $firstAfterSQL = ' FIRST'; break; default: $colNameAfterPrefixed = $this->_prefixedName( $columnNameAfter, $colNamePrefix ); $firstAfterSQL = ' AFTER ' . $this->_db->NameQuote( $colNameAfterPrefixed ); break; } $colNamePrefixed = $this->_prefixedName( $column, $colNamePrefix ); $sqlUpdate = ''; $updateResult = true; if ( isset( $allColumns[$colNamePrefixed] ) ) { // column exists already, change it: if ( $column->attributes( 'oldname' ) && array_key_exists( $this->_prefixedName( $column, $colNamePrefix, 'oldname' ), $allColumns ) ) { $oldColName = $this->_prefixedName( $column, $colNamePrefix, 'oldname' ); } else { $oldColName = $colNamePrefixed; } if ( $column->attributes( 'initialvalue' ) && ( $column->attributes( 'null' ) !== 'true' ) ) { // we do need to treat the old NULL values specially: $sqlUpdate = 'UPDATE ' . $this->_db->NameQuote( $tableName ) . "\n SET " . $this->_db->NameQuote( $colNamePrefixed ) . ' = ' . $this->_sqlCleanQuote( $column->attributes( 'initialvalue' ), $column->attributes( 'initialvaluetype' ) ) . "\n WHERE " . $this->_db->NameQuote( $oldColName ) . ' IS NULL' ; $updateResult = $this->_doQuery( $sqlUpdate ); } $alteration = 'CHANGE ' . $this->_db->NameQuote( $oldColName ); } else { // column doesn't exist, create it: $alteration = 'ADD'; } $sql = 'ALTER TABLE ' . $this->_db->NameQuote( $tableName ) . "\n " . $alteration . ' ' . $this->_db->NameQuote( $colNamePrefixed ) . ' ' . $this->_fullColumnType( $column ) . $firstAfterSQL ; $alterationResult = $this->_doQuery( $sql ); if ( $alterationResult && ( $alteration == 'ADD' ) ) { if ( $column->attributes( 'initialvalue' ) ) { $sqlUpdate = 'UPDATE ' . $this->_db->NameQuote( $tableName ) . "\n SET " . $this->_db->NameQuote( $colNamePrefixed ) . ' = ' . $this->_sqlCleanQuote( $column->attributes( 'initialvalue' ), $column->attributes( 'initialvaluetype' ) ) ; $updateResult = $this->_doQuery( $sqlUpdate ); } } if ( ! $alterationResult ) { $this->_setError( sprintf( '%s::changeColumn (%s) of Table %s Column %s failed with SQL error: %s', get_class( $this ), $alteration, $tableName, $colNamePrefixed, $this->_db->getErrorMsg() ), $sql ); return false; } elseif ( ! $updateResult ) { $this->_setError( sprintf( '%s::changeColumn (UPDATE) of Table %s Column %s failed with SQL error: %s', get_class( $this ), $alteration, $tableName, $colNamePrefixed, $this->_db->getErrorMsg() ), $sqlUpdate ); return false; } else { $this->_setLog( sprintf( 'Table %s Column %s %s successfully, type: %s', $tableName, $colNamePrefixed, ( $alteration == 'ADD' ? 'created' : 'changed' ), $this->_fullColumnType( $column ) ), ( $alteration == 'ADD' ? $sql . ( $sqlUpdate ? ";\n" . $sqlUpdate : '' ) : ( $sqlUpdate ? $sqlUpdate . ";\n" : '' ) . $sql ), 'change' ); return true; } } /** * Changes if an index exists or Creates a new index * @access private * * @param string $tableName Name of table (for error strings) * @param array $allColumns From $this->getAllTableColumns( $table ) * @param CBSimpleXMLElement $column Column to check * @param string $colNamePrefix Prefix to add to all column names * @return boolean TRUE: identical (no check on indexes), FALSE: errors are in $this->getErrors() */ function changeIndex( $tableName, &$allIndexes, &$index, $colNamePrefix ) { $indexName = $this->_prefixedName( $index, $colNamePrefix ); $queryParts = array(); if ( isset( $allIndexes[$indexName] ) ) { // index exists already,drop it: if ( $indexName == 'PRIMARY') { $queryParts[] = 'DROP PRIMARY KEY'; } else { $queryParts[] = 'DROP KEY ' . $this->_db->NameQuote( $indexName ); } $alteration = 'change'; } else { $alteration = 'new'; } // Now create new index: $queryParts[] = 'ADD ' . $this->_fullIndexType( $index, $colNamePrefix ); $sql = 'ALTER TABLE ' . $this->_db->NameQuote( $tableName ) . "\n " . implode( ",\n ", $queryParts ) ; $alterationResult = $this->_doQuery( $sql ); if ( ! $alterationResult ) { $this->_setError( sprintf( '%s::changeIndex (%s) of Table %s Index %s failed with SQL error: %s', get_class( $this ), $alteration, $tableName, $indexName, $this->_db->getErrorMsg() ), $sql ); return false; } else { $this->_setLog( sprintf( 'Table %s Index %s successfully %s', $tableName, $indexName, ( $alteration == 'new' ? 'created' : 'changed' ) ), $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 $rows <rows...> * @param CBSimpleXMLElement $row <row> to change * @param array $allColumns From $this->getAllTableColumns( $table ) : columns which were existing before upgrading columns called before this function * @param string $colNamePrefix Prefix to add to all column names * @param boolean $change TRUE: changes row, FALSE: checks row * @return boolean TRUE: identical, FALSE: errors are in $this->getErrors() */ function checkOrChangeRow( $tableName, &$rows, &$row, &$allColumns, $colNamePrefix, $change = true ) { $indexName = $this->_prefixedName( $row, $colNamePrefix, 'index', 'indextype' ); $indexValue = $row->attributes( 'value' ); $indexValueType = $row->attributes( 'valuetype' ); $rowsArray = $this->loadRows( $tableName, $indexName, $indexValue, $indexValueType ); $mismatchingFields = array(); if ( is_array( $rowsArray ) && ( count( $rowsArray ) > 0 ) ) { foreach ( $rowsArray as $rowData ) { foreach ( $row->children() as $field ) { if ( $field->name() == 'field' ) { $strictField = $field->attributes( 'strict' ); $fieldName = $this->_prefixedName( $field, $colNamePrefix ); if ( $strictField || ! isset( $allColumns[$fieldName] ) ) { // if field is strict, or if column has just been created: compare value to the should be one: $fieldValue = $field->attributes( 'value' ); $fieldValueType = $field->attributes( 'valuetype' ); if ( ( ! isset( $allColumns[$fieldName] ) ) || ( ! array_key_exists( $fieldName, $rowData ) ) || ( ( $strictField === 'true' ) && ( $this->_adjustToStrictType( $rowData->$fieldName, $fieldValueType ) !== $this->_phpCleanQuote( $fieldValue, $fieldValueType ) ) ) || ( ( $strictField === 'notnull' ) && ( $this->_adjustToStrictType( $rowData->$fieldName, $fieldValueType ) === null ) && ( $this->_phpCleanQuote( $fieldValue, $fieldValueType ) !== null ) ) || ( ( $strictField === 'notzero' ) && ( ( ( $this->_adjustToStrictType( $rowData->$fieldName, $fieldValueType ) === null ) || ( $this->_adjustToStrictType( $rowData->$fieldName, $fieldValueType ) == 0 ) ) && ( ! ( ( $this->_phpCleanQuote( $fieldValue, $fieldValueType ) === null ) || ( $this->_phpCleanQuote( $fieldValue, $fieldValueType ) === 0 ) ) ) ) ) || ( ( $strictField === 'notempty' ) && ( ( ( $this->_adjustToStrictType( $rowData->$fieldName, $fieldValueType ) === null ) || ( $this->_adjustToStrictType( $rowData->$fieldName, $fieldValueType ) == '' ) ) && ( ! ( ( $this->_phpCleanQuote( $fieldValue, $fieldValueType ) === null ) || ( $this->_phpCleanQuote( $fieldValue, $fieldValueType ) === '' ) ) ) ) ) ) { $mismatchingFields[$fieldName] = $this->_sqlCleanQuote( $fieldValue, $fieldValueType ); } } }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -