-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
8 changed files
with
196 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. | ||
// | ||
// This Source Code Form is subject to the terms of the MIT License. | ||
// If a copy of the MIT was not distributed with this file, | ||
// You can obtain one at https://github.com/gogf/gf. | ||
|
||
package sqlite | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/gogf/gf/v2/database/gdb" | ||
"github.com/gogf/gf/v2/errors/gerror" | ||
"github.com/gogf/gf/v2/text/gstr" | ||
"github.com/gogf/gf/v2/util/gconv" | ||
) | ||
|
||
// FormatUpsert returns SQL clause of type upsert for SQLite. | ||
// For example: ON CONFLICT (id) DO UPDATE SET ... | ||
func (d *Driver) FormatUpsert(columns []string, list gdb.List, option gdb.DoInsertOption) (string, error) { | ||
if len(option.OnConflict) == 0 { | ||
return "", gerror.New("Please specify conflict columns") | ||
} | ||
|
||
var onDuplicateStr string | ||
if option.OnDuplicateStr != "" { | ||
onDuplicateStr = option.OnDuplicateStr | ||
} else if len(option.OnDuplicateMap) > 0 { | ||
for k, v := range option.OnDuplicateMap { | ||
if len(onDuplicateStr) > 0 { | ||
onDuplicateStr += "," | ||
} | ||
switch v.(type) { | ||
case gdb.Raw, *gdb.Raw: | ||
onDuplicateStr += fmt.Sprintf( | ||
"%s=%s", | ||
d.Core.QuoteWord(k), | ||
v, | ||
) | ||
default: | ||
onDuplicateStr += fmt.Sprintf( | ||
"%s=EXCLUDED.%s", | ||
d.Core.QuoteWord(k), | ||
d.Core.QuoteWord(gconv.String(v)), | ||
) | ||
} | ||
} | ||
} else { | ||
for _, column := range columns { | ||
// If it's SAVE operation, do not automatically update the creating time. | ||
if d.Core.IsSoftCreatedFieldName(column) { | ||
continue | ||
} | ||
if len(onDuplicateStr) > 0 { | ||
onDuplicateStr += "," | ||
} | ||
onDuplicateStr += fmt.Sprintf( | ||
"%s=EXCLUDED.%s", | ||
d.Core.QuoteWord(column), | ||
d.Core.QuoteWord(column), | ||
) | ||
} | ||
} | ||
|
||
conflictKeys := gstr.Join(option.OnConflict, ",") | ||
|
||
return fmt.Sprintf("ON CONFLICT (%s) DO UPDATE SET ", conflictKeys) + onDuplicateStr, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters