func (res TagsResource) getTags(req *restful.Request, resp *restful.Response) { links := halgo.Links{}. Self("/api/lookups/tags"). Link("root", "/api"). Link("lookups", "/api/lookups") // go fetch our employmenttype data from the db tags := []TagDataModel{} stmt := database.TagsStatement{}.GetSelectAll() db, err := sqlx.Connect("mssql", "server=d101983;user id=retroUser;password=Password") if err != nil { log.Fatal("Could not connect to the database, " + err.Error()) return } defer db.Close() err = db.Select(&tags, stmt) if err != nil { resp.WriteError(500, errors.New("Error retrieving tags from database, "+err.Error())) return } data := TagResourceModel{ Links: links, Tags: tags, } resp.WriteEntity(data) }
// Connect to the database func Connect(d Databases) { var err error switch d.Type { case "MySQL": // Connect to MySQL if DB, err = sqlx.Connect("mysql", DSN(d.MySQL)); err != nil { log.Println("SQL Driver Error", err) } // Check if is alive if err := DB.Ping(); err != nil { log.Println("Database Error", err) } case "SQLite": // Connect to SQLite if DB, err = sqlx.Connect("sqlite3", d.SQLite.Parameter); err != nil { log.Println("SQL Driver Error", err) } // Check if is alive if err := DB.Ping(); err != nil { log.Println("Database Error", err) } default: log.Println("No registered database in config") } }
// TODO: Instead pass back *sql.DB func (r *RDPG) OpenDB(dbname string) error { if r.DB == nil { u, err := url.Parse(r.URI) if err != nil { log.Error(fmt.Sprintf("Failed parsing URI %s err: %s", r.URI, err)) } u.Path = dbname r.URI = u.String() db, err := sqlx.Connect("postgres", r.URI) if err != nil { log.Error(fmt.Sprintf("Failed connecting to %s err: %s", rdpgURI, err)) return err } r.DB = db } else { err := r.DB.Ping() if err != nil { db, err := sqlx.Connect("postgres", r.URI) if err != nil { log.Error(fmt.Sprintf("Failed connecting to %s err: %s", rdpgURI, err)) proc, _ := os.FindProcess(os.Getpid()) proc.Signal(syscall.SIGTERM) return err } r.DB = db } } return nil }
// FetchMetrics interface for mackerelplugin func (p PostgresPlugin) FetchMetrics() (map[string]interface{}, error) { db, err := sqlx.Connect("postgres", fmt.Sprintf("user=%s password=%s host=%s port=%s sslmode=%s connect_timeout=%d %s", p.Username, p.Password, p.Host, p.Port, p.SSLmode, p.Timeout, p.Option)) if err != nil { logger.Errorf("FetchMetrics: %s", err) return nil, err } defer db.Close() statStatDatabase, err := fetchStatDatabase(db) if err != nil { return nil, err } statConnections, err := fetchConnections(db) if err != nil { return nil, err } statDatabaseSize, err := fetchDatabaseSize(db) if err != nil { return nil, err } stat := make(map[string]interface{}) mergeStat(stat, statStatDatabase) mergeStat(stat, statConnections) mergeStat(stat, statDatabaseSize) return stat, err }
// NewApplication is the constructor for Application struct. // // If testing is true, connects to the "test" database. func NewApplication(testing bool) (*Application, error) { u, err := libunix.CurrentUser() if err != nil { return nil, err } dbname := "forty-thieves" if testing { dbname += "-test" } dsn := libenv.EnvWithDefault( "DSN", fmt.Sprintf("postgres://%v@localhost:5432/%s?sslmode=disable", u, dbname)) db, err := sqlx.Connect("postgres", dsn) if err != nil { return nil, err } cookieStoreSecret := libenv.EnvWithDefault("COOKIE_SECRET", "ittwiP92o0oi6P4i") app := &Application{} app.dsn = dsn app.db = db app.cookieStore = sessions.NewCookieStore([]byte(cookieStoreSecret)) return app, err }
func sqlxConn() *sqlx.DB { db, err := sqlx.Connect("mysql", mysqlDSN) if err != nil { log.Fatalln(err) } return db }
func initInjector(cmd *cobra.Command, args []string) { db, err := sqlx.Connect("postgres", viper.GetString("postgres-url")) if err != nil { fail(err) } mustProvide(inject.Object{Value: &drivers.DB}) mustProvide(inject.Object{Value: &drivers.Horizon}) mustProvide(inject.Object{Value: &uis.Console}) mustProvide(inject.Object{Value: &drivers.Editor}) mustProvide(inject.Object{Value: &drivers.HTTP}) mustProvide(inject.Object{Value: db}) mustProvide(inject.Object{ Name: "postgres-url", Value: viper.GetString("postgres-url"), }) mustProvide(inject.Object{ Name: "horizon-url", Value: viper.GetString("horizon-url"), }) if err := injector.Populate(); err != nil { fail(err) } }
func initializing() { conn, err := sqlx.Connect("mysql", config.GetConfig().GetString("database.mysql")) if err != nil { fmt.Printf("mysql connect err: %+v\n", err) } db = conn }
func Connect() (*sqlx.DB, error) { open := os.Getenv("DATABASE_URL") fmt.Println("connecting to", open) if parsedURL, err := pq.ParseURL(open); err == nil && parsedURL != "" { open = parsedURL } con, err := sqlx.Connect("postgres", open) if err != nil { fmt.Println(err) return nil, err } err = con.Ping() if err != nil { fmt.Println(err) return nil, err } return con, err }
func init() { info := config.File.GetStringMap("database") ConnectionInfo = DatabaseConnection{ info["user"].(string), info["host"].(string), info["database"].(string), } connString := fmt.Sprintf( "postgres://%s:@%s/%s?sslmode=disable", info["user"], info["host"], info["database"], ) Sq = sq.StatementBuilder.PlaceholderFormat(sq.Dollar) client, err := sqlx.Connect("postgres", connString) if err != nil { panic(err) } Client = client println("Connected to Postgres") }
func main() { // this Pings the database trying to connect, panics on error // use sqlx.Open() for sql.Open() semantics dbUserName := os.Getenv("DB_USERNAME") creds := fmt.Sprintf("user=%s dbname=initial sslmode=disable", dbUserName) fmt.Println(dbUserName) fmt.Println(creds) db, err := sqlx.Connect("postgres", creds) fmt.Println("yay db!") if err != nil { fmt.Println(err) } // exec the schema or fail; multi-statement Exec behavior varies between // database drivers; pq will exec them all, sqlite3 won't, ymmv db.MustExec(schema) tx := db.MustBegin() fmt.Println("starting seeding") tx.MustExec("INSERT INTO users (first_name, last_name, email) VALUES ($1, $2, $3)", "Theo", "Smith-Crespo", "*****@*****.**") tx.MustExec("INSERT INTO users (first_name, last_name, email) VALUES ($1, $2, $3)", "Janice", "Smith", "*****@*****.**") tx.MustExec("INSERT INTO users (first_name, last_name, email) VALUES ($1, $2, $3)", "Alvin", "Crespo", "*****@*****.**") tx.Commit() fmt.Println("ending seeding") }
func createDb(spec string) (*sqlx.DB, error) { db, err := sqlx.Connect("mysql", spec+"?parseTime=true&charset=utf8mb4,utf8") if err != nil { return nil, err } return db, nil }
// DBConnect connects to MySQL database func DBConnect() (*sqlx.DB, error) { // Generate connection string using configuration conn := fmt.Sprintf("%s:%s@/%s", Static.Config.DB.Database, Static.Config.DB.Username, Static.Config.DB.Password) // Return connection and associated errors return sqlx.Connect("mysql", conn) }
func main() { var ( cfgFile string role string ) flag.StringVar(&cfgFile, "c", "./conf/cfg.ini", "config file") flag.StringVar(&role, "r", "", "server role: scheduler, fetcher etc.") flag.Parse() config, err := utils.ReadConfig(cfgFile) if err == nil { if role == "scheduler" { db, _ := sqlx.Connect("mysql", config["scheduler"]["dsn"]) log.Infoln("db stats: ", db.Stats()) scheduler := scheduler.InitScheduler(db, config["scheduler"]) scheduler.Run() } else if role == "fetcher" { fetcher := fetcher.InitFetcher(config["fetcher"]) fetcher.Run() } else { fmt.Println("unknown role:", role) } } }
func searchHandler(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/json") query := req.URL.Query()["q"][0] cypher := `MATCH (movie:Movie) WHERE movie.title =~ {0} RETURN movie.title as title, movie.tagline as tagline, movie.released as released` db, err := sqlx.Connect("neo4j-cypher", neo4jURL) if err != nil { log.Println("error connecting to neo4j:", err) } defer db.Close() movies := []Movie{} param := "(?i).*" + query + ".*" err = db.Select(&movies, cypher, param) if err != nil { log.Println("error querying search:", err) } movieResults := []MovieResult{} for _, x := range movies { movieResults = append(movieResults, MovieResult{x}) } err = json.NewEncoder(w).Encode(movieResults) if err != nil { log.Println("error writing search response:", err) } }
func (config *DBConfig) ConnectToDB() *sqlx.DB { result, err := sqlx.Connect(config.Flavor, config.URI) if err != nil { log.Fatal(err) } return result }
func NewEntityManager(dbType string, url string) (em EntityManager, err error) { em.db, err = sqlx.Connect(dbType, url) em.log = logrus.WithFields(logrus.Fields{ "service": "EntityManager", }) return }
func TestFetchStatDatabase(t *testing.T) { db, _ := sqlx.Connect("testdb", "") columns := []string{"xact_commit", "xact_rollback", "blks_read", "blks_hit", "blk_read_time", "blk_write_time", "tup_returned", "tup_fetched", "tup_inserted", "tup_updated", "tup_deleted", "deadlocks", "temp_bytes"} testdb.StubQuery(`SELECT * FROM pg_stat_database`, testdb.RowsFromCSVString(columns, ` 1,2,3,4,5,6,7,8,9,10,11,12,13 10,20,30,40,50,60,70,80,90,100,110,120,130 `)) stat, err := fetchStatDatabase(db) expected := map[string]interface{}{ "xact_commit": uint64(11), "blks_hit": uint64(44), "tup_returned": uint64(77), } if err != nil { t.Errorf("Expected no error, but got %s instead", err) } if err = db.Close(); err != nil { t.Errorf("Error '%s' was not expected while closing the database", err) } if stat["xact_commit"] != expected["xact_commit"] { t.Error("should be 11") } if stat["blks_hit"] != expected["blks_hit"] { t.Error("should be 44") } if stat["tup_returned"] != expected["tup_returned"] { t.Error("should be 77") } }
func newDbForTest(t *testing.T) *sqlx.DB { var err error pguser, pgpass, pghost, pgport, pgsslmode := os.Getenv("PGUSER"), os.Getenv("PGPASSWORD"), os.Getenv("PGHOST"), os.Getenv("PGPORT"), os.Getenv("PGSSLMODE") if pguser == "" { pguser, err = libunix.CurrentUser() if err != nil { t.Fatalf("Getting current user should never fail. Error: %v", err) } } if pghost == "" { pghost = "localhost" } if pgport == "" { pgport = "5432" } if pgsslmode == "" { pgsslmode = "disable" } db, err := sqlx.Connect("postgres", fmt.Sprintf("postgres://%v@%v:%v/fpc-test?sslmode=%v&password=%v", pguser, pghost, pgport, pgsslmode, pgpass)) if err != nil { t.Fatalf("Connecting to local postgres should never fail. Error: %v", err) } return db }
func WithPG(ctx context.Context, credentials string) (context.Context, error) { db, err := sqlx.Connect("postgres", credentials) if err != nil { return ctx, err } return context.WithValue(ctx, "db:connection", db), nil }
func test(config map[string]map[string]string) { db, _ := sqlx.Connect("mysql", config["scheduler"]["dsn"]) log.Infoln("db stats: ", db.Stats()) taskDao := dao.InitTaskDao(db) crawlRules, _ := taskDao.GetWaitRules() crawlTasks := []types.CrawlTask{} for _, rule := range crawlRules { log.Infoln(rule.Domain, rule.Urlpath) crawlTasks = append(crawlTasks, taskDao.ConvertRuleToTask(rule)) } log.Infoln("get task: ", len(crawlTasks)) num, results, err := taskDao.AddNewTasks(crawlTasks) log.Infoln("add task: ", num, results, err) affectedRows, _ := taskDao.UpdateRules(crawlRules, results) log.Infoln("update rules: ", affectedRows) waitingTasks, _ := taskDao.GetWaitingTasks() for _, task := range waitingTasks { log.Infoln(task.Domain, task.Urlpath) } affectedRows, _ = taskDao.SetTasksStatus(waitingTasks, dao.TASK_FINISH) log.Infoln("set task status: ", affectedRows) }
func main() { flag.Parse() r := gin.Default() r.LoadHTMLGlob("templates/*") r.Use(static.Serve("/static", static.LocalFile("static", false))) dbx, err := sqlx.Connect("sqlite3", *dbName) if err != nil { log.Fatal(err) } api.New(r, "/api/v1", db.New(dbx)) r.GET("/", func(c *gin.Context) { c.HTML(http.StatusOK, "index.html", gin.H{"env": *env}) }) if err := r.Run(":" + *port); err != nil { panic(err) } }
// dbConnect connects to the database via sqlx. func dbConnect() (*sqlx.DB, error) { db, err := sqlx.Connect("mysql", strings.Join(dbCreds, "")) if err != nil { return nil, err } return db, nil }
// NewDB creates a new DB func NewDB(path string) (*DB, error) { db, err := sqlx.Connect("sqlite3", path) if err != nil { return nil, err } return &DB{db}, nil }
func Open(conf *core.Config) (*sqlx.DB, error) { type DBConfig struct { Development struct { Driver string Open string } } configText, err := ioutil.ReadFile(conf.GetFilePath(filepath.Join("database", "dbconf.yml"))) if err != nil { return nil, err } dbConfig := DBConfig{} err = yaml.Unmarshal(configText, &dbConfig) if err != nil { return nil, err } db, err := sqlx.Connect(dbConfig.Development.Driver, dbConfig.Development.Open) if err != nil { return nil, err } return db, nil }
// SqlRepository returns a new sqlRepository or panics if it cannot func SqlRepository() UserRepository { settings := settings.NewSettings("settings.json") db, err := sqlx.Connect(settings.DriverName(), settings.DataSource()) if err != nil { panic("Error connecting to db: " + err.Error()) } repo := &sqlRepository{ db: db, } datetime := "datetime" if settings.DriverName() == "postgres" { datetime = "timestamp with time zone" } schema := fmt.Sprintf(`CREATE TABLE IF NOT EXISTS authuser ( key text not null primary key, name text, email text, password text, created %s, modified %s, lastactive %s );`, datetime, datetime, datetime) _, err = repo.db.Exec(schema) return repo }
// Interface returns an interface to the local data store. Currently, it assumes // that the schema our tables and functions are in can be found in the search // path, so you'll need to ensure that it's set in the provided resource. // // Example resource: "user=enoch dbname=evetool search_path=eveindy" func Interface(driver, resource string, xmlAPI evego.XMLAPI) (LocalDB, error) { dbConn, err := sqlx.Connect(driver, resource) if err != nil { return nil, err } // Is resource a URL or the other thing? // Find out, then add/modify search_path parameter. d := &dbInterface{ db: dbConn, xmlAPI: xmlAPI, } // Prepare statements stmts := []struct { preparedStatement **sqlx.Stmt statementText string }{ // Pointer magic, stage 1: Pass the address of the pointer. {&d.getSessionStmt, getSessionStmt}, {&d.setTokenStmt, setTokenStmt}, {&d.getAPIKeysStmt, getAPIKeysStmt}, {&d.addAPIKeyStmt, addAPIKeyStmt}, {&d.deleteAPIKeyStmt, deleteAPIKeyStmt}, {&d.logoutSessionStmt, logoutSessionStmt}, {&d.apiKeyInsertToonStmt, apiKeyInsertToonStmt}, {&d.apiKeyListToonsStmt, apiKeyListToonsStmt}, {&d.apiKeyInsertSkillStmt, apiKeyInsertSkillStmt}, {&d.apiKeyClearSkillsStmt, apiKeyClearSkillsStmt}, {&d.getSkillStmt, getSkillStmt}, {&d.getSkillGroupStmt, getSkillGroupStmt}, {&d.apiKeyClearCorpStandingsStmt, apiKeyClearCorpStandingsStmt}, {&d.apiKeyClearFacStandingsStmt, apiKeyClearFacStandingsStmt}, {&d.apiKeyInsertCorpStandingsStmt, apiKeyInsertCorpStandingsStmt}, {&d.apiKeyInsertFacStandingsStmt, apiKeyInsertFacStandingsStmt}, {&d.getStandingsStmt, getStandingsStmt}, {&d.deleteToonsStmt, deleteToonsStmt}, {&d.clearOutpostsStmt, clearOutpostsStmt}, {&d.insertOutpostsStmt, insertOutpostsStmt}, {&d.searchStationsStmt, searchStationsStmt}, {&d.getStationStmt, getStationStmt}, {&d.clearBlueprintsStmt, clearBlueprintsStmt}, {&d.insertBlueprintStmt, insertBlueprintStmt}, {&d.getBlueprintsStmt, getBlueprintsStmt}, {&d.clearAssetsStmt, clearAssetsStmt}, {&d.insertAssetStmt, insertAssetStmt}, {&d.getAssetsStmt, getAssetsStmt}, {&d.unusedSalvageStmt, unusedSalvageStmt}, } for _, s := range stmts { prepared, err := dbConn.Preparex(s.statementText) if err != nil { log.Fatalf("Unable to prepare statement: %v\n%v", err, s.statementText) } // Pointer magic, stage 2: Dereference the pointer to the pointer // and set it to point to the statement we just prepared. *s.preparedStatement = prepared } return d, nil }
func (r *RDPG) connect() (db *sqlx.DB, err error) { db, err = sqlx.Connect("postgres", r.URI) if err != nil { log.Error(fmt.Sprintf("rdpg.Node#Connect() %s ! %s", r.URI, err)) } return db, err }
func InitSQLX(dialect, args string) (*sqlx.DB, error) { var db *sqlx.DB var err error var i int retry: for { db, err = sqlx.Connect(dialect, args) switch { case err == nil: break retry case i >= RETRY: return nil, err default: logs.Error(err) i++ } time.Sleep(TIMEOUT) } if db.Ping() != nil { return db, err } return db, nil }
func init() { usr, err := user.Current() if err != nil { panic(err) } workDir := filepath.Join(usr.HomeDir, ".pmusic") artDir := filepath.Join(workDir, "cArt") path := filepath.Join(workDir, "music.db3") err = os.MkdirAll(artDir, 0700) if err != nil { panic(err) } db, err = sqlx.Connect("sqlite3", path) if err != nil { panic(err) } dbm = modl.NewDbMap(&db.DB, modl.SqliteDialect{}) dbm.AddTable(Musician{}, "musician").SetKeys(true, "Uid") dbm.AddTable(Stations{}, "station").SetKeys(true, "Uid") dbm.AddTable(Album{}, "album").SetKeys(true, "Uid") dbm.AddTable(Track{}, "track").SetKeys(true, "Uid") dbm.AddTable(PTimeStamp{}, "ptimestamp").SetKeys(true, "Uid") err = dbm.CreateTablesIfNotExists() if err != nil { panic(err) } }