Overview

Namespaces

  • None
  • Yajra
    • Oci8
      • Auth
      • Connectors
      • Eloquent
      • Query
        • Grammars
        • Processors
      • Schema
        • Grammars

Classes

  • Yajra\Oci8\Auth\OracleUserProvider
  • Yajra\Oci8\Connectors\OracleConnector
  • Yajra\Oci8\Eloquent\OracleEloquent
  • Yajra\Oci8\Oci8Connection
  • Yajra\Oci8\Oci8ServiceProvider
  • Yajra\Oci8\Query\Grammars\OracleGrammar
  • Yajra\Oci8\Query\OracleBuilder
  • Yajra\Oci8\Query\Processors\OracleProcessor
  • Yajra\Oci8\Schema\Comment
  • Yajra\Oci8\Schema\Grammars\OracleGrammar
  • Yajra\Oci8\Schema\OracleAutoIncrementHelper
  • Yajra\Oci8\Schema\OracleBlueprint
  • Yajra\Oci8\Schema\OracleBuilder
  • Yajra\Oci8\Schema\Sequence
  • Yajra\Oci8\Schema\Trigger

Traits

  • Yajra\Oci8\OracleReservedWords

Functions

  • config_path
  • Overview
  • Namespace
  • Class
  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:      * @var \Illuminate\Database\Connection
 12:      */
 13:     protected $connection;
 14: 
 15:     /**
 16:      * @var \Yajra\Oci8\Schema\Trigger
 17:      */
 18:     protected $trigger;
 19: 
 20:     /**
 21:      * @var \Yajra\Oci8\Schema\Sequence
 22:      */
 23:     protected $sequence;
 24: 
 25:     /**
 26:      * @param \Illuminate\Database\Connection $connection
 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:      * create sequence and trigger for autoIncrement support
 37:      *
 38:      * @param  Blueprint $blueprint
 39:      * @param  string $table
 40:      * @return null
 41:      */
 42:     public function createAutoIncrementObjects(Blueprint $blueprint, $table)
 43:     {
 44:         $column = $this->getQualifiedAutoIncrementColumn($blueprint);
 45: 
 46:         // return if no qualified AI column
 47:         if (is_null($column)) {
 48:             return;
 49:         }
 50: 
 51:         $col   = $column->name;
 52:         $start = isset($column->start) ? $column->start : 1;
 53: 
 54:         // get table prefix
 55:         $prefix = $this->connection->getTablePrefix();
 56: 
 57:         // create sequence for auto increment
 58:         $sequenceName = $this->createObjectName($prefix, $table, $col, 'seq');
 59:         $this->sequence->create($sequenceName, $start, $column->nocache);
 60: 
 61:         // create trigger for auto increment work around
 62:         $triggerName = $this->createObjectName($prefix, $table, $col, 'trg');
 63:         $this->trigger->autoIncrement($prefix . $table, $col, $triggerName, $sequenceName);
 64:     }
 65: 
 66:     /**
 67:      * Get qualified autoincrement column.
 68:      *
 69:      * @param  Blueprint $blueprint
 70:      * @return Fluent|null
 71:      */
 72:     public function getQualifiedAutoIncrementColumn(Blueprint $blueprint)
 73:     {
 74:         $columns = $blueprint->getColumns();
 75: 
 76:         // search for primary key / autoIncrement column
 77:         foreach ($columns as $column) {
 78:             // if column is autoIncrement set the primary col name
 79:             if ($column->autoIncrement) {
 80:                 return $column;
 81:             }
 82:         }
 83: 
 84:         return null;
 85:     }
 86: 
 87:     /**
 88:      * Create an object name that limits to 30 chars.
 89:      *
 90:      * @param  string $prefix
 91:      * @param  string $table
 92:      * @param  string $col
 93:      * @param  string $type
 94:      * @return string
 95:      */
 96:     private function createObjectName($prefix, $table, $col, $type)
 97:     {
 98:         // max object name length is 30 chars
 99:         return substr($prefix . $table . '_' . $col . '_' . $type, 0, 30);
100:     }
101: 
102:     /**
103:      * Drop sequence and triggers if exists, autoincrement objects.
104:      *
105:      * @param  string $table
106:      * @return null
107:      */
108:     public function dropAutoIncrementObjects($table)
109:     {
110:         // drop sequence and trigger object
111:         $prefix = $this->connection->getTablePrefix();
112:         // get the actual primary column name from table
113:         $col = $this->getPrimaryKey($prefix . $table);
114:         // if primary key col is set, drop auto increment objects
115:         if (isset($col) and ! empty($col)) {
116:             // drop sequence for auto increment
117:             $sequenceName = $this->createObjectName($prefix, $table, $col, 'seq');
118:             $this->sequence->drop($sequenceName);
119: 
120:             // drop trigger for auto increment work around
121:             $triggerName = $this->createObjectName($prefix, $table, $col, 'trg');
122:             $this->trigger->drop($triggerName);
123:         }
124:     }
125: 
126:     /**
127:      * Get table's primary key.
128:      *
129:      * @param  string $table
130:      * @return string
131:      */
132:     public function getPrimaryKey($table)
133:     {
134:         if (! $table) {
135:             return '';
136:         }
137: 
138:         $data = $this->connection->selectOne("
139:             SELECT cols.column_name
140:             FROM all_constraints cons, all_cons_columns cols
141:             WHERE upper(cols.table_name) = upper('{$table}')
142:                 AND cons.constraint_type = 'P'
143:                 AND cons.constraint_name = cols.constraint_name
144:                 AND cons.owner = cols.owner
145:                 AND cols.position = 1
146:                 AND cons.owner = (select user from dual)
147:             ORDER BY cols.table_name, cols.position
148:             ");
149: 
150:         if (count($data)) {
151:             return $data->column_name;
152:         }
153: 
154:         return '';
155:     }
156: 
157:     /**
158:      * Get sequence instance.
159:      *
160:      * @return Sequence
161:      */
162:     public function getSequence()
163:     {
164:         return $this->sequence;
165:     }
166: 
167:     /**
168:      * Set sequence instance.
169:      *
170:      * @param Sequence $sequence
171:      */
172:     public function setSequence($sequence)
173:     {
174:         $this->sequence = $sequence;
175:     }
176: 
177:     /**
178:      * Get trigger instance.
179:      *
180:      * @return Trigger
181:      */
182:     public function getTrigger()
183:     {
184:         return $this->trigger;
185:     }
186: 
187:     /**
188:      * Set the trigger instance.
189:      *
190:      * @param Trigger $trigger
191:      */
192:     public function setTrigger($trigger)
193:     {
194:         $this->trigger = $trigger;
195:     }
196: }
197: 
API documentation generated by ApiGen