Skip to content

Commit 894ea83

Browse files
Merge pull request #3 from oracle-samples/callbacks-godoc
godoc added to callbacks
2 parents 77f7ce3 + 7a5c188 commit 894ea83

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

oracle/create.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,22 @@ import (
5050
"gorm.io/gorm/schema"
5151
)
5252

53+
// Create overrides GORM's create callback for Oracle.
54+
//
55+
// Behavior:
56+
// - If the schema has fields with default DB values and only one row is
57+
// being inserted, it builds an INSERT ... RETURNING statement.
58+
// - If no RETURNING is needed, it emits a standard INSERT.
59+
// - If multiple rows require RETURNING, it builds a PL/SQL block using
60+
// FORALL and BULK COLLECT; if an ON CONFLICT clause is present and
61+
// resolvable, it emits a MERGE.
62+
// - For that last case, it validates Dest (non-nil, non-empty slice with
63+
// no nil elements), normalizes bind variables for Oracle, and populates
64+
// destinations from OUT parameters.
65+
//
66+
// Register with:
67+
//
68+
// db.Callback().Create().Replace("gorm:create", oracle.Create)
5369
func Create(db *gorm.DB) {
5470
if db.Error != nil || db.Statement == nil {
5571
return

oracle/delete.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,31 @@ import (
4949
"gorm.io/gorm/schema"
5050
)
5151

52-
// Delete callback function for Oracle
52+
// Delete overrides GORM's delete callback for Oracle.
53+
//
54+
// Delete builds a safe, Oracle-compatible DELETE that supports soft deletes,
55+
// hard deletes, and optional RETURNING of deleted rows.
56+
//
57+
// Behavior:
58+
// - DELETE safety: checks for missing WHERE conditions and refuses to run
59+
// unless AllowGlobalUpdate is set or the WHERE clause has meaningful
60+
// conditions.
61+
// - Soft delete: if the model has a soft-delete field and Unscoped is false,
62+
// it lets GORM emit the UPDATE that marks rows as deleted. If a RETURNING
63+
// clause is present with soft delete, it executes via QueryContext so the
64+
// returned columns can be scanned.
65+
// - Hard delete + RETURNING: it emits a PL/SQL block that performs DELETE …
66+
// RETURNING BULK COLLECT INTO, wiring per-row sql.Out destinations so the
67+
// deleted rows (or selected columns) can be populated back into the
68+
// destination slice.
69+
// - Hard delete (no RETURNING): it emits a standard DELETE and executes it
70+
// via ExecContext.
71+
// - Expressions: it expands WHERE expressions, including IN with slices, and
72+
// normalizes bind variables for Oracle.
73+
//
74+
// Register with:
75+
//
76+
// db.Callback().Delete().Replace("gorm:delete", oracle.Delete)
5377
func Delete(db *gorm.DB) {
5478
if db.Error != nil || db.Statement == nil {
5579
return

oracle/update.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,29 @@ import (
5050
"gorm.io/gorm/schema"
5151
)
5252

53-
// Update callback function for Oracle
53+
// Update overrides GORM's update callback for Oracle.
54+
//
55+
// It builds Oracle-compatible UPDATE statements and supports:
56+
//
57+
// - Standard updates without RETURNING using GORM’s default SQL build, with
58+
// bind variable conversion for Oracle types.
59+
// - Updates with RETURNING, emitting a PL/SQL block that performs
60+
// UPDATE … RETURNING BULK COLLECT INTO for multi-row updates,
61+
// wiring sql.Out binds for each returned column and row.
62+
// - UPDATE safety: checks for missing WHERE conditions and refuses to run
63+
// unless AllowGlobalUpdate is set or the WHERE clause has meaningful
64+
// conditions (beyond soft-delete filters).
65+
// - Primary key WHERE injection when the destination object or slice has
66+
// identifiable PK values, to avoid unintended mass updates.
67+
// - Soft-delete compatibility: conditions on deleted_at are ignored for the
68+
// safety check, but preserved in the WHERE for the actual SQL.
69+
//
70+
// For updates with RETURNING, OUT bind results are mapped back into the
71+
// destination struct or slice using getUpdateReturningValues.
72+
//
73+
// Register with:
74+
//
75+
// db.Callback().Update().Replace("gorm:update", oracle.Update)
5476
func Update(db *gorm.DB) {
5577
if db.Error != nil || db.Statement == nil {
5678
return

0 commit comments

Comments
 (0)