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

Trigger   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 65
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A drop() 0 17 2
B autoIncrement() 0 16 5
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