Schema.php 16.9 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\sphinx;

10 11 12
use yii\base\Object;
use yii\caching\Cache;
use Yii;
13
use yii\caching\TagDependency;
Alexander Makarov committed
14
use yii\db\Exception;
15

16
/**
17
 * Schema represents the Sphinx schema information.
18
 *
19
 * @property string[] $indexNames All index names in the Sphinx. This property is read-only.
20
 * @property IndexSchema[] $indexSchemas The metadata for all indexes in the Sphinx. Each array element is an
21
 * instance of [[IndexSchema]] or its child class. This property is read-only.
22 23 24
 * @property array $indexTypes All index types in the Sphinx in format: index name => index type. This
 * property is read-only.
 * @property QueryBuilder $queryBuilder The query builder for this connection. This property is read-only.
25
 *
26 27 28
 * @author Paul Klimov <klimov.paul@gmail.com>
 * @since 2.0
 */
29
class Schema extends Object
30
{
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
    /**
     * The followings are the supported abstract column data types.
     */
    const TYPE_PK = 'pk';
    const TYPE_STRING = 'string';
    const TYPE_INTEGER = 'integer';
    const TYPE_BIGINT = 'bigint';
    const TYPE_FLOAT = 'float';
    const TYPE_TIMESTAMP = 'timestamp';
    const TYPE_BOOLEAN = 'boolean';

    /**
     * @var Connection the Sphinx connection
     */
    public $db;
    /**
     * @var array list of ALL index names in the Sphinx
     */
    private $_indexNames;
    /**
     * @var array list of ALL index types in the Sphinx (index name => index type)
     */
    private $_indexTypes;
    /**
     * @var array list of loaded index metadata (index name => IndexSchema)
     */
    private $_indexes = [];
    /**
     * @var QueryBuilder the query builder for this Sphinx connection
     */
    private $_builder;

63

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    /**
     * @var array mapping from physical column types (keys) to abstract column types (values)
     */
    public $typeMap = [
        'field' => self::TYPE_STRING,
        'string' => self::TYPE_STRING,
        'ordinal' => self::TYPE_STRING,
        'integer' => self::TYPE_INTEGER,
        'int' => self::TYPE_INTEGER,
        'uint' => self::TYPE_INTEGER,
        'bigint' => self::TYPE_BIGINT,
        'timestamp' => self::TYPE_TIMESTAMP,
        'bool' => self::TYPE_BOOLEAN,
        'float' => self::TYPE_FLOAT,
        'mva' => self::TYPE_INTEGER,
    ];

    /**
     * Loads the metadata for the specified index.
83
     * @param string $name index name
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
     * @return IndexSchema driver dependent index metadata. Null if the index does not exist.
     */
    protected function loadIndexSchema($name)
    {
        $index = new IndexSchema;
        $this->resolveIndexNames($index, $name);
        $this->resolveIndexType($index);

        if ($this->findColumns($index)) {
            return $index;
        } else {
            return null;
        }
    }

    /**
     * Resolves the index name.
     * @param IndexSchema $index the index metadata object
102
     * @param string $name the index name
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
     */
    protected function resolveIndexNames($index, $name)
    {
        $index->name = str_replace('`', '', $name);
    }

    /**
     * Resolves the index name.
     * @param IndexSchema $index the index metadata object
     */
    protected function resolveIndexType($index)
    {
        $indexTypes = $this->getIndexTypes();
        $index->type = array_key_exists($index->name, $indexTypes) ? $indexTypes[$index->name] : 'unknown';
        $index->isRuntime = ($index->type == 'rt');
    }

    /**
     * Obtains the metadata for the named index.
122 123
     * @param string $name index name. The index name may contain schema name if any. Do not quote the index name.
     * @param boolean $refresh whether to reload the index schema even if it is found in the cache.
124 125 126 127 128 129 130 131 132 133 134 135
     * @return IndexSchema index metadata. Null if the named index does not exist.
     */
    public function getIndexSchema($name, $refresh = false)
    {
        if (isset($this->_indexes[$name]) && !$refresh) {
            return $this->_indexes[$name];
        }

        $db = $this->db;
        $realName = $this->getRawIndexName($name);

        if ($db->enableSchemaCache && !in_array($name, $db->schemaCacheExclude, true)) {
136
            /* @var $cache Cache */
137
            $cache = is_string($db->schemaCache) ? Yii::$app->get($db->schemaCache, false) : $db->schemaCache;
138 139 140 141 142
            if ($cache instanceof Cache) {
                $key = $this->getCacheKey($name);
                if ($refresh || ($index = $cache->get($key)) === false) {
                    $index = $this->loadIndexSchema($realName);
                    if ($index !== null) {
143 144
                        $cache->set($key, $index, $db->schemaCacheDuration, new TagDependency([
                            'tags' => $this->getCacheTag(),
145 146 147 148 149 150 151 152 153 154 155 156 157
                        ]));
                    }
                }

                return $this->_indexes[$name] = $index;
            }
        }

        return $this->_indexes[$name] = $this->loadIndexSchema($realName);
    }

    /**
     * Returns the cache key for the specified index name.
158 159
     * @param string $name the index name
     * @return mixed the cache key
160 161 162 163 164 165 166 167 168 169 170 171
     */
    protected function getCacheKey($name)
    {
        return [
            __CLASS__,
            $this->db->dsn,
            $this->db->username,
            $name,
        ];
    }

    /**
172
     * Returns the cache tag name.
173
     * This allows [[refresh()]] to invalidate all cached index schemas.
174
     * @return string the cache tag name
175
     */
176
    protected function getCacheTag()
177 178 179 180 181 182 183 184 185 186
    {
        return md5(serialize([
            __CLASS__,
            $this->db->dsn,
            $this->db->username,
        ]));
    }

    /**
     * Returns the metadata for all indexes in the database.
187 188
     * @param boolean $refresh whether to fetch the latest available index schemas. If this is false,
     * cached data may be returned if available.
189
     * @return IndexSchema[] the metadata for all indexes in the Sphinx.
190
     * Each array element is an instance of [[IndexSchema]] or its child class.
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
     */
    public function getIndexSchemas($refresh = false)
    {
        $indexes = [];
        foreach ($this->getIndexNames($refresh) as $name) {
            if (($index = $this->getIndexSchema($name, $refresh)) !== null) {
                $indexes[] = $index;
            }
        }

        return $indexes;
    }

    /**
     * Returns all index names in the Sphinx.
206 207
     * @param boolean $refresh whether to fetch the latest available index names. If this is false,
     * index names fetched previously (if available) will be returned.
208 209 210 211 212 213 214 215 216 217 218 219 220
     * @return string[] all index names in the Sphinx.
     */
    public function getIndexNames($refresh = false)
    {
        if (!isset($this->_indexNames) || $refresh) {
            $this->initIndexesInfo();
        }

        return $this->_indexNames;
    }

    /**
     * Returns all index types in the Sphinx.
221 222 223
     * @param boolean $refresh whether to fetch the latest available index types. If this is false,
     * index types fetched previously (if available) will be returned.
     * @return array all index types in the Sphinx in format: index name => index type.
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
     */
    public function getIndexTypes($refresh = false)
    {
        if (!isset($this->_indexTypes) || $refresh) {
            $this->initIndexesInfo();
        }

        return $this->_indexTypes;
    }

    /**
     * Initializes information about name and type of all index in the Sphinx.
     */
    protected function initIndexesInfo()
    {
        $this->_indexNames = [];
        $this->_indexTypes = [];
        $indexes = $this->findIndexes();
        foreach ($indexes as $index) {
            $indexName = $index['Index'];
            $this->_indexNames[] = $indexName;
            $this->_indexTypes[$indexName] = $index['Type'];
        }
    }

    /**
     * Returns all index names in the Sphinx.
     * @return array all index names in the Sphinx.
     */
    protected function findIndexes()
    {
        $sql = 'SHOW TABLES';

        return $this->db->createCommand($sql)->queryAll();
    }

    /**
     * @return QueryBuilder the query builder for this connection.
     */
    public function getQueryBuilder()
    {
        if ($this->_builder === null) {
            $this->_builder = $this->createQueryBuilder();
        }

        return $this->_builder;
    }

    /**
     * Determines the PDO type for the given PHP data value.
274
     * @param mixed $data the data whose PDO type is to be determined
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
     * @return integer the PDO type
     * @see http://www.php.net/manual/en/pdo.constants.php
     */
    public function getPdoType($data)
    {
        static $typeMap = [
            // php type => PDO type
            'boolean' => \PDO::PARAM_BOOL,
            'integer' => \PDO::PARAM_INT,
            'string' => \PDO::PARAM_STR,
            'resource' => \PDO::PARAM_LOB,
            'NULL' => \PDO::PARAM_NULL,
        ];
        $type = gettype($data);

        return isset($typeMap[$type]) ? $typeMap[$type] : \PDO::PARAM_STR;
    }

    /**
     * Refreshes the schema.
     * This method cleans up all cached index schemas so that they can be re-created later
     * to reflect the Sphinx schema change.
     */
    public function refresh()
    {
300
        /* @var $cache Cache */
301
        $cache = is_string($this->db->schemaCache) ? Yii::$app->get($this->db->schemaCache, false) : $this->db->schemaCache;
302
        if ($this->db->enableSchemaCache && $cache instanceof Cache) {
303
            TagDependency::invalidate($cache, $this->getCacheTag());
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
        }
        $this->_indexNames = [];
        $this->_indexes = [];
    }

    /**
     * Creates a query builder for the Sphinx.
     * @return QueryBuilder query builder instance
     */
    public function createQueryBuilder()
    {
        return new QueryBuilder($this->db);
    }

    /**
     * Quotes a string value for use in a query.
     * Note that if the parameter is not a string, it will be returned without change.
321
     * @param string $str string to be quoted
322 323 324 325 326
     * @return string the properly quoted string
     * @see http://www.php.net/manual/en/function.PDO-quote.php
     */
    public function quoteValue($str)
    {
Qiang Xue committed
327
        if (is_string($str)) {
328
            return $this->db->getSlavePdo()->quote($str);
Qiang Xue committed
329
        } else {
330 331 332 333 334 335 336 337 338
            return $str;
        }
    }

    /**
     * Quotes a index name for use in a query.
     * If the index name contains schema prefix, the prefix will also be properly quoted.
     * If the index name is already quoted or contains '(' or '{{',
     * then this method will do nothing.
339
     * @param string $name index name
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
     * @return string the properly quoted index name
     * @see quoteSimpleTableName
     */
    public function quoteIndexName($name)
    {
        if (strpos($name, '(') !== false || strpos($name, '{{') !== false) {
            return $name;
        }

        return $this->quoteSimpleIndexName($name);
    }

    /**
     * Quotes a column name for use in a query.
     * If the column name contains prefix, the prefix will also be properly quoted.
     * If the column name is already quoted or contains '(', '[[' or '{{',
     * then this method will do nothing.
357
     * @param string $name column name
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
     * @return string the properly quoted column name
     * @see quoteSimpleColumnName
     */
    public function quoteColumnName($name)
    {
        if (strpos($name, '(') !== false || strpos($name, '[[') !== false || strpos($name, '{{') !== false) {
            return $name;
        }
        if (($pos = strrpos($name, '.')) !== false) {
            $prefix = $this->quoteIndexName(substr($name, 0, $pos)) . '.';
            $name = substr($name, $pos + 1);
        } else {
            $prefix = '';
        }

        return $prefix . $this->quoteSimpleColumnName($name);
    }

    /**
     * Quotes a index name for use in a query.
     * A simple index name has no schema prefix.
379
     * @param string $name index name
380 381 382 383 384 385 386 387 388 389
     * @return string the properly quoted index name
     */
    public function quoteSimpleIndexName($name)
    {
        return strpos($name, "`") !== false ? $name : "`" . $name . "`";
    }

    /**
     * Quotes a column name for use in a query.
     * A simple column name has no prefix.
390
     * @param string $name column name
391 392 393 394 395 396 397 398 399 400 401
     * @return string the properly quoted column name
     */
    public function quoteSimpleColumnName($name)
    {
        return strpos($name, '`') !== false || $name === '*' ? $name : '`' . $name . '`';
    }

    /**
     * Returns the actual name of a given index name.
     * This method will strip off curly brackets from the given index name
     * and replace the percentage character '%' with [[Connection::indexPrefix]].
402
     * @param string $name the index name to be converted
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
     * @return string the real name of the given index name
     */
    public function getRawIndexName($name)
    {
        if (strpos($name, '{{') !== false) {
            $name = preg_replace('/\\{\\{(.*?)\\}\\}/', '\1', $name);

            return str_replace('%', $this->db->tablePrefix, $name);
        } else {
            return $name;
        }
    }

    /**
     * Extracts the PHP type from abstract DB type.
418 419
     * @param ColumnSchema $column the column schema information
     * @return string PHP type name
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
     */
    protected function getColumnPhpType($column)
    {
        static $typeMap = [ // abstract type => php type
            'smallint' => 'integer',
            'integer' => 'integer',
            'bigint' => 'integer',
            'boolean' => 'boolean',
            'float' => 'double',
        ];
        if (isset($typeMap[$column->type])) {
            if ($column->type === 'bigint') {
                return PHP_INT_SIZE == 8 ? 'integer' : 'string';
            } elseif ($column->type === 'integer') {
                return PHP_INT_SIZE == 4 ? 'string' : 'integer';
            } else {
                return $typeMap[$column->type];
            }
        } else {
            return 'string';
        }
    }

    /**
     * Collects the metadata of index columns.
445 446 447
     * @param IndexSchema $index the index metadata
     * @return boolean whether the index exists in the database
     * @throws \Exception if DB query fails
448 449 450 451 452 453 454 455
     */
    protected function findColumns($index)
    {
        $sql = 'DESCRIBE ' . $this->quoteSimpleIndexName($index->name);
        try {
            $columns = $this->db->createCommand($sql)->queryAll();
        } catch (\Exception $e) {
            $previous = $e->getPrevious();
456
            if ($previous instanceof \PDOException && strpos($previous->getMessage(), 'SQLSTATE[42S02') !== false) {
457
                // index does not exist
458
                // https://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html#error_er_bad_table_error
459 460 461 462
                return false;
            }
            throw $e;
        }
463 464 465 466 467 468 469 470

        if (empty($columns[0]['Agent'])) {
            foreach ($columns as $info) {
                $column = $this->loadColumnSchema($info);
                $index->columns[$column->name] = $column;
                if ($column->isPrimaryKey) {
                    $index->primaryKey = $column->name;
                }
471
            }
472 473 474 475
        } else {
            // Distributed index :
            $agent = $this->getIndexSchema($columns[0]['Agent']);
            $index->columns = $agent->columns;
476 477 478 479 480 481 482
        }

        return true;
    }

    /**
     * Loads the column information into a [[ColumnSchema]] object.
483
     * @param array $info column information
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
     * @return ColumnSchema the column schema object
     */
    protected function loadColumnSchema($info)
    {
        $column = new ColumnSchema;

        $column->name = $info['Field'];
        $column->dbType = $info['Type'];

        $column->isPrimaryKey = ($column->name == 'id');

        $type = $info['Type'];
        if (isset($this->typeMap[$type])) {
            $column->type = $this->typeMap[$type];
        } else {
            $column->type = self::TYPE_STRING;
        }

        $column->isField = ($type == 'field');
        $column->isAttribute = !$column->isField;

        $column->isMva = ($type == 'mva');

        $column->phpType = $this->getColumnPhpType($column);

        return $column;
    }
Alexander Makarov committed
511 512

    /**
Qiang Xue committed
513
     * Converts a DB exception to a more concrete one if possible.
Alexander Makarov committed
514 515 516
     *
     * @param \Exception $e
     * @param string $rawSql SQL that produced exception
Qiang Xue committed
517
     * @return Exception
Alexander Makarov committed
518
     */
Qiang Xue committed
519
    public function convertException(\Exception $e, $rawSql)
Alexander Makarov committed
520 521
    {
        if ($e instanceof Exception) {
Qiang Xue committed
522
            return $e;
Alexander Makarov committed
523 524 525
        } else {
            $message = $e->getMessage()  . "\nThe SQL being executed was: $rawSql";
            $errorInfo = $e instanceof \PDOException ? $e->errorInfo : null;
Qiang Xue committed
526
            return new Exception($message, $errorInfo, (int) $e->getCode(), $e);
Alexander Makarov committed
527 528
        }
    }
Qiang Xue committed
529 530 531 532 533 534 535 536 537 538 539

    /**
     * Returns a value indicating whether a SQL statement is for read purpose.
     * @param string $sql the SQL statement
     * @return boolean whether a SQL statement is for read purpose.
     */
    public function isReadQuery($sql)
    {
        $pattern = '/^\s*(SELECT|SHOW|DESCRIBE)\b/i';
        return preg_match($pattern, $sql) > 0;
    }
AlexGx committed
540
}