Completed
Push — master ( 98947d...62bf16 )
by Arjay
02:01
created

Comment::setComments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
c 5
b 1
f 0
dl 0
loc 11
rs 9.4286
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace Yajra\Oci8\Schema;
4
5
use Illuminate\Database\Connection;
6
use Illuminate\Database\Schema\Grammars\Grammar;
7
use Yajra\Oci8\OracleReservedWords;
8
9
class Comment extends Grammar
10
{
11
    use OracleReservedWords;
12
13
    /**
14
     * @var \Illuminate\Database\Connection
15
     */
16
    protected $connection;
17
18
    /**
19
     * @param Connection $connection
20
     */
21
    public function __construct(Connection $connection)
22
    {
23
        $this->connection = $connection;
24
    }
25
26
    /**
27
     * Set table and column comments.
28
     *
29
     * @param  \Yajra\Oci8\Schema\OracleBlueprint $blueprint
30
     */
31
    public function setComments(OracleBlueprint $blueprint)
32
    {
33
        // Comment set by $table->comment = 'comment';
34
        $this->commentTable($blueprint);
35
36
        // Comments set by $table->string('column')->comment('comment');
37
        $this->fluentComments($blueprint);
38
39
        // Comments set by $table->commentColumns = ['column' => 'comment'];
40
        $this->commentColumns($blueprint);
41
    }
42
43
    /**
44
     * Run the comment on table statement.
45
     *
46
     * @param \Yajra\Oci8\Schema\OracleBlueprint $blueprint
47
     */
48
    private function commentTable(OracleBlueprint $blueprint)
49
    {
50
        $table = $this->wrapValue($blueprint->getTable());
51
52
        if ($blueprint->comment != null) {
53
            $this->connection->statement("comment on table {$table} is '{$blueprint->comment}'");
54
        }
55
    }
56
57
    /**
58
     * Add comments set via fluent setter.
59
     *
60
     * @param \Yajra\Oci8\Schema\OracleBlueprint $blueprint
61
     */
62
    private function fluentComments(OracleBlueprint $blueprint)
63
    {
64
        foreach ($blueprint->getColumns() as $column) {
65
            if (isset($column['comment'])) {
66
                $this->commentColumn($blueprint->getTable(), $column['name'], $column['comment']);
67
            }
68
        }
69
    }
70
71
    /**
72
     * Run the comment on column statement
73
     *
74
     * @param  string $table
75
     * @param  string $column
76
     * @param  string $comment
77
     */
78
    private function commentColumn($table, $column, $comment)
79
    {
80
        $table = $this->wrapValue($table);
81
82
        $column = $this->wrapValue($column);
83
84
        $this->connection->statement("comment on column {$table}.{$column} is '{$comment}'");
85
    }
86
87
    /**
88
     * Add comments on columns.
89
     *
90
     * @param \Yajra\Oci8\Schema\OracleBlueprint $blueprint
91
     */
92
    private function commentColumns(OracleBlueprint $blueprint)
93
    {
94
        foreach ($blueprint->commentColumns as $column => $comment) {
95
            $this->commentColumn($blueprint->getTable(), $column, $comment);
96
        }
97
    }
98
99
    /**
100
     * Wrap reserved words.
101
     *
102
     * @param string $value
103
     * @return string
104
     */
105
    protected function wrapValue($value)
106
    {
107
        return $this->isReserved($value) ? parent::wrapValue($value) : $value;
108
    }
109
}
110