开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (1)
标签 (93)
master
v2.0.1
v2.0.0
v1.7.2
v1.7.1
v1.7.0
v1.6.5
v1.6.4
v1.6.3
v1.6.2
v1.6.1
v1.6.0
v1.5.5
v1.5.4
v1.5.3
v1.5.2
v1.5.1
v1.5.0
v1.4.7
v1.4.6
v1.4.5
master
分支 (1)
标签 (93)
master
v2.0.1
v2.0.0
v1.7.2
v1.7.1
v1.7.0
v1.6.5
v1.6.4
v1.6.3
v1.6.2
v1.6.1
v1.6.0
v1.5.5
v1.5.4
v1.5.3
v1.5.2
v1.5.1
v1.5.0
v1.4.7
v1.4.6
v1.4.5
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (1)
标签 (93)
master
v2.0.1
v2.0.0
v1.7.2
v1.7.1
v1.7.0
v1.6.5
v1.6.4
v1.6.3
v1.6.2
v1.6.1
v1.6.0
v1.5.5
v1.5.4
v1.5.3
v1.5.2
v1.5.1
v1.5.0
v1.4.7
v1.4.6
v1.4.5
phpgsql
/
src
/
Fluent
/
Query.php
phpgsql
/
src
/
Fluent
/
Query.php
Query.php 22.52 KB
一键复制 编辑 原始数据 按行查看 历史
Jakub Trmota 提交于 2025年08月27日 04:06 +08:00 . Fluent: whereIf - can use callable parameters
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
<?php declare(strict_types=1);
namespace Forrest79\PhPgSql\Fluent;
use Forrest79\PhPgSql\Db;
/**
* @phpstan-import-type QueryParams from QueryBuilder
*/
class Query implements Db\Sql
{
public const string QUERY_SELECT = 'select';
public const string QUERY_INSERT = 'insert';
public const string QUERY_UPDATE = 'update';
public const string QUERY_DELETE = 'delete';
public const string QUERY_MERGE = 'merge';
public const string QUERY_TRUNCATE = 'truncate';
public const string PARAM_SELECT = 'select';
public const string PARAM_DISTINCT = 'distinct';
public const string PARAM_DISTINCTON = 'distinctOn';
public const string PARAM_TABLES = 'tables';
public const string PARAM_TABLE_TYPES = 'table-types';
public const string PARAM_ON_CONDITIONS = 'on-conditions';
public const string PARAM_LATERAL_TABLES = 'lateral-tables';
public const string PARAM_WHERE = 'where';
public const string PARAM_GROUPBY = 'groupBy';
public const string PARAM_HAVING = 'having';
public const string PARAM_ORDERBY = 'orderBy';
public const string PARAM_LIMIT = 'limit';
public const string PARAM_OFFSET = 'offset';
public const string PARAM_COMBINE_QUERIES = 'combine-queries';
public const string PARAM_INSERT_COLUMNS = 'insert-columns';
public const string PARAM_INSERT_ONCONFLICT = 'insert-onconflict';
public const string PARAM_RETURNING = 'returning';
public const string PARAM_DATA = 'data';
public const string PARAM_ROWS = 'rows';
public const string PARAM_MERGE = 'merge';
public const string PARAM_WITH = 'with';
public const string PARAM_PREFIX = 'prefix';
public const string PARAM_SUFFIX = 'suffix';
public const string TABLE_TYPE_MAIN = 'main';
public const string TABLE_TYPE_FROM = 'from';
public const string TABLE_TYPE_JOINS = 'joins';
public const string TABLE_TYPE_USING = 'using';
private const string JOIN_INNER = 'INNER JOIN';
private const string JOIN_LEFT_OUTER = 'LEFT OUTER JOIN';
private const string JOIN_RIGHT_OUTER = 'RIGHT OUTER JOIN';
private const string JOIN_FULL_OUTER = 'FULL OUTER JOIN';
public const string JOIN_CROSS = 'CROSS JOIN';
private const string COMBINE_UNION = 'UNION';
private const string COMBINE_UNION_ALL = 'UNION ALL';
private const string COMBINE_INTERSECT = 'INTERSECT';
private const string COMBINE_EXCEPT = 'EXCEPT';
public const string INSERT_ONCONFLICT_COLUMNS_OR_CONSTRAINT = 'columns-or-constraint';
public const string INSERT_ONCONFLICT_WHERE = 'where';
public const string INSERT_ONCONFLICT_DO = 'do';
public const string INSERT_ONCONFLICT_DO_WHERE = 'do-where';
public const string MERGE_WHEN_MATCHED = 'when-matched';
public const string MERGE_WHEN_NOT_MATCHED = 'when-not-matched';
public const string WITH_QUERIES = 'queries';
public const string WITH_QUERIES_SUFFIX = 'queries-suffix';
public const string WITH_QUERIES_NOT_MATERIALIZED = 'queries-not-materialized';
public const string WITH_RECURSIVE = 'recursive';
private const array DEFAULT_PARAMS = [
self::PARAM_SELECT => [],
self::PARAM_DISTINCT => false,
self::PARAM_DISTINCTON => [],
self::PARAM_TABLES => [],
self::PARAM_TABLE_TYPES => [
self::TABLE_TYPE_MAIN => null,
self::TABLE_TYPE_FROM => [],
self::TABLE_TYPE_JOINS => [],
self::TABLE_TYPE_USING => null,
],
self::PARAM_ON_CONDITIONS => [],
self::PARAM_LATERAL_TABLES => [],
self::PARAM_WHERE => null,
self::PARAM_GROUPBY => [],
self::PARAM_HAVING => null,
self::PARAM_ORDERBY => [],
self::PARAM_LIMIT => null,
self::PARAM_OFFSET => null,
self::PARAM_COMBINE_QUERIES => [],
self::PARAM_INSERT_COLUMNS => [],
self::PARAM_INSERT_ONCONFLICT => [
self::INSERT_ONCONFLICT_COLUMNS_OR_CONSTRAINT => null,
self::INSERT_ONCONFLICT_WHERE => null,
self::INSERT_ONCONFLICT_DO => null,
self::INSERT_ONCONFLICT_DO_WHERE => null,
],
self::PARAM_RETURNING => [],
self::PARAM_DATA => [],
self::PARAM_ROWS => [],
self::PARAM_MERGE => [],
self::PARAM_WITH => [
self::WITH_QUERIES => [],
self::WITH_QUERIES_SUFFIX => [],
self::WITH_QUERIES_NOT_MATERIALIZED => [],
self::WITH_RECURSIVE => false,
],
self::PARAM_PREFIX => [],
self::PARAM_SUFFIX => [],
];
private QueryBuilder $queryBuilder;
private string $queryType = self::QUERY_SELECT;
/** @phpstan-var QueryParams */
private array $params = self::DEFAULT_PARAMS;
private Db\SqlDefinition|null $sqlDefinition = null;
private Db\Query|null $dbQuery = null;
public function __construct(QueryBuilder $queryBuilder)
{
$this->queryBuilder = $queryBuilder;
}
/**
* @throws Exceptions\QueryException
*/
public function table(string|Db\Sql $table, string|null $alias = null): static
{
return $this->addTable(self::TABLE_TYPE_MAIN, $table, $alias);
}
/**
* @param array<int|string, string|int|bool|\BackedEnum|Db\Sql|null> $columns
* @throws Exceptions\QueryException
*/
public function select(array $columns): static
{
$this->resetQuery();
foreach ($columns as $alias => &$column) {
if (\is_int($alias)) {
$alias = null;
}
if ($column === null) {
$column = 'NULL';
} else if ($column === true) {
$column = 'TRUE';
} else if ($column === false) {
$column = 'FALSE';
}
$this->checkAlias($column, $alias);
if (!($column instanceof Db\Sql) && !\is_scalar($column) && !($column instanceof \BackedEnum)) {
throw Exceptions\QueryException::columnMustBeScalarOrEnumOrExpression();
}
}
$this->params[self::PARAM_SELECT] = \array_merge($this->params[self::PARAM_SELECT], $columns);
return $this;
}
/**
* @throws Exceptions\QueryException
*/
public function distinct(): static
{
$this->resetQuery();
$this->params[self::PARAM_DISTINCT] = true;
return $this;
}
/**
* @throws Exceptions\QueryException
*/
public function distinctOn(string|Db\Sql ...$on): static
{
$this->resetQuery();
$this->params[self::PARAM_DISTINCTON] = \array_merge($this->params[self::PARAM_DISTINCTON], $on);
return $this;
}
/**
* @throws Exceptions\QueryException
*/
public function from(string|Db\Sql $from, string|null $alias = null): static
{
return $this->addTable(self::TABLE_TYPE_FROM, $from, $alias);
}
/**
* @throws Exceptions\QueryException
*/
public function join(
string|Db\Sql $join,
string|null $alias = null,
string|Db\Sql|null $onCondition = null,
): static
{
return $this->innerJoin($join, $alias, $onCondition);
}
/**
* @throws Exceptions\QueryException
*/
public function innerJoin(
string|Db\Sql $join,
string|null $alias = null,
string|Db\Sql|null $onCondition = null,
): static
{
return $this->addTable(self::JOIN_INNER, $join, $alias, $onCondition);
}
/**
* @throws Exceptions\QueryException
*/
public function leftJoin(
string|Db\Sql $join,
string|null $alias = null,
string|Db\Sql|null $onCondition = null,
): static
{
return $this->leftOuterJoin($join, $alias, $onCondition);
}
/**
* @throws Exceptions\QueryException
*/
public function leftOuterJoin(
string|Db\Sql $join,
string|null $alias = null,
string|Db\Sql|null $onCondition = null,
): static
{
return $this->addTable(self::JOIN_LEFT_OUTER, $join, $alias, $onCondition);
}
/**
* @throws Exceptions\QueryException
*/
public function rightJoin(
string|Db\Sql $join,
string|null $alias = null,
string|Db\Sql|null $onCondition = null,
): static
{
return $this->rightOuterJoin($join, $alias, $onCondition);
}
/**
* @throws Exceptions\QueryException
*/
public function rightOuterJoin(
string|Db\Sql $join,
string|null $alias = null,
string|Db\Sql|null $onCondition = null,
): static
{
return $this->addTable(self::JOIN_RIGHT_OUTER, $join, $alias, $onCondition);
}
/**
* @throws Exceptions\QueryException
*/
public function fullJoin(
string|Db\Sql $join,
string|null $alias = null,
string|Db\Sql|null $onCondition = null,
): static
{
return $this->fullOuterJoin($join, $alias, $onCondition);
}
/**
* @throws Exceptions\QueryException
*/
public function fullOuterJoin(
string|Db\Sql $join,
string|null $alias = null,
string|Db\Sql|null $onCondition = null,
): static
{
return $this->addTable(self::JOIN_FULL_OUTER, $join, $alias, $onCondition);
}
/**
* @throws Exceptions\QueryException
*/
public function crossJoin(string|Db\Sql $join, string|null $alias = null): static
{
return $this->addTable(self::JOIN_CROSS, $join, $alias);
}
/**
* @throws Exceptions\QueryException
*/
private function addTable(
string $type,
string|Db\Sql $name,
string|null $alias,
string|Db\Sql|null $onCondition = null,
): static
{
$this->resetQuery();
$this->checkAlias($name, $alias);
if (\in_array($type, [self::TABLE_TYPE_MAIN, self::TABLE_TYPE_USING], true) && ($this->params[self::PARAM_TABLE_TYPES][$type] !== null)) {
throw ($type === self::TABLE_TYPE_MAIN) ? Exceptions\QueryException::onlyOneMainTable() : Exceptions\QueryException::mergeOnlyOneUsing();
}
if ($alias === null) {
$alias = $name;
\assert(\is_string($alias));
}
if (isset($this->params[self::PARAM_TABLES][$alias])) {
throw Exceptions\QueryException::tableAliasAlreadyExists($alias);
}
$this->params[self::PARAM_TABLES][$alias] = [$name, $type];
if (\in_array($type, [self::TABLE_TYPE_MAIN, self::TABLE_TYPE_USING], true)) {
$this->params[self::PARAM_TABLE_TYPES][$type] = $alias;
} else {
$this->params[self::PARAM_TABLE_TYPES][$type === self::TABLE_TYPE_FROM ? $type : self::TABLE_TYPE_JOINS][] = $alias;
}
if ($onCondition !== null) {
$this->getConditionParam(self::PARAM_ON_CONDITIONS, $alias)->add($onCondition);
}
return $this;
}
/**
* @throws Exceptions\QueryException
*/
public function on(string $alias, string|Db\Sql $condition, mixed ...$params): static
{
$this->resetQuery();
$this->getConditionParam(self::PARAM_ON_CONDITIONS, $alias)->add($condition, ...$params);
return $this;
}
public function lateral(string $alias): static
{
$this->params[self::PARAM_LATERAL_TABLES][$alias] = $alias;
return $this;
}
/**
* @throws Exceptions\QueryException
*/
public function where(string|Db\Sql $condition, mixed ...$params): static
{
$this->resetQuery();
$this->getConditionParam(self::PARAM_WHERE)->add($condition, ...$params);
return $this;
}
/**
* @throws Exceptions\QueryException
*/
public function whereIf(bool $ifCondition, string|Db\Sql $condition, mixed ...$params): static
{
if ($ifCondition) {
foreach ($params as $i => $param) {
if (is_callable($param)) {
$params[$i] = call_user_func($param);
}
}
return $this->where($condition, ...$params);
}
return $this;
}
/**
* @param list<string|list<mixed>|Db\Sql> $conditions
* @throws Exceptions\QueryException
*/
public function whereAnd(array $conditions = []): Condition
{
$this->resetQuery();
$condition = Condition::createAnd($conditions, null, $this);
$this->getConditionParam(self::PARAM_WHERE)->add($condition);
return $condition;
}
/**
* @param list<string|list<mixed>|Db\Sql> $conditions
* @throws Exceptions\QueryException
*/
public function whereOr(array $conditions = []): Condition
{
$this->resetQuery();
$condition = Condition::createOr($conditions, null, $this);
$this->getConditionParam(self::PARAM_WHERE)->add($condition);
return $condition;
}
/**
* @throws Exceptions\QueryException
*/
public function groupBy(string ...$columns): static
{
$this->resetQuery();
$this->params[self::PARAM_GROUPBY] = \array_merge($this->params[self::PARAM_GROUPBY], $columns);
return $this;
}
/**
* @throws Exceptions\QueryException
*/
public function having(string|Db\Sql $condition, mixed ...$params): static
{
$this->resetQuery();
$this->getConditionParam(self::PARAM_HAVING)->add($condition, ...$params);
return $this;
}
/**
* @param list<string|list<mixed>|Db\Sql> $conditions
* @throws Exceptions\QueryException
*/
public function havingAnd(array $conditions = []): Condition
{
$this->resetQuery();
$condition = Condition::createAnd($conditions, null, $this);
$this->getConditionParam(self::PARAM_HAVING)->add($condition);
return $condition;
}
/**
* @param list<string|list<mixed>|Db\Sql> $conditions
* @throws Exceptions\QueryException
*/
public function havingOr(array $conditions = []): Condition
{
$this->resetQuery();
$condition = Condition::createOr($conditions, null, $this);
$this->getConditionParam(self::PARAM_HAVING)->add($condition);
return $condition;
}
private function getConditionParam(string $param, string|null $alias = null): Condition
{
if ($param === self::PARAM_ON_CONDITIONS) {
if (!isset($this->params[$param][$alias])) {
$this->params[$param][$alias] = Condition::createAnd();
}
return $this->params[$param][$alias];
} else if (($param === self::PARAM_WHERE) || ($param === self::PARAM_HAVING)) {
if ($this->params[$param] === null) {
$this->params[$param] = Condition::createAnd();
}
return $this->params[$param];
}
throw new Exceptions\ShouldNotHappenException('Invalid argument: ' . $param);
}
/**
* @throws Exceptions\QueryException
*/
public function orderBy(string|Db\Sql ...$columns): static
{
$this->resetQuery();
$this->params[self::PARAM_ORDERBY] = \array_merge($this->params[self::PARAM_ORDERBY], $columns);
return $this;
}
/**
* @throws Exceptions\QueryException
*/
public function limit(int $limit): static
{
$this->resetQuery();
$this->params[self::PARAM_LIMIT] = $limit;
return $this;
}
/**
* @throws Exceptions\QueryException
*/
public function offset(int $offset): static
{
$this->resetQuery();
$this->params[self::PARAM_OFFSET] = $offset;
return $this;
}
public function union(string|Db\Sql $query): static
{
return $this->addCombine(self::COMBINE_UNION, $query);
}
public function unionAll(string|Db\Sql $query): static
{
return $this->addCombine(self::COMBINE_UNION_ALL, $query);
}
public function intersect(string|Db\Sql $query): static
{
return $this->addCombine(self::COMBINE_INTERSECT, $query);
}
public function except(string|Db\Sql $query): static
{
return $this->addCombine(self::COMBINE_EXCEPT, $query);
}
private function addCombine(string $type, string|Db\Sql $query): static
{
$this->params[self::PARAM_COMBINE_QUERIES][] = [$query, $type];
return $this;
}
/**
* @param list<string>|null $columns
* @throws Exceptions\QueryException
*/
public function insert(string|null $into = null, string|null $alias = null, array|null $columns = []): static
{
$this->resetQuery();
$this->queryType = self::QUERY_INSERT;
if ($into !== null) {
$this->table($into, $alias);
}
$this->params[self::PARAM_INSERT_COLUMNS] = $columns;
return $this;
}
/**
* @param array<string, mixed> $data
* @throws Exceptions\QueryException
*/
public function values(array $data): static
{
$this->resetQuery();
$this->queryType = self::QUERY_INSERT;
$this->params[self::PARAM_DATA] = $data + $this->params[self::PARAM_DATA];
return $this;
}
/**
* @param list<array<string, mixed>> $rows
* @throws Exceptions\QueryException
*/
public function rows(array $rows): static
{
$this->resetQuery();
$this->queryType = self::QUERY_INSERT;
$this->params[self::PARAM_ROWS] = \array_merge($this->params[self::PARAM_ROWS], $rows);
return $this;
}
/**
* @param string|list<string>|null $columnsOrConstraint
* @throws Exceptions\QueryException
*/
public function onConflict(string|array|null $columnsOrConstraint = null, string|Db\Sql|null $where = null): static
{
$this->resetQuery();
if (\is_string($columnsOrConstraint) && ($where !== null)) {
throw Exceptions\QueryException::onConflictWhereNotForConstraint();
}
$this->params[self::PARAM_INSERT_ONCONFLICT][self::INSERT_ONCONFLICT_COLUMNS_OR_CONSTRAINT] = $columnsOrConstraint ?? false;
$this->params[self::PARAM_INSERT_ONCONFLICT][self::INSERT_ONCONFLICT_WHERE] = $where === null ? null : Condition::createAnd()->add($where);
return $this;
}
/**
* @param array<int|string, string|Db\Sql> $set
* @throws Exceptions\QueryException
*/
public function doUpdate(array $set, string|Db\Sql|null $where = null): static
{
$this->resetQuery();
$this->params[self::PARAM_INSERT_ONCONFLICT][self::INSERT_ONCONFLICT_DO] = $set;
$this->params[self::PARAM_INSERT_ONCONFLICT][self::INSERT_ONCONFLICT_DO_WHERE] = $where === null ? null : Condition::createAnd()->add($where);
return $this;
}
/**
* @throws Exceptions\QueryException
*/
public function doNothing(): static
{
$this->resetQuery();
$this->params[self::PARAM_INSERT_ONCONFLICT][self::INSERT_ONCONFLICT_DO] = false;
$this->params[self::PARAM_INSERT_ONCONFLICT][self::INSERT_ONCONFLICT_DO_WHERE] = null;
return $this;
}
/**
* @throws Exceptions\QueryException
*/
public function update(string|null $table = null, string|null $alias = null): static
{
$this->resetQuery();
$this->queryType = self::QUERY_UPDATE;
if ($table !== null) {
$this->table($table, $alias);
}
return $this;
}
/**
* @param array<string, mixed> $data
* @throws Exceptions\QueryException
*/
public function set(array $data): static
{
$this->resetQuery();
$this->queryType = self::QUERY_UPDATE;
$this->params[self::PARAM_DATA] = $data + $this->params[self::PARAM_DATA];
return $this;
}
/**
* @throws Exceptions\QueryException
*/
public function delete(string|null $from = null, string|null $alias = null): static
{
$this->resetQuery();
$this->queryType = self::QUERY_DELETE;
if ($from !== null) {
$this->table($from, $alias);
}
return $this;
}
/**
* @param array<int|string, string|int|Db\Sql> $returning
* @throws Exceptions\QueryException
*/
public function returning(array $returning): static
{
$this->resetQuery();
$this->params[self::PARAM_RETURNING] = \array_merge($this->params[self::PARAM_RETURNING], $returning);
return $this;
}
/**
* @throws Exceptions\QueryException
*/
public function merge(string|null $into = null, string|null $alias = null): static
{
$this->resetQuery();
$this->queryType = self::QUERY_MERGE;
if ($into !== null) {
$this->table($into, $alias);
}
return $this;
}
/**
* @throws Exceptions\QueryException
*/
public function using(
string|Db\Sql $dataSource,
string|null $alias = null,
string|Db\Sql|null $onCondition = null,
): static
{
return $this->addTable(self::TABLE_TYPE_USING, $dataSource, $alias, $onCondition);
}
/**
* @throws Exceptions\QueryException
*/
public function whenMatched(string|Db\Sql $then, string|Db\Sql|null $condition = null): static
{
$this->resetQuery();
$this->params[self::PARAM_MERGE][] = [
self::MERGE_WHEN_MATCHED,
$then,
$condition === null ? null : Condition::createAnd()->add($condition),
];
return $this;
}
/**
* @throws Exceptions\QueryException
*/
public function whenNotMatched(string|Db\Sql $then, string|Db\Sql|null $condition = null): static
{
$this->resetQuery();
$this->params[self::PARAM_MERGE][] = [
self::MERGE_WHEN_NOT_MATCHED,
$then,
$condition === null ? null : Condition::createAnd()->add($condition),
];
return $this;
}
/**
* @throws Exceptions\QueryException
*/
public function truncate(string|null $table = null): static
{
$this->resetQuery();
$this->queryType = self::QUERY_TRUNCATE;
if ($table !== null) {
$this->table($table);
}
return $this;
}
/**
* @throws Exceptions\QueryException
*/
public function with(
string $as,
string|Db\Sql $query,
string|null $suffix = null,
bool $notMaterialized = false,
): static
{
$this->resetQuery();
$this->params[self::PARAM_WITH][self::WITH_QUERIES][$as] = $query;
if ($suffix !== null) {
$this->params[self::PARAM_WITH][self::WITH_QUERIES_SUFFIX][$as] = $suffix;
}
if ($notMaterialized) {
$this->params[self::PARAM_WITH][self::WITH_QUERIES_NOT_MATERIALIZED][$as] = true;
}
return $this;
}
/**
* @throws Exceptions\QueryException
*/
public function recursive(): static
{
$this->resetQuery();
$this->params[self::PARAM_WITH][self::WITH_RECURSIVE] = true;
return $this;
}
/**
* @throws Exceptions\QueryException
*/
public function prefix(string $queryPrefix, mixed ...$params): static
{
$this->resetQuery();
\array_unshift($params, $queryPrefix);
$this->params[self::PARAM_PREFIX][] = $params;
return $this;
}
/**
* @throws Exceptions\QueryException
*/
public function suffix(string $querySuffix, mixed ...$params): static
{
$this->resetQuery();
\array_unshift($params, $querySuffix);
$this->params[self::PARAM_SUFFIX][] = $params;
return $this;
}
public function has(string $param): bool
{
if (!\array_key_exists($param, self::DEFAULT_PARAMS)) {
throw Exceptions\QueryException::nonExistingQueryParam($param, \array_keys(self::DEFAULT_PARAMS));
}
return $this->params[$param] !== self::DEFAULT_PARAMS[$param];
}
protected function get(string $param): mixed
{
if (!\array_key_exists($param, self::DEFAULT_PARAMS)) {
throw Exceptions\QueryException::nonExistingQueryParam($param, \array_keys(self::DEFAULT_PARAMS));
}
return $this->params[$param];
}
/**
* @throws Exceptions\QueryException
*/
public function reset(string $param): static
{
if (!\array_key_exists($param, self::DEFAULT_PARAMS)) {
throw Exceptions\QueryException::nonExistingQueryParam($param, \array_keys(self::DEFAULT_PARAMS));
}
$this->resetQuery();
$this->params[$param] = self::DEFAULT_PARAMS[$param];
return $this;
}
protected function resetQuery(): void
{
$this->sqlDefinition = null;
$this->dbQuery = null;
}
/**
* @throws Exceptions\QueryException
*/
private function checkAlias(mixed $data, string|null $alias): void
{
if ((($data instanceof Db\Sql)) && ($alias === null)) {
throw Exceptions\QueryException::sqlMustHaveAlias();
}
}
/**
* @throws Exceptions\QueryBuilderException
*/
public function getSqlDefinition(): Db\SqlDefinition
{
if ($this->sqlDefinition === null) {
$this->sqlDefinition = $this->queryBuilder->createSqlDefinition($this->queryType, $this->params);
}
return $this->sqlDefinition;
}
public function toDbQuery(): Db\Query
{
if ($this->dbQuery === null) {
$this->dbQuery = Db\SqlDefinition::createQuery($this->getSqlDefinition());
}
return $this->dbQuery;
}
public function __clone()
{
$this->resetQuery();
foreach ($this->params[self::PARAM_ON_CONDITIONS] as $alias => $joinCondition) {
$this->params[self::PARAM_ON_CONDITIONS][$alias] = clone $joinCondition;
}
if ($this->params[self::PARAM_WHERE] !== null) {
$this->params[self::PARAM_WHERE] = clone $this->params[self::PARAM_WHERE];
}
if ($this->params[self::PARAM_HAVING] !== null) {
$this->params[self::PARAM_HAVING] = clone $this->params[self::PARAM_HAVING];
}
}
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

https://github.com/forrest79/phpgsql.git
暂无标签
BSD-3-Clause
使用 BSD-3-Clause 开源许可协议
取消

发行版

暂无发行版

贡献者

全部

语言

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PHP
1
https://gitee.com/statx/phpgsql.git
git@gitee.com:statx/phpgsql.git
statx
phpgsql
phpgsql
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /