Skip to content

Commit

Permalink
use atomic.Pointer[T] instead unsafe pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
peczenyj committed Jan 11, 2025
1 parent 8057e0d commit 828e549
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: '1.17.0' # The Go version to download (if necessary) and use.
go-version: '1.19.0' # The Go version to download (if necessary) and use.

# Install all the dependencies
- name: Install dependencies
Expand Down
8 changes: 4 additions & 4 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ func TestSQLCache(t *testing.T) {
defer putBuffer(buf)

buf.WriteString("test")
_, ok := defaultDialect.getCachedSQL(buf)
_, ok := defaultDialectPointer.Load().getCachedSQL(buf)
require.False(t, ok)

defaultDialect.putCachedSQL(buf, "test SQL")
sql, ok := defaultDialect.getCachedSQL(buf)
defaultDialectPointer.Load().putCachedSQL(buf, "test SQL")
sql, ok := defaultDialectPointer.Load().getCachedSQL(buf)
require.True(t, ok)
require.Equal(t, "test SQL", sql)

defaultDialect.ClearCache()
defaultDialectPointer.Load().ClearCache()
}
6 changes: 2 additions & 4 deletions dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"strconv"
"strings"
"sync"
"sync/atomic"
"unsafe"
)

// Dialect defines the method SQL statement is to be built.
Expand Down Expand Up @@ -40,7 +38,7 @@ var (
PostgreSQL *Dialect = &Dialect{}
)

var defaultDialect = NoDialect
var defaultDialectPointer = newAtomicPointer(NoDialect)

/*
SetDialect selects a Dialect to be used by default.
Expand All @@ -50,7 +48,7 @@ Dialect can be one of sqlf.NoDialect or sqlf.PostgreSQL
sqlf.SetDialect(sqlf.PostgreSQL)
*/
func SetDialect(newDefaultDialect *Dialect) {
atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&defaultDialect)), unsafe.Pointer(newDefaultDialect))
defaultDialectPointer.Store(newDefaultDialect)
}

/*
Expand Down
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
module github.com/leporo/sqlf

go 1.13
go 1.19

require (
github.com/mattn/go-sqlite3 v1.14.16
github.com/stretchr/testify v1.8.2
github.com/valyala/bytebufferpool v1.0.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
14 changes: 7 additions & 7 deletions stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Use New for special cases like this:
}
*/
func New(verb string, args ...interface{}) *Stmt {
return defaultDialect.New(verb, args...)
return defaultDialectPointer.Load().New(verb, args...)
}

/*
Expand All @@ -43,15 +43,15 @@ From starts a SELECT statement.
}
*/
func From(expr string, args ...interface{}) *Stmt {
return defaultDialect.From(expr, args...)
return defaultDialectPointer.Load().From(expr, args...)
}

/*
With starts a statement prepended by WITH clause
and closes a subquery passed as an argument.
*/
func With(queryName string, query *Stmt) *Stmt {
return defaultDialect.With(queryName, query)
return defaultDialectPointer.Load().With(queryName, query)
}

/*
Expand All @@ -70,7 +70,7 @@ Select starts a SELECT statement.
Note that From method can also be used to start a SELECT statement.
*/
func Select(expr string, args ...interface{}) *Stmt {
return defaultDialect.Select(expr, args...)
return defaultDialectPointer.Load().Select(expr, args...)
}

/*
Expand All @@ -85,7 +85,7 @@ Update starts an UPDATE statement.
}
*/
func Update(tableName string) *Stmt {
return defaultDialect.Update(tableName)
return defaultDialectPointer.Load().Update(tableName)
}

/*
Expand All @@ -101,7 +101,7 @@ InsertInto starts an INSERT statement.
}
*/
func InsertInto(tableName string) *Stmt {
return defaultDialect.InsertInto(tableName)
return defaultDialectPointer.Load().InsertInto(tableName)
}

/*
Expand All @@ -110,7 +110,7 @@ DeleteFrom starts a DELETE statement.
err := sqlf.DeleteFrom("table").Where("id = ?", id).ExecAndClose(ctx, db)
*/
func DeleteFrom(tableName string) *Stmt {
return defaultDialect.DeleteFrom(tableName)
return defaultDialectPointer.Load().DeleteFrom(tableName)
}

type stmtChunk struct {
Expand Down
9 changes: 9 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sqlf

import (
"sync/atomic"
"unsafe"
)

Expand All @@ -25,3 +26,11 @@ func insertAt(dest, src []interface{}, index int) []interface{} {
func bufToString(buf *[]byte) string {
return *(*string)(unsafe.Pointer(buf))
}

func newAtomicPointer[T any](initialValue *T) *atomic.Pointer[T] {
var pointer atomic.Pointer[T]

pointer.Store(initialValue)

return &pointer
}

0 comments on commit 828e549

Please sign in to comment.