diff --git a/lib/Doctrine/Formatter.php b/lib/Doctrine/Formatter.php new file mode 100644 index 000000000..9203290c8 --- /dev/null +++ b/lib/Doctrine/Formatter.php @@ -0,0 +1,217 @@ +. + */ +Doctrine::autoload('Doctrine_Connection_Module'); +/** + * Doctrine_Formatter + * + * @package Doctrine + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @category Object Relational Mapping + * @link www.phpdoctrine.com + * @since 1.0 + * @version $Revision$ + * @author Konsta Vesterinen + */ +class Doctrine_Formatter extends Doctrine_Connection_Module +{ + /** + * Quotes pattern (% and _) characters in a string) + * + * EXPERIMENTAL + * + * WARNING: this function is experimental and may change signature at + * any time until labelled as non-experimental + * + * @param string the input string to quote + * + * @return string quoted string + */ + public function escapePattern($text) + { + if ($this->string_quoting['escape_pattern']) { + $text = str_replace($this->string_quoting['escape_pattern'], + $this->string_quoting['escape_pattern'] . + $this->string_quoting['escape_pattern'], $text); + + foreach ($this->wildcards as $wildcard) { + $text = str_replace($wildcard, $this->string_quoting['escape_pattern'] . $wildcard, $text); + } + } + return $text; + } + /** + * convertBoolean + * some drivers need the boolean values to be converted into integers + * when using DQL API + * + * This method takes care of that conversion + * + * @param array $item + * @return void + */ + public function convertBooleans($item) + { + if (is_array($item)) { + foreach ($item as $k => $value) { + if (is_bool($item)) { + $item[$k] = (int) $value; + } + } + } else { + if (is_bool($item)) { + $item = (int) $item; + } + } + return $item; + } + /** + * Quote a string so it can be safely used as a table or column name + * + * Delimiting style depends on which database driver is being used. + * + * NOTE: just because you CAN use delimited identifiers doesn't mean + * you SHOULD use them. In general, they end up causing way more + * problems than they solve. + * + * Portability is broken by using the following characters inside + * delimited identifiers: + * + backtick (`) -- due to MySQL + * + double quote (") -- due to Oracle + * + brackets ([ or ]) -- due to Access + * + * Delimited identifiers are known to generally work correctly under + * the following drivers: + * + mssql + * + mysql + * + mysqli + * + oci8 + * + pgsql + * + sqlite + * + * InterBase doesn't seem to be able to use delimited identifiers + * via PHP 4. They work fine under PHP 5. + * + * @param string $str identifier name to be quoted + * @param bool $checkOption check the 'quote_identifier' option + * + * @return string quoted identifier string + */ + public function quoteIdentifier($str, $checkOption = true) + { + if ($checkOption && ! $this->getAttribute(Doctrine::ATTR_QUOTE_IDENTIFIER)) { + return $str; + } + $str = str_replace($this->properties['identifier_quoting']['end'], + $this->properties['identifier_quoting']['escape'] . + $this->properties['identifier_quoting']['end'], $str); + + return $this->properties['identifier_quoting']['start'] + . $str . $this->properties['identifier_quoting']['end']; + } + /** + * quote + * quotes given input parameter + * + * @param mixed $input parameter to be quoted + * @param string $type + * @return mixed + */ + public function quote($input, $type = null) + { + if ($type == null) { + $type = gettype($input); + } + switch ($type) { + case 'integer': + case 'enum': + case 'boolean': + case 'double': + case 'float': + case 'bool': + case 'int': + return $input; + case 'array': + case 'object': + $input = serialize($input); + case 'string': + case 'char': + case 'varchar': + case 'text': + case 'gzip': + case 'blob': + case 'clob': + return $this->dbh->quote($input); + } + } + /** + * Removes any formatting in an sequence name using the 'seqname_format' option + * + * @param string $sqn string that containts name of a potential sequence + * @return string name of the sequence with possible formatting removed + */ + public function fixSequenceName($sqn) + { + $seqPattern = '/^'.preg_replace('/%s/', '([a-z0-9_]+)', $this->getAttribute(Doctrine::ATTR_SEQNAME_FORMAT)).'$/i'; + $seqName = preg_replace($seqPattern, '\\1', $sqn); + + if ($seqName && ! strcasecmp($sqn, $this->getSequenceName($seqName))) { + return $seqName; + } + return $sqn; + } + /** + * Removes any formatting in an index name using the 'idxname_format' option + * + * @param string $idx string that containts name of anl index + * @return string name of the index with possible formatting removed + */ + public function fixIndexName($idx) + { + $indexPattern = '/^'.preg_replace('/%s/', '([a-z0-9_]+)', $this->getAttribute(Doctrine::ATTR_IDXNAME_FORMAT)).'$/i'; + $indexName = preg_replace($indexPattern, '\\1', $idx); + if ($indexName && ! strcasecmp($idx, $this->getIndexName($indexName))) { + return $indexName; + } + return $idx; + } + /** + * adds sequence name formatting to a sequence name + * + * @param string name of the sequence + * @return string formatted sequence name + */ + public function getSequenceName($sqn) + { + return sprintf($this->getAttribute(Doctrine::ATTR_SEQNAME_FORMAT), + preg_replace('/[^a-z0-9_\$.]/i', '_', $sqn)); + } + /** + * adds index name formatting to a index name + * + * @param string name of the index + * @return string formatted index name + */ + public function getIndexName($idx) + { + return sprintf($this->getAttribute(Doctrine::ATTR_IDXNAME_FORMAT), + preg_replace('/[^a-z0-9_\$]/i', '_', $idx)); + } +}