?? cb.sql.upgrader.php
字號:
$sqlColumns = array(); $auto_increment_initial_value = ''; foreach ( $columns->children() as $column ) { if ( $column->name() == 'column' ) { $colNamePrefixed = $this->_prefixedName( $column, $colNamePrefix ); $sqlColumns[] = "\n " . $this->_db->NameQuote( $colNamePrefixed ) . ' ' . $this->_fullColumnType( $column ) ; if ( (int) $column->attributes( 'auto_increment' ) ) { $auto_increment_initial_value = ' AUTO_INCREMENT=' . (int) $column->attributes( 'auto_increment' ); } } } $indexes =& $table->getElementByPath( 'indexes' ); if ( $indexes !== false ) { foreach ( $indexes->children() as $index ) { if ( $index->name() == 'index' ) { $sqlIndexText = $this->_fullIndexType( $index, $colNamePrefix ); if ( $sqlIndexText ) { $sqlColumns[] = "\n " . $sqlIndexText; } } } } $sql = 'CREATE TABLE ' . $this->_db->NameQuote( $tableName ) . ' (' . implode( ',', $sqlColumns ) . "\n) ENGINE=MyISAM" . $auto_increment_initial_value ; if ( ! $this->_doQuery( $sql ) ) { $this->_setError( sprintf( '%s::createTableof Table %s failed with SQL error: %s', get_class( $this ), $tableName, $this->_db->getErrorMsg() ), $sql ); return false; } else { $this->_setLog( sprintf( 'Table %s successfully created', $tableName ), $sql, 'change' ); return true; } } } return false; } /** * UTILITY FUNCTIONS: */ /** * Sets modifying query and performs it, IF NOT in dry run mode. * If in dry run mode, returns true * @access private * * @param string $sql * @return boolean */ function _doQuery( $sql ) { if ( $this->_dryRun ) { return true; } else { $this->_db->SetQuery( $sql ); return $this->_db->query(); } } /** * Utility: Checks if $needle is the $attribute of a child of $xml * @access private * * @param string $needle * @param CBSimpleXMLElement $xml * @param string $attribute * @param string $colNamePrefix Prefix to add to all column names * @return boolean */ function _inXmlChildrenAttribute( $needle, &$xml, $name, $attribute, $colNamePrefix) { foreach ( $xml->children() as $chld ) { if ( $chld->name() == $name ) { $colNamePrefixed = $this->_prefixedName( $chld, $colNamePrefix, $attribute ); if ( $needle == $colNamePrefixed ) { return true; } } } return false; } /** * Converts a XML description of a SQL column into a full SQL type * * <column name="_rate" nametype="namesuffix" type="sql:decimal(16,8)" unsigned="true" null="true" default="NULL" auto_increment="100" /> * * Returns: $fulltype: 'decimal(16,8) unsigned NULL DEFAULT NULL' * @access private * * @param CBSimpleXMLElement $column * @return string|boolean Full SQL creation type or FALSE in case of error */ function _fullColumnType( &$column ) { $fullType = false; if ( $column->name() == 'column' ) { // $colName = $column->attributes( 'name' ); // $colNameType = $column->attributes( 'nametype' ); // if ( $colNameType == 'namesuffix' ) { // $colName = $colNamePrefix . $colName; // } $type = $column->attributes( 'type' ); $unsigned = $column->attributes( 'unsigned' ); $null = $column->attributes( 'null' ); $default = $column->attributes( 'default' ); $auto_increment = $column->attributes( 'auto_increment' ); if ( cbStartOfStringMatch( $type, 'sql:' ) ) { $type = trim( substr( $type, 4 ) ); // remove 'sql:' if ( $type ) { $notQuoted = array( 'int', 'float', 'tinyint', 'bigint', 'decimal', 'boolean', 'bit', 'serial', 'smallint', 'mediumint', 'double', 'year' ); $isInt = false; foreach ( $notQuoted as $n ) { if ( cbStartOfStringMatch( $type, $n ) ) { $isInt = true; break; } } $fullType = $type; if ( $unsigned == 'true' ) { $fullType .= ' unsigned'; } if ( $null !== 'true' ) { $fullType .= ' NOT NULL'; } if ( ! in_array( $type, array( 'text', 'blob', 'tinytext', 'mediumtext', 'longtext', 'tinyblob', 'mediumblob', 'longblob' ))) { // BLOB and TEXT columns cannot have DEFAULT values. http://dev.mysql.com/doc/refman/5.0/en/blob.html if ( $default !== null ) { $fullType .= ' DEFAULT ' . ( ( $isInt || ( $default === 'NULL' ) ) ? $default : $this->_db->Quote( $default ) ); } elseif ( ! $auto_increment ) { // MySQL 5.0.51a and b have a bug: they need a default value always to be able to return it correctly in SHOW COLUMNS FROM ...: if ( $null === 'true' ) { $default = 'NULL'; } elseif ( $isInt ) { $default = 0; } elseif ( in_array( $type, array( 'datetime', 'date', 'time' ) ) ) { $default = $this->_db->getNullDate( $type ); } else { $default = ''; } $fullType .= ' DEFAULT ' . ( ( $isInt || ( $default === 'NULL' ) ) ? $default : $this->_db->Quote( $default ) ); } } if ( $auto_increment ) { $fullType .= ' auto_increment'; } } } } return $fullType; } /** * Converts a mysql type with 'sql:' prefix to a xmlsql sql:/const: type (without prefix). * * @param string $type MySql type, E.g.: 'sql:varchar(255)' (with 'sql:' prefix) * @return string Xmlsql type, E.g.: 'string' (without 'sql:' or 'const:' prefix) */ function mysqlToXmlsql( $type ) { $mysqlTypes = array( 'varchar' => 'string', 'character' => 'string', 'char' => 'string', 'binary' => 'string', 'varbinary' => 'string', 'tinyblob' => 'string', 'blob' => 'string', 'mediumblob' => 'string', 'longblob' => 'string', 'tinytext' => 'string', 'mediumtext' => 'string', 'longtext' => 'string', 'text' => 'string', 'tinyint' => 'int', 'smallint' => 'int', 'mediumint' => 'int', 'bigint' => 'int', 'integer' => 'int', 'int' => 'int', 'bit' => 'int', 'boolean' => 'int', 'year' => 'int', 'float' => 'float', 'double' => 'float', 'decimal' => 'float', 'date' => 'date', 'datetime' => 'datetime', 'timestamp' => 'datetime', 'time' => 'time', 'enum' => 'string' // missing since not in SQL standard: SET, and ENUM above is a little simplified since only partly supported. ); $cleanedType = preg_replace( '/^sql:([^\\(]*)\\(?.*/', '$1', $type ); if ( isset( $mysqlTypes[$cleanedType] ) ) { return $mysqlTypes[$cleanedType]; } else { trigger_error( sprintf( 'mysqlToXmlsql: Unknown SQL type %s (i am extracting "%s" from type)', $type, $cleanedType ), E_USER_WARNING ); return $type; } } /** * Returns the possible default default values for that type * * @param string $type * @return array of string */ function defaultValuesOfTypes( $type ) { $defaultNulls = array( 'string' => array( '' ), 'int' => array( '', '0' ), 'float' => array( '', '0' ), 'date' => array( '', '0000-00-00' ), 'datetime' => array( '', '0000-00-00 00:00:00' ), 'time' => array( '', '00:00:00' ), 'enum' => array( '' ) ); if ( isset( $defaultNulls[$type] ) ) { return $defaultNulls[$type]; } else { trigger_error( sprintf( 'defaultValuesOfTypes: Unknown SQL type %s', $type ), E_USER_WARNING ); return array( '', 0 ); } } /** * Cleans and makes a value SQL safe depending on the type that is enforced. * @access private * * @param mixed $fieldValue * @param string $type * @return string */ function _sqlCleanQuote( $fieldValue, $type ) { $typeArray = explode( ':', $type, 3 ); if ( count( $typeArray ) < 2 ) { $typeArray = array( 'const' , $type ); } switch ( $typeArray[1] ) { case 'int': $value = (int) $fieldValue; break; case 'float': $value = (float) $fieldValue; break; case 'formula': $value = $fieldValue; break; case 'field': // this is temporarly handled here $value = $this->_db->NameQuote( $fieldValue ); break; case 'datetime': if ( preg_match( '/^[0-9]{4}-[01][0-9]-[0-3][0-9] [0-2][0-9](:[0-5][0-9]){2}$/', $fieldValue ) ) { $value = $this->_db->Quote( $fieldValue ); } else { $value = "''"; } break; case 'date': if ( preg_match( '/^[0-9]{4}-[01][0-9]-[0-3][0-9]$/', $fieldValue ) ) { $value = $this->_db->Quote( $fieldValue ); } else { $value = "''"; } break; case 'string': $value = $this->_db->Quote( $fieldValue ); break; case 'null': if ( $fieldValue != 'NULL' ) { trigger_error( sprintf( 'CBSQLUpgrader::_sqlCleanQuote: ERROR: field type sql:null has not NULL value' ) ); } $value = 'NULL'; break; default: trigger_error( 'CBSQLUpgrader::_sqlQuoteValueType: ERROR_UNKNOWN_TYPE: ' . htmlspecialchars( $type ), E_USER_NOTICE ); $value = $this->_db->Quote( $fieldValue ); // false; break; } return (string) $value; } /** * Cleans and makes a value comparable to the SQL stored value in a comprofilerDBTable object, depending on the type that is enforced. * @access private * * @param mixed $fieldValue * @param string $type * @return string */ function _phpCleanQuote( $fieldValue, $type ) { $typeArray = explode( ':', $type, 3 ); if ( count( $typeArray ) < 2 ) { $typeArray = array( 'const' , $type ); } switch ( $typeArray[1] ) { case 'int': $value = (int) $fieldValue; break; case 'float': $value = (float) $fieldValue; break; case 'formula': $value = $fieldValue; // this is temporarly done so break; case 'field': // this is temporarly handled here $value = $fieldValue; break; case 'datetime': if ( preg_match( '/^[0-9]{4}-[01][0-9]-[0-3][0-9] [0-2][0-9](:[0-5][0-9]){2}$/', $fieldValue ) ) { $value = (string) $fieldValue; } else { $value = ''; } break; case 'date': if ( preg_match( '/^[0-9]{4}-[01][0-9]-[0-3][0-9]$/', $fieldValue ) ) { $value = (string) $fieldValue; } else { $value = ''; } break; case 'string': $value = (string) $fieldValue; break;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -