• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ICanBoogie / ActiveRecord / 4542546258

pending completion
4542546258

push

github

Olivier Laviale
Add 'belongs_to' to the SchemaBuilder

29 of 29 new or added lines in 4 files covered. (100.0%)

1356 of 1726 relevant lines covered (78.56%)

36.14 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/lib/ActiveRecord/Driver/MySQLDriver.php
1
<?php
2

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

12
namespace ICanBoogie\ActiveRecord\Driver;
13

14
use ICanBoogie\ActiveRecord\Schema;
15
use ICanBoogie\ActiveRecord\SchemaColumn;
16
use ICanBoogie\ActiveRecord\SchemaIndex;
17

18
use function array_filter;
19
use function array_map;
20
use function implode;
21
use function is_array;
22

23
/**
24
 * Connection driver for MySQL.
25
 */
26
final class MySQLDriver extends BasicDriver
27
{
28
    /**
29
     * @inheritDoc
30
     */
31
    protected function render_create_table(string $table_name, Schema $schema): string
32
    {
33
        $connection = $this->connection;
×
34
        $quoted_table_name = $this->quote_identifier($table_name);
×
35
        $lines = $this->render_create_table_lines($schema);
×
36
        $lines = implode(",\n    ", array_filter($lines));
×
37

38
        $maybeCollate = $connection->collate ? " COLLATE $connection->collate" : '';
×
39

40
        return <<<SQL
×
41
        CREATE TABLE $quoted_table_name (
×
42
            $lines
×
43
        ){$maybeCollate}
×
44
        SQL;
×
45
    }
46

47
    /**
48
     * @inheritDoc
49
     */
50
    protected function render_create_index(string $table_name, SchemaIndex $index): string
51
    {
52
        $quoted_table_name = $this->quote_identifier($table_name);
×
53
        $maybeUnique = $index->unique ? 'UNIQUE ' : '';
×
54
        $index_name = $this->quote_identifier($index->name ?? implode('_', $index->columns));
×
55
        $indexed_columns = implode(
×
56
            ', ',
×
57
            array_map(
×
58
                fn($column_id) => $this->quote_identifier($column_id),
×
59
                $index->columns
×
60
            )
×
61
        );
×
62

63
        return <<<SQL
×
64
            CREATE {$maybeUnique}INDEX $index_name ON $quoted_table_name ($indexed_columns)
×
65
            SQL;
×
66
    }
67

68
    /**
69
     * @inheritDoc
70
     */
71
    public function render_column(SchemaColumn $column): string
72
    {
73
        return (string)$column;
×
74
    }
75

76
    /**
77
     * @inheritdoc
78
     */
79
    public function table_exists(string $name): bool
80
    {
81
        $tables = $this->connection->query('SHOW TABLES')->all(\PDO::FETCH_COLUMN);
×
82

83
        return \in_array($name, $tables);
×
84
    }
85

86
    /**
87
     * @inheritdoc
88
     */
89
    public function optimize(): void
90
    {
91
        $connection = $this->connection;
×
92
        $tables = $connection->query('SHOW TABLES')->all(\PDO::FETCH_COLUMN);
×
93
        $connection->exec('OPTIMIZE TABLE ' . implode(', ', $tables));
×
94
    }
95

96
    /**
97
     * Renders primary key clause to create table.
98
     */
99
    private function render_create_table_primary_key(Schema $schema): string
100
    {
101
        $primary = $schema->primary;
×
102

103
        if (!$primary) {
×
104
            return '';
×
105
        }
106

107
        if (is_array($primary)) {
×
108
            $quoted_primary_key = implode(
×
109
                ', ',
×
110
                array_map(
×
111
                    fn(string $column_id) => $this->quote_identifier($column_id),
×
112
                    $primary
×
113
                )
×
114
            );
×
115
        } else {
116
            $quoted_primary_key = $this->quote_identifier($primary);
×
117
        }
118

119
        return "PRIMARY KEY($quoted_primary_key)";
×
120
    }
121
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc