|
12 | 12 | * [`GroupBy`](#group_by) |
13 | 13 | * [`Having`](#having) |
14 | 14 | * [`Window`](#window) |
| 15 | + * [`With`](#with) |
15 | 16 | * [`SetError`](#seterror) |
16 | 17 | * Executing Queries |
17 | 18 | * [`ScanStructs`](#scan-structs) - Scans rows into a slice of structs |
@@ -613,13 +614,132 @@ Output: |
613 | 614 | SELECT * FROM "test" GROUP BY "age" HAVING (SUM("income") > 1000) |
614 | 615 | ``` |
615 | 616 |
|
| 617 | +<a name="with"></a> |
| 618 | +**[`With`](https://godoc.org/github.com/doug-martin/goqu/#SelectDataset.With)** |
| 619 | + |
| 620 | +To use CTEs in `SELECT` statements you can use the `With` method. |
| 621 | + |
| 622 | +Simple Example |
| 623 | + |
| 624 | +```go |
| 625 | +sql, _, _ := goqu.From("one"). |
| 626 | + With("one", goqu.From().Select(goqu.L("1"))). |
| 627 | + Select(goqu.Star()). |
| 628 | + ToSQL() |
| 629 | +fmt.Println(sql) |
| 630 | +``` |
| 631 | + |
| 632 | +Output: |
| 633 | + |
| 634 | +``` |
| 635 | +WITH one AS (SELECT 1) SELECT * FROM "one" |
| 636 | +``` |
| 637 | + |
| 638 | +Dependent `WITH` clauses: |
| 639 | + |
| 640 | +```go |
| 641 | +sql, _, _ = goqu.From("derived"). |
| 642 | + With("intermed", goqu.From("test").Select(goqu.Star()).Where(goqu.C("x").Gte(5))). |
| 643 | + With("derived", goqu.From("intermed").Select(goqu.Star()).Where(goqu.C("x").Lt(10))). |
| 644 | + Select(goqu.Star()). |
| 645 | + ToSQL() |
| 646 | +fmt.Println(sql) |
| 647 | +``` |
| 648 | + |
| 649 | +Output: |
| 650 | +``` |
| 651 | +WITH intermed AS (SELECT * FROM "test" WHERE ("x" >= 5)), derived AS (SELECT * FROM "intermed" WHERE ("x" < 10)) SELECT * FROM "derived" |
| 652 | +``` |
| 653 | + |
| 654 | +`WITH` clause with arguments |
| 655 | + |
| 656 | +```go |
| 657 | +sql, _, _ = goqu.From("multi"). |
| 658 | + With("multi(x,y)", goqu.From().Select(goqu.L("1"), goqu.L("2"))). |
| 659 | + Select(goqu.C("x"), goqu.C("y")). |
| 660 | + ToSQL() |
| 661 | +fmt.Println(sql) |
| 662 | +``` |
| 663 | + |
| 664 | +Output: |
| 665 | +``` |
| 666 | +WITH multi(x,y) AS (SELECT 1, 2) SELECT "x", "y" FROM "multi" |
| 667 | +``` |
| 668 | + |
| 669 | +Using a `InsertDataset`. |
| 670 | + |
| 671 | +```go |
| 672 | +insertDs := goqu.Insert("foo").Rows(goqu.Record{"user_id": 10}).Returning("id") |
| 673 | + |
| 674 | +ds := goqu.From("bar"). |
| 675 | + With("ins", insertDs). |
| 676 | + Select("bar_name"). |
| 677 | + Where(goqu.Ex{"bar.user_id": goqu.I("ins.user_id")}) |
| 678 | + |
| 679 | +sql, _, _ := ds.ToSQL() |
| 680 | +fmt.Println(sql) |
| 681 | + |
| 682 | +sql, args, _ := ds.Prepared(true).ToSQL() |
| 683 | +fmt.Println(sql, args) |
| 684 | +``` |
| 685 | +Output: |
| 686 | +``` |
| 687 | +WITH ins AS (INSERT INTO "foo" ("user_id") VALUES (10) RETURNING "id") SELECT "bar_name" FROM "bar" WHERE ("bar"."user_id" = "ins"."user_id") |
| 688 | +WITH ins AS (INSERT INTO "foo" ("user_id") VALUES (?) RETURNING "id") SELECT "bar_name" FROM "bar" WHERE ("bar"."user_id" = "ins"."user_id") [10] |
| 689 | +``` |
| 690 | + |
| 691 | +Using an `UpdateDataset` |
| 692 | + |
| 693 | +```go |
| 694 | +updateDs := goqu.Update("foo").Set(goqu.Record{"bar": "baz"}).Returning("id") |
| 695 | + |
| 696 | +ds := goqu.From("bar"). |
| 697 | + With("upd", updateDs). |
| 698 | + Select("bar_name"). |
| 699 | + Where(goqu.Ex{"bar.user_id": goqu.I("ins.user_id")}) |
| 700 | + |
| 701 | +sql, _, _ := ds.ToSQL() |
| 702 | +fmt.Println(sql) |
| 703 | + |
| 704 | +sql, args, _ := ds.Prepared(true).ToSQL() |
| 705 | +fmt.Println(sql, args) |
| 706 | +``` |
| 707 | + |
| 708 | +Output: |
| 709 | +``` |
| 710 | +WITH upd AS (UPDATE "foo" SET "bar"='baz' RETURNING "id") SELECT "bar_name" FROM "bar" WHERE ("bar"."user_id" = "upd"."user_id") |
| 711 | +WITH upd AS (UPDATE "foo" SET "bar"=? RETURNING "id") SELECT "bar_name" FROM "bar" WHERE ("bar"."user_id" = "upd"."user_id") [baz] |
| 712 | +``` |
| 713 | + |
| 714 | +Using a `DeleteDataset` |
| 715 | + |
| 716 | +```go |
| 717 | +deleteDs := goqu.Delete("foo").Where(goqu.Ex{"bar": "baz"}).Returning("id") |
| 718 | + |
| 719 | +ds := goqu.From("bar"). |
| 720 | + With("del", deleteDs). |
| 721 | + Select("bar_name"). |
| 722 | + Where(goqu.Ex{"bar.user_id": goqu.I("del.user_id")}) |
| 723 | + |
| 724 | +sql, _, _ := ds.ToSQL() |
| 725 | +fmt.Println(sql) |
| 726 | + |
| 727 | +sql, args, _ := ds.Prepared(true).ToSQL() |
| 728 | +fmt.Println(sql, args) |
| 729 | +``` |
| 730 | + |
| 731 | +Output: |
| 732 | +``` |
| 733 | +WITH del AS (DELETE FROM "foo" WHERE ("bar" = 'baz') RETURNING "id") SELECT "bar_name" FROM "bar" WHERE ("bar"."user_id" = "del"."user_id") |
| 734 | +WITH del AS (DELETE FROM "foo" WHERE ("bar" = ?) RETURNING "id") SELECT "bar_name" FROM "bar" WHERE ("bar"."user_id" = "del"."user_id") [baz] |
| 735 | +``` |
616 | 736 |
|
617 | 737 | <a name="window"></a> |
618 | 738 | **[`Window Function`](https://godoc.org/github.com/doug-martin/goqu/#SelectDataset.Window)** |
619 | 739 |
|
620 | 740 | **NOTE** currently only the `postgres`, `mysql8` (NOT `mysql`) and the default dialect support `Window Function` |
621 | 741 |
|
622 | | -To use windowing in select you can use the `Over` method on an `SQLFunction` |
| 742 | +To use windowing in `SELECT` statements you can use the `Over` method on an `SQLFunction` |
623 | 743 |
|
624 | 744 | ```go |
625 | 745 | sql, _, _ := goqu.From("test").Select( |
@@ -916,3 +1036,4 @@ fmt.Printf("\nIds := %+v", ids) |
916 | 1036 | ``` |
917 | 1037 |
|
918 | 1038 |
|
| 1039 | + |
0 commit comments