<commit_before>package storm

import (
	"database/sql"
	"database/sql/driver"
	"errors"
	"fmt"
	"io/ioutil"
	"log"
	"strconv"
	"time"

	_ "github.com/mattn/go-sqlite3"
)

type testCustomType int64

func (tct *testCustomType) Scan(value interface{}) (err error) {
	switch v := value.(type) {
	case string:
		var in int
		in, err = strconv.Atoi(v)
		*tct = (testCustomType)(in)
		return
	case int64:
		*tct = (testCustomType)(v)
		return
	case int:
		*tct = (testCustomType)(v)
		return
	}
	return nil
}

func (tct *testCustomType) Value() (driver.Value, error) {
	return int64(*tct), nil
}

type testStructure struct {
	Id   int
	Name string

	//test invoke params
	onInsertInvoked      bool
	onPostInserteInvoked bool
	onUpdateInvoked      bool
	onPostUpdateInvoked  bool
	onDeleteInvoked      bool
	onPostDeleteInvoked  bool
	onInitInvoked        bool
}

//all posibile callbacks
func (t *testStructure) OnInsert()     { t.onInsertInvoked = true }
func (t *testStructure) OnPostInsert() { t.onPostInserteInvoked = true }
func (t *testStructure) OnUpdate()     { t.onUpdateInvoked = true }
func (t *testStructure) OnPostUpdate() { t.onPostUpdateInvoked = true }
func (t *testStructure) OnDelete()     { t.onDeleteInvoked = true }
func (t *testStructure) OnPostDelete() { t.onPostDeleteInvoked = true }
func (t *testStructure) OnInit()       { t.onInitInvoked = true }

type testAllTypeStructure struct {
	Id             int
	TestCustomType testCustomType `db:"type(int)"`
	Time           time.Time
	Byte           []byte
	String         string
	Int            int
	Int64          int64
	Float64        float64
	Bool           bool
	NullString     sql.NullString
	NullInt        sql.NullInt64
	NullFloat      sql.NullFloat64
	NullBool       sql.NullBool
}

func newTestStorm() *Storm {
	s, err := Open(`sqlite3`, `:memory:`)
	if err != nil {
		panic(err)
	}

	s.Log(log.New(ioutil.Discard, "", 0))
	s.RegisterStructure((*testStructure)(nil), `testStructure`)
	s.RegisterStructure((*testAllTypeStructure)(nil), `testAllTypeStructure`)
	s.db.Exec("DROP TABLE `testStructure`")
	s.db.Exec("CREATE TABLE `testStructure` (`id` INTEGER PRIMARY KEY, `name` TEXT)")
	s.db.Exec("DROP TABLE `testAllTypeStructure`")
	s.db.Exec("CREATE TABLE `testAllTypeStructure` " +
		"(`id` INTEGER PRIMARY KEY,`test_custom_type` INTEGER,`time` DATETIME,`byte` BLOB,`string` TEXT,`int` INTEGER,`int64` BIGINT," +
		"`float64` REAL,`bool` BOOL,`null_string` TEXT,`null_int` BIGINT,`null_float` REAL,`null_bool` BOOL)")
	s.db.SetMaxIdleConns(10)
	s.db.SetMaxOpenConns(10)

	return s
}

func newTestStormFile() *Storm {

	//create unique temporary datastore
	tmp, err := ioutil.TempFile("", "storm_test.sqlite_")
	if err != nil {
		panic(err)
	}
	tmp.Close()

	s, _ := Open(`sqlite3`, `file:`+tmp.Name()+`?mode=rwc`)
	s.Log(log.New(ioutil.Discard, "", 0))
	s.RegisterStructure((*testStructure)(nil), `testStructure`)
	s.RegisterStructure((*testAllTypeStructure)(nil), `testAllTypeStructure`)
	s.db.Exec("DROP TABLE `testStructure`")
	s.db.Exec("CREATE TABLE `testStructure` (`id` INTEGER PRIMARY KEY, `name` TEXT)")
	s.db.Exec("DROP TABLE `testAllTypeStructure`")
	s.db.Exec("CREATE TABLE `testAllTypeStructure` " +
		"(`id` INTEGER PRIMARY KEY,`test_custom_type` INTEGER,`time` DATETIME,`byte` BLOB,`string` TEXT,`int` INTEGER,`int64` BIGINT," +
		"`float64` REAL,`bool` BOOL,`null_string` TEXT,`null_int` BIGINT,`null_float` REAL,`null_bool` BOOL)")
	s.db.SetMaxIdleConns(10)
	s.db.SetMaxOpenConns(10)

	return s
}

func assertEntity(actual *testStructure, expected *testStructure) error {
	if actual == nil {
		return errors.New(`nil record returned`)
	}

	if actual == expected {
		return fmt.Errorf("data mismatch expected `%v` but got `%v`", expected, actual)
	}

	return nil
}

func assertTableExist(table string, db sqlCommon) (result int, err error) {

	//sqlite3 way
	res := db.QueryRow(`SELECT 1 FROM sqlite_master WHERE type='table' AND name='` + table + `'`)

	err = res.Scan(&result)
	if err == sql.ErrNoRows {
		err = nil
		result = 0
	}
	return
}
<commit_msg>small typo of use of a pointer type in valuer interface, never use a pointer type<commit_after>package storm

import (
	"database/sql"
	"database/sql/driver"
	"errors"
	"fmt"
	"io/ioutil"
	"log"
	"strconv"
	"time"

	_ "github.com/mattn/go-sqlite3"
)

type testCustomType int64

func (tct *testCustomType) Scan(value interface{}) (err error) {
	switch v := value.(type) {
	case []byte:
		var in int
		in, err = strconv.Atoi(string(v))
		*tct = (testCustomType)(in)
		return
	case string:
		var in int
		in, err = strconv.Atoi(v)
		*tct = (testCustomType)(in)
		return
	case int64:
		*tct = (testCustomType)(v)
		return
	case int:
		*tct = (testCustomType)(v)
		return
	}
	return errors.New("Cannot convert input to a custom type")
}

func (tct testCustomType) Value() (driver.Value, error) {
	return int64(tct), nil
}

type testStructure struct {
	Id   int
	Name string

	//test invoke params
	onInsertInvoked      bool
	onPostInserteInvoked bool
	onUpdateInvoked      bool
	onPostUpdateInvoked  bool
	onDeleteInvoked      bool
	onPostDeleteInvoked  bool
	onInitInvoked        bool
}

//all posibile callbacks
func (t *testStructure) OnInsert()     { t.onInsertInvoked = true }
func (t *testStructure) OnPostInsert() { t.onPostInserteInvoked = true }
func (t *testStructure) OnUpdate()     { t.onUpdateInvoked = true }
func (t *testStructure) OnPostUpdate() { t.onPostUpdateInvoked = true }
func (t *testStructure) OnDelete()     { t.onDeleteInvoked = true }
func (t *testStructure) OnPostDelete() { t.onPostDeleteInvoked = true }
func (t *testStructure) OnInit()       { t.onInitInvoked = true }

type testAllTypeStructure struct {
	Id             int
	TestCustomType testCustomType `db:"type(int)"`
	Time           time.Time
	Byte           []byte
	String         string
	Int            int
	Int64          int64
	Float64        float64
	Bool           bool
	NullString     sql.NullString
	NullInt        sql.NullInt64
	NullFloat      sql.NullFloat64
	NullBool       sql.NullBool
}


type testTable1 struct {
	Id int
	Name string
	Table2 *testTable2 `db:oneToMany(testTable2, id)`
	Table2Id int `db:oneToMany(testTable2, id)`
}

type testTable2 struct {
	Id int
	Name string
	
	//This will be automaticly populated
	Tables1 []*testTable1 `db:ManyToMany(testTableRelation)`
}

type testTableRelation struct {
	Id int
	Table1Id int `db:oneToMany(testTable1, id)`
	Table2Id int `db:oneToMany(testTable2, id)`
}

func newTestStorm() *Storm {
	s, err := Open(`sqlite3`, `:memory:`)
	if err != nil {
		panic(err)
	}

	s.Log(log.New(ioutil.Discard, "", 0))
	s.RegisterStructure((*testStructure)(nil), `testStructure`)
	s.RegisterStructure((*testAllTypeStructure)(nil), `testAllTypeStructure`)
	s.db.Exec("DROP TABLE `testStructure`")
	s.db.Exec("CREATE TABLE `testStructure` (`id` INTEGER PRIMARY KEY, `name` TEXT)")
	s.db.Exec("DROP TABLE `testAllTypeStructure`")
	s.db.Exec("CREATE TABLE `testAllTypeStructure` " +
		"(`id` INTEGER PRIMARY KEY,`test_custom_type` INTEGER,`time` DATETIME,`byte` BLOB,`string` TEXT,`int` INTEGER,`int64` BIGINT," +
		"`float64` REAL,`bool` BOOL,`null_string` TEXT,`null_int` BIGINT,`null_float` REAL,`null_bool` BOOL)")
	s.db.SetMaxIdleConns(10)
	s.db.SetMaxOpenConns(10)

	return s
}

func newTestStormFile() *Storm {

	//create unique temporary datastore
	tmp, err := ioutil.TempFile("", "storm_test.sqlite_")
	if err != nil {
		panic(err)
	}
	tmp.Close()

	s, _ := Open(`sqlite3`, `file:`+tmp.Name()+`?mode=rwc`)
	s.Log(log.New(ioutil.Discard, "", 0))
	s.RegisterStructure((*testStructure)(nil), `testStructure`)
	s.RegisterStructure((*testAllTypeStructure)(nil), `testAllTypeStructure`)
	s.db.Exec("DROP TABLE `testStructure`")
	s.db.Exec("CREATE TABLE `testStructure` (`id` INTEGER PRIMARY KEY, `name` TEXT)")
	s.db.Exec("DROP TABLE `testAllTypeStructure`")
	s.db.Exec("CREATE TABLE `testAllTypeStructure` " +
		"(`id` INTEGER PRIMARY KEY,`test_custom_type` INTEGER,`time` DATETIME,`byte` BLOB,`string` TEXT,`int` INTEGER,`int64` BIGINT," +
		"`float64` REAL,`bool` BOOL,`null_string` TEXT,`null_int` BIGINT,`null_float` REAL,`null_bool` BOOL)")
	s.db.SetMaxIdleConns(10)
	s.db.SetMaxOpenConns(10)

	return s
}

func assertEntity(actual *testStructure, expected *testStructure) error {
	if actual == nil {
		return errors.New(`nil record returned`)
	}

	if actual == expected {
		return fmt.Errorf("data mismatch expected `%v` but got `%v`", expected, actual)
	}

	return nil
}

func assertTableExist(table string, db sqlCommon) (result int, err error) {

	//sqlite3 way
	res := db.QueryRow(`SELECT 1 FROM sqlite_master WHERE type='table' AND name='` + table + `'`)

	err = res.Scan(&result)
	if err == sql.ErrNoRows {
		err = nil
		result = 0
	}
	return
}

{'fraction_non_alphanumeric': 0.13747710622710624, 'fraction_numerical': 0.011103479853479854, 'mean_word_length': 4.282345828295043, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5673894', 'n_tokens_mistral': 3357, 'n_tokens_neox': 3174, 'n_words': 745}