Skip to content

Commit

Permalink
if postgres do returning the pk
Browse files Browse the repository at this point in the history
  • Loading branch information
amirrezaask committed May 26, 2022
1 parent dcda588 commit 0155e14
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
1 change: 1 addition & 0 deletions .vscode/dryrun.log
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
make --dry-run --always-make --keep-going --print-directory
make: Entering directory `/Users/amirreza/src/github.com/golobby/orm'
go test -coverprofile=cover.out ./...
go tool cover -html=cover.out
Expand Down
13 changes: 6 additions & 7 deletions .vscode/targets.log
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ make all --print-data-base --no-builtin-variables --no-builtin-rules --question

# This program built for i386-apple-darwin11.3.0

make: *** No rule to make target `all'. Stop.


# Make data base, printed on Wed May 25 21:13:51 2022
# Make data base, printed on Fri May 27 01:52:16 2022

# Variables

Expand Down Expand Up @@ -36,9 +38,6 @@ FZF_DEFAULT_COMMAND = rg --files
VSCODE_NLS_CONFIG = {"locale":"en-us","availableLanguages":{},"_languagePackSupport":true}
# environment
_ = /usr/bin/make

make: *** No rule to make target `all'. Stop.

# makefile (from `Makefile', line 1)
MAKEFILE_LIST := Makefile
# environment
Expand All @@ -50,15 +49,15 @@ VSCODE_VERBOSE_LOGGING = true
# environment
__CFBundleIdentifier = com.microsoft.VSCode
# environment
VSCODE_IPC_HOOK_EXTHOST = /var/folders/hb/y34qrz9s5bb0_3csl5dn6m600000gn/T/vscode-ipc-80293e5e-d035-4120-8f38-a52192a892f2.sock
VSCODE_IPC_HOOK_EXTHOST = /var/folders/hb/y34qrz9s5bb0_3csl5dn6m600000gn/T/vscode-ipc-65c5fbec-d0c4-4968-aa3f-0cf650f6fb31.sock
# environment
VSCODE_CWD = /
# environment
GOPROXY = https://proxy.golang.org,direct
# environment
PATH = /Users/amirreza/.emacs.d/bin/:/Applications/Emacs.app/Contents/MacOS:/Users/amirreza/src/github.com/amirrezaask/dotfiles/bin:/Users/amirreza/bin:/opt/homebrew/bin:/bin:/Users/amirreza/.luarocks/bin:/Users/amirreza/.config/composer/vendor/bin:/bin:/Users/amirreza/.cargo/bin:/Users/amirreza/.local/bin:/usr/local/Postman:/Users/amirreza/.local/elixir-ls/:/Users/amirreza/.cache/rebar3/bin:/home/amirreza/.fzf/bin:/opt/homebrew/opt/fzf/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public:/usr/local/share/dotnet:~/.dotnet/tools:/usr/local/go/bin:/Users/amirreza/.composer/vendor/bin
# environment
LOG_DST = /Users/amirreza/Library/Application Support/Code/logs/20220524T175645/exthost9/dendron.dendron/dendron.log
LOG_DST = /Users/amirreza/Library/Application Support/Code/logs/20220524T175645/exthost14/dendron.dendron/dendron.log
# environment
GOPATH = /Users/amirreza
# environment
Expand Down Expand Up @@ -248,6 +247,6 @@ bench:
# strcache size: total = 4096 / max = 4096 / min = 4096 / avg = 4096
# strcache free: total = 4087 / max = 4087 / min = 4087 / avg = 4087

# Finished Make data base on Wed May 25 21:13:51 2022
# Finished Make data base on Fri May 27 01:52:16 2022


9 changes: 7 additions & 2 deletions orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,17 @@ func Insert(objs ...Entity) error {
values = append(values, genericValuesOf(obj, false))
}

q, args := insertStmt{
is := insertStmt{
PlaceHolderGenerator: s.getDialect().PlaceHolderGenerator,
Table: s.getTable(),
Columns: cols,
Values: values,
}.ToSql()
}

if s.getDialect().DriverName == "postgres" {
is.Returning = s.pkName()
}
q, args := is.ToSql()

res, err := s.getConnection().exec(q, args...)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,7 @@ type insertStmt struct {
Table string
Columns []string
Values [][]interface{}
Returning string
}

func (i insertStmt) flatValues() []interface{} {
Expand Down Expand Up @@ -777,6 +778,9 @@ func (i insertStmt) ToSql() (string, []interface{}) {
strings.Join(i.Columns, ","),
i.getValuesStr(),
)
if i.Returning != "" {
base += "RETURNING " + i.Returning
}
return base, i.flatValues()
}

Expand Down

0 comments on commit 0155e14

Please sign in to comment.