Skip to content

Commit

Permalink
Merge pull request #777 from yajra/10x-max-length
Browse files Browse the repository at this point in the history
  • Loading branch information
yajra authored Mar 1, 2023
2 parents 326e5bc + 485ec7d commit fa9923c
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 17 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ class Config {

Then run your laravel installation...

## Oracle Max Name Length

By default, DB object name are limited to 30 characters. To increase the limit, you can set the `ORA_MAX_NAME_LEN=128` in your `.env` file.

Note: this config requires **Oracle 12c02 or higher**.

## [Laravel 5.2++] Oracle User Provider

When using oracle, we may encounter a problem on authentication because oracle queries are case sensitive by default.
Expand Down
11 changes: 11 additions & 0 deletions src/Oci8/Oci8Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ public function withTablePrefix(Grammar $grammar)
public function withSchemaPrefix(Grammar $grammar)
{
$grammar->setSchemaPrefix($this->getConfigSchemaPrefix());
$grammar->setMaxLength($this->getConfigMaxLength());

return $grammar;
}
Expand All @@ -367,6 +368,16 @@ protected function getConfigSchemaPrefix()
return isset($this->config['prefix_schema']) ? $this->config['prefix_schema'] : '';
}

/**
* Get config max length.
*
* @return string
*/
protected function getConfigMaxLength()
{
return isset($this->config['max_name_len']) ? $this->config['max_name_len'] : 30;
}

/**
* Get the default schema grammar instance.
*
Expand Down
31 changes: 28 additions & 3 deletions src/Oci8/Query/Grammars/OracleGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ class OracleGrammar extends Grammar
/**
* @var string
*/
protected $schema_prefix = '';
protected $schemaPrefix = '';

/**
* @var int
*/
protected $maxLength;

/**
* Compile a delete statement with joins into SQL.
Expand Down Expand Up @@ -258,7 +263,17 @@ public function wrapTable($table)
*/
public function getSchemaPrefix()
{
return ! empty($this->schema_prefix) ? $this->wrapValue($this->schema_prefix).'.' : '';
return ! empty($this->schemaPrefix) ? $this->wrapValue($this->schemaPrefix).'.' : '';
}

/**
* Get max length.
*
* @return int
*/
public function getMaxLength()
{
return ! empty($this->maxLength) ? $this->maxLength : 30;
}

/**
Expand All @@ -268,7 +283,17 @@ public function getSchemaPrefix()
*/
public function setSchemaPrefix($prefix)
{
$this->schema_prefix = $prefix;
$this->schemaPrefix = $prefix;
}

/**
* Set max length.
*
* @param int $length
*/
public function setMaxLength($length)
{
$this->maxLength = $length;
}

/**
Expand Down
34 changes: 30 additions & 4 deletions src/Oci8/Schema/Grammars/OracleGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ class OracleGrammar extends Grammar
/**
* @var string
*/
protected $schema_prefix = '';
protected $schemaPrefix = '';

/**
* @var int
*/
protected $maxLength = 30;

/**
* If this Grammar supports schema changes wrapped in a transaction.
Expand Down Expand Up @@ -92,7 +97,17 @@ public function wrapTable($table)
*/
public function getSchemaPrefix()
{
return ! empty($this->schema_prefix) ? $this->schema_prefix.'.' : '';
return ! empty($this->schemaPrefix) ? $this->schemaPrefix.'.' : '';
}

/**
* Get max length.
*
* @return int
*/
public function getMaxLength()
{
return ! empty($this->maxLength) ? $this->maxLength : 30;
}

/**
Expand All @@ -102,7 +117,17 @@ public function getSchemaPrefix()
*/
public function setSchemaPrefix($prefix)
{
$this->schema_prefix = $prefix;
$this->schemaPrefix = $prefix;
}

/**
* Set max length.
*
* @param int $length
*/
public function setMaxLength($length)
{
$this->maxLength = $length;
}

/**
Expand Down Expand Up @@ -369,7 +394,8 @@ public function compileDropPrimary(Blueprint $blueprint, Fluent $command)
private function dropConstraint(Blueprint $blueprint, Fluent $command, $type)
{
$table = $this->wrapTable($blueprint);
$index = substr($command->index, 0, 30);

$index = substr($command->index, 0, $this->getMaxLength());

if ($type === 'index') {
return "drop index {$index}";
Expand Down
9 changes: 2 additions & 7 deletions src/Oci8/Schema/OracleAutoIncrementHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function getQualifiedAutoIncrementColumn(Blueprint $blueprint)
}

/**
* Create an object name that limits to 30 or 126 chars depending on the server version.
* Create an object name that limits to 30 or 128 chars depending on the server version.
*
* @param string $prefix
* @param string $table
Expand All @@ -93,12 +93,7 @@ public function getQualifiedAutoIncrementColumn(Blueprint $blueprint)
*/
private function createObjectName($prefix, $table, $col, $type)
{
$version = (int) filter_var($this->connection->getConfig('server_version'), FILTER_SANITIZE_NUMBER_INT);

$maxLength = 30;
if ($version >= 12) {
$maxLength = 126;
}
$maxLength = $this->connection->getSchemaGrammar()->getMaxLength();

return substr($prefix.$table.'_'.$col.'_'.$type, 0, $maxLength);
}
Expand Down
21 changes: 18 additions & 3 deletions src/Oci8/Schema/OracleBlueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ class OracleBlueprint extends Blueprint
*/
protected $prefix;

/**
* Database object max length variable.
*
* @var int
*/
protected $maxLength = 30;

/**
* Set table prefix settings.
*
Expand All @@ -37,6 +44,16 @@ public function setTablePrefix($prefix = '')
$this->prefix = $prefix;
}

/**
* Set database object max length name settings.
*
* @param int $maxLength
*/
public function setMaxLength($maxLength = 30)
{
$this->maxLength = $maxLength;
}

/**
* Create a default index name for the table.
*
Expand All @@ -59,9 +76,7 @@ protected function createIndexName($type, array $columns)
$index = strtolower($this->prefix.$this->table.'_'.implode('_', $columns).'_'.$type);

$index = str_replace(['-', '.'], '_', $index);

// shorten the name if it is longer than 30 chars
while (strlen($index) > 30) {
while (strlen($index) > $this->maxLength) {
$parts = explode('_', $index);

for ($i = 0; $i < count($parts); $i++) {
Expand Down
1 change: 1 addition & 0 deletions src/Oci8/Schema/OracleBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ protected function createBlueprint($table, Closure $callback = null)
{
$blueprint = new OracleBlueprint($table, $callback);
$blueprint->setTablePrefix($this->connection->getTablePrefix());
$blueprint->setMaxLength($this->grammar->getMaxLength());

return $blueprint;
}
Expand Down
1 change: 1 addition & 0 deletions src/config/oracle.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
'edition' => env('DB_EDITION', 'ora$base'),
'server_version' => env('DB_SERVER_VERSION', '11g'),
'load_balance' => env('DB_LOAD_BALANCE', 'yes'),
'max_name_len' => env('ORA_MAX_NAME_LEN', 30),
'dynamic' => [],
],
'sessionVars' => [
Expand Down
23 changes: 23 additions & 0 deletions tests/Database/Oci8SchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,29 @@ public function testAddingBinary()
$this->assertEquals('alter table users add ( foo blob not null )', $statements[0]);
}

public function testBasicCreateTableWithPrimaryAndLongForeignKeys()
{
$blueprint = new Blueprint('users');
$blueprint->setMaxLength(120);
$blueprint->setTablePrefix('prefix_');
$blueprint->create();
$blueprint->integer('id')->primary();
$blueprint->string('email');
$blueprint->integer('very_long_foo_bar_id');
$blueprint->foreign('very_long_foo_bar_id')->references('id')->on('orders');
$grammar = $this->getGrammar();
$grammar->setTablePrefix('prefix_');
$grammar->setMaxLength(120);

$conn = $this->getConnection();

$statements = $blueprint->toSql($conn, $grammar);

$this->assertEquals(1, count($statements));
$this->assertEquals('create table prefix_users ( id number(10,0) not null, email varchar2(255) not null, very_long_foo_bar_id number(10,0) not null, constraint prefix_users_very_long_foo_bar_id_fk foreign key ( very_long_foo_bar_id ) references prefix_orders ( id ), constraint prefix_users_id_pk primary key ( id ) )',
$statements[0]);
}

public function testDropAllTables()
{
$statement = $this->getGrammar()->compileDropAllTables();
Expand Down

0 comments on commit fa9923c

Please sign in to comment.