Esempio 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")
	}
}
Esempio n. 2
0
func TestPredicationsEq(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)

	n := table.Field("id").Eq(&ast.Literal{2})

	v := ast.ToSql{connection}
	s := v.GetEquality(n.(ast.Equality))
	if s != "`Users`.`id` = 2" {
		t.Log(s)
		t.Errorf("Failed to get predication Equality")
	}
}
Esempio n. 3
0
func TestSelectManagerJoin(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)
	t2 := table.Alias()
	m1.Join(t2).Project("*")

	s := m1.ToSql()
	if s != "SELECT * FROM `Users` INNER JOIN `Users` `Users_1` " {
		t.Log(s)
		t.Errorf("Failed to get Join for select manager")
	}
}
Esempio n. 4
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")
	}
}
Esempio n. 5
0
func TestSelectManagerLimit(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)
	m1.Limit(7)
	s := m1.ToSql()
	if s != "SELECT FROM `Users`  LIMIT 7" {
		t.Log(s)
		t.Errorf("Failed to get Offset for select manager")
	}

	m1.RemoveLimit()
	s = m1.ToSql()
	if s != "SELECT FROM `Users` " {
		t.Log(s)
		t.Errorf("Failed to get Offset for select manager")
	}
}
Esempio n. 6
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")
	}
}