blob: da8f68c56ccd534008b28e31d4173e5185c43379 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Alex Bilbie84445d02011-03-10 16:43:39 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Alex Bilbie84445d02011-03-10 16:43:39 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev0e8968a2012-01-26 02:04:37 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev0e8968a2012-01-26 02:04:37 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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 *
Alex Bilbie84445d02011-03-10 16:43:39 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Alex Bilbie84445d02011-03-10 16:43:39 +000023 * @link http://codeigniter.com
Andrey Andreevbf940582012-06-10 07:05:05 +030024 * @since Version 2.0.3
Alex Bilbie84445d02011-03-10 16:43:39 +000025 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Alex Bilbie84445d02011-03-10 16:43:39 +000028
Alex Bilbie84445d02011-03-10 16:43:39 +000029/**
Alex Bilbie01ab0042011-03-18 19:24:30 +000030 * SQLSRV Forge Class
Alex Bilbie84445d02011-03-10 16:43:39 +000031 *
32 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050033 * @author EllisLab Dev Team
Alex Bilbie84445d02011-03-10 16:43:39 +000034 * @link http://codeigniter.com/user_guide/database/
35 */
Alex Bilbie01ab0042011-03-18 19:24:30 +000036class CI_DB_sqlsrv_forge extends CI_DB_forge {
Alex Bilbie84445d02011-03-10 16:43:39 +000037
Andrey Andreevc98e93a2012-11-02 02:04:59 +020038 /**
Andrey Andreeva287a342012-11-05 23:19:59 +020039 * CREATE TABLE IF statement
Andrey Andreevc98e93a2012-11-02 02:04:59 +020040 *
41 * @var string
42 */
Andrey Andreeva287a342012-11-05 23:19:59 +020043 protected $_create_table_if = "IF NOT EXISTS (SELECT * FROM sysobjects WHERE ID = object_id(N'%s') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)\nCREATE TABLE";
44
45 /**
46 * DROP TABLE IF statement
47 *
48 * @var string
49 */
50 protected $_drop_table_if = "IF EXISTS (SELECT * FROM sysobjects WHERE ID = object_id(N'%s') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)\nDROP TABLE";
51
52 /**
53 * UNSIGNED support
54 *
55 * @var array
56 */
57 protected $_unsigned = array(
58 'TINYINT' => 'SMALLINT',
59 'SMALLINT' => 'INT',
60 'INT' => 'BIGINT',
61 'REAL' => 'FLOAT'
62 );
Alex Bilbie84445d02011-03-10 16:43:39 +000063
Andrey Andreevc98e93a2012-11-02 02:04:59 +020064 // --------------------------------------------------------------------
65
Alex Bilbie84445d02011-03-10 16:43:39 +000066 /**
Andrey Andreeva287a342012-11-05 23:19:59 +020067 * ALTER TABLE
Alex Bilbie84445d02011-03-10 16:43:39 +000068 *
Andrey Andreeva287a342012-11-05 23:19:59 +020069 * @param string $alter_type ALTER type
70 * @param string $table Table name
71 * @param mixed $field Column definition
72 * @return string|string[]
Alex Bilbie84445d02011-03-10 16:43:39 +000073 */
Andrey Andreeva287a342012-11-05 23:19:59 +020074 protected function _alter_table($alter_type, $table, $field)
Alex Bilbie84445d02011-03-10 16:43:39 +000075 {
Andrey Andreeva287a342012-11-05 23:19:59 +020076 if (in_array($alter_type, array('ADD', 'DROP'), TRUE))
Alex Bilbie84445d02011-03-10 16:43:39 +000077 {
Andrey Andreeva287a342012-11-05 23:19:59 +020078 return parent::_alter_table($alter_type, $table, $field);
Alex Bilbie84445d02011-03-10 16:43:39 +000079 }
80
Andrey Andreeva287a342012-11-05 23:19:59 +020081 $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table).' ALTER COLUMN ';
82 $sqls = array();
83 for ($i = 0, $c = count($field); $i < $c; $i++)
Alex Bilbie84445d02011-03-10 16:43:39 +000084 {
Andrey Andreeva287a342012-11-05 23:19:59 +020085 $sqls[] = $sql.$this->_process_column($field[$i]);
Alex Bilbie84445d02011-03-10 16:43:39 +000086 }
87
Andrey Andreeva287a342012-11-05 23:19:59 +020088 return $sqls;
Alex Bilbie84445d02011-03-10 16:43:39 +000089 }
90
91 // --------------------------------------------------------------------
92
93 /**
Andrey Andreeva287a342012-11-05 23:19:59 +020094 * Field attribute TYPE
Alex Bilbie84445d02011-03-10 16:43:39 +000095 *
Andrey Andreeva287a342012-11-05 23:19:59 +020096 * Performs a data type mapping between different databases.
Alex Bilbie84445d02011-03-10 16:43:39 +000097 *
Andrey Andreeva287a342012-11-05 23:19:59 +020098 * @param array &$attributes
99 * @return void
Alex Bilbie84445d02011-03-10 16:43:39 +0000100 */
Andrey Andreeva287a342012-11-05 23:19:59 +0200101 protected function _attr_type(&$attributes)
Alex Bilbie84445d02011-03-10 16:43:39 +0000102 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200103 switch (strtoupper($attributes['TYPE']))
Alex Bilbie84445d02011-03-10 16:43:39 +0000104 {
Andrey Andreeva287a342012-11-05 23:19:59 +0200105 case 'MEDIUMINT':
106 $attributes['TYPE'] = 'INTEGER';
107 $attributes['UNSIGNED'] = FALSE;
108 return;
109 case 'INTEGER':
110 $attributes['TYPE'] = 'INT';
111 return;
112 default: return;
Alex Bilbie84445d02011-03-10 16:43:39 +0000113 }
Andrey Andreeva287a342012-11-05 23:19:59 +0200114 }
Alex Bilbie84445d02011-03-10 16:43:39 +0000115
Andrey Andreeva287a342012-11-05 23:19:59 +0200116 // --------------------------------------------------------------------
117
118 /**
119 * Field attribute AUTO_INCREMENT
120 *
121 * @param array &$attributes
122 * @param array &$field
123 * @return void
124 */
125 protected function _attr_auto_increment(&$attributes, &$field)
126 {
127 if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE && stripos($field['type'], 'int') !== FALSE)
128 {
129 $field['auto_increment'] = ' IDENTITY(1,1)';
130 }
Alex Bilbie84445d02011-03-10 16:43:39 +0000131 }
132
Alex Bilbie84445d02011-03-10 16:43:39 +0000133}
134
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200135/* End of file sqlsrv_forge.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400136/* Location: ./system/database/drivers/sqlsrv/sqlsrv_forge.php */