Formatter.php 7.89 KB
Newer Older
zYne's avatar
zYne committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
<?php
/*
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.phpdoctrine.com>.
 */
Doctrine::autoload('Doctrine_Connection_Module');
/**
 * Doctrine_Formatter
 *
 * @package     Doctrine
26
 * @subpackage  Formatter
zYne's avatar
zYne committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @link        www.phpdoctrine.com
 * @since       1.0
 * @version     $Revision$
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 */
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']) {
zYne's avatar
zYne committed
50 51 52 53 54
            $tmp = $this->conn->string_quoting;

            $text = str_replace($tmp['escape_pattern'], 
                                $tmp['escape_pattern'] .
                                $tmp['escape_pattern'], $text);
zYne's avatar
zYne committed
55 56

            foreach ($this->wildcards as $wildcard) {
zYne's avatar
zYne committed
57
                $text = str_replace($wildcard, $tmp['escape_pattern'] . $wildcard, $text);
zYne's avatar
zYne committed
58 59 60 61
            }
        }
        return $text;
    }
62

zYne's avatar
zYne committed
63
    /**
zYne's avatar
zYne committed
64
     * convertBooleans
zYne's avatar
zYne committed
65 66 67 68 69 70 71 72 73 74
     * 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)
    {
75
        if (is_array($item)) {
zYne's avatar
zYne committed
76
            foreach ($item as $k => $value) {
zYne's avatar
zYne committed
77
                if (is_bool($value)) {
zYne's avatar
zYne committed
78 79 80 81 82 83 84 85 86 87
                    $item[$k] = (int) $value;
                }
            }
        } else {
            if (is_bool($item)) {
                $item = (int) $item;
            }
        }
        return $item;
    }
88

zYne's avatar
zYne committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    /**
     * 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 (<kbd>`</kbd>) -- due to MySQL
     *   + double quote (<kbd>"</kbd>) -- due to Oracle
     *   + brackets (<kbd>[</kbd> or <kbd>]</kbd>) -- 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)
    {
zYne's avatar
zYne committed
123
        if ($checkOption && ! $this->conn->getAttribute(Doctrine::ATTR_QUOTE_IDENTIFIER)) {
zYne's avatar
zYne committed
124 125
            return $str;
        }
zYne's avatar
zYne committed
126 127 128 129
        $tmp = $this->conn->identifier_quoting;
        $str = str_replace($tmp['end'],
                           $tmp['escape'] .
                           $tmp['end'], $str);
zYne's avatar
zYne committed
130

zYne's avatar
zYne committed
131
        return $tmp['start'] . $str . $tmp['end'];
zYne's avatar
zYne committed
132
    }
133

zYne's avatar
zYne committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    /**
     * 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':
154
            case 'decimal':
zYne's avatar
zYne committed
155 156 157 158 159 160 161 162 163 164 165 166
            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':
zYne's avatar
zYne committed
167 168
                $this->conn->connect();

zYne's avatar
zYne committed
169
                return $this->conn->getDbh()->quote($input);
zYne's avatar
zYne committed
170 171
        }
    }
172

zYne's avatar
zYne committed
173 174 175 176 177 178 179 180
    /**
     * 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)
    {
zYne's avatar
zYne committed
181
        $seqPattern = '/^'.preg_replace('/%s/', '([a-z0-9_]+)',  $this->conn->getAttribute(Doctrine::ATTR_SEQNAME_FORMAT)).'$/i';
zYne's avatar
zYne committed
182 183 184 185 186 187 188
        $seqName    = preg_replace($seqPattern, '\\1', $sqn);

        if ($seqName && ! strcasecmp($sqn, $this->getSequenceName($seqName))) {
            return $seqName;
        }
        return $sqn;
    }
189

zYne's avatar
zYne committed
190 191 192 193 194 195 196 197
    /**
     * 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)
    {
zYne's avatar
zYne committed
198
        $indexPattern   = '/^'.preg_replace('/%s/', '([a-z0-9_]+)', $this->conn->getAttribute(Doctrine::ATTR_IDXNAME_FORMAT)).'$/i';
zYne's avatar
zYne committed
199 200 201 202 203 204
        $indexName      = preg_replace($indexPattern, '\\1', $idx);
        if ($indexName && ! strcasecmp($idx, $this->getIndexName($indexName))) {
            return $indexName;
        }
        return $idx;
    }
205

zYne's avatar
zYne committed
206 207 208 209 210 211 212 213
    /**
     * adds sequence name formatting to a sequence name
     *
     * @param string    name of the sequence
     * @return string   formatted sequence name
     */
    public function getSequenceName($sqn)
    {
zYne's avatar
zYne committed
214
        return sprintf($this->conn->getAttribute(Doctrine::ATTR_SEQNAME_FORMAT),
zYne's avatar
zYne committed
215 216
            preg_replace('/[^a-z0-9_\$.]/i', '_', $sqn));
    }
217

zYne's avatar
zYne committed
218 219 220 221 222 223 224 225
    /**
     * adds index name formatting to a index name
     *
     * @param string    name of the index
     * @return string   formatted index name
     */
    public function getIndexName($idx)
    {
zYne's avatar
zYne committed
226
        return sprintf($this->conn->getAttribute(Doctrine::ATTR_IDXNAME_FORMAT),
zYne's avatar
zYne committed
227 228
                preg_replace('/[^a-z0-9_\$]/i', '_', $idx));
    }
229 230 231 232 233 234 235 236 237 238 239 240
    
    /**
     * adds table name formatting to a table name
     *
     * @param string    name of the table
     * @return string   formatted table name
     */
    public function getTableName($table)
    {
        return sprintf($this->conn->getAttribute(Doctrine::ATTR_TBLNAME_FORMAT),
                preg_replace('/[^a-z0-9_\$]/i', '_', $table));
    }
241
}