• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Golang sqlx.MustConnect函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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


示例16: newConveyor

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

	c := New(db)
	c.BuildQueue = NewBuildQueue(100)

	return c
}
开发者ID:emmetog,项目名称:conveyor,代码行数:11,代码来源:conveyor_test.go


示例17: Database

// This method collects all the errors and submits them to Rollbar
func Database(connString string) gin.HandlerFunc {
	db := sqlx.MustConnect("mysql", connString)
	if err := RunMigrations(db); err != nil {
		panic(err)
	}

	return func(c *gin.Context) {
		c.Set(DBKey, db)
		c.Next()
	}
}
开发者ID:nazwa,项目名称:Galaxy-Empires,代码行数:12,代码来源:database.go


示例18: InitDatabase

func InitDatabase() {
	db = sqlx.MustConnect("mysql", dbConnString)
	db.Ping()
	var check TableCheck
	err := db.Get(&check, checkIfSchemaExists)
	if err != nil {
		log.Fatalf("error connecting to the database: %v", err)
	}
	if check.DoesExist < 1 {
		db.MustExec(schema)
	}
}
开发者ID:wakermahmud,项目名称:jekyll-build-server,代码行数:12,代码来源:database.go


示例19: main

func main() {
	db := sqlx.MustConnect("postgres", "host=/run/postgresql user=radex dbname=vsmail sslmode=disable")
	defer db.Close()

	urls := []string{
		"http://speedyspin.ru/french-junior-cadet-open-2016",
		"http://speedyspin.ru/obzor-victas-v-15-extra",
		"http://speedyspin.ru/ttsport-vperedi-planety-vsei",
		"http://speedyspin.ru/raketki-dlia-nastol-nogo-tennisa",
		"http://speedyspin.ru/nastol-nyi-tennis-video",
		"http://speedyspin.ru/itogi-goda",
		"http://speedyspin.ru/baiki-chast-1",
		"http://speedyspin.ru/baiki-chast-2",
		"http://speedyspin.ru/komandnyi-kubok-sankt-peterburga-sredi-iunoshei-i-devushek-1997-goda-rozhdeniia-i-molozhe",
		"http://speedyspin.ru/pervenstvo-sankt-peterburga-sredi-par",
		"http://speedyspin.ru/pervyi-pro-tur-vziat",
		"http://speedyspin.ru/turnir-sil-neishikh-sportsmenov-rossii-top-24-2015",
		"http://speedyspin.ru/vii-letniaia-spartakiada-uchashchikhsia-rossii-2015",
		"http://speedyspin.ru/zakryli-ligu-v",
		"http://speedyspin.ru/vania-nikulin-smog",
		"http://speedyspin.ru/pobeda-na-rodine-il-icha",
		"http://speedyspin.ru/trenerskie-sekrety",
		"http://speedyspin.ru/krasotishcha-osnovanie-stiga-emerald-i-nakladki-stiga-airoc-m",
		"http://speedyspin.ru/nagrada-nashla-geroia",
		"http://speedyspin.ru/poedinok-robota-s-olimpiiskim-chempionom",
		"http://speedyspin.ru/patsanchik-k-uspekhu-prishel",
		"http://speedyspin.ru/xv-mezhdunarodnyi-turnir-po-nastol-nomu-tennisu-pamiati-nikolaia-nikitina",
		"http://speedyspin.ru/osennie-kanikuly-2014",
		"http://speedyspin.ru/pobeda-v-podmoskov-e",
		"http://speedyspin.ru/komandnoe-pervenstvo-sankt-peterburga-sredi-iuniorov-2014",
		"http://speedyspin.ru/vse-te-zhe-vse-tam-zhe-chast-2",
		"http://speedyspin.ru/euro-minichamp-2014",
		"http://speedyspin.ru/vse-te-zhe-vse-tam-zhe",
		"http://speedyspin.ru/euro-minichamp-s-2014",
		"http://speedyspin.ru/albena-2014",
		"http://speedyspin.ru/my-luchshie-na-pervenstve-rossii",
		"http://speedyspin.ru/my-opiat-sil-ny-v-smeshannykh-parakh",
		"http://speedyspin.ru/pervenstvo-sportivnykh-shkol-sankt-peterburga",
		"http://speedyspin.ru/shestaia-letniaia-spartakiada-uchashchikhsia-rossii-2013-goda",
		"http://speedyspin.ru/shkola-olimpiiskogo-rezerva-kometa",
		"http://speedyspin.ru/diussh-2-krasnogvardeiskogo-raiona",
		"http://speedyspin.ru/diussh-2-kalininskogo-raiona",
		"http://speedyspin.ru/klub-nastol-nogo-tennisa-belye-molnii",
	}

	for _, url := range urls {
		p := Post{}
		p.Fill(url)
		id := p.New(db)
		fmt.Println(id)
	}

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


示例20: main

func main() {
	// Must.... functions will panic on fail
	db := sqlx.MustConnect("mysql", "root:[email protected](127.0.0.1:7701)/gotraining")
	var item Item

	// We'll get most recent item and map it into our struct
	err := db.Get(&item, "SELECT * FROM items ORDER BY id DESC LIMIT 1")
	if err != nil {
		panic(err)
	}

	fmt.Printf("id: %d, %s, %s", item.Id, item.Name.String, item.Description.String)
}
开发者ID:exu,项目名称:go-workshops,代码行数:13,代码来源:20-sqlx-get.go



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang sqlx.NewDb函数代码示例发布时间:2022-05-23
下一篇:
Golang sqlx.Connect函数代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap