-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
134 additions
and
17 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package postgresql | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/GoWebProd/uuid7" | ||
"github.com/jackc/pgx/v5/pgtype" | ||
) | ||
|
||
type UUID uuid7.UUID | ||
|
||
func (u *UUID) ScanUUID(v pgtype.UUID) error { | ||
if !v.Valid { | ||
return fmt.Errorf("cannot scan NULL into *uuid.UUID") | ||
} | ||
|
||
*u = v.Bytes | ||
return nil | ||
} | ||
|
||
func (u UUID) UUIDValue() (pgtype.UUID, error) { | ||
return pgtype.UUID{Bytes: [16]byte(u), Valid: true}, nil | ||
} | ||
|
||
func TryWrapUUIDEncodePlan(value interface{}) (plan pgtype.WrappedEncodePlanNextSetter, nextValue interface{}, ok bool) { | ||
switch value := value.(type) { | ||
case uuid7.UUID: | ||
return &wrapUUIDEncodePlan{}, UUID(value), true | ||
} | ||
|
||
return nil, nil, false | ||
} | ||
|
||
type wrapUUIDEncodePlan struct { | ||
next pgtype.EncodePlan | ||
} | ||
|
||
func (plan *wrapUUIDEncodePlan) SetNext(next pgtype.EncodePlan) { plan.next = next } | ||
|
||
func (plan *wrapUUIDEncodePlan) Encode(value interface{}, buf []byte) (newBuf []byte, err error) { | ||
return plan.next.Encode(UUID(value.(uuid7.UUID)), buf) | ||
} | ||
|
||
func TryWrapUUIDScanPlan(target interface{}) (plan pgtype.WrappedScanPlanNextSetter, nextDst interface{}, ok bool) { | ||
switch target := target.(type) { | ||
case *uuid7.UUID: | ||
return &wrapUUIDScanPlan{}, (*UUID)(target), true | ||
} | ||
|
||
return nil, nil, false | ||
} | ||
|
||
type wrapUUIDScanPlan struct { | ||
next pgtype.ScanPlan | ||
} | ||
|
||
func (plan *wrapUUIDScanPlan) SetNext(next pgtype.ScanPlan) { plan.next = next } | ||
|
||
func (plan *wrapUUIDScanPlan) Scan(src []byte, dst interface{}) error { | ||
return plan.next.Scan(src, (*UUID)(dst.(*uuid7.UUID))) | ||
} | ||
|
||
type UUIDCodec struct { | ||
pgtype.UUIDCodec | ||
} | ||
|
||
func (UUIDCodec) DecodeValue(tm *pgtype.Map, oid uint32, format int16, src []byte) (interface{}, error) { | ||
if src == nil { | ||
return nil, nil | ||
} | ||
|
||
var target uuid7.UUID | ||
scanPlan := tm.PlanScan(oid, format, &target) | ||
if scanPlan == nil { | ||
return nil, fmt.Errorf("PlanScan did not find a plan") | ||
} | ||
|
||
err := scanPlan.Scan(src, &target) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return target, nil | ||
} | ||
|
||
// RegisterUuid7 registers the github.com/GoWebProd/uuid7 integration with a pgtype.Map. | ||
func RegisterUuid7(tm *pgtype.Map) { | ||
tm.TryWrapEncodePlanFuncs = append([]pgtype.TryWrapEncodePlanFunc{TryWrapUUIDEncodePlan}, tm.TryWrapEncodePlanFuncs...) | ||
tm.TryWrapScanPlanFuncs = append([]pgtype.TryWrapScanPlanFunc{TryWrapUUIDScanPlan}, tm.TryWrapScanPlanFuncs...) | ||
|
||
tm.RegisterType(&pgtype.Type{ | ||
Name: "uuid", | ||
OID: pgtype.UUIDOID, | ||
Codec: UUIDCodec{}, | ||
}) | ||
} |