DboSource::name PHP Method

name() public method

Strips fields out of SQL functions before quoting. Results of this method are stored in a memory cache. This improves performance, but because the method uses a hashing algorithm it can have collisions. Setting DboSource::$cacheMethods to false will disable the memory cache.
public name ( mixed $data ) : string
$data mixed Either a string with a column to quote. An array of columns to quote or an object from DboSource::expression() or DboSource::identifier()
return string SQL field
    public function name($data)
    {
        if (is_object($data) && isset($data->type)) {
            return $data->value;
        }
        if ($data === '*') {
            return '*';
        }
        if (is_array($data)) {
            foreach ($data as $i => $dataItem) {
                $data[$i] = $this->name($dataItem);
            }
            return $data;
        }
        $cacheKey = md5($this->startQuote . $data . $this->endQuote);
        if ($return = $this->cacheMethod(__FUNCTION__, $cacheKey)) {
            return $return;
        }
        $data = trim($data);
        if (preg_match('/^[\\w-]+(?:\\.[^ \\*]*)*$/', $data)) {
            // string, string.string
            if (strpos($data, '.') === false) {
                // string
                return $this->cacheMethod(__FUNCTION__, $cacheKey, $this->startQuote . $data . $this->endQuote);
            }
            $items = explode('.', $data);
            return $this->cacheMethod(__FUNCTION__, $cacheKey, $this->startQuote . implode($this->endQuote . '.' . $this->startQuote, $items) . $this->endQuote);
        }
        if (preg_match('/^[\\w-]+\\.\\*$/', $data)) {
            // string.*
            return $this->cacheMethod(__FUNCTION__, $cacheKey, $this->startQuote . str_replace('.*', $this->endQuote . '.*', $data));
        }
        if (preg_match('/^([\\w-]+)\\((.*)\\)$/', $data, $matches)) {
            // Functions
            return $this->cacheMethod(__FUNCTION__, $cacheKey, $matches[1] . '(' . $this->name($matches[2]) . ')');
        }
        if (preg_match('/^([\\w-]+(\\.[\\w-]+|\\(.*\\))*)\\s+' . preg_quote($this->alias) . '\\s*([\\w-]+)$/i', $data, $matches)) {
            return $this->cacheMethod(__FUNCTION__, $cacheKey, preg_replace('/\\s{2,}/', ' ', $this->name($matches[1]) . ' ' . $this->alias . ' ' . $this->name($matches[3])));
        }
        if (preg_match('/^[\\w-_\\s]*[\\w-_]+/', $data)) {
            return $this->cacheMethod(__FUNCTION__, $cacheKey, $this->startQuote . $data . $this->endQuote);
        }
        return $this->cacheMethod(__FUNCTION__, $cacheKey, $data);
    }

Usage Example

Exemplo n.º 1
0
 /**
  * SQL file header
  *
  * @param  $datasource
  * @return string
  */
 function _createSqlDumpHeader($datasource)
 {
     $sql = array();
     $sql[] = $this->hr(0);
     $sql[] = '-- ' . $this->message;
     $sql[] = '-- generated on: ' . date('Y-m-d H:i:s') . ' : ' . time();
     $sql[] = $this->hr(0);
     $sql[] = '';
     if (preg_match('/^mysql/i', $this->DataSource->config['driver'])) {
         $sql[] = 'use ' . $this->DataSource->name($this->DataSource->config['database']) . ';';
     }
     if (!empty($this->DataSource->config['encoding'])) {
         $sql[] = 'SET NAMES ' . $this->DataSource->value($this->DataSource->config['encoding']) . ';';
     }
     return $this->out($sql);
 }
All Usage Examples Of DboSource::name
DboSource