|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yajra\Oci8\Schema; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Connection; |
|
6
|
|
|
use Yajra\Oci8\OracleReservedWords; |
|
7
|
|
|
|
|
8
|
|
|
class Trigger |
|
9
|
|
|
{ |
|
10
|
|
|
use OracleReservedWords; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @var \Illuminate\Database\Connection|\Yajra\Oci8\Oci8Connection |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $connection; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param Connection $connection |
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct(Connection $connection) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->connection = $connection; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Function to create auto increment trigger for a table. |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $table |
|
29
|
|
|
* @param string $column |
|
30
|
|
|
* @param string $triggerName |
|
31
|
|
|
* @param string $sequenceName |
|
32
|
|
|
* @return boolean |
|
33
|
|
|
*/ |
|
34
|
|
|
public function autoIncrement($table, $column, $triggerName, $sequenceName) |
|
35
|
|
|
{ |
|
36
|
|
|
if (! $table || ! $column || ! $triggerName || ! $sequenceName) { |
|
37
|
|
|
return false; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$table = $this->wrapValue($table); |
|
41
|
|
|
$column = $this->wrapValue($column); |
|
42
|
|
|
|
|
43
|
|
|
return $this->connection->statement(" |
|
44
|
|
|
create trigger $triggerName |
|
45
|
|
|
before insert on {$table} |
|
46
|
|
|
for each row |
|
47
|
|
|
begin |
|
48
|
|
|
if :new.{$column} is null then |
|
49
|
|
|
select {$sequenceName}.nextval into :new.{$column} from dual; |
|
50
|
|
|
end if; |
|
51
|
|
|
end;"); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Wrap value if reserved word. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $value |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function wrapValue($value) |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->isReserved($value) ? '"' . $value . '"' : $value; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Function to safely drop trigger db object. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $name |
|
69
|
|
|
* @return boolean |
|
70
|
|
|
*/ |
|
71
|
|
|
public function drop($name) |
|
72
|
|
|
{ |
|
73
|
|
|
if (! $name) { |
|
74
|
|
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $this->connection->statement("declare |
|
78
|
|
|
e exception; |
|
79
|
|
|
pragma exception_init(e,-4080); |
|
80
|
|
|
begin |
|
81
|
|
|
execute immediate 'drop trigger {$name}'; |
|
82
|
|
|
exception |
|
83
|
|
|
when e then |
|
84
|
|
|
null; |
|
85
|
|
|
end;"); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|