1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\Oci8\Schema; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Connection; |
6
|
|
|
|
7
|
|
|
class Sequence |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var \Illuminate\Database\Connection|\Yajra\Oci8\Oci8Connection |
11
|
|
|
*/ |
12
|
|
|
protected $connection; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @param Connection $connection |
16
|
|
|
*/ |
17
|
3 |
|
public function __construct(Connection $connection) |
18
|
|
|
{ |
19
|
3 |
|
$this->connection = $connection; |
20
|
3 |
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* function to create oracle sequence. |
24
|
|
|
* |
25
|
|
|
* @param string $name |
26
|
|
|
* @param int $start |
27
|
|
|
* @param bool $nocache |
28
|
|
|
* @return bool |
29
|
|
|
*/ |
30
|
3 |
|
public function create($name, $start = 1, $nocache = false, $min = 1, $max = false, $increment = 1) |
31
|
|
|
{ |
32
|
3 |
|
if (! $name) { |
33
|
|
|
return false; |
34
|
|
|
} |
35
|
|
|
|
36
|
3 |
|
if ($this->connection->getConfig('prefix_schema')) { |
37
|
|
|
$name = $this->connection->getConfig('prefix_schema') . '.' . $name; |
38
|
3 |
|
} |
39
|
|
|
|
40
|
|
|
$nocache = $nocache ? 'nocache' : ''; |
41
|
|
|
|
42
|
|
|
$max = $max ? " maxvalue {$max}" : ""; |
43
|
|
|
|
44
|
|
|
$sequence_stmt = "create sequence {$name} minvalue {$min} {$max} start with {$start} increment by {$increment} {$nocache}"; |
45
|
|
|
|
46
|
|
|
return $this->connection->statement($sequence_stmt); |
47
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* function to safely drop sequence db object. |
52
|
|
|
* |
53
|
|
|
* @param string $name |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
public function drop($name) |
57
|
|
|
{ |
58
|
|
|
// check if a valid name and sequence exists |
59
|
|
|
if (! $name || ! $this->exists($name)) { |
60
|
|
|
return false; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $this->connection->statement(" |
64
|
|
|
declare |
65
|
|
|
e exception; |
66
|
|
|
pragma exception_init(e,-02289); |
67
|
|
|
begin |
68
|
|
|
execute immediate 'drop sequence {$name}'; |
69
|
|
|
exception |
70
|
|
|
when e then |
71
|
|
|
null; |
72
|
|
|
end;"); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* function to check if sequence exists. |
77
|
|
|
* |
78
|
|
|
* @param string $name |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
|
public function exists($name) |
82
|
|
|
{ |
83
|
|
|
if (! $name) { |
84
|
|
|
return false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this->connection->selectOne( |
88
|
|
|
"select * from all_sequences where sequence_name=upper('{$name}') and sequence_owner=upper(user)" |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* get sequence next value. |
94
|
|
|
* |
95
|
|
|
* @param string $name |
96
|
|
|
* @return int |
97
|
|
|
*/ |
98
|
|
|
public function nextValue($name) |
99
|
|
|
{ |
100
|
|
|
if (! $name) { |
101
|
|
|
return 0; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this->connection->selectOne("SELECT $name.NEXTVAL as id FROM DUAL")->id; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* same function as lastInsertId. added for clarity with oracle sql statement. |
109
|
|
|
* |
110
|
|
|
* @param string $name |
111
|
|
|
* @return int |
112
|
|
|
*/ |
113
|
|
|
public function currentValue($name) |
114
|
|
|
{ |
115
|
|
|
return $this->lastInsertId($name); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* function to get oracle sequence last inserted id. |
120
|
|
|
* |
121
|
|
|
* @param string $name |
122
|
|
|
* @return int |
123
|
|
|
*/ |
124
|
|
|
public function lastInsertId($name) |
125
|
|
|
{ |
126
|
|
|
// check if a valid name and sequence exists |
127
|
|
|
if (! $name || ! $this->exists($name)) { |
128
|
|
|
return 0; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $this->connection->selectOne("select {$name}.currval as id from dual")->id; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|