Example #1
0
func DoTestForeignKey(assert *assrt.Assert, info dialectInfo) {
	mg, q := setupDb(assert, info)
	defer mg.Close()
	defer q.Close()
	type user struct {
		Id   int64
		Name string
	}
	type post struct {
		Id       int64
		Title    string
		AuthorId int64
		Author   *user
	}
	aUser := &user{
		Name: "john",
	}
	aPost := &post{
		Title: "A Title",
	}
	mg.dropTableIfExists(aPost)
	mg.dropTableIfExists(aUser)
	mg.CreateTableIfNotExists(aUser)
	mg.CreateTableIfNotExists(aPost)

	affected, err := q.Save(aUser)
	assert.Nil(err)
	aPost.AuthorId = int64(aUser.Id)
	affected, err = q.Save(aPost)
	assert.Equal(1, affected)
	pst := new(post)
	pst.Id = aPost.Id
	err = q.Find(pst)
	assert.MustNil(err)
	assert.Equal(aPost.Id, pst.Id)
	assert.Equal("john", pst.Author.Name)

	pst.Author = nil
	err = q.OmitFields("Author").Find(pst)
	assert.MustNil(err)
	assert.MustNil(pst.Author)

	err = q.OmitJoin().Find(pst)
	assert.MustNil(err)
	assert.MustNil(pst.Author)

	var psts []*post
	err = q.FindAll(&psts)
	assert.MustNil(err)
	assert.OneLen(psts)
	assert.Equal("john", psts[0].Author.Name)
}
Example #2
0
func DoTestCreateTable(assert *assrt.Assert, info dialectInfo) {
	assert.Logf("Dialect %T\n", info.dialect)
	mg, _ := setupDb(assert, info)
	{
		type AddColumn struct {
			Prim Id
		}
		table := &AddColumn{}
		mg.dropTableIfExists(table)
		mg.CreateTableIfNotExists(table)
		columns := mg.Dialect.ColumnsInTable(mg, table)
		assert.OneLen(columns)
		assert.True(columns["prim"])
	}
	table := &AddColumn{}
	mg.CreateTableIfNotExists(table)
	columns := mg.Dialect.ColumnsInTable(mg, table)
	assert.Equal(4, len(columns))
}
Example #3
0
func DoTestCreateTable(assert *assrt.Assert, info dialectInfo) {
	assert.Logf("Dialect %T\n", info.dialect)
	mg, _ := setupDb(assert, info)
	defer mg.Close()
	{
		type AddColumn struct {
			Prim int64 `qbs:"pk"`
		}
		table := &AddColumn{}
		mg.dropTableIfExists(table)
		mg.CreateTableIfNotExists(table)
		columns := mg.Dialect.ColumnsInTable(mg, table)
		assert.OneLen(columns)
		assert.True(columns["prim"])
	}
	table := &AddColumn{}
	mg.CreateTableIfNotExists(table)
	assert.True(mg.Dialect.IndexExists(mg, "add_column", "add_column_first_last"))
	columns := mg.Dialect.ColumnsInTable(mg, table)
	assert.Equal(4, len(columns))
}