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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 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 274 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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 
<?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;

use ICanBoogie\Accessor\AccessorTrait;
use ICanBoogie\PropertyNotDefined;

/**
 * Representation of a schema column.
 *
 * @property-read string $formatted_attributes
 * @property-read string $formatted_auto_increment
 * @property-read string $formatted_charset
 * @property-read string $formatted_comment
 * @property-read string $formatted_default
 * @property-read string $formatted_index
 * @property-read string $formatted_null
 * @property-read string $formatted_type
 * @property-read string|array $primary
 */
class SchemaColumn
{
    const TYPE_BLOB = 'blob';
    const TYPE_BOOLEAN = 'boolean';
    const TYPE_INTEGER = 'integer';
    const TYPE_TEXT = 'text';
    const TYPE_VARCHAR = 'varchar';

    use AccessorTrait;

    /**
     * @var string
     */
    public $type;

    /**
     * @var string|int
     */
    public $size;

    /**
     * @var bool
     */
    public $unsigned = false;

    /**
     * @var mixed
     */
    public $default;

    /**
     * @var bool
     */
    public $null = false;

    /**
     * @var bool
     */
    public $unique = false;

    /**
     * @var bool
     */
    protected $primary = false;

    protected function get_primary()
    {
        return $this->primary;
    }

    /**
     * @var bool
     */
    public $auto_increment = false;

    /**
     * @var bool
     */
    public $indexed = false;

    /**
     * @var string
     */
    public $charset;

    /**
     * @var string
     */
    public $comment;

    /**
     * @param array $options
     */
    public function __construct(array $options)
    {
        static $option_translate = [

            0 => 'type',
            1 => 'size',
            'auto increment' => 'auto_increment'

        ];

        foreach ($options as $option => $value)
        {
            if (isset($option_translate[$option]))
            {
                $option = $option_translate[$option];
            }

            if (!property_exists($this, $option))
            {
                throw new PropertyNotDefined([ $option, $this ]);
            }

            $this->$option = $value;
        }

        switch ($this->type)
        {
            case 'serial':

                $this->type = self::TYPE_INTEGER;
                $this->size = 'big';
                $this->unsigned = true;
                $this->primary = true;
                $this->null = false;
                $this->auto_increment = true;

                break;

            case 'foreign':

                $this->type = self::TYPE_INTEGER;
                $this->size = 'big';
                $this->unsigned = true;
                $this->null = false;
                $this->indexed = !$this->primary;

                break;
        }
    }

    /**
     * Returns the formatted type, including the size.
     *
     * @return string
     */
    protected function get_formatted_type()
    {
        $rc = '';

        $type = $this->type;
        $size = $this->size;

        if (!$size && $type == self::TYPE_VARCHAR)
        {
            $size = 255;
        }

        switch ($type)
        {
            case self::TYPE_INTEGER:
            case self::TYPE_TEXT:
            case self::TYPE_BLOB:

                $t = [

                    self::TYPE_BLOB => 'BLOB',
                    self::TYPE_INTEGER => 'INT',
                    self::TYPE_TEXT => 'TEXT',

                ][ $type ];

                if (is_numeric($size))
                {
                    $rc .= "$t( $size )";
                }
                else
                {
                    $rc .= strtoupper($size) . $t;
                }

                break;

            default:

                if ($size)
                {
                    $rc .= strtoupper($type) . "( $size )";
                }
                else
                {
                    $rc .= strtoupper($type);
                }
        }

        return $rc;
    }

    /**
     * Returns the formatted default.
     *
     * @return string
     */
    protected function get_formatted_default()
    {
        $default = $this->default;

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

        switch ($default)
        {
            case 'CURRENT_TIMESTAMP':

                return "DEFAULT $default";

            default:

                return "DEFAULT '$default'";
        }
    }

    /**
     * Returns the formatted attributes.
     *
     * @return string
     */
    protected function get_formatted_attributes()
    {
        return $this->unsigned ? 'UNSIGNED' : '';
    }

    /**
     * Returns the formatted null.
     *
     * @return string
     */
    protected function get_formatted_null()
    {
        return $this->null ? 'NULL' : 'NOT NULL';
    }

    /**
     * Returns the formatted index.
     *
     * @return string
     */
    protected function get_formatted_index()
    {
        return implode(' ', array_filter([

            $this->primary ? 'PRIMARY KEY' : '',
            $this->unique ? 'UNIQUE' : ''

        ]));
    }

    /**
     * Returns the formatted comment.
     *
     * @return string
     */
    protected function get_formatted_comment()
    {
        return $this->comment ? "`$this->comment`" : '';
    }

    /**
     * Returns the formatted charset.
     *
     * @return string
     */
    protected function get_formatted_charset()
    {
        $charset = $this->charset;

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

        list($charset, $collate) = explode('/', $charset) + [ 1 => null ];

        return "CHARSET $charset" . ($collate ? " COLLATE {$charset}_{$collate}" : '');
    }

    /**
     * Returns the formatted auto increment.
     *
     * @return string
     */
    protected function get_formatted_auto_increment()
    {
        return $this->auto_increment ? 'AUTO_INCREMENT' : '';
    }

    /**
     * Whether the column is a serial column.
     *
     * @return bool
     */
    protected function get_is_serial()
    {
        return $this->type == self::TYPE_INTEGER && !$this->null && $this->auto_increment && $this->primary;
    }

    /**
     * Renders the column into a string.
     *
     * @return string
     */
    public function render()
    {
        return implode(' ', array_filter([

            $this->formatted_type,
            $this->formatted_attributes,
            $this->formatted_charset,
            $this->formatted_null,
            $this->formatted_auto_increment,
            $this->formatted_default,
            $this->formatted_comment

        ]));
    }

    public function __toString()
    {
        return (string) $this->render();
    }
}
ICanBoogie/ActiveRecord master API documentation generated by ApiGen