本文整理汇总了Golang中database/sql.Register函数的典型用法代码示例。如果您正苦于以下问题:Golang Register函数的具体用法?Golang Register怎么用?Golang Register使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Register函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: OpenFromConnPool
// OpenFromConnPool takes the existing *pgx.ConnPool pool and returns a *sql.DB
// with pool as the backend. This enables full control over the connection
// process and configuration while maintaining compatibility with the
// database/sql interface. In addition, by calling Driver() on the returned
// *sql.DB and typecasting to *stdlib.Driver a reference to the pgx.ConnPool can
// be reaquired later. This allows fast paths targeting pgx to be used while
// still maintaining compatibility with other databases and drivers.
//
// pool connection size must be at least 2.
func OpenFromConnPool(pool *pgx.ConnPool) (*sql.DB, error) {
d := &Driver{Pool: pool}
name := fmt.Sprintf("pgx-%d", openFromConnPoolCount)
openFromConnPoolCount++
sql.Register(name, d)
db, err := sql.Open(name, "")
if err != nil {
return nil, err
}
// Presumably OpenFromConnPool is being used because the user wants to use
// database/sql most of the time, but fast path with pgx some of the time.
// Allow database/sql to use all the connections, but release 2 idle ones.
// Don't have database/sql immediately release all idle connections because
// that would mean that prepared statements would be lost (which kills
// performance if the prepared statements constantly have to be reprepared)
stat := pool.Stat()
if stat.MaxConnections <= 2 {
return nil, errors.New("pool connection size must be at least 2")
}
db.SetMaxIdleConns(stat.MaxConnections - 2)
db.SetMaxOpenConns(stat.MaxConnections)
return db, nil
}
开发者ID:jonasi,项目名称:pgx,代码行数:35,代码来源:sql.go
示例2: init
func init() {
mu.Lock()
defer mu.Unlock()
if !driverRegistered("mysql") {
sql.Register("mysql", &mysql.MySQLDriver{})
}
if !driverRegistered("mariadb") {
sql.Register("mariadb", &mysql.MySQLDriver{})
}
}
开发者ID:wawandco,项目名称:transporter,代码行数:12,代码来源:migrator.go
示例3: init
// init computes the supported versions into a byte slice and register the driver.
func init() {
for i, v := range versionsSupported {
binary.BigEndian.PutUint32(handshakeRequest[i*4:i*4+4], v)
}
sql.Register("neo4j-bolt", &neoDriver{})
}
开发者ID:yolii,项目名称:neoql,代码行数:8,代码来源:driver.go
示例4: initDatabase
func initDatabase() models.Control {
var dbDriver string
sql.Register(dbDriver, &sqlite3.SQLiteDriver{})
database, err := sql.Open(dbDriver, "db")
if err != nil {
fmt.Println("Failed to create the handle")
}
if err := database.Ping(); err != nil {
fmt.Println("Failed to keep connection alive")
}
if err != nil {
log.Fatal(err)
}
_, err = database.Exec(
"CREATE TABLE IF NOT EXISTS Config (id integer PRIMARY KEY, notifications_limit integer NOT NULL, timeout integer NOT NULL)",
)
if err != nil {
log.Fatal(err)
}
config := models.Config{NotificationsLimit: 3, Timeout: 10}
_ = database.QueryRow(
"SELECT notifications_limit, timeout FROM Config ORDER BY id DESC LIMIT 1",
).Scan(&config.NotificationsLimit, &config.Timeout)
controller := models.Control{Config: config, Database: database}
return controller
}
开发者ID:dorumd,项目名称:go-osx-email-notifications,代码行数:34,代码来源:client.go
示例5: main
func main() {
// Load and validate config file.
config := LoadConfig("config.json")
// Register SQLite driver.
sqlite3conn := []*sqlite3.SQLiteConn{}
sql.Register(config.DbName,
&sqlite3.SQLiteDriver{
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
sqlite3conn = append(sqlite3conn, conn)
return nil
},
})
// Open SQLite database and assign it to the contxt.
db, err := sql.Open(config.DbName, config.DbPath)
if err != nil {
log.Fatal(err.Error())
}
// Create the application context.
ctx := server.NewAppContext(db, config.AuthKey)
defer db.Close()
// Create mux and register the handlers.
mux := http.NewServeMux()
mux.Handle("/gcm/devices/", server.NewGCMHandler(ctx))
mux.Handle("/apn/devices/", server.NewAPNHandler(ctx))
// Attemp to start the server.
address := fmt.Sprintf("%s:%d", config.Address, config.Port)
log.Fatal(http.ListenAndServe(address, mux))
}
开发者ID:contactlab,项目名称:CLABPush-Go,代码行数:34,代码来源:main.go
示例6: main
func main() {
sql.Register("sqlite3_with_extensions",
&sqlite3.SQLiteDriver{
Extensions: []string{
"sqlite3_mod_regexp",
},
})
db, err := sql.Open("sqlite3_with_extensions", ":memory:")
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Force db to make a new connection in pool
// by putting the original in a transaction
tx, err := db.Begin()
if err != nil {
log.Fatal(err)
}
defer tx.Commit()
// New connection works (hopefully!)
rows, err := db.Query("select 'hello world' where 'hello world' regexp '^hello.*d$'")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
var helloworld string
rows.Scan(&helloworld)
fmt.Println(helloworld)
}
}
开发者ID:FihlaTV,项目名称:bridge-server,代码行数:34,代码来源:extension.go
示例7: BenchmarkCustomFunctions
func BenchmarkCustomFunctions(b *testing.B) {
customFunctionOnce.Do(func() {
custom_add := func(a, b int64) int64 {
return a + b
}
sql.Register("sqlite3_BenchmarkCustomFunctions", &SQLiteDriver{
ConnectHook: func(conn *SQLiteConn) error {
// Impure function to force sqlite to reexecute it each time.
if err := conn.RegisterFunc("custom_add", custom_add, false); err != nil {
return err
}
return nil
},
})
})
db, err := sql.Open("sqlite3_BenchmarkCustomFunctions", ":memory:")
if err != nil {
b.Fatal("Failed to open database:", err)
}
defer db.Close()
b.ResetTimer()
for i := 0; i < b.N; i++ {
var i int64
err = db.QueryRow("SELECT custom_add(1,2)").Scan(&i)
if err != nil {
b.Fatal("Failed to run custom add:", err)
}
}
}
开发者ID:hujunlong,项目名称:go-sqlite3,代码行数:32,代码来源:sqlite3_test.go
示例8: main
func main() {
sql.Register("sqlite3_with_extensions",
&sqlite3.SQLiteDriver{
Extensions: []string{
"sqlite3_mod_vtable",
},
})
db, err := sql.Open("sqlite3_with_extensions", ":memory:")
if err != nil {
log.Fatal(err)
}
defer db.Close()
db.Exec("create virtual table repo using github(id, full_name, description, html_url)")
rows, err := db.Query("select id, full_name, description, html_url from repo")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
var id, full_name, description, html_url string
rows.Scan(&id, &full_name, &description, &html_url)
fmt.Printf("%s: %s\n\t%s\n\t%s\n\n", id, full_name, description, html_url)
}
}
开发者ID:zhsj,项目名称:elvish,代码行数:27,代码来源:extension.go
示例9: GetDatabase
// GetDatabase initialises a new database for a NameCache.
func GetDatabase(cfg *DBConfig) (*sql.DB, error) {
driver := "sqlite3_sous"
conn := InMemory
if cfg != nil {
if cfg.Driver != "" {
driver = cfg.Driver
}
if cfg.Connection != "" {
conn = cfg.Connection
}
}
if driver == "sqlite3" {
driver = "sqlite3_sous"
}
registerSQLOnce.Do(func() {
sql.Register(driver, &sqlite.SQLiteDriver{
ConnectHook: func(conn *sqlite.SQLiteConn) error {
if err := conn.RegisterFunc("semverEqual", semverEqual, true); err != nil {
return err
}
return nil
},
})
})
db, err := sql.Open(driver, conn) //only call once
return db, errors.Wrapf(err, "get DB/open: %v", cfg)
}
开发者ID:opentable,项目名称:sous,代码行数:31,代码来源:image_mapping.go
示例10: init
func init() {
err := initDriver()
if err != nil {
panic(err)
}
sql.Register("odbc", &drv)
}
开发者ID:shaybix,项目名称:odbc,代码行数:7,代码来源:driver.go
示例11: TestOpen
func TestOpen(t *testing.T) {
cfgfile := os.Getenv("DBCONFIG")
if cfgfile == "" {
cfgfile = "config.toml"
}
cfg := new(Config)
if f, err := os.Open(cfgfile); err != nil {
t.Fatal(err, "(did you set the DBCONFIG env variable?)")
} else {
if _, err := toml.DecodeReader(f, cfg); err != nil {
t.Fatal(err)
}
}
d := NewDriver("ClusterSql", mysql.MySQLDriver{})
for _, ncfg := range cfg.Nodes {
if ncfg.Password != "" {
d.AddNode(ncfg.Name, fmt.Sprintf("%s:%[email protected](%s:%d)/%s", ncfg.UserName, ncfg.Password, ncfg.HostName, ncfg.Port, ncfg.DBName))
} else {
d.AddNode(ncfg.Name, fmt.Sprintf("%[email protected](%s:%d)/%s", ncfg.UserName, ncfg.HostName, ncfg.Port, ncfg.DBName))
}
}
sql.Register("cluster", d)
var err error
db, err = sql.Open("cluster", "galera")
if err != nil {
t.Error(err)
}
}
开发者ID:mhannig,项目名称:clustersql,代码行数:31,代码来源:cluster_test.go
示例12: Register
// Register a txdb sql driver under the given sql driver name
// which can be used to open a single transaction based database
// connection.
//
// When Open is called any number of times it returns
// the same transaction connection. Any Begin, Commit calls
// will not start or close the transaction.
//
// When Close is called, the transaction is rolled back.
//
// Use drv and dsn as the standard sql properties for
// your test database connection to be isolated within transaction.
//
// Note: if you open a secondary database, make sure to differianciate
// the dsn string when opening the sql.DB. The transaction will be
// isolated within that dsn
func Register(name, drv, dsn string) {
sql.Register(name, &txDriver{
dsn: dsn,
drv: drv,
conns: make(map[string]*conn),
})
}
开发者ID:DATA-DOG,项目名称:go-txdb,代码行数:23,代码来源:db.go
示例13: init
func init() {
hook := func(conn *sqlite3.SQLiteConn) error {
_, err := conn.Exec("PRAGMA foreign_keys = ON", nil)
return err
}
driver := &sqlite3.SQLiteDriver{ConnectHook: hook}
sql.Register("sql_fk", driver)
}
开发者ID:alankm,项目名称:vorteil,代码行数:8,代码来源:core.go
示例14: init
func init() {
sql.Register("sqlite3", &Driver{})
if os.Getenv("SQLITE_LOG") != "" {
ConfigLog(func(d interface{}, err error, msg string) {
log.Printf("%s: %s, %s\n", d, err, msg)
}, "SQLITE")
}
}
开发者ID:pkf,项目名称:gosqlite,代码行数:8,代码来源:driver.go
示例15: init
func init() {
fmt.Println("init()", reflect.TypeOf(globalStubDriver))
// for stub test: not call in init() usually
db, err := globalStubDriver.Open("DataSourceName")
fmt.Println("db, err=\t", db, err)
sql.Register("stubdriver", globalStubDriver)
}
开发者ID:nakagami,项目名称:stubdriver,代码行数:9,代码来源:driver.go
示例16: init
func init() {
dvr = NewFakeDialect()
sql.Register("dali", dvr)
dbHandle, err := sql.Open("dali", "")
if err != nil {
panic(err)
}
db = NewDB(dbHandle, dvr)
}
开发者ID:mibk,项目名称:dali,代码行数:9,代码来源:loading_test.go
示例17: init
func init() {
sql.Register("sqlite3_textql",
&sqlite3.SQLiteDriver{
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
sqlite3conn = append(sqlite3conn, conn)
return nil
},
})
}
开发者ID:kangkot,项目名称:textql,代码行数:9,代码来源:sqlite.go
示例18: init
func init() {
sql.Register("sqlite3_fk",
&sqlite3.SQLiteDriver{
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
_, err := conn.Exec("PRAGMA foreign_keys = ON", nil)
return err
},
})
}
开发者ID:alankm,项目名称:privileges,代码行数:9,代码来源:database.go
示例19: init
func init() {
sql.Register("sqlite3", &impl{open: defaultOpen})
if os.Getenv("SQLITE_LOG") != "" {
ConfigLog(func(d interface{}, err error, msg string) {
log.Printf("%s: %s, %s\n", d, err, msg)
}, "SQLITE")
}
ConfigMemStatus(false)
}
开发者ID:gwenn,项目名称:gosqlite,代码行数:9,代码来源:driver.go
示例20: init
func init() {
sql.Register("csv", &csvDriver{})
db, err := sql.Open("ql", "memory:///dev/null")
if err != nil {
panic(err)
}
defer db.Close()
qldrv = db.Driver()
}
开发者ID:go-hep,项目名称:csvutil,代码行数:10,代码来源:driver.go
注:本文中的database/sql.Register函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论