Completed
Push — develop ( 3b8e4a...656576 )
by Arjay
03:44 queued 02:00
created

Trigger::drop()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 17
rs 9.4286
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Yajra\Oci8\Schema;
4
5
use Illuminate\Database\Connection;
6
7
class Trigger
8
{
9
    /**
10
     * @var \Illuminate\Database\Connection
11
     */
12
    protected $connection;
13
14
    /**
15
     * @param Connection $connection
16
     */
17
    public function __construct(Connection $connection)
18
    {
19
        $this->connection = $connection;
20
    }
21
22
    /**
23
     * function to create auto increment trigger for a table
24
     *
25
     * @param  string $table
26
     * @param  string $column
27
     * @param  string $triggerName
28
     * @param  string $sequenceName
29
     * @return boolean
30
     */
31
    public function autoIncrement($table, $column, $triggerName, $sequenceName)
32
    {
33
        if (! $table or ! $column or ! $triggerName or ! $sequenceName) {
34
            return false;
35
        }
36
37
        return $this->connection->statement("
38
            create trigger $triggerName
39
            before insert on {$table}
40
            for each row
41
                begin
42
            if :new.{$column} is null then
43
                select {$sequenceName}.nextval into :new.{$column} from dual;
44
            end if;
45
            end;");
46
    }
47
48
    /**
49
     * function to safely drop trigger db object
50
     *
51
     * @param  string $name
52
     * @return boolean
53
     */
54
    public function drop($name)
55
    {
56
        if (! $name) {
57
            return false;
58
        }
59
60
        return $this->connection->statement("
61
            declare
62
                e exception;
63
                pragma exception_init(e,-4080);
64
            begin
65
                execute immediate 'drop trigger {$name}';
66
            exception
67
            when e then
68
                null;
69
            end;");
70
    }
71
}
72