Example #1
1
File: sql.go Project: jonasi/pgx
// 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
}
Example #2
0
func init() {
	mu.Lock()
	defer mu.Unlock()

	if !driverRegistered("mysql") {
		sql.Register("mysql", &mysql.MySQLDriver{})
	}

	if !driverRegistered("mariadb") {
		sql.Register("mariadb", &mysql.MySQLDriver{})
	}
}
Example #3
0
// 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{})
}
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
}
Example #5
0
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))
}
Example #6
0
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)
	}
}
Example #7
0
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)
		}
	}
}
Example #8
0
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)
	}
}
Example #9
0
// 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)
}
Example #10
0
func init() {
	err := initDriver()
	if err != nil {
		panic(err)
	}
	sql.Register("odbc", &drv)
}
Example #11
0
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:%s@tcp(%s:%d)/%s", ncfg.UserName, ncfg.Password, ncfg.HostName, ncfg.Port, ncfg.DBName))
		} else {
			d.AddNode(ncfg.Name, fmt.Sprintf("%s@tcp(%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)
	}
}
Example #12
0
// 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),
	})
}
Example #13
0
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)
}
Example #14
0
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")
	}
}
Example #15
0
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)
}
Example #16
0
func init() {
	dvr = NewFakeDialect()
	sql.Register("dali", dvr)
	dbHandle, err := sql.Open("dali", "")
	if err != nil {
		panic(err)
	}
	db = NewDB(dbHandle, dvr)
}
Example #17
0
func init() {
	sql.Register("sqlite3_textql",
		&sqlite3.SQLiteDriver{
			ConnectHook: func(conn *sqlite3.SQLiteConn) error {
				sqlite3conn = append(sqlite3conn, conn)
				return nil
			},
		})
}
Example #18
0
func init() {
	sql.Register("sqlite3_fk",
		&sqlite3.SQLiteDriver{
			ConnectHook: func(conn *sqlite3.SQLiteConn) error {
				_, err := conn.Exec("PRAGMA foreign_keys = ON", nil)
				return err
			},
		})
}
Example #19
0
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)
}
Example #20
0
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()
}
Example #21
0
File: ora.go Project: ricsmania/ora
func init() {
	var err error
	_drv.sqlPkgEnv, err = OpenEnv(nil)
	if err != nil {
		errE(err)
	}
	// database/sql/driver expects binaryFloat to return float64 (not the Rset default of float32)
	_drv.sqlPkgEnv.cfg.StmtCfg.Rset.binaryFloat = F64
	sql.Register(Name, _drv)
}
func SetupConnectionCountingDriver(delegateDriverName, sqlDataSource, newDriverName string) {
	delegateDBConn, err := sql.Open(delegateDriverName, sqlDataSource)
	if err == nil {
		// ignoring any connection errors since we only need this to access the driver struct
		delegateDBConn.Close()
	}

	connectionCountingDriver := &connectionCountingDriver{delegateDBConn.Driver()}
	sql.Register(newDriverName, connectionCountingDriver)
}
Example #23
0
func init() {
	regex := func(re, s string) (bool, error) {
		return regexp.MatchString(re, s)
	}
	sql.Register("sqlite3_custom",
		&sqlite3.SQLiteDriver{
			ConnectHook: func(conn *sqlite3.SQLiteConn) error {
				return conn.RegisterFunc("REGEXP", regex, true)
			},
		})
}
Example #24
0
// Once the exporter tool is installed, just invoke the command with the
// appropriate parameters, like:
// exporter -in yourdatabase.db -out youroutputfile.csv
func main() {

	// Parse the flags from the command line.
	flag.Parse()

	cfg := loadConfig(*configFile)

	ctx := &Context{}

	// Use FieldsExporter if there are export fields. Default to RawExporter.
	if len(cfg.Fields) > 0 {
		exporter := NewFieldExporter(cfg.Fields)
		ctx.Exporter = exporter
	} else {
		exporter := new(RawExporter)
		ctx.Exporter = exporter
	}

	// CSV Params.
	ctx.Out.CSVFile = *outputFile

	// DB params.
	ctx.In.Path = *inputFile
	ctx.In.User = *dbUser
	ctx.In.Password = *dbPassword

	// Prepare the db connection pool.
	sqlite3conn := []*sqlite3.SQLiteConn{}
	sql.Register("contactlab.push.exporter.SQLITE",
		&sqlite3.SQLiteDriver{
			ConnectHook: func(conn *sqlite3.SQLiteConn) error {
				sqlite3conn = append(sqlite3conn, conn)
				return nil
			},
		})

	// Open a connection to the database.
	log.Printf("Connecting to %s...\n", ctx.In.Path)
	db, err := sql.Open("contactlab.push.exporter.SQLITE", ctx.In.Path)
	if err != nil {
		log.Fatalln(err)
	}
	defer db.Close()
	ctx.In.Connection = db

	err = prepareCSV(ctx)
	if err != nil {
		log.Fatalln(err)
	}

	log.Println("Done!")
}
func dostuff() error {

	//http://golang-basic.blogspot.co.nz/2014/06/golang-database-step-by-step-guide-on.html
	var DB_DRIVER string
	sql.Register(DB_DRIVER, &sqlite3.SQLiteDriver{})

	_, err := sql.Open(DB_DRIVER, "mysqlite_3")
	if err != nil {
		log.Println("ERROR: Failed to create the handle.")
	}

	return nil
}
Example #26
0
func TestCustomRegister2(t *testing.T) {
	sql.Register("sqlite3FK", sqlite.NewDriver(nil, func(c *sqlite.Conn) error {
		_, err := c.EnableFKey(true)
		return err
	}))
	db, err := sql.Open("sqlite3FK", ":memory:")
	checkNoError(t, err, "Error while opening customized db: %s")
	defer checkSqlDbClose(db, t)
	conn := sqlite.Unwrap(db)
	fk, err := conn.IsFKeyEnabled()
	checkNoError(t, err, "Error while reading foreign_keys status: %s")
	assert.Tf(t, fk, "foreign_keys = %t; want %t", fk, true)
}
Example #27
0
// NewDbMapFromConfig functions similarly to NewDbMap, but it takes the
// decomposed form of the connection string, a *mysql.Config.
func NewDbMapFromConfig(config *mysql.Config, maxOpenConns int) (*gorp.DbMap, error) {
	adjustMySQLConfig(config)

	// We always want strict mode. Rather than leaving this up to DB config, we
	// prefix each statement with it.
	prefix := "SET STATEMENT sql_mode='STRICT_ALL_TABLES' FOR "

	// If a read timeout is set, we set max_statement_time to 95% of that, and
	// long_query_time to 80% of that. That way we get logs of queries that are
	// close to timing out but not yet doing so, and our queries get stopped by
	// max_statement_time before timing out the read. This generates clearer
	// errors, and avoids unnecessary reconnects.
	if config.ReadTimeout != 0 {
		// In MariaDB, max_statement_time and long_query_time are both seconds.
		// Note: in MySQL (which we don't use), max_statement_time is millis.
		readTimeout := config.ReadTimeout.Seconds()
		prefix = fmt.Sprintf(
			"SET STATEMENT max_statement_time=%g, long_query_time=%g, sql_mode='STRICT_ALL_TABLES' FOR ",
			readTimeout*0.95, readTimeout*0.80)
	}

	// The way we generate a customized database driver means that we need to
	// choose a name to register the driver with. Because this function can be
	// called multiple times with different parameters, we choose a random name
	// each time we register to avoid conflicts with other DB instances.
	// We use crypto/rand rather than math.Rand not out of a particular necessity
	// for high-quality randomness, but simply because using non-crypto rand is a
	// code smell.
	driverNum, err := rand.Int(rand.Reader, big.NewInt(math.MaxInt64))
	if err != nil {
		return nil, err
	}
	driverName := fmt.Sprintf("mysql-%d", driverNum)
	sql.Register(driverName, prefixdb.New(prefix, mysql.MySQLDriver{}))

	db, err := sqlOpen(driverName, config.FormatDSN())
	if err != nil {
		return nil, err
	}
	if err = db.Ping(); err != nil {
		return nil, err
	}
	setMaxOpenConns(db, maxOpenConns)

	dialect := gorp.MySQLDialect{Engine: "InnoDB", Encoding: "UTF8"}
	dbmap := &gorp.DbMap{Db: db, Dialect: dialect, TypeConverter: BoulderTypeConverter{}}

	initTables(dbmap)

	return dbmap, err
}
Example #28
0
func init() {
	sql.Register("sqlite3_gipam",
		&sqlite.SQLiteDriver{
			ConnectHook: func(conn *sqlite.SQLiteConn) error {
				if err := conn.RegisterFunc("prefixIsInside", dbPrefixIsInside, true); err != nil {
					return err
				}
				if err := conn.RegisterFunc("prefixLen", dbPrefixLen, true); err != nil {
					return err
				}
				return nil
			},
		})
}
Example #29
0
func TestAggregatorRegistration(t *testing.T) {
	customSum := func() *sumAggregator {
		var ret sumAggregator
		return &ret
	}

	sql.Register("sqlite3_AggregatorRegistration", &SQLiteDriver{
		ConnectHook: func(conn *SQLiteConn) error {
			if err := conn.RegisterAggregator("customSum", customSum, true); err != nil {
				return err
			}
			return nil
		},
	})
	db, err := sql.Open("sqlite3_AggregatorRegistration", ":memory:")
	if err != nil {
		t.Fatal("Failed to open database:", err)
	}
	defer db.Close()

	_, err = db.Exec("create table foo (department integer, profits integer)")
	if err != nil {
		// trace feature is not implemented
		t.Skip("Failed to create table:", err)
	}

	_, err = db.Exec("insert into foo values (1, 10), (1, 20), (2, 42)")
	if err != nil {
		t.Fatal("Failed to insert records:", err)
	}

	tests := []struct {
		dept, sum int64
	}{
		{1, 30},
		{2, 42},
	}

	for _, test := range tests {
		var ret int64
		err = db.QueryRow("select customSum(profits) from foo where department = $1 group by department", test.dept).Scan(&ret)
		if err != nil {
			t.Fatal("Query failed:", err)
		}
		if ret != test.sum {
			t.Fatalf("Custom sum returned wrong value, got %d, want %d", ret, test.sum)
		}
	}
}
Example #30
0
func init() {
	d := &Driver{}
	sql.Register("pgx", d)

	databaseSqlOids = make(map[pgx.Oid]bool)
	databaseSqlOids[pgx.BoolOid] = true
	databaseSqlOids[pgx.ByteaOid] = true
	databaseSqlOids[pgx.Int2Oid] = true
	databaseSqlOids[pgx.Int4Oid] = true
	databaseSqlOids[pgx.Int8Oid] = true
	databaseSqlOids[pgx.Float4Oid] = true
	databaseSqlOids[pgx.Float8Oid] = true
	databaseSqlOids[pgx.DateOid] = true
	databaseSqlOids[pgx.TimestampTzOid] = true
}