Example #1
0
func Init(dbDir string) {
	DbFile = fmt.Sprintf("%s/walter.sqlite3", dbDir)
	db, err := genmai.New(&genmai.SQLite3Dialect{}, DbFile)
	if err != nil {
		panic(err)
	}

	defer db.Close()

	tables := [...]interface{}{
		&Project{},
		&Report{},
		&Commit{},
		&Stage{},
		&User{},
	}

	for _, t := range tables {
		if err := db.CreateTable(t); err != nil {
			if !regexp.MustCompile(`already exists`).Match([]byte(err.Error())) {
				panic(err)
				return
			}
		}
	}

}
Example #2
0
func main() {
	// setup genmai
	db, err := genmai.New(&genmai.SQLite3Dialect{}, ":memory:")
	if err != nil {
		log.Fatalln(err)
	}
	if err := db.CreateTableIfNotExists(&NodeInfo{}); err != nil {
		log.Fatalln(err)
	}

	// setup pongo
	pongo2.DefaultSet.SetBaseDirectory("templates")

	fantasy := &Fantasy{
		DB: db,
	}
	pongo2.Globals["Fantasy"] = fantasy

	// setup goji
	goji.Use(func(c *web.C, h http.Handler) http.Handler {
		fn := func(w http.ResponseWriter, r *http.Request) {
			c.Env["Fantasy"] = fantasy
			h.ServeHTTP(w, r)
		}
		return http.HandlerFunc(fn)
	})

	goji.Get("/", index)
	goji.Post("/create", create)
	goji.Get("/console/:id", console)
	goji.Get("/assets/*", http.FileServer(http.Dir(".")))
	goji.Serve()
}
Example #3
0
func Example() {
	db, err := genmai.New(&genmai.SQLite3Dialect{}, ":memory:")
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()
	for _, query := range []string{
		`CREATE TABLE test_model (
			id INTEGER NOT NULL PRIMARY KEY,
			name TEXT NOT NULL,
			addr TEXT NOT NULL
		)`,
		`INSERT INTO test_model VALUES (1, 'test1', 'addr1')`,
		`INSERT INTO test_model VALUES (2, 'test2', 'addr2')`,
		`INSERT INTO test_model VALUES (3, 'test3', 'addr3')`,
	} {
		if _, err := db.DB().Exec(query); err != nil {
			log.Fatal(err)
		}
	}
	var results []TestModel
	// SELECT * FROM "test_model";
	if err := db.Select(&results); err != nil {
		log.Fatal(err)
	}
	fmt.Println(results)
	// Output: [{1 test1 addr1} {2 test2 addr2} {3 test3 addr3}]
}
Example #4
0
func GetHandler() *genmai.DB {
	db, err := genmai.New(&genmai.SQLite3Dialect{}, DbFile)
	if err != nil {
		panic(err)
	}

	return db
}
Example #5
0
func GetDb() (*genmai.DB, error) {
	url := "root:root@tcp(127.0.0.1:3306)/test?charset=utf8mb4&parseTime=true"
	db, err := genmai.New(&genmai.MySQLDialect{}, url)
	if err != nil {
		return nil, err
	}
	db.SetLogOutput(os.Stdout)
	return db, nil
}
Example #6
0
func ExampleDB_Select_in() {
	db, err := genmai.New(&genmai.SQLite3Dialect{}, ":memory:")
	if err != nil {
		log.Fatal(err)
	}
	var results []TestModel
	// SELECT "test_model".* FROM "test_model" WHERE "id" IN (1, 3, 5);
	if err := db.Select(&results, db.Where("id").In(1, 3, 5)); err != nil {
		log.Fatal(err)
	}
	fmt.Println(results)
}
Example #7
0
func ExampleDB_Select_whereNested() {
	db, err := genmai.New(&genmai.SQLite3Dialect{}, ":memory:")
	if err != nil {
		log.Fatal(err)
	}
	var results []TestModel
	// SELECT "test_model".* FROM "test_model" WHERE "id" = 1 OR ("name" = "alice" AND "addr" != "Tokyo");
	if err := db.Select(&results, db.Where("id", "=", 1).Or(db.Where("name", "=", "alice").And("addr", "!=", "Tokyo"))); err != nil {
		log.Fatal(err)
	}
	fmt.Println(results)
}
Example #8
0
func ExampleDB_Select_columns() {
	db, err := genmai.New(&genmai.SQLite3Dialect{}, ":memory:")
	if err != nil {
		log.Fatal(err)
	}
	var results []TestModel
	// SELECT "test_model"."id", "test_model"."name" FROM "test_model";
	if err := db.Select(&results, []string{"id", "name"}); err != nil {
		log.Fatal(err)
	}
	fmt.Println(results)
}
Example #9
0
func ExampleDB_Select_countDistinct() {
	db, err := genmai.New(&genmai.SQLite3Dialect{}, ":memory:")
	if err != nil {
		log.Fatal(err)
	}
	var result int64
	// SELECT COUNT(DISTINCT "test_model"."name") FROM "test_model";
	if err := db.Select(&result, db.Count(db.Distinct("name")), db.From(TestModel{})); err != nil {
		log.Fatal(err)
	}
	fmt.Println(result)
}
Example #10
0
func ExampleDB_Select_distinct() {
	db, err := genmai.New(&genmai.SQLite3Dialect{}, ":memory:")
	if err != nil {
		log.Fatal(err)
	}
	var results []TestModel
	// SELECT DISTINCT "test_model"."name" FROM "test_model";
	if err := db.Select(&results, db.Distinct("name")); err != nil {
		log.Fatal(err)
	}
	fmt.Println(results)
}
Example #11
0
func ExampleDB_Select_offset() {
	db, err := genmai.New(&genmai.SQLite3Dialect{}, ":memory:")
	if err != nil {
		log.Fatal(err)
	}
	var results []TestModel
	// SELECT "test_model".* FROM "test_model" OFFSET 10;
	if err := db.Select(&results, db.Offset(10)); err != nil {
		log.Fatal(err)
	}
	fmt.Println(results)
}
Example #12
0
func ExampleDB_Select_limit() {
	db, err := genmai.New(&genmai.SQLite3Dialect{}, ":memory:")
	if err != nil {
		log.Fatal(err)
	}
	var results []TestModel
	// SELECT "test_model".* FROM "test_model" LIMIT 3;
	if err := db.Select(&results, db.Limit(3)); err != nil {
		log.Fatal(err)
	}
	fmt.Println(results)
}
Example #13
0
func ExampleDB_Select_orderByMultiple() {
	db, err := genmai.New(&genmai.SQLite3Dialect{}, ":memory:")
	if err != nil {
		log.Fatal(err)
	}
	var results []TestModel
	// SELECT "test_model".* FROM "test_model" ORDER BY "name" DESC, "addr" DESC;
	if err := db.Select(&results, db.OrderBy("name", genmai.DESC, "addr", genmai.DESC)); err != nil {
		log.Fatal(err)
	}
	fmt.Println(results)
}
Example #14
0
func ExampleDB_Select_like() {
	db, err := genmai.New(&genmai.SQLite3Dialect{}, ":memory:")
	if err != nil {
		log.Fatal(err)
	}
	var results []TestModel
	// SELECT "test_model".* FROM "test_model" WHERE "name" LIKE "alice%";
	if err := db.Select(&results, db.Where("name").Like("alice%")); err != nil {
		log.Fatal(err)
	}
	fmt.Println(results)
}
Example #15
0
func ExampleDB_Select_leftJoin() {
	db, err := genmai.New(&genmai.SQLite3Dialect{}, ":memory:")
	if err != nil {
		log.Fatal(err)
	}
	var results []TestModel
	// SELECT "test_model".* FROM "test_model" LEFT JOIN "m2" ON "test_model"."name" = "m2"."body" WHERE "m2"."body" IS NULL;
	if err := db.Select(&results, "name", db.LeftJoin(&M2{}).On("name", "=", "body").Where(&M2{}, "body").IsNull()); err != nil {
		log.Fatal(err)
	}
	fmt.Println(results)
}
func GenmaiRead(id int) {

	connString := "host=192.168.200.10 user=postgres password=postgres dbname=test sslmode=disable"
	db, err := genmai.New(&genmai.PostgresDialect{}, connString)
	checkErr(err)
	defer db.Close()

	var results []Users
	//if err := db.Select(&results); err != nil {
	if err := db.Select(&results, db.Where("id", "=", 1)); err != nil {
		panic(err)
	}
}
Example #17
0
func main() {
	// setup tables
	db, err := genmai.New(&genmai.SQLite3Dialect{}, "./wiki.db")
	if err != nil {
		log.Fatalln(err)
	}
	if err := db.CreateTableIfNotExists(&Page{}); err != nil {
		log.Fatalln(err)
	}

	// setup pongo
	pongo2.DefaultSet.SetBaseDirectory("view")

	wiki := &Wiki{
		URL: "/",
		DB:  db,
	}
	pongo2.Globals["wiki"] = wiki
	pongo2.RegisterFilter("to_localdate", func(in *pongo2.Value, param *pongo2.Value) (out *pongo2.Value, err *pongo2.Error) {
		date, ok := in.Interface().(time.Time)
		if !ok {
			return nil, &pongo2.Error{
				Sender:   "to_localdate",
				ErrorMsg: fmt.Sprintf("Date must be of type time.Time not %T ('%v')", in, in),
			}
		}
		return pongo2.AsValue(date.Local()), nil
	})

	goji.Use(middleware.Recoverer)
	goji.Use(middleware.NoCache)
	goji.Use(func(c *web.C, h http.Handler) http.Handler {
		fn := func(w http.ResponseWriter, r *http.Request) {
			c.Env["Wiki"] = wiki
			h.ServeHTTP(w, r)
		}
		return http.HandlerFunc(fn)

	})

	goji.Get("/assets/*", http.FileServer(http.Dir(".")))
	goji.Get("/", showPages)
	goji.Get("/wiki/:title", showPage)
	goji.Get("/wiki/:title/edit", editPage)
	goji.Post("/wiki/:title", postPage)

	goji.Serve()
}
Example #18
0
func ExampleDB_Select_join() {
	db, err := genmai.New(&genmai.SQLite3Dialect{}, ":memory:")
	if err != nil {
		log.Fatal(err)
	}
	type M2 struct {
		Id   int64
		Body string
	}
	var results []TestModel
	// SELECT "test_model".* FROM "test_model" JOIN "m2" ON "test_model"."id" = "m2"."id";
	if err := db.Select(&results, "name", db.Join(&M2{}).On("id")); err != nil {
		log.Fatal(err)
	}
	fmt.Println(results)
}
Example #19
0
func ExampleDB_Select_complex() {
	db, err := genmai.New(&genmai.SQLite3Dialect{}, ":memory:")
	if err != nil {
		log.Fatal(err)
	}
	var results []TestModel
	// SELECT "test_model"."name" FROM "test_model"
	//   WHERE "name" LIKE "%alice%" OR ("id" > 100 AND "id" < 200) OR ("id" BETWEEN 700 AND 1000)
	//   ORDER BY "id" ASC LIMIT 2 OFFSET 5
	if err := db.Select(&results, "name", db.Where("name").
		Like("%alice%").
		Or(db.Where("id", ">", 100).And("id", "<", 200)).
		Or(db.Where("id").Between(700, 1000)).
		Limit(2).Offset(5).OrderBy("id", genmai.ASC),
	); err != nil {
		log.Fatal(err)
	}
	fmt.Println(results)
}
Example #20
0
func init() {
	for name, dbconf := range DatabaseMap {
		var d genmai.Dialect
		switch dbconf.Driver {
		case "mysql":
			d = &genmai.MySQLDialect{}
		case "postgres":
			d = &genmai.PostgresDialect{}
		case "sqlite3":
			d = &genmai.SQLite3Dialect{}
		default:
			panic(fmt.Errorf("kocha: genmai: unsupported driver type: %v", dbconf.Driver))
		}
		db, err := genmai.New(d, dbconf.DSN)
		if err != nil {
			panic(err)
		}
		dbMap[name] = db
	}
}
Example #21
0
File: db.go Project: woremacx/kocha
// Begin starts a transaction of Genmai.
// If unsupported driver name given or any error occurred, it returns nil and error.
func (t *GenmaiTransaction) Begin(driverName, dsn string) (tx interface{}, err error) {
	var d genmai.Dialect
	switch driverName {
	case "mysql":
		d = &genmai.MySQLDialect{}
	case "postgres":
		d = &genmai.PostgresDialect{}
	case "sqlite3":
		d = &genmai.SQLite3Dialect{}
	default:
		return nil, fmt.Errorf("kocha: migration: genmai: unsupported driver type `%v'", driverName)
	}
	t.tx, err = genmai.New(d, dsn)
	if err != nil {
		return nil, err
	}
	if err := t.tx.Begin(); err != nil {
		return nil, err
	}
	return t.tx, nil
}
Example #22
0
func connect() (*genmai.DB, error) {
	return genmai.New(&genmai.SQLite3Dialect{}, "./development.db")
}