Passed
Push — master ( b9affb...b0865c )
by Jonas
14:48 queued 32s
created

CompilesViews   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 56
ccs 9
cts 9
cp 1
rs 10
c 1
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A compileRefreshMaterializedView() 0 3 1
A compileCreateView() 0 9 4
A compileViewExists() 0 3 1
A compileDropView() 0 5 2
1
<?php
2
3
namespace Staudenmeir\LaravelMigrationViews\Schema\Grammars;
4
5
trait CompilesViews
6
{
7
    /**
8
     * Compile the query to create a view.
9
     *
10
     * @param string $name
11
     * @param string $query
12
     * @param array|null $columns
13
     * @param bool $orReplace
14
     * @param bool $materialized
15
     * @return string
16 40
     */
17
    public function compileCreateView($name, $query, $columns, $orReplace, bool $materialized = false)
18 40
    {
19
        $orReplaceSql = $orReplace ? 'or replace ' : '';
20 40
21
        $materializedSql = $materialized ? 'materialized ' : '';
22 40
23
        $columns = $columns ? '('.$this->columnize($columns).') ' : '';
0 ignored issues
show
Bug introduced by
It seems like columnize() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

23
        $columns = $columns ? '('.$this->/** @scrutinizer ignore-call */ columnize($columns).') ' : '';
Loading history...
24
25
        return 'create '.$orReplaceSql.$materializedSql.'view '.$this->wrapTable($name).' '.$columns.'as '.$query;
0 ignored issues
show
Bug introduced by
It seems like wrapTable() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

25
        return 'create '.$orReplaceSql.$materializedSql.'view '.$this->/** @scrutinizer ignore-call */ wrapTable($name).' '.$columns.'as '.$query;
Loading history...
26
    }
27
28
    /**
29
     * Compile the query to drop a view.
30
     *
31
     * @param string $name
32 33
     * @param bool $ifExists
33
     * @return string
34 33
     */
35
    public function compileDropView($name, $ifExists)
36 33
    {
37
        $ifExists = $ifExists ? 'if exists ' : '';
38
39
        return 'drop view '.$ifExists.$this->wrapTable($name);
40
    }
41
42
    /**
43
     * Compile the query to determine if a view exists.
44 6
     *
45
     * @return string
46 6
     */
47
    public function compileViewExists()
48
    {
49
        return 'select * from information_schema.views where table_schema = ? and table_name = ?';
50
    }
51
52
    /**
53
     * Compile the query to refresh a materialized view.
54
     *
55
     * @param string $name
56
     * @return string
57
     */
58
    public function compileRefreshMaterializedView(string $name): string
59
    {
60
        return 'refresh materialized view ' . $this->wrapTable($name);
61
    }
62
}
63