@@ -638,7 +638,7 @@ func (r *APIRepo) insertSecurityConfig(tx *sql.Tx, apiId string, security *model
638638 INSERT INTO xhub_signature_security (api_uuid, enabled, secret, algorithm, header)
639639 VALUES (?, ?, ?, ?, ?)
640640 `
641- _ , err := tx .Exec (xHubQuery , apiId , security .XHubSignature .Enabled ,
641+ _ , err := tx .Exec (r . db . Rebind ( xHubQuery ) , apiId , security .XHubSignature .Enabled ,
642642 security .XHubSignature .Secret , security .XHubSignature .Algorithm , security .XHubSignature .Header )
643643 if err != nil {
644644 return err
@@ -670,7 +670,7 @@ func (r *APIRepo) loadSecurityConfig(apiId string) (*model.SecurityConfig, error
670670 SELECT enabled, secret, algorithm, header
671671 FROM xhub_signature_security WHERE api_uuid = ?
672672 `
673- err = r .db .QueryRow (xHubQuery , apiId ).Scan (& xHub .Enabled ,
673+ err = r .db .QueryRow (r . db . Rebind ( xHubQuery ) , apiId ).Scan (& xHub .Enabled ,
674674 & xHub .Secret , & xHub .Algorithm , & xHub .Header )
675675 if err == nil {
676676 security .XHubSignature = xHub
@@ -882,19 +882,32 @@ func (r *APIRepo) insertChannel(tx *sql.Tx, apiId string, channel *model.Channel
882882 }
883883 }
884884 // Insert channel
885- channelQuery := `
885+ var channelID int64
886+ if r .db .Driver () == "postgres" || r .db .Driver () == "postgresql" {
887+ // PostgreSQL: use RETURNING to get the generated ID
888+ channelQuery := `
889+ INSERT INTO api_operations (api_uuid, name, description, method, path, authentication_required, scopes)
890+ VALUES (?, ?, ?, ?, ?, ?, ?)
891+ RETURNING id`
892+ if err := tx .QueryRow (r .db .Rebind (channelQuery ), apiId , channel .Name , channel .Description ,
893+ channel .Request .Method , channel .Request .Name , authRequired , scopesJSON ).Scan (& channelID ); err != nil {
894+ return err
895+ }
896+ } else {
897+ // SQLite (and other drivers that support LastInsertId)
898+ channelQuery := `
886899 INSERT INTO api_operations (api_uuid, name, description, method, path, authentication_required, scopes)
887900 VALUES (?, ?, ?, ?, ?, ?, ?)`
888- result , err := tx .Exec (channelQuery , apiId , channel .Name , channel .Description ,
889- channel .Request .Method , channel .Request .Name , authRequired , scopesJSON )
890-
891- if err != nil {
892- return err
893- }
901+ result , err := tx .Exec (r .db .Rebind (channelQuery ), apiId , channel .Name , channel .Description ,
902+ channel .Request .Method , channel .Request .Name , authRequired , scopesJSON )
903+ if err != nil {
904+ return err
905+ }
894906
895- channelID , err := result .LastInsertId ()
896- if err != nil {
897- return err
907+ channelID , err = result .LastInsertId ()
908+ if err != nil {
909+ return err
910+ }
898911 }
899912
900913 // Insert policies
@@ -927,7 +940,7 @@ func (r *APIRepo) loadChannels(apiId string) ([]model.Channel, error) {
927940 SELECT id, name, description, method, path, authentication_required, scopes
928941 FROM api_operations WHERE api_uuid = ?
929942 `
930- rows , err := r .db .Query (query , apiId )
943+ rows , err := r .db .Query (r . db . Rebind ( query ) , apiId )
931944 if err != nil {
932945 return nil , err
933946 }
0 commit comments