Overview

Packages

  • application
    • commands
    • components
      • actions
      • filters
      • leftWidget
      • permissions
      • sortableWidget
      • util
      • webupdater
      • x2flow
        • actions
        • triggers
      • X2GridView
      • X2Settings
    • controllers
    • models
      • embedded
    • modules
      • accounts
        • controllers
        • models
      • actions
        • controllers
        • models
      • calendar
        • controllers
        • models
      • charts
        • models
      • contacts
        • controllers
        • models
      • docs
        • components
        • controllers
        • models
      • groups
        • controllers
        • models
      • marketing
        • components
        • controllers
        • models
      • media
        • controllers
        • models
      • mobile
        • components
      • opportunities
        • controllers
        • models
      • products
        • controllers
        • models
      • quotes
        • controllers
        • models
      • services
        • controllers
        • models
      • template
        • models
      • users
        • controllers
        • models
      • workflow
        • controllers
        • models
      • x2Leads
        • controllers
        • models
  • Net
  • None
  • PHP
  • system
    • base
    • caching
      • dependencies
    • collections
    • console
    • db
      • ar
      • schema
        • cubrid
        • mssql
        • mysql
        • oci
        • pgsql
        • sqlite
    • i18n
      • gettext
    • logging
    • test
    • utils
    • validators
    • web
      • actions
      • auth
      • filters
      • form
      • helpers
      • renderers
      • services
      • widgets
        • captcha
        • pagers
  • Text
    • Highlighter
  • zii
    • behaviors
    • widgets
      • grid
      • jui

Classes

  • CMssqlColumnSchema
  • CMssqlCommandBuilder
  • CMssqlPdoAdapter
  • CMssqlSchema
  • CMssqlSqlsrvPdoAdapter
  • CMssqlTableSchema
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * CMssqlSchema class file.
  4:  *
  5:  * @author Qiang Xue <qiang.xue@gmail.com>
  6:  * @author Christophe Boulain <Christophe.Boulain@gmail.com>
  7:  * @link http://www.yiiframework.com/
  8:  * @copyright 2008-2013 Yii Software LLC
  9:  * @license http://www.yiiframework.com/license/
 10:  */
 11: 
 12: /**
 13:  * CMssqlSchema is the class for retrieving metadata information from a MS SQL Server database.
 14:  *
 15:  * @author Qiang Xue <qiang.xue@gmail.com>
 16:  * @author Christophe Boulain <Christophe.Boulain@gmail.com>
 17:  * @package system.db.schema.mssql
 18:  */
 19: class CMssqlSchema extends CDbSchema
 20: {
 21:     const DEFAULT_SCHEMA='dbo';
 22: 
 23:     /**
 24:      * @var array the abstract column types mapped to physical column types.
 25:      * @since 1.1.6
 26:      */
 27:     public $columnTypes=array(
 28:         'pk' => 'int IDENTITY PRIMARY KEY',
 29:         'bigpk' => 'bigint IDENTITY PRIMARY KEY',
 30:         'string' => 'varchar(255)',
 31:         'text' => 'text',
 32:         'integer' => 'int',
 33:         'bigint' => 'bigint',
 34:         'float' => 'float',
 35:         'decimal' => 'decimal',
 36:         'datetime' => 'datetime',
 37:         'timestamp' => 'timestamp',
 38:         'time' => 'time',
 39:         'date' => 'date',
 40:         'binary' => 'binary',
 41:         'boolean' => 'bit',
 42:     );
 43: 
 44:     /**
 45:      * Quotes a table name for use in a query.
 46:      * A simple table name does not schema prefix.
 47:      * @param string $name table name
 48:      * @return string the properly quoted table name
 49:      * @since 1.1.6
 50:      */
 51:     public function quoteSimpleTableName($name)
 52:     {
 53:         return '['.$name.']';
 54:     }
 55: 
 56:     /**
 57:      * Quotes a column name for use in a query.
 58:      * A simple column name does not contain prefix.
 59:      * @param string $name column name
 60:      * @return string the properly quoted column name
 61:      * @since 1.1.6
 62:      */
 63:     public function quoteSimpleColumnName($name)
 64:     {
 65:         return '['.$name.']';
 66:     }
 67: 
 68:     /**
 69:      * Compares two table names.
 70:      * The table names can be either quoted or unquoted. This method
 71:      * will consider both cases.
 72:      * @param string $name1 table name 1
 73:      * @param string $name2 table name 2
 74:      * @return boolean whether the two table names refer to the same table.
 75:      */
 76:     public function compareTableNames($name1,$name2)
 77:     {
 78:         $name1=str_replace(array('[',']'),'',$name1);
 79:         $name2=str_replace(array('[',']'),'',$name2);
 80:         return parent::compareTableNames(strtolower($name1),strtolower($name2));
 81:     }
 82: 
 83:     /**
 84:      * Resets the sequence value of a table's primary key.
 85:      * The sequence will be reset such that the primary key of the next new row inserted
 86:      * will have the specified value or max value of a primary key plus one (i.e. sequence trimming).
 87:      * @param CDbTableSchema $table the table schema whose primary key sequence will be reset
 88:      * @param integer|null $value the value for the primary key of the next new row inserted.
 89:      * If this is not set, the next new row's primary key will have the max value of a primary
 90:      * key plus one (i.e. sequence trimming).
 91:      * @since 1.1.6
 92:      */
 93:     public function resetSequence($table,$value=null)
 94:     {
 95:         if($table->sequenceName===null)
 96:             return;
 97:         if($value!==null)
 98:             $value=(int)($value)-1;
 99:         else
100:             $value=(int)$this->getDbConnection()
101:                 ->createCommand("SELECT MAX([{$table->primaryKey}]) FROM {$table->rawName}")
102:                 ->queryScalar();
103:         $name=strtr($table->rawName,array('['=>'',']'=>''));
104:         $this->getDbConnection()
105:             ->createCommand("DBCC CHECKIDENT ('$name',RESEED,$value)")
106:             ->execute();
107:     }
108: 
109:     private $_normalTables=array();  // non-view tables
110:     /**
111:      * Enables or disables integrity check.
112:      * @param boolean $check whether to turn on or off the integrity check.
113:      * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema.
114:      * @since 1.1.6
115:      */
116:     public function checkIntegrity($check=true,$schema='')
117:     {
118:         $enable=$check ? 'CHECK' : 'NOCHECK';
119:         if(!isset($this->_normalTables[$schema]))
120:             $this->_normalTables[$schema]=$this->findTableNames($schema,false);
121:         $db=$this->getDbConnection();
122:         foreach($this->_normalTables[$schema] as $tableName)
123:         {
124:             $tableName=$this->quoteTableName($tableName);
125:             $db->createCommand("ALTER TABLE $tableName $enable CONSTRAINT ALL")->execute();
126:         }
127:     }
128: 
129:     /**
130:      * Loads the metadata for the specified table.
131:      * @param string $name table name
132:      * @return CMssqlTableSchema driver dependent table metadata. Null if the table does not exist.
133:      */
134:     protected function loadTable($name)
135:     {
136:         $table=new CMssqlTableSchema;
137:         $this->resolveTableNames($table,$name);
138:         //if (!in_array($table->name, $this->tableNames)) return null;
139:         $table->primaryKey=$this->findPrimaryKey($table);
140:         $table->foreignKeys=$this->findForeignKeys($table);
141:         if($this->findColumns($table))
142:         {
143:             return $table;
144:         }
145:         else
146:             return null;
147:     }
148: 
149:     /**
150:      * Generates various kinds of table names.
151:      * @param CMssqlTableSchema $table the table instance
152:      * @param string $name the unquoted table name
153:      */
154:     protected function resolveTableNames($table,$name)
155:     {
156:         $parts=explode('.',str_replace(array('[',']'),'',$name));
157:         if(($c=count($parts))==3)
158:         {
159:             // Catalog name, schema name and table name provided
160:             $table->catalogName=$parts[0];
161:             $table->schemaName=$parts[1];
162:             $table->name=$parts[2];
163:             $table->rawName=$this->quoteTableName($table->catalogName).'.'.$this->quoteTableName($table->schemaName).'.'.$this->quoteTableName($table->name);
164:         }
165:         elseif ($c==2)
166:         {
167:             // Only schema name and table name provided
168:             $table->name=$parts[1];
169:             $table->schemaName=$parts[0];
170:             $table->rawName=$this->quoteTableName($table->schemaName).'.'.$this->quoteTableName($table->name);
171:         }
172:         else
173:         {
174:             // Only the name given, we need to get at least the schema name
175:             //if (empty($this->_schemaNames)) $this->findTableNames();
176:             $table->name=$parts[0];
177:             $table->schemaName=self::DEFAULT_SCHEMA;
178:             $table->rawName=$this->quoteTableName($table->schemaName).'.'.$this->quoteTableName($table->name);
179:         }
180:     }
181: 
182:     /**
183:      * Gets the primary key column(s) details for the given table.
184:      * @param CMssqlTableSchema $table table
185:      * @return mixed primary keys (null if no pk, string if only 1 column pk, or array if composite pk)
186:      */
187:     protected function findPrimaryKey($table)
188:     {
189:         $kcu='INFORMATION_SCHEMA.KEY_COLUMN_USAGE';
190:         $tc='INFORMATION_SCHEMA.TABLE_CONSTRAINTS';
191:         if (isset($table->catalogName))
192:         {
193:             $kcu=$table->catalogName.'.'.$kcu;
194:             $tc=$table->catalogName.'.'.$tc;
195:         }
196: 
197:         $sql = <<<EOD
198:         SELECT k.column_name field_name
199:         FROM {$this->quoteTableName($kcu)} k
200:         LEFT JOIN {$this->quoteTableName($tc)} c
201:         ON k.table_name = c.table_name
202:             AND k.constraint_name = c.constraint_name
203:         WHERE c.constraint_type ='PRIMARY KEY'
204:             AND k.table_name = :table
205:             AND k.table_schema = :schema
206: EOD;
207:         $command = $this->getDbConnection()->createCommand($sql);
208:         $command->bindValue(':table', $table->name);
209:         $command->bindValue(':schema', $table->schemaName);
210:         $primary=$command->queryColumn();
211:         switch (count($primary))
212:         {
213:             case 0: // No primary key on table
214:                 $primary=null;
215:                 break;
216:             case 1: // Only 1 primary key
217:                 $primary=$primary[0];
218:                 break;
219:         }
220:         return $primary;
221:     }
222: 
223:     /**
224:      * Gets foreign relationship constraint keys and table name
225:      * @param CMssqlTableSchema $table table
226:      * @return array foreign relationship table name and keys.
227:      */
228:     protected function findForeignKeys($table)
229:     {
230:         $rc='INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS';
231:         $kcu='INFORMATION_SCHEMA.KEY_COLUMN_USAGE';
232:         if (isset($table->catalogName))
233:         {
234:             $kcu=$table->catalogName.'.'.$kcu;
235:             $rc=$table->catalogName.'.'.$rc;
236:         }
237: 
238:         //From http://msdn2.microsoft.com/en-us/library/aa175805(SQL.80).aspx
239:         $sql = <<<EOD
240:         SELECT
241:              KCU1.CONSTRAINT_NAME AS 'FK_CONSTRAINT_NAME'
242:            , KCU1.TABLE_NAME AS 'FK_TABLE_NAME'
243:            , KCU1.COLUMN_NAME AS 'FK_COLUMN_NAME'
244:            , KCU1.ORDINAL_POSITION AS 'FK_ORDINAL_POSITION'
245:            , KCU2.CONSTRAINT_NAME AS 'UQ_CONSTRAINT_NAME'
246:            , KCU2.TABLE_NAME AS 'UQ_TABLE_NAME'
247:            , KCU2.COLUMN_NAME AS 'UQ_COLUMN_NAME'
248:            , KCU2.ORDINAL_POSITION AS 'UQ_ORDINAL_POSITION'
249:         FROM {$this->quoteTableName($rc)} RC
250:         JOIN {$this->quoteTableName($kcu)} KCU1
251:         ON KCU1.CONSTRAINT_CATALOG = RC.CONSTRAINT_CATALOG
252:            AND KCU1.CONSTRAINT_SCHEMA = RC.CONSTRAINT_SCHEMA
253:            AND KCU1.CONSTRAINT_NAME = RC.CONSTRAINT_NAME
254:         JOIN {$this->quoteTableName($kcu)} KCU2
255:         ON KCU2.CONSTRAINT_CATALOG =
256:         RC.UNIQUE_CONSTRAINT_CATALOG
257:            AND KCU2.CONSTRAINT_SCHEMA =
258:         RC.UNIQUE_CONSTRAINT_SCHEMA
259:            AND KCU2.CONSTRAINT_NAME =
260:         RC.UNIQUE_CONSTRAINT_NAME
261:            AND KCU2.ORDINAL_POSITION = KCU1.ORDINAL_POSITION
262:         WHERE KCU1.TABLE_NAME = :table
263: EOD;
264:         $command = $this->getDbConnection()->createCommand($sql);
265:         $command->bindValue(':table', $table->name);
266:         $fkeys=array();
267:         foreach($command->queryAll() as $info)
268:         {
269:             $fkeys[$info['FK_COLUMN_NAME']]=array($info['UQ_TABLE_NAME'],$info['UQ_COLUMN_NAME'],);
270: 
271:         }
272:         return $fkeys;
273:     }
274: 
275: 
276:     /**
277:      * Collects the table column metadata.
278:      * @param CMssqlTableSchema $table the table metadata
279:      * @return boolean whether the table exists in the database
280:      */
281:     protected function findColumns($table)
282:     {
283:         $columnsTable="INFORMATION_SCHEMA.COLUMNS";
284:         $where=array();
285:         $where[]="t1.TABLE_NAME='".$table->name."'";
286:         if (isset($table->catalogName))
287:         {
288:             $where[]="t1.TABLE_CATALOG='".$table->catalogName."'";
289:             $columnsTable = $table->catalogName.'.'.$columnsTable;
290:         }
291:         if (isset($table->schemaName))
292:             $where[]="t1.TABLE_SCHEMA='".$table->schemaName."'";
293: 
294:         $sql="SELECT t1.*, columnproperty(object_id(t1.table_schema+'.'+t1.table_name), t1.column_name, 'IsIdentity') AS IsIdentity, ".
295:              "CONVERT(VARCHAR, t2.value) AS Comment FROM ".$this->quoteTableName($columnsTable)." AS t1 ".
296:              "LEFT OUTER JOIN sys.extended_properties AS t2 ON t1.ORDINAL_POSITION = t2.minor_id AND ".
297:              "object_name(t2.major_id) = t1.TABLE_NAME AND t2.class=1 AND t2.class_desc='OBJECT_OR_COLUMN' AND t2.name='MS_Description' ".
298:              "WHERE ".join(' AND ',$where);
299:         try
300:         {
301:             $columns=$this->getDbConnection()->createCommand($sql)->queryAll();
302:             if(empty($columns))
303:                 return false;
304:         }
305:         catch(Exception $e)
306:         {
307:             return false;
308:         }
309: 
310:         foreach($columns as $column)
311:         {
312:             $c=$this->createColumn($column);
313:             if (is_array($table->primaryKey))
314:                 $c->isPrimaryKey=in_array($c->name, $table->primaryKey);
315:             else
316:                 $c->isPrimaryKey=strcasecmp($c->name,$table->primaryKey)===0;
317: 
318:             $c->isForeignKey=isset($table->foreignKeys[$c->name]);
319:             $table->columns[$c->name]=$c;
320:             if ($c->autoIncrement && $table->sequenceName===null)
321:                 $table->sequenceName=$table->name;
322:         }
323:         return true;
324:     }
325: 
326:     /**
327:      * Creates a table column.
328:      * @param array $column column metadata
329:      * @return CDbColumnSchema normalized column metadata
330:      */
331:     protected function createColumn($column)
332:     {
333:         $c=new CMssqlColumnSchema;
334:         $c->name=$column['COLUMN_NAME'];
335:         $c->rawName=$this->quoteColumnName($c->name);
336:         $c->allowNull=$column['IS_NULLABLE']=='YES';
337:         if ($column['NUMERIC_PRECISION_RADIX']!==null)
338:         {
339:             // We have a numeric datatype
340:             $c->size=$c->precision=$column['NUMERIC_PRECISION']!==null?(int)$column['NUMERIC_PRECISION']:null;
341:             $c->scale=$column['NUMERIC_SCALE']!==null?(int)$column['NUMERIC_SCALE']:null;
342:         }
343:         elseif ($column['DATA_TYPE']=='image' || $column['DATA_TYPE']=='text')
344:             $c->size=$c->precision=null;
345:         else
346:             $c->size=$c->precision=($column['CHARACTER_MAXIMUM_LENGTH']!== null)?(int)$column['CHARACTER_MAXIMUM_LENGTH']:null;
347:         $c->autoIncrement=$column['IsIdentity']==1;
348:         $c->comment=$column['Comment']===null ? '' : $column['Comment'];
349: 
350:         $c->init($column['DATA_TYPE'],$column['COLUMN_DEFAULT']);
351:         return $c;
352:     }
353: 
354:     /**
355:      * Returns all table names in the database.
356:      * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema.
357:      * If not empty, the returned table names will be prefixed with the schema name.
358:      * @param boolean $includeViews whether to include views in the result. Defaults to true.
359:      * @return array all table names in the database.
360:      */
361:     protected function findTableNames($schema='',$includeViews=true)
362:     {
363:         if($schema==='')
364:             $schema=self::DEFAULT_SCHEMA;
365:         if($includeViews)
366:             $condition="TABLE_TYPE in ('BASE TABLE','VIEW')";
367:         else
368:             $condition="TABLE_TYPE='BASE TABLE'";
369:         $sql=<<<EOD
370: SELECT TABLE_NAME FROM [INFORMATION_SCHEMA].[TABLES]
371: WHERE TABLE_SCHEMA=:schema AND $condition
372: EOD;
373:         $command=$this->getDbConnection()->createCommand($sql);
374:         $command->bindParam(":schema", $schema);
375:         $rows=$command->queryAll();
376:         $names=array();
377:         foreach ($rows as $row)
378:         {
379:             if ($schema == self::DEFAULT_SCHEMA)
380:                 $names[]=$row['TABLE_NAME'];
381:             else
382:                 $names[]=$schema.'.'.$row['TABLE_NAME'];
383:         }
384: 
385:         return $names;
386:     }
387: 
388:     /**
389:      * Creates a command builder for the database.
390:      * This method overrides parent implementation in order to create a MSSQL specific command builder
391:      * @return CDbCommandBuilder command builder instance
392:      */
393:     protected function createCommandBuilder()
394:     {
395:         return new CMssqlCommandBuilder($this);
396:     }
397: 
398:     /**
399:      * Builds a SQL statement for renaming a DB table.
400:      * @param string $table the table to be renamed. The name will be properly quoted by the method.
401:      * @param string $newName the new table name. The name will be properly quoted by the method.
402:      * @return string the SQL statement for renaming a DB table.
403:      * @since 1.1.6
404:      */
405:     public function renameTable($table, $newName)
406:     {
407:         return "sp_rename '$table', '$newName'";
408:     }
409: 
410:     /**
411:      * Builds a SQL statement for renaming a column.
412:      * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method.
413:      * @param string $name the old name of the column. The name will be properly quoted by the method.
414:      * @param string $newName the new name of the column. The name will be properly quoted by the method.
415:      * @return string the SQL statement for renaming a DB column.
416:      * @since 1.1.6
417:      */
418:     public function renameColumn($table, $name, $newName)
419:     {
420:         return "sp_rename '$table.$name', '$newName', 'COLUMN'";
421:     }
422: 
423:     /**
424:      * Builds a SQL statement for changing the definition of a column.
425:      * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method.
426:      * @param string $column the name of the column to be changed. The name will be properly quoted by the method.
427:      * @param string $type the new column type. The {@link getColumnType} method will be invoked to convert abstract column type (if any)
428:      * into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL.
429:      * For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'.
430:      * @return string the SQL statement for changing the definition of a column.
431:      * @since 1.1.6
432:      */
433:     public function alterColumn($table, $column, $type)
434:     {
435:         $type=$this->getColumnType($type);
436:         $sql='ALTER TABLE ' . $this->quoteTableName($table) . ' ALTER COLUMN '
437:             . $this->quoteColumnName($column) . ' '
438:             . $this->getColumnType($type);
439:         return $sql;
440:     }
441: }
442: 
API documentation generated by ApiGen 2.8.0