当前位置: 首页>>代码示例>>Golang>>正文


Golang sqlx.MustConnect函数代码示例

本文整理汇总了Golang中github.com/jmoiron/sqlx.MustConnect函数的典型用法代码示例。如果您正苦于以下问题:Golang MustConnect函数的具体用法?Golang MustConnect怎么用?Golang MustConnect使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了MustConnect函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: newConveyor

func newConveyor(t testing.TB) *Conveyor {
	db := sqlx.MustConnect("postgres", databaseURL)
	if err := core.Reset(db); err != nil {
		t.Fatal(err)
	}

	c := core.New(db)
	c.BuildQueue = core.NewBuildQueue(100)
	c.Logger = logs.Discard

	ch := make(chan core.BuildContext)
	c.BuildQueue.Subscribe(ch)

	w := worker.New(c, worker.Options{
		Builder: builder.BuilderFunc(func(ctx context.Context, w io.Writer, options builder.BuildOptions) (string, error) {
			io.WriteString(w, "Pulling base image\n")
			return "remind101/acme-inc:1234", nil
		}),
		BuildRequests: ch,
	})

	go w.Start()

	return &Conveyor{
		Conveyor: c,
		worker:   w,
	}
}
开发者ID:atmos,项目名称:conveyor,代码行数:28,代码来源:api_test.go

示例2: main

func main() {
	// Connect to the database
	db := db.NewDB(sqlx.MustConnect(os.Getenv("DATABASE_KIND"), os.Getenv("DATABASE_URL")))

	// URL of the program
	// TODO: move to an external file
	urls := map[string]string{
		"reCaptchaSecret": "secrets/googleReCaptcha",
		"reCaptchaCheck":  "https://www.google.com/recaptcha/api/siteverify",
		"jwtSecret":       "secrets/jwt",
	}

	// Create the GoBox Main Server
	server, err := web.NewServer(db, urls)

	if err != nil {
		fmt.Printf("Cannot initialize server (error: %v)\n", err)
		return
	}

	port := flag.Arg(0)

	if port == "" {
		port = "8083"
	}

	// And listen
	log.Println("Server running")
	log.Println(server.ListenAndServer(":" + port))

}
开发者ID:simonedegiacomi,项目名称:goboxserver,代码行数:31,代码来源:main.go

示例3: main

func main() {
	var dbname, user string
	flag.StringVar(&dbname, "db", "zaymyonline", "a string var")
	flag.StringVar(&user, "user", "radex", "a string var")
	flag.Parse()
	connectStr := fmt.Sprintf("host=/run/postgresql user=%s dbname=%s sslmode=disable", user, dbname)
	fmt.Println(connectStr)
	db := sqlx.MustConnect("postgres", connectStr)
	defer db.Close()

	posts_sql := `CREATE TABLE IF NOT EXISTS posts (
	    id serial PRIMARY KEY,
	    slug varchar(200),
	    title varchar(200),
	    shortmessage text,
	    body text,
	    created timestamp DEFAULT now(),
	    modified timestamp DEFAULT now(),
	    contenttype varchar(20),
	    tags varchar(30)[],
	    categories varchar(30)[],
	    status smallint DEFAULT 0,
	    allowcomments boolean DEFAULT TRUE
	);`

	res, err := db.Exec(posts_sql)
	if err != nil {
		panic(err)
	}

	fmt.Println(res)

}
开发者ID:xenamorph,项目名称:quick,代码行数:33,代码来源:dbinit.go

示例4: MustConnect

func MustConnect(cfg *config.Config) *DB {
	db, err := New(sqlx.MustConnect("postgres", cfg.DatabaseURL), cfg)
	if err != nil {
		panic(err)
	}
	return db
}
开发者ID:thesoftwarefactoryuk,项目名称:podbaby,代码行数:7,代码来源:database.go

示例5: Setup

// Returns a StockDB with all  tables, panics if can't connect to db or make tables
// <requires> "$ createdb -Olocaluser trendydb", for Local
// <requires> "$ createdb -Olocaluser trendytestdb", for TestLocal
// <side effect> sets the global DB to the returned db
func (db *StockDB) Setup(env Environment) *StockDB {
	// TODO(jhurwich) implement user/pass/address switch based on local or prod environment
	var dbname, password, host, user, suffix string
	switch env {
	case Local:
		dbname = "trendydb"
		password = "localpass"
		host = "localhost"
		user = "localuser"
		suffix = "?sslmode=disable"
	case Production:
		// TODO(jhurwich) define for production environment
	case TestLocal:
		dbname = "trendytestdb"
		password = "localpass"
		host = "localhost"
		user = "localuser"
		suffix = "?sslmode=disable"
	}
	dbSource := fmt.Sprintf("postgres://%s:%[email protected]%s/%s%s", user, password, host, dbname, suffix)

	// initialize the db, note that it's a global object, it is never closed
	db = &StockDB{*(sqlx.MustConnect("postgres", dbSource))}
	db.CreateIfNotExists()
	DB = db // not entirely sure why we need this line with the address assignment two up, but whatever
	return db
}
开发者ID:jhurwich,项目名称:Trendy,代码行数:31,代码来源:db.go

示例6: main

func main() {
	db := sqlx.MustConnect("sqlite3", ":memory:")

	if err := createFooTable(db); err != nil {
		log.Fatal("couldn't create table: ", err)
	}

	for i := 0; i < 10; i++ {
		id, err := insertFoo(db, "hello world "+strconv.Itoa(i))
		if err != nil {
			log.Fatal("failed to insert value: ", err)
		}
		log.Print("inserted foo record ", id)
	}

	h := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fooListEndpoint(w, r, &FooStore{db: db})
	}))
	h = handlers.LoggingHandler(os.Stdout, h)
	h = handlers.ContentTypeHandler(h, "application/json")

	http.Handle("/foo/", h)

	flag.Parse()
	log.Print("starting http server on ", *addr)
	if err := http.ListenAndServe(*addr, nil); err != nil {
		log.Printf("http server failed: ", err)
	}
}
开发者ID:peefourtee,项目名称:godatabasesql,代码行数:29,代码来源:api.go

示例7: main

func main() {

	db := sqlx.MustConnect("mysql", "root:[email protected](127.0.0.1:7701)/gotraining")
	rows, err := db.Queryx("SELECT id, name, description FROM items")

	if err != nil {
		panic(err)
	}

	for rows.Next() {
		var item Item
		err = rows.StructScan(&item)

		if err != nil {
			panic(err)
		}

		fmt.Printf(
			"%d - %s:  %s\n===================\n",
			item.Id,
			item.Nme.String,
			item.Dsc.String,
		)
	}
}
开发者ID:exu,项目名称:go-workshops,代码行数:25,代码来源:20-sqlx-query_x.go

示例8: NewConnection

func NewConnection() *sqlx.DB {
	dbconfig, err := config()
	if err != nil {
		log.Panic(err)
	}
	return sqlx.MustConnect(dbconfig.Driver.Name, dbconfig.Driver.OpenStr)
}
开发者ID:cmc333333,项目名称:noticestats,代码行数:7,代码来源:db.go

示例9: main

func main() {
	db := sqlx.MustConnect("postgres", "postgres://localhost:15432/auth_sh?sslmode=disable")
	defer db.Close()

	s := oauth.New(db)

	m := martini.Classic()
	m.Use(handlers.NoCache())
	m.Use(sessions.Sessions("auth-sh", sessions.NewCookieStore([]byte("secret123"))))
	m.Use(render.Renderer(render.Options{Layout: "layout"}))
	m.Use(dump_request)
	m.Map(db)
	m.Map(s)

	m.Get("/login", handlers.GET_login)
	m.Get("/link", handlers.GET_link)
	m.Any("/authorize", handlers.GET_authorize)
	m.Any("/token", handlers.GET_token)
	m.Any("/info", handlers.GET_info)
	m.Get("/continue/:provider", handlers.GET_continue)
	m.Get("/logout", handlers.GET_logout)
	m.Get("/callback", handlers.GET_callback)

	m.Get("/", handlers.MayAuthenticate(), handlers.GET_home)
	m.Get("/me", handlers.MustAuthenticate(), handlers.GET_me)
	m.Get("/profile", handlers.MustAuthenticate(), handlers.GET_profile)
	m.Post("/applications", handlers.MustAuthenticate(), handlers.POST_application)

	m.Run()
}
开发者ID:rogeriomarques,项目名称:oapx,代码行数:30,代码来源:main.go

示例10: newDB

func newDB(c *cli.Context) *sqlx.DB {
	db := sqlx.MustConnect("postgres", c.String("db"))
	if err := conveyor.MigrateUp(db); err != nil {
		panic(err)
	}
	return db
}
开发者ID:atmos,项目名称:conveyor,代码行数:7,代码来源:factories.go

示例11: main

func main() {
	db := sqlx.MustConnect("sqlite3", ":memory:")

	if err := createFooTable(db); err != nil {
		log.Fatal("couldn't create table: ", err)
	}

	id, err := insertFoo(db, "hello world")
	if err != nil {
		log.Fatal("failed to insert value: ", err)
	}
	log.Print("inserted foo record ", id)

	foos, err := queryAllFoo(db)
	if err != nil {
		log.Fatal("failed to query all foos: ", err)
	}
	log.Printf("foos: %+v", foos)

	foo, err := querySingleFoo(db, id)
	if err != nil {
		log.Fatal("failed to query single foo: ", err)
	}
	log.Printf("found single foo: %+v", foo)
}
开发者ID:peefourtee,项目名称:godatabasesql,代码行数:25,代码来源:sqlx_queries.go

示例12: initDB

func initDB() *sqlx.DB {
	log.Printf("Initialising database at %s...\n", *DB_URI)
	db := sqlx.MustConnect("sqlite3", *DB_URI)
	appinit.CreateDBSchema(db)

	return db
}
开发者ID:3onyc,项目名称:3do,代码行数:7,代码来源:3do.go

示例13: TestAccount

func TestAccount(t *testing.T) {
	Convey("with a database", t, func() {
		Convey("create account", func() {
			db := sqlx.MustConnect("postgres", "postgres://localhost:15432/auth_sh?sslmode=disable")
			tx := db.MustBegin()

			defer db.Close()
			defer tx.Rollback()

			identity := &Identity{}
			err := CreateIdentity(tx, identity)

			account := &Account{
				IdentityId: identity.Id,
				RemoteId:   "fb:145478142",
				Name:       "Simon Menke",
				Email:      "[email protected]",
				Picture:    "",
				RawProfile: []byte("{}"),
				RawToken:   []byte("{}"),
			}
			err = CreateAccount(tx, account)
			So(err, ShouldBeNil)
			So(account.Id, ShouldBeGreaterThan, 0)
		})
	})
}
开发者ID:rogeriomarques,项目名称:oapx,代码行数:27,代码来源:account_test.go

示例14: init

func init() {
	models.DB = model.NewDB(sqlx.MustConnect("postgres", DSN))
	err := models.DB.Ping()
	if err != nil {
		log.Fatal(err)
	}

	log.Println("Connected to test DB.")
}
开发者ID:nucleardump,项目名称:go-wires,代码行数:9,代码来源:helpers_test.go

示例15: Connect

func Connect(addr string) *Store {
	// db := sqlx.MustConnect("pgx", addr)
	db := sqlx.MustConnect("postgres", addr)
	db.SetMaxIdleConns(4)
	db.SetMaxOpenConns(16)
	db.MapperFunc(snaker.CamelToSnake)
	dat.EnableInterpolation = true

	return &Store{db: db, ru: runner.NewDB(db.DB, "postgres")}
}
开发者ID:gogrademe,项目名称:api,代码行数:10,代码来源:store.go


注:本文中的github.com/jmoiron/sqlx.MustConnect函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。