CDbSchema.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. <?php
  2. /**
  3. * CDbSchema class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CDbSchema is the base class for retrieving metadata information.
  12. *
  13. * @property CDbConnection $dbConnection Database connection. The connection is active.
  14. * @property array $tables The metadata for all tables in the database.
  15. * Each array element is an instance of {@link CDbTableSchema} (or its child class).
  16. * The array keys are table names.
  17. * @property array $tableNames All table names in the database.
  18. * @property CDbCommandBuilder $commandBuilder The SQL command builder for this connection.
  19. *
  20. * @author Qiang Xue <qiang.xue@gmail.com>
  21. * @package system.db.schema
  22. * @since 1.0
  23. */
  24. abstract class CDbSchema extends CComponent
  25. {
  26. /**
  27. * @var array the abstract column types mapped to physical column types.
  28. * @since 1.1.6
  29. */
  30. public $columnTypes=array();
  31. private $_tableNames=array();
  32. private $_tables=array();
  33. private $_connection;
  34. private $_builder;
  35. private $_cacheExclude=array();
  36. /**
  37. * Loads the metadata for the specified table.
  38. * @param string $name table name
  39. * @return CDbTableSchema driver dependent table metadata, null if the table does not exist.
  40. */
  41. abstract protected function loadTable($name);
  42. /**
  43. * Constructor.
  44. * @param CDbConnection $conn database connection.
  45. */
  46. public function __construct($conn)
  47. {
  48. $this->_connection=$conn;
  49. foreach($conn->schemaCachingExclude as $name)
  50. $this->_cacheExclude[$name]=true;
  51. }
  52. /**
  53. * @return CDbConnection database connection. The connection is active.
  54. */
  55. public function getDbConnection()
  56. {
  57. return $this->_connection;
  58. }
  59. /**
  60. * Obtains the metadata for the named table.
  61. * @param string $name table name
  62. * @param boolean $refresh if we need to refresh schema cache for a table.
  63. * Parameter available since 1.1.9
  64. * @return CDbTableSchema table metadata. Null if the named table does not exist.
  65. */
  66. public function getTable($name,$refresh=false)
  67. {
  68. if($refresh===false && isset($this->_tables[$name]))
  69. return $this->_tables[$name];
  70. else
  71. {
  72. if($this->_connection->tablePrefix!==null && strpos($name,'{{')!==false)
  73. $realName=preg_replace('/\{\{(.*?)\}\}/',$this->_connection->tablePrefix.'$1',$name);
  74. else
  75. $realName=$name;
  76. // temporarily disable query caching
  77. if($this->_connection->queryCachingDuration>0)
  78. {
  79. $qcDuration=$this->_connection->queryCachingDuration;
  80. $this->_connection->queryCachingDuration=0;
  81. }
  82. if(!isset($this->_cacheExclude[$name]) && ($duration=$this->_connection->schemaCachingDuration)>0 && $this->_connection->schemaCacheID!==false && ($cache=Yii::app()->getComponent($this->_connection->schemaCacheID))!==null)
  83. {
  84. $key='yii:dbschema'.$this->_connection->connectionString.':'.$this->_connection->username.':'.$name;
  85. $table=$cache->get($key);
  86. if($refresh===true || $table===false)
  87. {
  88. $table=$this->loadTable($realName);
  89. if($table!==null)
  90. $cache->set($key,$table,$duration);
  91. }
  92. $this->_tables[$name]=$table;
  93. }
  94. else
  95. $this->_tables[$name]=$table=$this->loadTable($realName);
  96. if(isset($qcDuration)) // re-enable query caching
  97. $this->_connection->queryCachingDuration=$qcDuration;
  98. return $table;
  99. }
  100. }
  101. /**
  102. * Returns the metadata for all tables in the database.
  103. * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema.
  104. * @return array the metadata for all tables in the database.
  105. * Each array element is an instance of {@link CDbTableSchema} (or its child class).
  106. * The array keys are table names.
  107. */
  108. public function getTables($schema='')
  109. {
  110. $tables=array();
  111. foreach($this->getTableNames($schema) as $name)
  112. {
  113. if(($table=$this->getTable($name))!==null)
  114. $tables[$name]=$table;
  115. }
  116. return $tables;
  117. }
  118. /**
  119. * Returns all table names in the database.
  120. * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema.
  121. * If not empty, the returned table names will be prefixed with the schema name.
  122. * @return array all table names in the database.
  123. */
  124. public function getTableNames($schema='')
  125. {
  126. if(!isset($this->_tableNames[$schema]))
  127. $this->_tableNames[$schema]=$this->findTableNames($schema);
  128. return $this->_tableNames[$schema];
  129. }
  130. /**
  131. * @return CDbCommandBuilder the SQL command builder for this connection.
  132. */
  133. public function getCommandBuilder()
  134. {
  135. if($this->_builder!==null)
  136. return $this->_builder;
  137. else
  138. return $this->_builder=$this->createCommandBuilder();
  139. }
  140. /**
  141. * Refreshes the schema.
  142. * This method resets the loaded table metadata and command builder
  143. * so that they can be recreated to reflect the change of schema.
  144. */
  145. public function refresh()
  146. {
  147. if(($duration=$this->_connection->schemaCachingDuration)>0 && $this->_connection->schemaCacheID!==false && ($cache=Yii::app()->getComponent($this->_connection->schemaCacheID))!==null)
  148. {
  149. foreach(array_keys($this->_tables) as $name)
  150. {
  151. if(!isset($this->_cacheExclude[$name]))
  152. {
  153. $key='yii:dbschema'.$this->_connection->connectionString.':'.$this->_connection->username.':'.$name;
  154. $cache->delete($key);
  155. }
  156. }
  157. }
  158. $this->_tables=array();
  159. $this->_tableNames=array();
  160. $this->_builder=null;
  161. }
  162. /**
  163. * Quotes a table name for use in a query.
  164. * If the table name contains schema prefix, the prefix will also be properly quoted.
  165. * @param string $name table name
  166. * @return string the properly quoted table name
  167. * @see quoteSimpleTableName
  168. */
  169. public function quoteTableName($name)
  170. {
  171. if(strpos($name,'.')===false)
  172. return $this->quoteSimpleTableName($name);
  173. $parts=explode('.',$name);
  174. foreach($parts as $i=>$part)
  175. $parts[$i]=$this->quoteSimpleTableName($part);
  176. return implode('.',$parts);
  177. }
  178. /**
  179. * Quotes a simple table name for use in a query.
  180. * A simple table name does not schema prefix.
  181. * @param string $name table name
  182. * @return string the properly quoted table name
  183. * @since 1.1.6
  184. */
  185. public function quoteSimpleTableName($name)
  186. {
  187. return "'".$name."'";
  188. }
  189. /**
  190. * Quotes a column name for use in a query.
  191. * If the column name contains prefix, the prefix will also be properly quoted.
  192. * @param string $name column name
  193. * @return string the properly quoted column name
  194. * @see quoteSimpleColumnName
  195. */
  196. public function quoteColumnName($name)
  197. {
  198. if(($pos=strrpos($name,'.'))!==false)
  199. {
  200. $prefix=$this->quoteTableName(substr($name,0,$pos)).'.';
  201. $name=substr($name,$pos+1);
  202. }
  203. else
  204. $prefix='';
  205. return $prefix . ($name==='*' ? $name : $this->quoteSimpleColumnName($name));
  206. }
  207. /**
  208. * Quotes a simple column name for use in a query.
  209. * A simple column name does not contain prefix.
  210. * @param string $name column name
  211. * @return string the properly quoted column name
  212. * @since 1.1.6
  213. */
  214. public function quoteSimpleColumnName($name)
  215. {
  216. return '"'.$name.'"';
  217. }
  218. /**
  219. * Compares two table names.
  220. * The table names can be either quoted or unquoted. This method
  221. * will consider both cases.
  222. * @param string $name1 table name 1
  223. * @param string $name2 table name 2
  224. * @return boolean whether the two table names refer to the same table.
  225. */
  226. public function compareTableNames($name1,$name2)
  227. {
  228. $name1=str_replace(array('"','`',"'"),'',$name1);
  229. $name2=str_replace(array('"','`',"'"),'',$name2);
  230. if(($pos=strrpos($name1,'.'))!==false)
  231. $name1=substr($name1,$pos+1);
  232. if(($pos=strrpos($name2,'.'))!==false)
  233. $name2=substr($name2,$pos+1);
  234. if($this->_connection->tablePrefix!==null)
  235. {
  236. if(strpos($name1,'{')!==false)
  237. $name1=$this->_connection->tablePrefix.str_replace(array('{','}'),'',$name1);
  238. if(strpos($name2,'{')!==false)
  239. $name2=$this->_connection->tablePrefix.str_replace(array('{','}'),'',$name2);
  240. }
  241. return $name1===$name2;
  242. }
  243. /**
  244. * Resets the sequence value of a table's primary key.
  245. * The sequence will be reset such that the primary key of the next new row inserted
  246. * will have the specified value or max value of a primary key plus one (i.e. sequence trimming).
  247. * @param CDbTableSchema $table the table schema whose primary key sequence will be reset
  248. * @param integer|null $value the value for the primary key of the next new row inserted.
  249. * If this is not set, the next new row's primary key will have the max value of a primary
  250. * key plus one (i.e. sequence trimming).
  251. * @since 1.1
  252. */
  253. public function resetSequence($table,$value=null)
  254. {
  255. }
  256. /**
  257. * Enables or disables integrity check.
  258. * @param boolean $check whether to turn on or off the integrity check.
  259. * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema.
  260. * @since 1.1
  261. */
  262. public function checkIntegrity($check=true,$schema='')
  263. {
  264. }
  265. /**
  266. * Creates a command builder for the database.
  267. * This method may be overridden by child classes to create a DBMS-specific command builder.
  268. * @return CDbCommandBuilder command builder instance
  269. */
  270. protected function createCommandBuilder()
  271. {
  272. return new CDbCommandBuilder($this);
  273. }
  274. /**
  275. * Returns all table names in the database.
  276. * This method should be overridden by child classes in order to support this feature
  277. * because the default implementation simply throws an exception.
  278. * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema.
  279. * If not empty, the returned table names will be prefixed with the schema name.
  280. * @throws CDbException if current schema does not support fetching all table names
  281. * @return array all table names in the database.
  282. */
  283. protected function findTableNames($schema='')
  284. {
  285. throw new CDbException(Yii::t('yii','{class} does not support fetching all table names.',
  286. array('{class}'=>get_class($this))));
  287. }
  288. /**
  289. * Converts an abstract column type into a physical column type.
  290. * The conversion is done using the type map specified in {@link columnTypes}.
  291. * These abstract column types are supported (using MySQL as example to explain the corresponding
  292. * physical types):
  293. * <ul>
  294. * <li>pk: an auto-incremental primary key type, will be converted into "int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY"</li>
  295. * <li>string: string type, will be converted into "varchar(255)"</li>
  296. * <li>text: a long string type, will be converted into "text"</li>
  297. * <li>integer: integer type, will be converted into "int(11)"</li>
  298. * <li>boolean: boolean type, will be converted into "tinyint(1)"</li>
  299. * <li>float: float number type, will be converted into "float"</li>
  300. * <li>decimal: decimal number type, will be converted into "decimal"</li>
  301. * <li>datetime: datetime type, will be converted into "datetime"</li>
  302. * <li>timestamp: timestamp type, will be converted into "timestamp"</li>
  303. * <li>time: time type, will be converted into "time"</li>
  304. * <li>date: date type, will be converted into "date"</li>
  305. * <li>binary: binary data type, will be converted into "blob"</li>
  306. * </ul>
  307. *
  308. * If the abstract type contains two or more parts separated by spaces (e.g. "string NOT NULL"), then only
  309. * the first part will be converted, and the rest of the parts will be appended to the conversion result.
  310. * For example, 'string NOT NULL' is converted to 'varchar(255) NOT NULL'.
  311. * @param string $type abstract column type
  312. * @return string physical column type.
  313. * @since 1.1.6
  314. */
  315. public function getColumnType($type)
  316. {
  317. if(isset($this->columnTypes[$type]))
  318. return $this->columnTypes[$type];
  319. elseif(($pos=strpos($type,' '))!==false)
  320. {
  321. $t=substr($type,0,$pos);
  322. return (isset($this->columnTypes[$t]) ? $this->columnTypes[$t] : $t).substr($type,$pos);
  323. }
  324. else
  325. return $type;
  326. }
  327. /**
  328. * Builds a SQL statement for creating a new DB table.
  329. *
  330. * The columns in the new table should be specified as name-definition pairs (e.g. 'name'=>'string'),
  331. * where name stands for a column name which will be properly quoted by the method, and definition
  332. * stands for the column type which can contain an abstract DB type.
  333. * The {@link getColumnType} method will be invoked to convert any abstract type into a physical one.
  334. *
  335. * If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly
  336. * inserted into the generated SQL.
  337. *
  338. * @param string $table the name of the table to be created. The name will be properly quoted by the method.
  339. * @param array $columns the columns (name=>definition) in the new table.
  340. * @param string $options additional SQL fragment that will be appended to the generated SQL.
  341. * @return string the SQL statement for creating a new DB table.
  342. * @since 1.1.6
  343. */
  344. public function createTable($table, $columns, $options=null)
  345. {
  346. $cols=array();
  347. foreach($columns as $name=>$type)
  348. {
  349. if(is_string($name))
  350. $cols[]="\t".$this->quoteColumnName($name).' '.$this->getColumnType($type);
  351. else
  352. $cols[]="\t".$type;
  353. }
  354. $sql="CREATE TABLE ".$this->quoteTableName($table)." (\n".implode(",\n",$cols)."\n)";
  355. return $options===null ? $sql : $sql.' '.$options;
  356. }
  357. /**
  358. * Builds a SQL statement for renaming a DB table.
  359. * @param string $table the table to be renamed. The name will be properly quoted by the method.
  360. * @param string $newName the new table name. The name will be properly quoted by the method.
  361. * @return string the SQL statement for renaming a DB table.
  362. * @since 1.1.6
  363. */
  364. public function renameTable($table, $newName)
  365. {
  366. return 'RENAME TABLE ' . $this->quoteTableName($table) . ' TO ' . $this->quoteTableName($newName);
  367. }
  368. /**
  369. * Builds a SQL statement for dropping a DB table.
  370. * @param string $table the table to be dropped. The name will be properly quoted by the method.
  371. * @return string the SQL statement for dropping a DB table.
  372. * @since 1.1.6
  373. */
  374. public function dropTable($table)
  375. {
  376. return "DROP TABLE ".$this->quoteTableName($table);
  377. }
  378. /**
  379. * Builds a SQL statement for truncating a DB table.
  380. * @param string $table the table to be truncated. The name will be properly quoted by the method.
  381. * @return string the SQL statement for truncating a DB table.
  382. * @since 1.1.6
  383. */
  384. public function truncateTable($table)
  385. {
  386. return "TRUNCATE TABLE ".$this->quoteTableName($table);
  387. }
  388. /**
  389. * Builds a SQL statement for adding a new DB column.
  390. * @param string $table the table that the new column will be added to. The table name will be properly quoted by the method.
  391. * @param string $column the name of the new column. The name will be properly quoted by the method.
  392. * @param string $type the column type. The {@link getColumnType} method will be invoked to convert abstract column type (if any)
  393. * into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL.
  394. * For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'.
  395. * @return string the SQL statement for adding a new column.
  396. * @since 1.1.6
  397. */
  398. public function addColumn($table, $column, $type)
  399. {
  400. return 'ALTER TABLE ' . $this->quoteTableName($table)
  401. . ' ADD ' . $this->quoteColumnName($column) . ' '
  402. . $this->getColumnType($type);
  403. }
  404. /**
  405. * Builds a SQL statement for dropping a DB column.
  406. * @param string $table the table whose column is to be dropped. The name will be properly quoted by the method.
  407. * @param string $column the name of the column to be dropped. The name will be properly quoted by the method.
  408. * @return string the SQL statement for dropping a DB column.
  409. * @since 1.1.6
  410. */
  411. public function dropColumn($table, $column)
  412. {
  413. return "ALTER TABLE ".$this->quoteTableName($table)
  414. ." DROP COLUMN ".$this->quoteColumnName($column);
  415. }
  416. /**
  417. * Builds a SQL statement for renaming a column.
  418. * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method.
  419. * @param string $name the old name of the column. The name will be properly quoted by the method.
  420. * @param string $newName the new name of the column. The name will be properly quoted by the method.
  421. * @return string the SQL statement for renaming a DB column.
  422. * @since 1.1.6
  423. */
  424. public function renameColumn($table, $name, $newName)
  425. {
  426. return "ALTER TABLE ".$this->quoteTableName($table)
  427. . " RENAME COLUMN ".$this->quoteColumnName($name)
  428. . " TO ".$this->quoteColumnName($newName);
  429. }
  430. /**
  431. * Builds a SQL statement for changing the definition of a column.
  432. * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method.
  433. * @param string $column the name of the column to be changed. The name will be properly quoted by the method.
  434. * @param string $type the new column type. The {@link getColumnType} method will be invoked to convert abstract column type (if any)
  435. * into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL.
  436. * For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'.
  437. * @return string the SQL statement for changing the definition of a column.
  438. * @since 1.1.6
  439. */
  440. public function alterColumn($table, $column, $type)
  441. {
  442. return 'ALTER TABLE ' . $this->quoteTableName($table) . ' CHANGE '
  443. . $this->quoteColumnName($column) . ' '
  444. . $this->quoteColumnName($column) . ' '
  445. . $this->getColumnType($type);
  446. }
  447. /**
  448. * Builds a SQL statement for adding a foreign key constraint to an existing table.
  449. * The method will properly quote the table and column names.
  450. * @param string $name the name of the foreign key constraint.
  451. * @param string $table the table that the foreign key constraint will be added to.
  452. * @param string $columns the name of the column to that the constraint will be added on. If there are multiple columns, separate them with commas.
  453. * @param string $refTable the table that the foreign key references to.
  454. * @param string $refColumns the name of the column that the foreign key references to. If there are multiple columns, separate them with commas.
  455. * @param string $delete the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL
  456. * @param string $update the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL
  457. * @return string the SQL statement for adding a foreign key constraint to an existing table.
  458. * @since 1.1.6
  459. */
  460. public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete=null, $update=null)
  461. {
  462. $columns=preg_split('/\s*,\s*/',$columns,-1,PREG_SPLIT_NO_EMPTY);
  463. foreach($columns as $i=>$col)
  464. $columns[$i]=$this->quoteColumnName($col);
  465. $refColumns=preg_split('/\s*,\s*/',$refColumns,-1,PREG_SPLIT_NO_EMPTY);
  466. foreach($refColumns as $i=>$col)
  467. $refColumns[$i]=$this->quoteColumnName($col);
  468. $sql='ALTER TABLE '.$this->quoteTableName($table)
  469. .' ADD CONSTRAINT '.$this->quoteColumnName($name)
  470. .' FOREIGN KEY ('.implode(', ', $columns).')'
  471. .' REFERENCES '.$this->quoteTableName($refTable)
  472. .' ('.implode(', ', $refColumns).')';
  473. if($delete!==null)
  474. $sql.=' ON DELETE '.$delete;
  475. if($update!==null)
  476. $sql.=' ON UPDATE '.$update;
  477. return $sql;
  478. }
  479. /**
  480. * Builds a SQL statement for dropping a foreign key constraint.
  481. * @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method.
  482. * @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method.
  483. * @return string the SQL statement for dropping a foreign key constraint.
  484. * @since 1.1.6
  485. */
  486. public function dropForeignKey($name, $table)
  487. {
  488. return 'ALTER TABLE '.$this->quoteTableName($table)
  489. .' DROP CONSTRAINT '.$this->quoteColumnName($name);
  490. }
  491. /**
  492. * Builds a SQL statement for creating a new index.
  493. * @param string $name the name of the index. The name will be properly quoted by the method.
  494. * @param string $table the table that the new index will be created for. The table name will be properly quoted by the method.
  495. * @param string $column the column(s) that should be included in the index. If there are multiple columns, please separate them
  496. * by commas. Each column name will be properly quoted by the method, unless a parenthesis is found in the name.
  497. * @param boolean $unique whether to add UNIQUE constraint on the created index.
  498. * @return string the SQL statement for creating a new index.
  499. * @since 1.1.6
  500. */
  501. public function createIndex($name, $table, $column, $unique=false)
  502. {
  503. $cols=array();
  504. $columns=preg_split('/\s*,\s*/',$column,-1,PREG_SPLIT_NO_EMPTY);
  505. foreach($columns as $col)
  506. {
  507. if(strpos($col,'(')!==false)
  508. $cols[]=$col;
  509. else
  510. $cols[]=$this->quoteColumnName($col);
  511. }
  512. return ($unique ? 'CREATE UNIQUE INDEX ' : 'CREATE INDEX ')
  513. . $this->quoteTableName($name).' ON '
  514. . $this->quoteTableName($table).' ('.implode(', ',$cols).')';
  515. }
  516. /**
  517. * Builds a SQL statement for dropping an index.
  518. * @param string $name the name of the index to be dropped. The name will be properly quoted by the method.
  519. * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method.
  520. * @return string the SQL statement for dropping an index.
  521. * @since 1.1.6
  522. */
  523. public function dropIndex($name, $table)
  524. {
  525. return 'DROP INDEX '.$this->quoteTableName($name).' ON '.$this->quoteTableName($table);
  526. }
  527. /**
  528. * Builds a SQL statement for adding a primary key constraint to an existing table.
  529. * @param string $name the name of the primary key constraint.
  530. * @param string $table the table that the primary key constraint will be added to.
  531. * @param string|array $columns comma separated string or array of columns that the primary key will consist of.
  532. * Array value can be passed since 1.1.14.
  533. * @return string the SQL statement for adding a primary key constraint to an existing table.
  534. * @since 1.1.13
  535. */
  536. public function addPrimaryKey($name,$table,$columns)
  537. {
  538. if(is_string($columns))
  539. $columns=preg_split('/\s*,\s*/',$columns,-1,PREG_SPLIT_NO_EMPTY);
  540. foreach($columns as $i=>$col)
  541. $columns[$i]=$this->quoteColumnName($col);
  542. return 'ALTER TABLE ' . $this->quoteTableName($table) . ' ADD CONSTRAINT '
  543. . $this->quoteColumnName($name) . ' PRIMARY KEY ('
  544. . implode(', ', $columns). ' )';
  545. }
  546. /**
  547. * Builds a SQL statement for removing a primary key constraint to an existing table.
  548. * @param string $name the name of the primary key constraint to be removed.
  549. * @param string $table the table that the primary key constraint will be removed from.
  550. * @return string the SQL statement for removing a primary key constraint from an existing table.
  551. * @since 1.1.13
  552. */
  553. public function dropPrimaryKey($name,$table)
  554. {
  555. return 'ALTER TABLE ' . $this->quoteTableName($table) . ' DROP CONSTRAINT '
  556. . $this->quoteColumnName($name);
  557. }
  558. }