/system/database/drivers/oci8/oci8_forge.php

https://github.com/andigehle/CodeIgniter · PHP · 153 lines · 69 code · 19 blank · 65 comment · 13 complexity · e5de6a4cb5078c40b5967a9d18a5bb95 MD5 · raw file

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.2.4 or newer
  6. *
  7. * NOTICE OF LICENSE
  8. *
  9. * Licensed under the Open Software License version 3.0
  10. *
  11. * This source file is subject to the Open Software License (OSL 3.0) that is
  12. * bundled with this package in the files license.txt / license.rst. It is
  13. * also available through the world wide web at this URL:
  14. * http://opensource.org/licenses/OSL-3.0
  15. * If you did not receive a copy of the license and are unable to obtain it
  16. * through the world wide web, please send an email to
  17. * licensing@ellislab.com so we can send you a copy immediately.
  18. *
  19. * @package CodeIgniter
  20. * @author EllisLab Dev Team
  21. * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
  22. * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
  23. * @link http://codeigniter.com
  24. * @since Version 1.0
  25. * @filesource
  26. */
  27. /**
  28. * Oracle Forge Class
  29. *
  30. * @category Database
  31. * @author EllisLab Dev Team
  32. * @link http://codeigniter.com/user_guide/database/
  33. */
  34. class CI_DB_oci8_forge extends CI_DB_forge {
  35. protected $_create_database = FALSE;
  36. protected $_drop_database = FALSE;
  37. protected $_drop_table = 'DROP TABLE %s';
  38. /**
  39. * Create Table
  40. *
  41. * @param string the table name
  42. * @param array the fields
  43. * @param mixed primary key(s)
  44. * @param mixed key(s)
  45. * @param bool should 'IF NOT EXISTS' be added to the SQL
  46. * @return string
  47. */
  48. protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
  49. {
  50. $sql = 'CREATE TABLE ';
  51. if ($if_not_exists === TRUE)
  52. {
  53. $sql .= 'IF NOT EXISTS ';
  54. }
  55. $sql .= $this->db->escape_identifiers($table).' (';
  56. $current_field_count = 0;
  57. foreach ($fields as $field => $attributes)
  58. {
  59. // Numeric field names aren't allowed in databases, so if the key is
  60. // numeric, we know it was assigned by PHP and the developer manually
  61. // entered the field information, so we'll simply add it to the list
  62. if (is_numeric($field))
  63. {
  64. $sql .= "\n\t".$attributes;
  65. }
  66. else
  67. {
  68. $attributes = array_change_key_case($attributes, CASE_UPPER);
  69. $sql .= "\n\t".$this->db->protect_identifiers($field).' '.$attributes['TYPE']
  70. .((isset($attributes['UNSINGED']) && $attributes['UNSIGNED'] === TRUE) ? ' UNSIGNED' : '')
  71. .(isset($attributes['DEFAULT']) ? " DEFAULT '".$attributes['DEFAULT']."'" : '')
  72. .((isset($attributes['NULL']) && $attributes['NULL'] === TRUE) ? '' : ' NOT NULL')
  73. .(isset($attributes['CONSTRAINT']) ? ' CONSTRAINT '.$attributes['CONSTRAINT'] : '');
  74. }
  75. // don't add a comma on the end of the last field
  76. if (++$current_field_count < count($fields))
  77. {
  78. $sql .= ',';
  79. }
  80. }
  81. if (count($primary_keys) > 0)
  82. {
  83. $primary_keys = $this->db->protect_identifiers($primary_keys);
  84. $sql .= ",\n\tCONSTRAINT ".$table.' PRIMARY KEY ('.implode(', ', $primary_keys).')';
  85. }
  86. if (is_array($keys) && count($keys) > 0)
  87. {
  88. foreach ($keys as $key)
  89. {
  90. if (is_array($key))
  91. {
  92. $key = $this->db->protect_identifiers($key);
  93. }
  94. else
  95. {
  96. $key = array($this->db->protect_identifiers($key));
  97. }
  98. $sql .= ",\n\tUNIQUE COLUMNS (".implode(', ', $key).')';
  99. }
  100. }
  101. return $sql."\n)";
  102. }
  103. // --------------------------------------------------------------------
  104. /**
  105. * Alter table query
  106. *
  107. * Generates a platform-specific query so that a table can be altered
  108. * Called by add_column(), drop_column(), and column_alter(),
  109. *
  110. * @param string the ALTER type (ADD, DROP, CHANGE)
  111. * @param string the column name
  112. * @param string the table name
  113. * @param string the column definition
  114. * @param string the default value
  115. * @param bool should 'NOT NULL' be added
  116. * @param string the field after which we should add the new field
  117. * @return string
  118. */
  119. protected function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
  120. {
  121. $sql = 'ALTER TABLE '.$this->db->protect_identifiers($table).' '.$alter_type.' '.$this->db->protect_identifiers($column_name);
  122. // DROP has everything it needs now.
  123. if ($alter_type === 'DROP')
  124. {
  125. return $sql;
  126. }
  127. return $sql.' '.$column_definition
  128. .($default_value != '' ? ' DEFAULT "'.$default_value.'"' : '')
  129. .($null === NULL ? ' NULL' : ' NOT NULL')
  130. .($after_field != '' ? ' AFTER '.$this->db->protect_identifiers($after_field) : '');
  131. }
  132. }
  133. /* End of file oci8_forge.php */
  134. /* Location: ./system/database/drivers/oci8/oci8_forge.php */