Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

216 lines
6.7KB

  1. <?php
  2. namespace App\Kintone;
  3. use Closure;
  4. use Illuminate\Support\Carbon;
  5. use Illuminate\Support\Collection;
  6. class KintoneRecordQuery
  7. {
  8. private Collection $query;
  9. private string $order = "";
  10. private string $appName;
  11. public function __construct(string $appName)
  12. {
  13. $this->query = collect();
  14. $this->appName = $appName;
  15. }
  16. public function toQuery(): string
  17. {
  18. $ret = "";
  19. foreach ($this->query as $ele) {
  20. $ret .= $ele;
  21. $ret .= " ";
  22. }
  23. $ret .= $this->order;
  24. logger(sprintf("QUERY[%s]:%s", $this->appName, $ret));
  25. return $ret;
  26. }
  27. /**
  28. * @param string|\Closure(static $query): static $column
  29. */
  30. public function where(string|Closure $column, string|int|null $condition = null, KintoneRecordQueryOperator $operator = KintoneRecordQueryOperator::EQ)
  31. {
  32. return $this->whereQuery("and", $operator->value, $column, $condition);
  33. }
  34. /**
  35. * @param string|\Closure(static $query): static $column
  36. */
  37. public function notWhere(string|Closure $column, string|int|null $condition = null, KintoneRecordQueryOperator $operator = KintoneRecordQueryOperator::NEQ)
  38. {
  39. return $this->whereQuery("and", $operator->value, $column, $condition);
  40. }
  41. /**
  42. * @param string|\Closure(static $query): static $column
  43. */
  44. public function orWhere(string|Closure $column, string|int|null $condition = null, KintoneRecordQueryOperator $operator = KintoneRecordQueryOperator::EQ)
  45. {
  46. $this->whereQuery("or", $operator->value, $column, $condition);
  47. return $this;
  48. }
  49. /**
  50. * @param string|\Closure(static $query): static $column
  51. */
  52. public function notOrWhere(string|Closure $column, string|int|null $condition = null, KintoneRecordQueryOperator $operator = KintoneRecordQueryOperator::NEQ)
  53. {
  54. return $this->whereQuery("or", $operator->value, $column, $condition);
  55. }
  56. /**
  57. * @param string|\Closure(static $query): static $column
  58. */
  59. public function whereIn(string|Closure $column, array $condition)
  60. {
  61. return $this->whereQuery("and", "in", $column, $condition);
  62. }
  63. /**
  64. * @param string|\Closure(static $query): static $column
  65. */
  66. public function whereNotIn(string|Closure $column, array $condition)
  67. {
  68. return $this->whereQuery("and", "not in", $column, $condition);
  69. }
  70. /**
  71. * @param string|\Closure(static $query): static $column
  72. */
  73. public function orWhereIn(string|Closure $column, array $condition)
  74. {
  75. return $this->whereQuery("or", "not in", $column, $condition);
  76. }
  77. public function whereDate(string $column, Carbon $date, KintoneRecordQueryOperator $operator = KintoneRecordQueryOperator::EQ, bool $allowNull = false)
  78. {
  79. if (!$allowNull) {
  80. $this->whereNotNull($column);
  81. }
  82. return $this->whereQuery("and", $operator->value, $column, $date->format('Y-m-d'));
  83. }
  84. public function whereDateTime(string $column, Carbon $date, KintoneRecordQueryOperator $operator = KintoneRecordQueryOperator::EQ, bool $allowNull = false)
  85. {
  86. if (!$allowNull) {
  87. $this->whereNotNull($column);
  88. }
  89. return $this->whereQuery("and", $operator->value, $column, $date->toIso8601ZuluString());
  90. }
  91. public function orWhereDate(string $column, Carbon $date, KintoneRecordQueryOperator $operator = KintoneRecordQueryOperator::EQ, bool $allowNull = false)
  92. {
  93. if (!$allowNull) {
  94. $this->whereNotNull($column);
  95. }
  96. return $this->whereQuery("or", $operator->value, $column, $date->format('Y-m-d'));
  97. }
  98. public function orWhereDateTime(string $column, Carbon $date, KintoneRecordQueryOperator $operator = KintoneRecordQueryOperator::EQ, bool $allowNull = false)
  99. {
  100. if (!$allowNull) {
  101. $this->whereNotNull($column);
  102. }
  103. return $this->whereQuery("or", $operator->value, $column, $date->toIso8601ZuluString());
  104. }
  105. public function whereNull(string $column)
  106. {
  107. return $this->where($column, "");
  108. }
  109. public function owWhereNull(string $column)
  110. {
  111. return $this->orWhere($column, "");
  112. }
  113. public function whereNotNull(string $column)
  114. {
  115. return $this->notWhere($column, "");
  116. }
  117. public function owWhereNotNull(string $column)
  118. {
  119. return $this->notOrWhere($column, "");
  120. }
  121. public function orderByAsc(string $column)
  122. {
  123. return $this->orderBy($column, "asc");
  124. }
  125. public function orderByDesc(string $column)
  126. {
  127. return $this->orderBy($column, "desc");
  128. }
  129. private function orderBy(string $column, string $order)
  130. {
  131. $this->order = sprintf("order by %s %s", $column, $order);
  132. return $this;
  133. }
  134. /**
  135. * @param string|\Closure(static $query): static $column
  136. */
  137. private function whereQuery(
  138. string $andOr,
  139. string $operator,
  140. string|Closure $column,
  141. string|int|array|null $condition
  142. ) {
  143. if (!$this->query->isEmpty()) {
  144. $this->query->push($andOr);
  145. }
  146. if (is_string($column)) {
  147. if (is_string($condition)) {
  148. $this->query->push(sprintf(
  149. '%s %s "%s"',
  150. $column,
  151. $operator,
  152. $condition,
  153. ));
  154. return $this;
  155. }
  156. if (is_int($condition)) {
  157. $this->query->push(sprintf(
  158. '%s %s %d',
  159. $column,
  160. $operator,
  161. $condition,
  162. ));
  163. return $this;
  164. }
  165. if (is_array($condition)) {
  166. $in = "";
  167. foreach ($condition as $ele) {
  168. if (is_string($ele)) {
  169. if ($in !== "") {
  170. $in .= ", ";
  171. }
  172. $in .= sprintf('"%s"', $ele);
  173. }
  174. if (is_integer($ele)) {
  175. if ($in !== "") {
  176. $in .= ", ";
  177. }
  178. $in .= sprintf('%d', $ele);
  179. }
  180. }
  181. $this->query->push(sprintf(
  182. '%s %s (%s)',
  183. $column,
  184. $operator,
  185. $in,
  186. ));
  187. return $this;
  188. }
  189. }
  190. if ($column instanceof Closure) {
  191. $nestedCondition = new self($this->appName);
  192. $column($nestedCondition);
  193. $this->query->push(sprintf(
  194. '(%s)',
  195. $nestedCondition->toQuery()
  196. ));
  197. return $this;
  198. }
  199. return $this;
  200. }
  201. }