1: <?php
2:
3: namespace Yajra\Oci8\Schema;
4:
5: use Illuminate\Database\Connection;
6: use Illuminate\Database\Schema\Blueprint;
7:
8: class OracleAutoIncrementHelper
9: {
10: 11: 12:
13: protected $connection;
14:
15: 16: 17:
18: protected $trigger;
19:
20: 21: 22:
23: protected $sequence;
24:
25: 26: 27:
28: public function __construct(Connection $connection)
29: {
30: $this->connection = $connection;
31: $this->sequence = new Sequence($connection);
32: $this->trigger = new Trigger($connection);
33: }
34:
35: 36: 37: 38: 39: 40: 41:
42: public function createAutoIncrementObjects(Blueprint $blueprint, $table)
43: {
44: $column = $this->getQualifiedAutoIncrementColumn($blueprint);
45:
46:
47: if (is_null($column)) {
48: return;
49: }
50:
51: $col = $column->name;
52: $start = isset($column->start) ? $column->start : 1;
53:
54:
55: $prefix = $this->connection->getTablePrefix();
56:
57:
58: $sequenceName = $this->createObjectName($prefix, $table, $col, 'seq');
59: $this->sequence->create($sequenceName, $start, $column->nocache);
60:
61:
62: $triggerName = $this->createObjectName($prefix, $table, $col, 'trg');
63: $this->trigger->autoIncrement($prefix . $table, $col, $triggerName, $sequenceName);
64: }
65:
66: 67: 68: 69: 70: 71:
72: public function getQualifiedAutoIncrementColumn(Blueprint $blueprint)
73: {
74: $columns = $blueprint->getColumns();
75:
76:
77: foreach ($columns as $column) {
78:
79: if ($column->autoIncrement) {
80: return $column;
81: }
82: }
83:
84: return null;
85: }
86:
87: 88: 89: 90: 91: 92: 93: 94: 95:
96: private function createObjectName($prefix, $table, $col, $type)
97: {
98:
99: return substr($prefix . $table . '_' . $col . '_' . $type, 0, 30);
100: }
101:
102: 103: 104: 105: 106: 107:
108: public function dropAutoIncrementObjects($table)
109: {
110:
111: $prefix = $this->connection->getTablePrefix();
112:
113: $col = $this->getPrimaryKey($prefix . $table);
114:
115: if (isset($col) && ! empty($col)) {
116:
117: $sequenceName = $this->createObjectName($prefix, $table, $col, 'seq');
118: $this->sequence->drop($sequenceName);
119:
120:
121: $triggerName = $this->createObjectName($prefix, $table, $col, 'trg');
122: $this->trigger->drop($triggerName);
123: }
124: }
125:
126: 127: 128: 129: 130: 131:
132: public function getPrimaryKey($table)
133: {
134: if (! $table) {
135: return '';
136: }
137:
138: $sql = "SELECT cols.column_name
139: FROM all_constraints cons, all_cons_columns cols
140: WHERE upper(cols.table_name) = upper('{$table}')
141: AND cons.constraint_type = 'P'
142: AND cons.constraint_name = cols.constraint_name
143: AND cons.owner = cols.owner
144: AND cols.position = 1
145: AND cons.owner = (select user from dual)
146: ORDER BY cols.table_name, cols.position";
147: $data = $this->connection->selectOne($sql);
148:
149: if (count($data)) {
150: return $data->column_name;
151: }
152:
153: return '';
154: }
155:
156: 157: 158: 159: 160:
161: public function getSequence()
162: {
163: return $this->sequence;
164: }
165:
166: 167: 168: 169: 170:
171: public function setSequence($sequence)
172: {
173: $this->sequence = $sequence;
174: }
175:
176: 177: 178: 179: 180:
181: public function getTrigger()
182: {
183: return $this->trigger;
184: }
185:
186: 187: 188: 189: 190:
191: public function setTrigger($trigger)
192: {
193: $this->trigger = $trigger;
194: }
195: }
196: