Пример #1
0
func TestQuery(t *testing.T) {
	bv := make(map[string]interface{})
	bv["foo"] = int64(20)
	in := &Query{"abcd", bv, 24, 0, 0}
	encoded := bytes.NewBuffer(make([]byte, 0, 8))
	in.MarshalBson(encoded)
	expected, _ := bson.Marshal(in)
	compare(t, encoded.Bytes(), expected)

	var ret Query
	ret.UnmarshalBson(encoded)
	assertTrue(ret.Sql == in.Sql, "Sql", t)
	assertTrue(ret.BindVariables["foo"] == in.BindVariables["foo"], "bind vars", t)
}
Пример #2
0
func TestQueryResult(t *testing.T) {
	fields := make([]mysql.Field, 2)
	fields[0] = mysql.Field{"name0", 0}
	fields[1] = mysql.Field{"name1", 1}

	rows := make([][]interface{}, 1)
	rows[0] = make([]interface{}, 2)
	rows[0][0] = "val0"
	rows[0][1] = "val1"
	in := &QueryResult{fields, 5, 0, rows}
	encoded := bytes.NewBuffer(make([]byte, 0, 8))
	in.MarshalBson(encoded)
	expected, _ := bson.Marshal(in)
	compare(t, encoded.Bytes(), expected)

	var ret QueryResult
	ret.UnmarshalBson(encoded)
	assertTrue(ret.Fields[1].Name == in.Fields[1].Name, "fields", t)
	assertTrue(ret.Rows[0][1] == in.Rows[0][1], "rows", t)
}
Пример #3
0
func main() {
	infile := flag.String("input", "", "input file name")
	outfile := flag.String("output", "", "output file name (bson)")
	flag.Parse()
	outfd, err := os.Create(*outfile)
	if err != nil {
		panic(fmt.Sprintf("Could not open file %s", *outfile))
	}
	defer outfd.Close()
	lineno := 0
	skipLines := false
	for sql := range iterateFile(*infile) {
		lineno++
		// skip admin user actions
		lstr := strings.ToLower(sql)
		if strings.HasPrefix(lstr, "use") {
			if strings.HasPrefix(lstr, "use admin") {
				skipLines = true
			} else {
				skipLines = false
			}
		}
		if skipLines {
			continue
		}
		if newsql, bvars, err := Normalize(sql); err != nil {
			fmt.Printf("Line %d: Error: %v\n", lineno, err)
		} else {
			if newsql == "" {
				continue
			}
			nq := &NormalizedQuery{[]byte(newsql), bvars, lineno}
			if data, err := bson.Marshal(nq); err != nil {
				fmt.Printf("Line %d: Error: %v\n", lineno, err)
			} else {
				outfd.Write(data)
			}
		}
	}
}
func (m VitessBsonSerializer) Marshal(o interface{}) []byte {
	d, _ := vitessbson.Marshal(o)
	return d
}