PageRenderTime 25ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/system/database/drivers/odbc/odbc_forge.php

https://bitbucket.org/code_noodle/scratchpad
PHP | 160 lines | 72 code | 23 blank | 65 comment | 16 complexity | 9daab1954b51c974cef5f3eaca682457 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. * ODBC Forge Class
  29. *
  30. * @category Database
  31. * @author EllisLab Dev Team
  32. * @link http://codeigniter.com/database/
  33. */
  34. class CI_DB_odbc_forge extends CI_DB_forge {
  35. protected $_drop_table = 'DROP TABLE %s';
  36. /**
  37. * Create Table
  38. *
  39. * @param string the table name
  40. * @param array the fields
  41. * @param mixed primary key(s)
  42. * @param mixed key(s)
  43. * @param bool should 'IF NOT EXISTS' be added to the SQL
  44. * @return bool
  45. */
  46. protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
  47. {
  48. $sql = 'CREATE TABLE ';
  49. if ($if_not_exists === TRUE)
  50. {
  51. $sql .= 'IF NOT EXISTS ';
  52. }
  53. $sql .= $this->db->escape_identifiers($table).' (';
  54. $current_field_count = 0;
  55. foreach ($fields as $field => $attributes)
  56. {
  57. // Numeric field names aren't allowed in databases, so if the key is
  58. // numeric, we know it was assigned by PHP and the developer manually
  59. // entered the field information, so we'll simply add it to the list
  60. if (is_numeric($field))
  61. {
  62. $sql .= "\n\t".$attributes;
  63. }
  64. else
  65. {
  66. $attributes = array_change_key_case($attributes, CASE_UPPER);
  67. $sql .= "\n\t".$this->db->escape_identifiers($field).' '.$attributes['TYPE'];
  68. empty($attributes['CONSTRAINT']) OR $sql .= '('.$attributes['CONSTRAINT'].')';
  69. if ( ! empty($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE)
  70. {
  71. $sql .= ' UNSIGNED';
  72. }
  73. if (isset($attributes['DEFAULT']))
  74. {
  75. $sql .= " DEFAULT '".$attributes['DEFAULT']."'";
  76. }
  77. $sql .= ( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE)
  78. ? ' NULL' : ' NOT NULL';
  79. if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE)
  80. {
  81. $sql .= ' AUTO_INCREMENT';
  82. }
  83. }
  84. // don't add a comma on the end of the last field
  85. if (++$current_field_count < count($fields))
  86. {
  87. $sql .= ',';
  88. }
  89. }
  90. if (count($primary_keys) > 0)
  91. {
  92. $sql .= ",\n\tPRIMARY KEY (".implode(', ', $this->escape_identifiers($primary_keys)).')';
  93. }
  94. if (is_array($keys) && count($keys) > 0)
  95. {
  96. foreach ($keys as $key)
  97. {
  98. $key = is_array($key)
  99. ? $this->db->escape_identifiers($key)
  100. : array($this->db->escape_identifiers($key));
  101. $sql .= ",\n\tFOREIGN KEY (".implode(', ', $key).')';
  102. }
  103. }
  104. return $sql."\n)";
  105. }
  106. // --------------------------------------------------------------------
  107. /**
  108. * Alter table query
  109. *
  110. * Generates a platform-specific query so that a table can be altered
  111. * Called by add_column(), drop_column(), and column_alter(),
  112. *
  113. * @param string the ALTER type (ADD, DROP, CHANGE)
  114. * @param string the column name
  115. * @param string the table name
  116. * @param string the column definition
  117. * @param string the default value
  118. * @param bool should 'NOT NULL' be added
  119. * @param string the field after which we should add the new field
  120. * @return string
  121. */
  122. protected function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '')
  123. {
  124. $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table).' '.$alter_type.' '.$this->db->escape_identifiers($column_name);
  125. // DROP has everything it needs now.
  126. if ($alter_type === 'DROP')
  127. {
  128. return $sql;
  129. }
  130. return $sql.' '.$column_definition
  131. .($default_value != '' ? ' DEFAULT "'.$default_value.'"' : '')
  132. .($null === NULL ? ' NULL' : ' NOT NULL')
  133. .($after_field != '' ? ' AFTER '.$this->db->escape_identifiers($after_field) : '');
  134. }
  135. }
  136. /* End of file odbc_forge.php */
  137. /* Location: ./system/database/drivers/odbc/odbc_forge.php */