Ejemplo n.º 1
0
func TestSelectManagerOrderAscending(t *testing.T) {
	connection, err := db.MySQLNewConnection(DB_SOCK, DB_USER, DB_PASSWD, DB_NAME)

	if err != nil {
		t.Log(err.String())
	}

	table, err := GetTable("Users", connection)
	m1 := NewSelectManagerFromTable(-1, connection, table.Table)
	m2 := m1
	m3 := m1
	m1.Order(table.Field("id").Ascending())
	s := m1.ToSql()
	if s != "SELECT FROM `Users`  ORDER BY `Users`.`id` ASC" {
		t.Log(s)
		t.Errorf("Failed to get Order On for select manager ASC")
	}

	m2.Order([]ast.Node{table.Field("id").Ascending(), ast.NewSqlLiteral("login").Ascending()}...)
	s = m2.ToSql()
	if s != "SELECT FROM `Users`  ORDER BY `Users`.`id` ASC, login ASC" {
		t.Log(s)
		t.Errorf("Failed to get Order For for select manager ASC")
	}

	m3.Order([]ast.Node{table.Field("id").Ascending(), ast.NewSqlLiteral("login").Descending()}...)
	s = m3.ToSql()
	if s != "SELECT FROM `Users`  ORDER BY `Users`.`id` ASC, login DESC" {
		t.Log(s)
		t.Errorf("Failed to get Order For for select manager ASC/DESC")
	}
}
Ejemplo n.º 2
0
func TestSelectManagerAs(t *testing.T) {
	connection, err := db.MySQLNewConnection(DB_SOCK, DB_USER, DB_PASSWD, DB_NAME)

	if err != nil {
		t.Log(err.String())
	}

	table, err := GetTable("Users", connection)
	m1 := NewSelectManagerFromTable(-1, connection, table.Table)
	n := m1.Project(ast.NewSqlLiteral("*")).As("t")

	m2 := NewSelectManager(-1, connection)
	m2.From(n).Project("*")
	s := m2.ToSql()
	if s != "SELECT * FROM (SELECT * FROM `Users` ) `t` " {
		t.Log(s)
		t.Errorf("Failed to get As for select manager")
	}
}
Ejemplo n.º 3
0
func TestSelectManagerGroup(t *testing.T) {
	connection, err := db.MySQLNewConnection(DB_SOCK, DB_USER, DB_PASSWD, DB_NAME)

	if err != nil {
		t.Log(err.String())
	}

	table, err := GetTable("Users", connection)
	m1 := NewSelectManagerFromTable(-1, connection, table.Table)
	m2 := NewSelectManagerFromTable(-1, connection, table.Table)
	m1.Group(table.Field("id"))
	s := m1.ToSql()
	if s != "SELECT FROM `Users`  GROUP BY `Users`.`id`" {
		t.Log(s)
		t.Errorf("Failed to get Order On for select manager")
	}

	m2.Group([]ast.Node{table.Field("id"), ast.NewSqlLiteral("login")}...)
	s = m2.ToSql()
	if s != "SELECT FROM `Users`  GROUP BY `Users`.`id`, login" {
		t.Log(s)
		t.Errorf("Failed to get Order For for select manager")
	}
}
Ejemplo n.º 4
0
func (m *SelectManager) Project(any interface{}) *SelectManager {
	switch val := any.(type) {
	case ast.Node:
		m.project(val)
	case string:
		m.project(ast.NewSqlLiteral(any.(string)))
	case bool:
		m.project(ast.NewSqlLiteral(strconv.Btoa(any.(bool))))
	case int:
		m.project(ast.NewSqlLiteral(strconv.Itoa(any.(int))))
	case int64:
		m.project(ast.NewSqlLiteral(strconv.Itoa64(any.(int64))))
	case float32:
		m.project(ast.NewSqlLiteral(strconv.Ftoa32(any.(float32), 'f', 0)))
	case float64:
		m.project(ast.NewSqlLiteral(strconv.Ftoa64(any.(float64), 'f', 0)))
	}
	return m
}
Ejemplo n.º 5
0
func (m SelectManager) As(alias string) (n ast.Node) {
	grouping := ast.Grouping{ast.Unary{m.Ast}}
	l := ast.NewSqlLiteral(alias)
	n = ast.NewTableAliasByNode(grouping, nil, &l)
	return
}