예제 #1
0
파일: table_test.go 프로젝트: rjp/crud
func TestNewTableQuery(t *testing.T) {
	id, err := sql.NewOptions("name=id type=int(11) auto-increment=100 unique unsigned primary-key")
	assert.Nil(t, err)

	key, err := sql.NewOptions("varchar(30) required default='yolo' name=key")
	assert.Nil(t, err)

	bio, err := sql.NewOptions("text name=bio")
	assert.Nil(t, err)

	ignored, err := sql.NewOptions("-")
	assert.Nil(t, err)

	query := sql.NewTableQuery("user", []*sql.Options{id, key, bio, ignored}, false)
	lines := strings.Split(query, "\n")
	assert.Equal(t, len(lines), 6)
	assert.Equal(t, lines[0], "CREATE TABLE `user` (")
	assert.Equal(t, lines[1], "  `id` int(11) UNSIGNED UNIQUE AUTO_INCREMENT,")
	assert.Equal(t, lines[2], "  `key` varchar(30) NOT NULL DEFAULT 'yolo',")
	assert.Equal(t, lines[3], "  `bio` text,")
	assert.Equal(t, lines[4], "  PRIMARY KEY (`id`)")
	assert.Equal(t, lines[5], ") AUTO_INCREMENT=100;")

	query = sql.NewTableQuery("user", []*sql.Options{id, key, bio, ignored}, true)
	lines = strings.Split(query, "\n")
	assert.Equal(t, lines[0], "CREATE TABLE IF NOT EXISTS `user` (")
}
예제 #2
0
파일: db.go 프로젝트: rjp/crud
func (db *DB) CreateTable(st interface{}, ifexists bool) error {
	t, err := NewTable(st)
	if err != nil {
		return err
	}

	_, err = db.Exec(sql.NewTableQuery(t.SQLName, t.SQLOptions(), ifexists))
	return err
}