Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom collation functions #97

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

special
Copy link

@special special commented Aug 8, 2020

// CreateCollation registers a Go function as a SQLite collation function.
//
// These function are used with the COLLATE operator to implement custom sorting in queries.
//
// The xCompare function must return an integer that is negative, zero, or positive if the first
// string is less than, equal to, or greater than the second, respectively. The function must
// always return the same result for the same inputs and must be commutative.
//
// These are the same properties as strings.Compare().
//
// https://sqlite.org/datatype3.html#collation
// https://sqlite.org/c3ref/create_collation.html
func (conn *Conn) CreateCollation(name string, xCompare func(string, string) int) error { 

CreateCollation registers a Go function as a sqlite collation, similar
to CreateFunction. These can be used in queries for custom sorting.
Copy link
Collaborator

@AdamSLevy AdamSLevy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. minor changes please:

  • use RWMutex
  • Remove conn from xcollation


x := &xcollation{
name: name,
conn: conn,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think a collation ever needs to know its own conn actually.

}

var xcollations = struct {
mu sync.Mutex
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make this a sync.RWMutex

Comment on lines +98 to +100
xcollations.mu.Lock()
x := xcollations.m[int(ptr)]
xcollations.mu.Unlock()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
xcollations.mu.Lock()
x := xcollations.m[int(ptr)]
xcollations.mu.Unlock()
xcollations.mu.RLock()
x := xcollations.m[int(ptr)]
xcollations.mu.RUnlock()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants