Completed
Push — master ( 9b777b...ee89da )
by Arjay
14:21 queued 10s
created

Sequence::wrap()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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