Exemplo n.º 1
0
func TestParseExpressions(t *testing.T) {

	for _, test := range parseTests {
		exprTree, err := expr.ParseExpression(test.qlText)
		//u.Infof("After Parse:  %v", err)
		switch {
		case err == nil && !test.ok:
			t.Errorf("%q: 1 expected error; got none", test.name)
			continue
		case err != nil && test.ok:
			t.Errorf("%q: 2 unexpected error: %v", test.name, err)
			continue
		case err != nil && !test.ok:
			// expected error, got one
			if *VerboseTests {
				u.Infof("%s: %s\n\t%s", test.name, test.qlText, err)
			}
			continue
		}
		var result string
		result = exprTree.Root.StringAST()
		if result != test.result {
			t.Errorf("reslen: %v vs %v", len(result), len(test.result))
			t.Errorf("\n%s \n\t'%v'\nexpected\n\t'%v'", test.name, result, test.result)
		}
	}
}
Exemplo n.º 2
0
func NewVm(exprText string) (*Vm, error) {
	t, err := expr.ParseExpression(exprText)
	if err != nil {
		return nil, err
	}
	m := &Vm{
		Tree: t,
	}
	return m, nil
}
Exemplo n.º 3
0
func TestNodePb(t *testing.T) {
	t.Parallel()
	for _, exprText := range pbTests {
		et, err := expr.ParseExpression(exprText)
		assert.T(t, err == nil, "Should not error parse expr but got ", err, "for ", exprText)
		pb := et.Root.ToPB()
		assert.Tf(t, pb != nil, "was nil PB: %#v", et.Root)
		pbBytes, err := proto.Marshal(pb)
		assert.Tf(t, err == nil, "Should not error on proto.Marshal but got [%v] for %s pb:%#v", err, exprText, pb)
		n2, err := expr.NodeFromPb(pbBytes)
		assert.T(t, err == nil, "Should not error from pb but got ", err, "for ", exprText)
		assert.T(t, et.Root.Equal(n2), "Equal?")
		u.Infof("pre/post: \n\t%s\n\t%s", et.Root, n2)
	}
}
Exemplo n.º 4
0
// First keyword was SHOW
func (m *Sqlbridge) parseShow() (*SqlShow, error) {

	/*
		don't currently support all these
		http://dev.mysql.com/doc/refman/5.7/en/show.html

		SHOW [FULL] COLUMNS FROM tbl_name [FROM db_name] [like_or_where]
		SHOW CREATE DATABASE db_name
		SHOW CREATE TABLE tbl_name
		SHOW CREATE TRIGGER trigger_name
		SHOW CREATE VIEW view_name
		SHOW DATABASES [like_or_where]
		SHOW ENGINE engine_name {STATUS | MUTEX}
		SHOW [STORAGE] ENGINES
		SHOW INDEX FROM tbl_name [FROM db_name]
		SHOW [FULL] TABLES [FROM db_name] [like_or_where]
		SHOW TRIGGERS [FROM db_name] [like_or_where]
		SHOW [GLOBAL | SESSION] VARIABLES [like_or_where]
		SHOW WARNINGS [LIMIT [offset,] row_count]
	*/
	likeLhs := "Table"
	req := &SqlShow{}
	req.Raw = m.l.RawInput()
	m.Next() // Consume Show

	//u.Infof("cur: %v", m.Cur())
	switch strings.ToLower(m.Cur().V) {
	case "full":
		req.Full = true
		m.Next()
	case "global", "session":
		req.Scope = strings.ToLower(m.Next().V)
		//u.Infof("scope:%q   next:%v", req.Scope, m.Cur())
	case "create":
		// SHOW CREATE TABLE `temp_schema`.`users`
		req.ShowType = "create"
		m.Next() // consume create
		req.Create = true
		//u.Debugf("create what %v", m.Cur())
		req.CreateWhat = m.Next().V // {TABLE | DATABASE | EVENT ...}
		//u.Debugf("create which %v", m.Cur())
		if m.Cur().T == lex.TokenIdentity {
			req.Identity = m.Next().V
			return req, nil
		}
		return nil, fmt.Errorf("Expected IDENTITY for SHOW CREATE {TABLE | DATABASE | EVENT} IDENTITY but got %s", m.Cur())
	}

	//u.Debugf("show %v", m.Cur())
	objectType := strings.ToLower(m.Cur().V)
	switch objectType {
	case "databases":
		req.ShowType = "databases"
		m.Next()
	case "variables":
		req.ShowType = "variables"
		likeLhs = "Variable_name"
		m.Next()
	case "columns":
		m.Next() // consume columns
		likeLhs = "Field"
		req.ShowType = "columns"
		//SHOW [FULL] COLUMNS {FROM | IN} tbl_name [{FROM | IN} db_name]  [LIKE 'pattern' | WHERE expr]
		// | Field      | Type     | Null | Key | Default | Extra          |
		if err := m.parseShowFromTable(req); err != nil {
			return nil, err
		}
		if err := m.parseShowFromDatabase(req); err != nil {
			return nil, err
		}
	case "tables":
		req.ShowType = objectType
		m.Next() // consume Tables
		// SHOW [FULL] TABLES [FROM db_name] [like_or_where]
		if err := m.parseShowFromDatabase(req); err != nil {
			return nil, err
		}
	}

	//u.Debugf("show %v", m.Cur())
	switch m.Cur().T {
	case lex.TokenEOF, lex.TokenEOS:
		return req, nil
	case lex.TokenLike:
		// SHOW TABLES LIKE '%'
		//u.Debugf("doing Like: %v %v", m.Cur(), m.Peek())
		m.Next() // Consume Like
		ex, err := expr.ParseExpression(fmt.Sprintf("%s LIKE %q", likeLhs, m.Cur().V))
		m.Next()
		if err != nil {
			u.Errorf("Error parsing fake expression: %v", err)
		} else {
			req.Like = ex.Root
		}
		//u.Debugf("doing Like: %v %v", m.Cur(), m.Peek())
	case lex.TokenWhere:
		m.Next() // consume where
		//u.Debugf("doing where: %v %v", m.Cur(), m.Peek())
		tree := expr.NewTreeFuncs(m.SqlTokenPager, m.funcs)
		if err := m.parseNode(tree); err != nil {
			u.Errorf("could not parse: %v", err)
			return nil, err
		}
		req.Where = tree.Root
	}

	return req, nil
}