ConstraintFinderTrait.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\db;
  8. /**
  9. * ConstraintFinderTrait provides methods for getting a table constraint information.
  10. *
  11. * @property CheckConstraint[][] $schemaChecks Check constraints for all tables in the database.
  12. * Each array element is an array of [[CheckConstraint]] or its child classes. This property is read-only.
  13. * @property DefaultValueConstraint[] $schemaDefaultValues Default value constraints for all tables in the database.
  14. * Each array element is an array of [[DefaultValueConstraint]] or its child classes. This property is read-only.
  15. * @property ForeignKeyConstraint[][] $schemaForeignKeys Foreign keys for all tables in the database. Each
  16. * array element is an array of [[ForeignKeyConstraint]] or its child classes. This property is read-only.
  17. * @property IndexConstraint[][] $schemaIndexes Indexes for all tables in the database. Each array element is
  18. * an array of [[IndexConstraint]] or its child classes. This property is read-only.
  19. * @property Constraint[] $schemaPrimaryKeys Primary keys for all tables in the database. Each array element
  20. * is an instance of [[Constraint]] or its child class. This property is read-only.
  21. * @property IndexConstraint[][] $schemaUniques Unique constraints for all tables in the database.
  22. * Each array element is an array of [[IndexConstraint]] or its child classes. This property is read-only.
  23. *
  24. * @author Sergey Makinen <sergey@makinen.ru>
  25. * @since 2.0.13
  26. */
  27. trait ConstraintFinderTrait
  28. {
  29. /**
  30. * Returns the metadata of the given type for the given table.
  31. * @param string $name table name. The table name may contain schema name if any. Do not quote the table name.
  32. * @param string $type metadata type.
  33. * @param bool $refresh whether to reload the table metadata even if it is found in the cache.
  34. * @return mixed metadata.
  35. */
  36. abstract protected function getTableMetadata($name, $type, $refresh);
  37. /**
  38. * Returns the metadata of the given type for all tables in the given schema.
  39. * @param string $schema the schema of the metadata. Defaults to empty string, meaning the current or default schema name.
  40. * @param string $type metadata type.
  41. * @param bool $refresh whether to fetch the latest available table metadata. If this is `false`,
  42. * cached data may be returned if available.
  43. * @return array array of metadata.
  44. */
  45. abstract protected function getSchemaMetadata($schema, $type, $refresh);
  46. /**
  47. * Loads a primary key for the given table.
  48. * @param string $tableName table name.
  49. * @return Constraint|null primary key for the given table, `null` if the table has no primary key.
  50. */
  51. abstract protected function loadTablePrimaryKey($tableName);
  52. /**
  53. * Loads all foreign keys for the given table.
  54. * @param string $tableName table name.
  55. * @return ForeignKeyConstraint[] foreign keys for the given table.
  56. */
  57. abstract protected function loadTableForeignKeys($tableName);
  58. /**
  59. * Loads all indexes for the given table.
  60. * @param string $tableName table name.
  61. * @return IndexConstraint[] indexes for the given table.
  62. */
  63. abstract protected function loadTableIndexes($tableName);
  64. /**
  65. * Loads all unique constraints for the given table.
  66. * @param string $tableName table name.
  67. * @return Constraint[] unique constraints for the given table.
  68. */
  69. abstract protected function loadTableUniques($tableName);
  70. /**
  71. * Loads all check constraints for the given table.
  72. * @param string $tableName table name.
  73. * @return CheckConstraint[] check constraints for the given table.
  74. */
  75. abstract protected function loadTableChecks($tableName);
  76. /**
  77. * Loads all default value constraints for the given table.
  78. *
  79. * @param string $tableName table name.
  80. * @return DefaultValueConstraint[] default value constraints for the given table.
  81. */
  82. abstract protected function loadTableDefaultValues($tableName);
  83. /**
  84. * Obtains the primary key for the named table.
  85. * @param string $name table name. The table name may contain schema name if any. Do not quote the table name.
  86. * @param bool $refresh whether to reload the information even if it is found in the cache.
  87. * @return Constraint|null table primary key, `null` if the table has no primary key.
  88. */
  89. public function getTablePrimaryKey($name, $refresh = false)
  90. {
  91. return $this->getTableMetadata($name, 'primaryKey', $refresh);
  92. }
  93. /**
  94. * Returns primary keys for all tables in the database.
  95. * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema name.
  96. * @param bool $refresh whether to fetch the latest available table schemas. If this is `false`,
  97. * cached data may be returned if available.
  98. * @return Constraint[] primary keys for all tables in the database.
  99. * Each array element is an instance of [[Constraint]] or its child class.
  100. */
  101. public function getSchemaPrimaryKeys($schema = '', $refresh = false)
  102. {
  103. return $this->getSchemaMetadata($schema, 'primaryKey', $refresh);
  104. }
  105. /**
  106. * Obtains the foreign keys information for the named table.
  107. * @param string $name table name. The table name may contain schema name if any. Do not quote the table name.
  108. * @param bool $refresh whether to reload the information even if it is found in the cache.
  109. * @return ForeignKeyConstraint[] table foreign keys.
  110. */
  111. public function getTableForeignKeys($name, $refresh = false)
  112. {
  113. return $this->getTableMetadata($name, 'foreignKeys', $refresh);
  114. }
  115. /**
  116. * Returns foreign keys for all tables in the database.
  117. * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema name.
  118. * @param bool $refresh whether to fetch the latest available table schemas. If this is false,
  119. * cached data may be returned if available.
  120. * @return ForeignKeyConstraint[][] foreign keys for all tables in the database.
  121. * Each array element is an array of [[ForeignKeyConstraint]] or its child classes.
  122. */
  123. public function getSchemaForeignKeys($schema = '', $refresh = false)
  124. {
  125. return $this->getSchemaMetadata($schema, 'foreignKeys', $refresh);
  126. }
  127. /**
  128. * Obtains the indexes information for the named table.
  129. * @param string $name table name. The table name may contain schema name if any. Do not quote the table name.
  130. * @param bool $refresh whether to reload the information even if it is found in the cache.
  131. * @return IndexConstraint[] table indexes.
  132. */
  133. public function getTableIndexes($name, $refresh = false)
  134. {
  135. return $this->getTableMetadata($name, 'indexes', $refresh);
  136. }
  137. /**
  138. * Returns indexes for all tables in the database.
  139. * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema name.
  140. * @param bool $refresh whether to fetch the latest available table schemas. If this is false,
  141. * cached data may be returned if available.
  142. * @return IndexConstraint[][] indexes for all tables in the database.
  143. * Each array element is an array of [[IndexConstraint]] or its child classes.
  144. */
  145. public function getSchemaIndexes($schema = '', $refresh = false)
  146. {
  147. return $this->getSchemaMetadata($schema, 'indexes', $refresh);
  148. }
  149. /**
  150. * Obtains the unique constraints information for the named table.
  151. * @param string $name table name. The table name may contain schema name if any. Do not quote the table name.
  152. * @param bool $refresh whether to reload the information even if it is found in the cache.
  153. * @return Constraint[] table unique constraints.
  154. */
  155. public function getTableUniques($name, $refresh = false)
  156. {
  157. return $this->getTableMetadata($name, 'uniques', $refresh);
  158. }
  159. /**
  160. * Returns unique constraints for all tables in the database.
  161. * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema name.
  162. * @param bool $refresh whether to fetch the latest available table schemas. If this is false,
  163. * cached data may be returned if available.
  164. * @return Constraint[][] unique constraints for all tables in the database.
  165. * Each array element is an array of [[Constraint]] or its child classes.
  166. */
  167. public function getSchemaUniques($schema = '', $refresh = false)
  168. {
  169. return $this->getSchemaMetadata($schema, 'uniques', $refresh);
  170. }
  171. /**
  172. * Obtains the check constraints information for the named table.
  173. * @param string $name table name. The table name may contain schema name if any. Do not quote the table name.
  174. * @param bool $refresh whether to reload the information even if it is found in the cache.
  175. * @return CheckConstraint[] table check constraints.
  176. */
  177. public function getTableChecks($name, $refresh = false)
  178. {
  179. return $this->getTableMetadata($name, 'checks', $refresh);
  180. }
  181. /**
  182. * Returns check constraints for all tables in the database.
  183. * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema name.
  184. * @param bool $refresh whether to fetch the latest available table schemas. If this is false,
  185. * cached data may be returned if available.
  186. * @return CheckConstraint[][] check constraints for all tables in the database.
  187. * Each array element is an array of [[CheckConstraint]] or its child classes.
  188. */
  189. public function getSchemaChecks($schema = '', $refresh = false)
  190. {
  191. return $this->getSchemaMetadata($schema, 'checks', $refresh);
  192. }
  193. /**
  194. * Obtains the default value constraints information for the named table.
  195. * @param string $name table name. The table name may contain schema name if any. Do not quote the table name.
  196. * @param bool $refresh whether to reload the information even if it is found in the cache.
  197. * @return DefaultValueConstraint[] table default value constraints.
  198. */
  199. public function getTableDefaultValues($name, $refresh = false)
  200. {
  201. return $this->getTableMetadata($name, 'defaultValues', $refresh);
  202. }
  203. /**
  204. * Returns default value constraints for all tables in the database.
  205. * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema name.
  206. * @param bool $refresh whether to fetch the latest available table schemas. If this is false,
  207. * cached data may be returned if available.
  208. * @return DefaultValueConstraint[] default value constraints for all tables in the database.
  209. * Each array element is an array of [[DefaultValueConstraint]] or its child classes.
  210. */
  211. public function getSchemaDefaultValues($schema = '', $refresh = false)
  212. {
  213. return $this->getSchemaMetadata($schema, 'defaultValues', $refresh);
  214. }
  215. }