📄 xmldbgenerator.class.php
字号:
/// Prepend the prefix $name = $this->prefix . $name; $name = substr(trim($name), 0, $this->names_max_length - 1 - strlen($suffix)); //Max names_max_length /// Add the suffix $namewithsuffix = $name; if ($suffix) { $namewithsuffix = $namewithsuffix . '_' . $suffix; } /// If the calculated name is in the cache, or if we detect it by introspecting the DB let's modify if if (in_array($namewithsuffix, $used_names) || $this->isNameInUse($namewithsuffix, $suffix, $tablename)) { $counter = 2; /// If have free space, we add 2 if (strlen($namewithsuffix) < $this->names_max_length) { $newname = $name . $counter; /// Else replace the last char by 2 } else { $newname = substr($name, 0, strlen($name)-1) . $counter; } $newnamewithsuffix = $newname; if ($suffix) { $newnamewithsuffix = $newnamewithsuffix . '_' . $suffix; } /// Now iterate until not used name is found, incrementing the counter while (in_array($newnamewithsuffix, $used_names) || $this->isNameInUse($newnamewithsuffix, $suffix, $tablename)) { $counter++; $newname = substr($name, 0, strlen($newname)-1) . $counter; $newnamewithsuffix = $newname; if ($suffix) { $newnamewithsuffix = $newnamewithsuffix . '_' . $suffix; } } $namewithsuffix = $newnamewithsuffix; } /// Add the name to the cache $used_names[$tablename.'-'.$fields.'-'.$suffix] = $namewithsuffix; /// Quote it if necessary (reserved words) $namewithsuffix = $this->getEncQuoted($namewithsuffix); return $namewithsuffix; } /** * Given any string (or one array), enclose it by the proper quotes * if it's a reserved word */ function getEncQuoted($input) { if (is_array($input)) { foreach ($input as $key=>$content) { $input[$key] = $this->getEncQuoted($content); } return $input; } else { /// Always lowercase $input = strtolower($input); /// if reserved or quote_all, quote it if ($this->quote_all || in_array($input, $this->reserved_words)) { $input = $this->quote_string . $input . $this->quote_string; } return $input; } } /** * Given one XMLDB Statement, build the needed SQL insert sentences to execute it */ function getExecuteInsertSQL($statement) { $results = array(); //Array where all the sentences will be stored if ($sentences = $statement->getSentences()) { foreach ($sentences as $sentence) { /// Get the list of fields $fields = $statement->getFieldsFromInsertSentence($sentence); /// Get the values of fields $values = $statement->getValuesFromInsertSentence($sentence); /// Look if we have some CONCAT value and transform it dinamically foreach($values as $key => $value) { /// Trim single quotes $value = trim($value,"'"); if (stristr($value, 'CONCAT') !== false){ /// Look for data between parentesis preg_match("/CONCAT\s*\((.*)\)$/is", trim($value), $matches); if (isset($matches[1])) { $part = $matches[1]; /// Convert the comma separated string to an array $arr = XMLDBObject::comma2array($part); if ($arr) { $value = $this->getConcatSQL($arr); } } } /// Values to be sent to DB must be properly escaped $value = addslashes($value); /// Back trimmed quotes $value = "'" . $value . "'"; /// Back to the array $values[$key] = $value; } /// Iterate over fields, escaping them if necessary foreach($fields as $key => $field) { $fields[$key] = $this->getEncQuoted($field); } /// Build the final SQL sentence and add it to the array of results $sql = 'INSERT INTO ' . $this->getEncQuoted($this->prefix . $statement->getTable()) . '(' . implode(', ', $fields) . ') ' . 'VALUES (' . implode(', ', $values) . ')'; $results[] = $sql; } } return $results; } /** * Given one array of elements, build de proper CONCAT expresion, based * in the $concat_character setting. If such setting is empty, then * MySQL's CONCAT function will be used instead */ function getConcatSQL($elements) { /// Replace double quoted elements by single quotes foreach($elements as $key => $element) { $element = trim($element); if (substr($element, 0, 1) == '"' && substr($element, -1, 1) == '"') { $elements[$key] = "'" . trim($element, '"') . "'"; } } /// Now call the standard sql_concat() DML function return call_user_func_array('sql_concat', $elements); } /** * Given one string (or one array), ends it with statement_end */ function getEndedStatements ($input) { if (is_array($input)) { foreach ($input as $key=>$content) { $input[$key] = $this->getEndedStatements($content); } return $input; } else { $input = trim($input) . $this->statement_end; return $input; } } /** * Returns the name (string) of the sequence used in the table for the autonumeric pk * Only some DB have this implemented */ function getSequenceFromDB($xmldb_table) { return false; } /** * Given one object name and it's type (pk, uk, fk, ck, ix, uix, seq, trg) * return if such name is currently in use (true) or no (false) * (MySQL requires the whole XMLDBTable object to be specified, so we add it always) * (invoked from getNameForObject() * Only some DB have this implemented */ function isNameInUse($object_name, $type, $table_name) { return false; //For generators not implementing introspecion, //we always return with the name being free to be used }/// ALL THESE FUNCTION MUST BE CUSTOMISED BY ALL THE XMLDGenerator classes /** * Given one XMLDB Type, lenght and decimals, returns the DB proper SQL type */ function getTypeSQL ($xmldb_type, $xmldb_length=null, $xmldb_decimals=null) { return 'code for type(precision) goes to function getTypeSQL()'; } /** * Given one XMLDB Field, return its enum SQL to be added inline with the column definition */ function getEnumSQL ($xmldb_field) { return 'code for inline enum declaration goes to function getEnumSQL(). Can be disabled with enum_inline_code=false'; } /** * Returns the code needed to create one enum for the xmldb_table and xmldb_field passes */ function getEnumExtraSQL ($xmldb_table, $xmldb_field) { return 'Code for extra enum SQL goes to getEnumExtraSQL(). Can be disabled with enum_extra_code=false'; } /** * Returns the code (array of statements) needed to execute extra statements on field rename */ function getRenameFieldExtraSQL ($xmldb_table, $xmldb_field) { return array('Code for field rename goes to getRenameFieldExtraSQL(). Can be disabled with rename_column_extra_code=false;'); } /** * Returns the code (array of statements) needed * to create one sequence for the xmldb_table and xmldb_field passes */ function getCreateSequenceSQL ($xmldb_table, $xmldb_field) { return array('Code for extra sequence SQL goes to getCreateSequenceSQL(). Can be disabled with sequence_extra_code=false'); } /** * Returns the code (array of statements) needed to add one comment to the table */ function getCommentSQL ($xmldb_table) { return array('Code for table comment goes to getCommentSQL(). Can be disabled with add_table_comments=false;'); } /** * Returns the code (array of statements) needed to execute extra statements on table rename */ function getRenameTableExtraSQL ($xmldb_table) { return array('Code for table rename goes to getRenameTableExtraSQL(). Can be disabled with rename_table_extra_code=false;'); } /** * Returns the code (array of statements) needed to execute extra statements on table drop */ function getDropTableExtraSQL ($xmldb_table) { return array('Code for table drop goes to getDropTableExtraSQL(). Can be disabled with drop_table_extra_code=false;'); } /** * Given one XMLDBTable and one XMLDBField, return the SQL statements needded to drop its enum * (usually invoked from getModifyEnumSQL() */ function getDropEnumSQL($xmldb_table, $xmldb_field) { return array('Code to drop one enum goes to getDropEnumSQL()'); } /** * Given one XMLDBTable and one XMLDBField, return the SQL statements needded to add its enum * (usually invoked from getModifyEnumSQL() */ function getCreateEnumSQL($xmldb_table, $xmldb_field) { return array('Code to create one enum goes to getCreateEnumSQL()'); } /** * Given one XMLDBTable and one XMLDBField, return the SQL statements needded to drop its default * (usually invoked from getModifyDefaultSQL() */ function getDropDefaultSQL($xmldb_table, $xmldb_field) { return array('Code to drop one default goes to getDropDefaultSQL()'); } /** * Given one XMLDBTable and one optional XMLDBField, return one array with all the check * constrainst found for that table (or field). Must exist for each DB supported. * (usually invoked from find_check_constraint_name) */ function getCheckConstraintsFromDB($xmldb_table, $xmldb_field=null) { return array('Code to fetch check constraints goes to getCheckConstraintsFromDB()'); } /** * Given one XMLDBTable and one XMLDBField, return the SQL statements needded to add its default * (usually invoked from getModifyDefaultSQL() */ function getCreateDefaultSQL($xmldb_table, $xmldb_field) { return array('Code to create one default goes to getCreateDefaultSQL()'); } /** * Returns an array of reserved words (lowercase) for this DB * You MUST provide the real list for each DB inside every XMLDB class */ function getReservedWords() { /// Some well-know reserved words $reserved_words = array ( 'user', 'scale', 'type', 'comment', 'view', 'value', 'table', 'index', 'key', 'sequence', 'trigger' ); return $reserved_words; } /** * Returns an array of tables to be built without prefix (lowercase) * It's enough to keep updated here this function. */ function getTablesWithoutPrefix() { /// Some well-known tables to be created without prefix $tables = array ( 'adodb_logsql' ); return $tables; }}?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -