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

179 lines
5.8KB

  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. public function where(string|Closure $column, string|int|null $condition = null, KintoneRecordQueryOperator $operator = KintoneRecordQueryOperator::EQ)
  28. {
  29. return $this->whereQuery("and", $operator->value, $column, $condition);
  30. }
  31. public function notWhere(string|Closure $column, string|int|null $condition = null, KintoneRecordQueryOperator $operator = KintoneRecordQueryOperator::NEQ)
  32. {
  33. return $this->whereQuery("and", $operator->value, $column, $condition);
  34. }
  35. public function orWhere(string|Closure $column, string|int|null $condition = null, KintoneRecordQueryOperator $operator = KintoneRecordQueryOperator::EQ)
  36. {
  37. $this->whereQuery("or", $operator->value, $column, $condition);
  38. return $this;
  39. }
  40. public function notOrWhere(string|Closure $column, string|int|null $condition = null, KintoneRecordQueryOperator $operator = KintoneRecordQueryOperator::NEQ)
  41. {
  42. return $this->whereQuery("or", $operator->value, $column, $condition);
  43. }
  44. public function whereIn(string|Closure $column, array $condition)
  45. {
  46. return $this->whereQuery("and", "in", $column, $condition);
  47. }
  48. public function whereNotIn(string|Closure $column, array $condition)
  49. {
  50. return $this->whereQuery("and", "not in", $column, $condition);
  51. }
  52. public function orWhereIn(string|Closure $column, array $condition)
  53. {
  54. return $this->whereQuery("or", "not in", $column, $condition);
  55. }
  56. public function whereDate(string $column, Carbon $date, KintoneRecordQueryOperator $operator = KintoneRecordQueryOperator::EQ)
  57. {
  58. return $this->whereNotNull($column)->whereQuery("and", $operator->value, $column, $date->format('Y-m-d'));
  59. }
  60. public function whereDateTime(string $column, Carbon $date, KintoneRecordQueryOperator $operator = KintoneRecordQueryOperator::EQ)
  61. {
  62. return $this->whereNotNull($column)->whereQuery("and", $operator->value, $column, $date->toIso8601ZuluString());
  63. }
  64. public function orWhereDate(string $column, Carbon $date, KintoneRecordQueryOperator $operator = KintoneRecordQueryOperator::EQ)
  65. {
  66. return $this->whereNotNull($column)->whereQuery("or", $operator->value, $column, $date->format('Y-m-d'));
  67. }
  68. public function orWhereDateTime(string $column, Carbon $date, KintoneRecordQueryOperator $operator = KintoneRecordQueryOperator::EQ)
  69. {
  70. return $this->whereNotNull($column)->whereQuery("or", $operator->value, $column, $date->toIso8601ZuluString());
  71. }
  72. public function whereNull(string $column)
  73. {
  74. return $this->where($column, "");
  75. }
  76. public function owWhereNull(string $column)
  77. {
  78. return $this->orWhere($column, "");
  79. }
  80. public function whereNotNull(string $column)
  81. {
  82. return $this->notWhere($column, "");
  83. }
  84. public function owWhereNotNull(string $column)
  85. {
  86. return $this->notOrWhere($column, "");
  87. }
  88. public function orderByAsc(string $column)
  89. {
  90. return $this->orderBy($column, "asc");
  91. }
  92. public function orderByDesc(string $column)
  93. {
  94. return $this->orderBy($column, "desc");
  95. }
  96. private function orderBy(string $column, string $order)
  97. {
  98. $this->order = sprintf("order by %s %s", $column, $order);
  99. return $this;
  100. }
  101. private function whereQuery(
  102. string $andOr,
  103. string $operator,
  104. string|Closure $column,
  105. string|int|array|null $condition
  106. ) {
  107. if (!$this->query->isEmpty()) {
  108. $this->query->push($andOr);
  109. }
  110. if (is_string($column)) {
  111. if (is_string($condition)) {
  112. $this->query->push(sprintf(
  113. '%s %s "%s"',
  114. $column,
  115. $operator,
  116. $condition,
  117. ));
  118. return $this;
  119. }
  120. if (is_int($condition)) {
  121. $this->query->push(sprintf(
  122. '%s %s %d',
  123. $column,
  124. $operator,
  125. $condition,
  126. ));
  127. return $this;
  128. }
  129. if (is_array($condition)) {
  130. $in = "";
  131. foreach ($condition as $ele) {
  132. if (is_string($ele)) {
  133. if ($in !== "") {
  134. $in .= ", ";
  135. }
  136. $in .= sprintf('"%s"', $ele);
  137. }
  138. if (is_integer($ele)) {
  139. if ($in !== "") {
  140. $in .= ", ";
  141. }
  142. $in .= sprintf('%d', $ele);
  143. }
  144. }
  145. $this->query->push(sprintf(
  146. '%s %s (%s)',
  147. $column,
  148. $operator,
  149. $in,
  150. ));
  151. return $this;
  152. }
  153. }
  154. if ($column instanceof Closure) {
  155. $nestedCondition = new self($this->appName);
  156. $column($nestedCondition);
  157. $this->query->push(sprintf(
  158. '(%s)',
  159. $nestedCondition->toQuery()
  160. ));
  161. return $this;
  162. }
  163. }
  164. }