Skip to content

Commit

Permalink
Update xsql options
Browse files Browse the repository at this point in the history
  • Loading branch information
onanying committed Oct 12, 2023
1 parent 61a9813 commit 80f19fd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
6 changes: 3 additions & 3 deletions src/xsql/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ func New(db *sql.DB, opts ...SqlOption) *DB {
}

func (t *DB) mergeOptions(opts []SqlOption) *sqlOptions {
opt := *t.Options // copy
cp := *t.Options // copy
for _, o := range opts {
o.apply(&opt)
o.apply(&cp)
}
return &opt
return &cp
}

func (t *DB) Insert(data interface{}, opts ...SqlOption) (sql.Result, error) {
Expand Down
46 changes: 23 additions & 23 deletions src/xsql/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ type sqlOptions struct {
}

func mergeOptions(opts []SqlOption) *sqlOptions {
opt := DefaultOptions // copy
cp := DefaultOptions // copy
for _, o := range opts {
o.apply(&opt)
o.apply(&cp)
}
return &opt
return &cp
}

type SqlOption interface {
Expand All @@ -76,58 +76,58 @@ func (fdo *funcSqlOption) apply(do *sqlOptions) {
}

func WithTag(tag string) SqlOption {
return &funcSqlOption{func(opt *sqlOptions) {
opt.Tag = tag
return &funcSqlOption{func(opts *sqlOptions) {
opts.Tag = tag
}}
}

func WithInsertKey(insertKey string) SqlOption {
return &funcSqlOption{func(opt *sqlOptions) {
opt.InsertKey = insertKey
return &funcSqlOption{func(opts *sqlOptions) {
opts.InsertKey = insertKey
}}
}

func WithPlaceholder(placeholder string) SqlOption {
return &funcSqlOption{func(opt *sqlOptions) {
opt.Placeholder = placeholder
return &funcSqlOption{func(opts *sqlOptions) {
opts.Placeholder = placeholder
}}
}

func WithColumnQuotes(columnQuotes string) SqlOption {
return &funcSqlOption{func(opt *sqlOptions) {
opt.ColumnQuotes = columnQuotes
return &funcSqlOption{func(opts *sqlOptions) {
opts.ColumnQuotes = columnQuotes
}}
}

func WithTimeLayout(timeLayout string) SqlOption {
return &funcSqlOption{func(opt *sqlOptions) {
opt.TimeLayout = timeLayout
return &funcSqlOption{func(opts *sqlOptions) {
opts.TimeLayout = timeLayout
}}
}

func WithTimeLocation(timeLocation *time.Location) SqlOption {
return &funcSqlOption{func(opt *sqlOptions) {
opt.TimeLocation = timeLocation
return &funcSqlOption{func(opts *sqlOptions) {
opts.TimeLocation = timeLocation
}}
}

func WithTimeFunc(f TimeFunc) SqlOption {
return &funcSqlOption{func(opt *sqlOptions) {
opt.TimeFunc = f
return &funcSqlOption{func(opts *sqlOptions) {
opts.TimeFunc = f
}}
}

func WithDebugFunc(f DebugFunc) SqlOption {
return &funcSqlOption{func(opt *sqlOptions) {
opt.DebugFunc = f
return &funcSqlOption{func(opts *sqlOptions) {
opts.DebugFunc = f
}}
}

func UseOracle() SqlOption {
return &funcSqlOption{func(opt *sqlOptions) {
opt.Placeholder = `:%d`
opt.ColumnQuotes = `"`
opt.TimeFunc = func(placeholder string) string {
return &funcSqlOption{func(opts *sqlOptions) {
opts.Placeholder = `:%d`
opts.ColumnQuotes = `"`
opts.TimeFunc = func(placeholder string) string {
return fmt.Sprintf("TO_TIMESTAMP(%s, 'SYYYY-MM-DD HH24:MI:SS:FF6')", placeholder)
}
}}
Expand Down

0 comments on commit 80f19fd

Please sign in to comment.