ICanBoogie/ActiveRecord master
  • Namespace
  • Class

Namespaces

  • ICanBoogie
    • ActiveRecord
      • Driver

Classes

  • ICanBoogie\ActiveRecord
  • ICanBoogie\ActiveRecord\ActiveRecordCacheBase
  • ICanBoogie\ActiveRecord\BelongsToRelation
  • ICanBoogie\ActiveRecord\Connection
  • ICanBoogie\ActiveRecord\ConnectionCollection
  • ICanBoogie\ActiveRecord\ConnectionOptions
  • ICanBoogie\ActiveRecord\DateTimePropertySupport
  • ICanBoogie\ActiveRecord\Driver\BasicDriver
  • ICanBoogie\ActiveRecord\Driver\MySQLDriver
  • ICanBoogie\ActiveRecord\Driver\SQLiteDriver
  • ICanBoogie\ActiveRecord\HasManyRelation
  • ICanBoogie\ActiveRecord\Helpers
  • ICanBoogie\ActiveRecord\Model
  • ICanBoogie\ActiveRecord\ModelCollection
  • ICanBoogie\ActiveRecord\Query
  • ICanBoogie\ActiveRecord\Relation
  • ICanBoogie\ActiveRecord\RelationCollection
  • ICanBoogie\ActiveRecord\RelationNotDefined
  • ICanBoogie\ActiveRecord\RuntimeActiveRecordCache
  • ICanBoogie\ActiveRecord\Schema
  • ICanBoogie\ActiveRecord\SchemaColumn
  • ICanBoogie\ActiveRecord\Statement
  • ICanBoogie\ActiveRecord\Table

Interfaces

  • ICanBoogie\ActiveRecord\ActiveRecordCache
  • ICanBoogie\ActiveRecord\Driver
  • ICanBoogie\ActiveRecord\Exception

Traits

  • ICanBoogie\ActiveRecord\CreatedAtProperty
  • ICanBoogie\ActiveRecord\DateProperty
  • ICanBoogie\ActiveRecord\DateTimeProperty
  • ICanBoogie\ActiveRecord\FinishAtProperty
  • ICanBoogie\ActiveRecord\FinishedAtProperty
  • ICanBoogie\ActiveRecord\StartAtProperty
  • ICanBoogie\ActiveRecord\StartedAtProperty
  • ICanBoogie\ActiveRecord\UpdatedAtProperty

Exceptions

  • ICanBoogie\ActiveRecord\ActiveRecordClassNotValid
  • ICanBoogie\ActiveRecord\ConnectionAlreadyEstablished
  • ICanBoogie\ActiveRecord\ConnectionNotDefined
  • ICanBoogie\ActiveRecord\ConnectionNotEstablished
  • ICanBoogie\ActiveRecord\DriverNotDefined
  • ICanBoogie\ActiveRecord\ModelAlreadyInstantiated
  • ICanBoogie\ActiveRecord\ModelNotDefined
  • ICanBoogie\ActiveRecord\RecordNotFound
  • ICanBoogie\ActiveRecord\ScopeNotDefined
  • ICanBoogie\ActiveRecord\StatementInvocationFailed
  • ICanBoogie\ActiveRecord\StatementNotValid
  • ICanBoogie\ActiveRecord\UnableToSetFetchMode

Functions

  • ICanBoogie\ActiveRecord\extract_charset_and_collate
  • ICanBoogie\ActiveRecord\get_model
  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  26  27  28  29  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  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 
<?php

/*
 * This file is part of the ICanBoogie package.
 *
 * (c) Olivier Laviale <olivier.laviale@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace ICanBoogie\ActiveRecord\Driver;

use ICanBoogie\ActiveRecord\Driver;
use ICanBoogie\ActiveRecord\Schema;
use ICanBoogie\ActiveRecord\SchemaColumn;

/**
 * Connection driver for MySQL.
 */
class MySQLDriver extends BasicDriver
{
    /**
     * @inheritdoc
     */
    public function render_column(SchemaColumn $column)
    {
        return (string) $column;
    }

    /**
     * @inheritdoc
     */
    public function create_table($unprefixed_table_name, Schema $schema)
    {
        $statement = $this->render_create_table($unprefixed_table_name, $schema);
        $this->connection->exec($statement);

        $this->create_indexes($unprefixed_table_name, $schema);
        $this->create_unique_indexes($unprefixed_table_name, $schema);
    }

    /**
     * @inheritdoc
     */
    public function create_indexes($unprefixed_table_name, Schema $schema)
    {
        $this->create_indexes_of('', $unprefixed_table_name, $schema->indexes);
    }

    /**
     * @inheritdoc
     */
    public function create_unique_indexes($unprefixed_table_name, Schema $schema)
    {
        $this->create_indexes_of('UNIQUE', $unprefixed_table_name, $schema->unique_indexes);
    }

    /**
     * @inheritdoc
     */
    public function table_exists($unprefixed_name)
    {
        $tables = $this->connection->query('SHOW TABLES')->all(\PDO::FETCH_COLUMN);

        return in_array($this->resolve_table_name($unprefixed_name), $tables);
    }

    /**
     * @inheritdoc
     */
    public function optimize()
    {
        $connection = $this->connection;
        $tables = $connection->query('SHOW TABLES')->all(\PDO::FETCH_COLUMN);
        $connection->exec('OPTIMIZE TABLE ' . implode(', ', $tables));
    }

    /**
     * Renders _create table_ statement.
     *
     * @param string $unprefixed_table_name
     * @param Schema $schema
     *
     * @return string
     */
    protected function render_create_table($unprefixed_table_name, Schema $schema)
    {
        $connection = $this->connection;
        $quoted_table_name = $this->resolve_quoted_table_name($unprefixed_table_name);
        $lines = $this->render_create_table_lines($schema);
        $lines[] = $this->render_create_table_primary_key($schema);

        return "CREATE TABLE $quoted_table_name\n(\n\t" . implode(",\n\t", array_filter($lines)) . "\n)"
        . " CHARACTER SET $connection->charset  COLLATE $connection->collate";
    }

    /**
     * Renders the lines used to create a table.
     *
     * @param Schema $schema
     *
     * @return array
     */
    protected function render_create_table_lines(Schema $schema)
    {
        $lines = [];

        foreach ($schema as $column_id => $column)
        {
            $lines[$column_id] = $this->render_create_table_line($schema, $column_id, $column);
        }

        return $lines;
    }

    /**
     * Renders a line used to create a table.
     *
     * @param Schema $schema
     * @param string $column_id
     * @param SchemaColumn $column
     *
     * @return string
     */
    protected function render_create_table_line(Schema $schema, $column_id, $column)
    {
        return $this->quote_identifier($column_id) . " " . $this->render_column($column);
    }

    /**
     * Renders primary key clause to create table.
     *
     * @param Schema $schema
     *
     * @return string
     */
    protected function render_create_table_primary_key(Schema $schema)
    {
        $primary = $schema->primary;

        if (!$primary)
        {
            return '';
        }

        $quoted_primary_key = $this->quote_identifier($primary);

        if (is_array($quoted_primary_key))
        {
            $quoted_primary_key = implode(', ', $quoted_primary_key);
        }

        return "PRIMARY KEY($quoted_primary_key)";
    }

    /**
     * Creates indexes of a give type.
     *
     * @param string $type e.g. "UNIQUE".
     * @param string $unprefixed_table_name
     * @param array $indexes
     */
    protected function create_indexes_of($type, $unprefixed_table_name, array $indexes)
    {
        if (!$indexes)
        {
            return;
        }

        $connection = $this->connection;
        $quoted_table_name = $this->resolve_quoted_table_name($unprefixed_table_name);

        if ($type)
        {
            $type .= ' ';
        }

        foreach ($indexes as $index_name => $column_names)
        {
            $column_names = $this->quote_identifier($column_names);
            $rendered_column_names = implode(', ', $column_names);
            $quoted_index_name = $this->quote_identifier($this->resolve_index_name($unprefixed_table_name, $index_name));

            $connection->exec("CREATE {$type}INDEX $quoted_index_name ON $quoted_table_name ($rendered_column_names)");
        }
    }
}
ICanBoogie/ActiveRecord master API documentation generated by ApiGen