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 |
|
$name = $this->wrap($name); |
37
|
|
|
|
38
|
3 |
|
$nocache = $nocache ? 'nocache' : ''; |
39
|
|
|
|
40
|
|
|
$max = $max ? " maxvalue {$max}" : ''; |
41
|
|
|
|
42
|
|
|
$sequence_stmt = "create sequence {$name} minvalue {$min} {$max} start with {$start} increment by {$increment} {$nocache}"; |
43
|
|
|
|
44
|
|
|
return $this->connection->statement($sequence_stmt); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Wrap sequence name with schema prefix. |
49
|
|
|
* |
50
|
|
|
* @param string $name |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
public function wrap($name) |
54
|
|
|
{ |
55
|
|
|
if ($this->connection->getConfig('prefix_schema')) { |
56
|
|
|
return $this->connection->getConfig('prefix_schema') . '.' . $name; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $name; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* function to safely drop sequence db object. |
64
|
|
|
* |
65
|
|
|
* @param string $name |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
|
|
public function drop($name) |
69
|
|
|
{ |
70
|
|
|
// check if a valid name and sequence exists |
71
|
|
|
if (! $name || ! $this->exists($name)) { |
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$name = $this->wrap($name); |
76
|
|
|
|
77
|
|
|
return $this->connection->statement(" |
78
|
|
|
declare |
79
|
|
|
e exception; |
80
|
|
|
pragma exception_init(e,-02289); |
81
|
|
|
begin |
82
|
|
|
execute immediate 'drop sequence {$name}'; |
83
|
|
|
exception |
84
|
|
|
when e then |
85
|
|
|
null; |
86
|
|
|
end;"); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* function to check if sequence exists. |
91
|
|
|
* |
92
|
|
|
* @param string $name |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
|
View Code Duplication |
public function exists($name) |
96
|
|
|
{ |
97
|
|
|
if (! $name) { |
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$name = $this->wrap($name); |
102
|
|
|
|
103
|
|
|
return $this->connection->selectOne( |
104
|
|
|
"select * from all_sequences where sequence_name=upper('{$name}') and sequence_owner=upper(user)" |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* get sequence next value. |
110
|
|
|
* |
111
|
|
|
* @param string $name |
112
|
|
|
* @return int |
113
|
|
|
*/ |
114
|
|
View Code Duplication |
public function nextValue($name) |
115
|
|
|
{ |
116
|
|
|
if (! $name) { |
117
|
|
|
return 0; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$name = $this->wrap($name); |
121
|
|
|
|
122
|
|
|
return $this->connection->selectOne("SELECT $name.NEXTVAL as id FROM DUAL")->id; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* same function as lastInsertId. added for clarity with oracle sql statement. |
127
|
|
|
* |
128
|
|
|
* @param string $name |
129
|
|
|
* @return int |
130
|
|
|
*/ |
131
|
|
|
public function currentValue($name) |
132
|
|
|
{ |
133
|
|
|
return $this->lastInsertId($name); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* function to get oracle sequence last inserted id. |
138
|
|
|
* |
139
|
|
|
* @param string $name |
140
|
|
|
* @return int |
141
|
|
|
*/ |
142
|
|
View Code Duplication |
public function lastInsertId($name) |
143
|
|
|
{ |
144
|
|
|
// check if a valid name and sequence exists |
145
|
|
|
if (! $name || ! $this->exists($name)) { |
146
|
|
|
return 0; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$name = $this->wrap($name); |
150
|
|
|
|
151
|
|
|
return $this->connection->selectOne("select {$name}.currval as id from dual")->id; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|