ActiveQuery.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace common\components;
  3. /**
  4. * Created by PhpStorm.
  5. * User: leo
  6. * Date: 2018/3/31
  7. * Time: 下午5:39
  8. */
  9. class ActiveQuery extends \yii\db\ActiveQuery {
  10. /**
  11. * 原始是否启用从库的配置
  12. * @var
  13. */
  14. public $oriEnableSlaves;
  15. /**
  16. * 从哪个数据库查询
  17. * @var string
  18. */
  19. public $fromDb = '';
  20. /**
  21. * 查找一条数据
  22. * @param null $db
  23. * @return array|null|\yii\db\ActiveRecord
  24. */
  25. public function one($db = null)
  26. {
  27. $result = parent::one($db);
  28. $this->_switchSlaves($db);
  29. return $result;
  30. }
  31. /**
  32. * 查询全部数据
  33. * @param null $db
  34. * @return array|\yii\db\ActiveRecord[]
  35. */
  36. public function all($db = null)
  37. {
  38. $result = parent::all($db);
  39. $this->_switchSlaves($db);
  40. return $result;
  41. }
  42. /**
  43. * 批量查询逐条返回
  44. * @param int $batchSize
  45. * @param null $db
  46. * @return \yii\db\BatchQueryResult
  47. */
  48. public function each($batchSize = 100, $db = null)
  49. {
  50. $result = parent::each($batchSize, $db);
  51. $this->_switchSlaves($db);
  52. return $result;
  53. }
  54. /**
  55. * 批量查询,批量返回
  56. * @param int $batchSize
  57. * @param null $db
  58. * @return \yii\db\BatchQueryResult
  59. */
  60. public function batch($batchSize = 100, $db = null)
  61. {
  62. $result = parent::batch($batchSize, $db);
  63. $this->_switchSlaves($db);
  64. return $result;
  65. }
  66. /**
  67. * 增加可以从指定库查表的功能
  68. * @param null $db
  69. * @return \yii\db\Command
  70. * @throws \yii\base\InvalidConfigException
  71. */
  72. public function createCommand($db = null)
  73. {
  74. /* @var $modelClass ActiveRecord */
  75. $modelClass = $this->modelClass;
  76. if ($db === null) {
  77. if($this->fromDb === ''){
  78. $db = $modelClass::getDb();
  79. } else {
  80. $db = \Yii::$app->get($this->fromDb);
  81. }
  82. }
  83. return parent::createCommand($db);
  84. }
  85. /**
  86. * 获取全部表分区
  87. * @param null $db
  88. * @return array
  89. */
  90. public function allPart($db = null){
  91. if ($this->emulateExecution) {
  92. return [];
  93. }
  94. if($db === null){
  95. $db = $this->modelClass::getDb();
  96. }
  97. // 从主库去获取表分区
  98. $db->enableSlaves = false;
  99. $tableName = $this->modelClass::tableName();
  100. $sql = "SELECT * FROM USER_TAB_PARTITIONS WHERE TABLE_NAME='$tableName'";
  101. $rows = $db->createCommand($sql)->queryAll();
  102. // 切换回原始设置
  103. $db->enableSlaves = $this->oriEnableSlaves;
  104. return $this->populate($rows);
  105. }
  106. /**
  107. * 从分区表中查出年月所在分区的相关数据(如果有from()放在from()后面使用)
  108. * @param $yearMonth
  109. * @param string $format
  110. * @return $this
  111. */
  112. public function yearMonth($yearMonth, $format='YYYYMM'){
  113. // if(!$this->from){
  114. // $tableName = $this->modelClass::tableName();
  115. // } else {
  116. // $tableName = $this->from[0];
  117. // }
  118. // $alias = '';
  119. // $pregResult = null;
  120. // if(preg_match('/^\s*(\S+)\s+(AS\s+|as\s+)?(\S+)\s*$/', $tableName, $pregResult)){
  121. // $tableName = $pregResult[1];
  122. // $alias = ' '.$pregResult[3];
  123. // }
  124. // if(!$yearMonth){
  125. // $yearMonth = '201701';
  126. // }
  127. // $this->from = [$tableName." PARTITION FOR(TO_DATE('$yearMonth'".','."'$format'))$alias"];
  128. return $this;
  129. }
  130. /**
  131. * 从指定数据库查询
  132. * @param string $fromDb
  133. * @return $this
  134. */
  135. public function fromDb(string $fromDb){
  136. $this->fromDb = $fromDb;
  137. return $this;
  138. }
  139. /**
  140. * 切换回原始的主从设置
  141. * @param null $db
  142. */
  143. private function _switchSlaves($db = null){
  144. if($db === null){
  145. $db = $this->modelClass::getDb();
  146. }
  147. $db->enableSlaves = $this->oriEnableSlaves;
  148. }
  149. /**
  150. * 要筛选的字段,不用*
  151. * @param array $append
  152. * @return $this
  153. */
  154. public function selectNoText($append = []){
  155. $modeClass = $this->modelClass;
  156. $columns = $modeClass::getTableSchema()->columns;
  157. $result = array_filter($columns, function($col){
  158. return in_array($col->dbType, ['text', 'longtext', 'tinytext', 'CLOB']);
  159. });
  160. if($result){
  161. foreach($result as $k=>$v){
  162. unset($columns[$k]);
  163. }
  164. }
  165. $selects = array_map(function ($value) use($modeClass){
  166. return $modeClass::tableName().'.'.$value;
  167. }, array_keys($columns));
  168. if($append){
  169. $selects = array_merge($selects, $append);
  170. }
  171. $this->select($selects);
  172. return $this;
  173. }
  174. /**
  175. * 以ID筛选
  176. * @param $id
  177. * @param string $key
  178. * @return $this
  179. */
  180. public function is($id, $key = 'ID'){
  181. $this->where($key .'=:' . $key, [$key=>$id]);
  182. return $this;
  183. }
  184. }