Beispiel #1
0
// driverArgsToMap turns driver.Value list into a parameter map
// for neo4j parameters
func driverArgsToMap(args []driver.Value) (map[string]interface{}, error) {
	output := map[string]interface{}{}
	for _, arg := range args {
		argBytes, ok := arg.([]byte)
		if !ok {
			return nil, errors.New("You must pass only a gob encoded map to the Exec/Query args")
		}

		m, err := encoding.Unmarshal(argBytes)
		if err != nil {
			return nil, err
		}

		for k, v := range m.(map[string]interface{}) {
			output[k] = v
		}

	}

	return output, nil
}
func main() {

	t := &test{[]byte{1, 2, 3}}
	t1 := &test{}
	fmt.Printf("TEE: %#v\n", t)
	fmt.Printf("TEE1: %#v\n", t1)
	*t1 = *t
	t = nil
	fmt.Printf("TEE: %#v\n", t)
	fmt.Printf("TEE1: %#v\n", t1)

	args := map[string]interface{}{
		"a": 1,
		"b": 34234.34323,
		"c": "string",
		"d": []interface{}{1, 2, 3},
		"e": true,
		"f": nil,
	}

	enc, err := encoding.Marshal(args)
	if err != nil {
		fmt.Printf("MARSHALLERR: %#v\n", err)
	}
	fmt.Printf("ENCODED: %#v\n", enc)

	decoded, err := encoding.Unmarshal(enc)
	if err != nil {
		fmt.Printf("UNMARSHALLERR: %#v\n", err)
	}

	fmt.Printf("DECODED: %#v\n", decoded)

	buf := &bytes.Buffer{}
	err = gob.NewEncoder(buf).Encode(args)
	if err != nil {
		fmt.Printf("GOBERR: %#v", err)
	}

	var y map[string]interface{}
	gob.NewDecoder(buf).Decode(&y)

	fmt.Printf("GOB: %#v\n", y)

	fmt.Printf("LenMake: %d\n", len(make([]interface{}, 15)))

	fmt.Println("test.b:", len(test{}.b))
	buf = bytes.NewBuffer([]byte{byte(0xf0)})
	var x int
	binary.Read(buf, binary.BigEndian, &x)
	fmt.Println("Marker: 0xf0 ", 0xf0, "INT: ", x)

	buf = bytes.NewBuffer([]byte{byte(0xff)})
	binary.Read(buf, binary.BigEndian, &x)
	fmt.Println("Marker: 0xff ", 0xff, "INT: ", x)

	buf = bytes.NewBuffer([]byte{byte(0x7f)})
	binary.Read(buf, binary.BigEndian, &x)
	fmt.Println("Marker: 0x7f ", 0x7f, "INT: ", x)

	buf = &bytes.Buffer{}
	binary.Write(buf, binary.BigEndian, int8(-1))
	fmt.Printf("BYTES: %#x\n", buf.Bytes())
	binary.Write(buf, binary.BigEndian, int8(127))
	fmt.Printf("BYTES: %#x\n", buf.Bytes())
	fmt.Printf("INT1: %#x\n", byte(1))
	fmt.Printf("INT34234234: %#x\n", byte(234))
	fmt.Println("DONE")

	fmt.Printf("MAX INT8: %d\n", math.MaxInt8)
	fmt.Printf("MAX INT16: %d\n", math.MaxInt16)
	fmt.Printf("MAX INT32: %d\n", math.MaxInt32)
	fmt.Printf("MAX INT64: %d\n", math.MaxInt64)

	fmt.Printf("MIN INT8: %d\n", math.MinInt8)
	fmt.Printf("MIN INT16: %d\n", math.MinInt16)
	fmt.Printf("MIN INT32: %d\n", math.MinInt32)
	fmt.Printf("MIN INT64: %d\n", math.MinInt64)

	fmt.Printf("MAX UINT8: %d\n", math.MaxUint8)
	fmt.Printf("MAX UINT16: %d\n", math.MaxUint16)
	fmt.Printf("MAX UINT32: %d\n", math.MaxUint32)
	z := fmt.Sprint(uint64(math.MaxUint64))
	fmt.Printf("MAX UINT64: %s\n", z)

	fmt.Println("Marker: 0xf0 ", 0xf0, "INT: ", int(byte(0xf0)))
	fmt.Println("Marker: 0xf0 byte{}", 0xf0, "INT: ", int(0xf0))
	_ = `b1 71 99 cb  80 0 0 0  0 0 0 0  ca 80 0 0

	 0 c9 80 0  c8 80 f0 7f  c9 7f ff ca  7f ff ff ff

	 cb 7f ff ff  ff ff ff ff  ff`
}
func TestBoltStmt_SqlQueryAndExec(t *testing.T) {
	if neo4jConnStr == "" {
		t.Skip("Cannot run this test when in recording mode")
	}

	db, err := sql.Open("neo4j-bolt", neo4jConnStr)
	if err != nil {
		t.Fatalf("An error occurred opening conn: %s", err)
	}
	defer db.Close()

	args := map[string]interface{}{
		"a": 1,
		"b": 34234.34323,
		"c": "string",
		"d": []interface{}{1, 2, 3},
		"e": true,
		"f": nil,
	}
	arg, err := encoding.Marshal(args)
	if err != nil {
		t.Fatalf("An error occurred marshalling args: %s", err)
	}

	stmt, err := db.Prepare(`CREATE path=(f:FOO {a: {a}, b: {b}, c: {c}, d: {d}, e: {e}, f: {f}})-[b:BAR]->(c:BAZ) RETURN f.a, f.b, f.c, f.d, f.e, f.f, f, b, path`)
	if err != nil {
		t.Fatalf("An error occurred preparing statement: %s", err)
	}
	rows, err := stmt.Query(arg)
	if err != nil {
		t.Fatalf("An error occurred querying statement: %s", err)
	}
	var a int
	var b float64
	var c string
	var d []byte
	var e bool
	var f interface{}
	var node []byte
	var rel []byte
	var path []byte
	if !rows.Next() {
		t.Fatalf("Rows.Next failed")
	}
	err = rows.Scan(&a, &b, &c, &d, &e, &f, &node, &rel, &path)
	if err != nil {
		t.Fatalf("An error occurred scanning row: %s", err)
	}
	defer rows.Close()

	if a != 1 {
		t.Fatalf("Unexpected value for a. Expected: %#v  Got: %#v", 1, a)
	}
	if b != 34234.34323 {
		t.Fatalf("Unexpected value for b. Expected: %#v  Got: %#v", 34234.34323, b)
	}
	if c != "string" {
		t.Fatalf("Unexpected value for c. Expected: %#v  Got: %#v", "string", b)
	}

	dVal, err := encoding.Unmarshal(d)
	if err != nil {
		t.Fatalf("Error occurred decoding item: %s", err)
	}
	if !reflect.DeepEqual(dVal.([]interface{}), []interface{}{int64(1), int64(2), int64(3)}) {
		t.Fatalf("Unexpected value for d. Expected: %#v  Got: %#v", []interface{}{1, 2, 3}, dVal)
	}

	if !e {
		t.Fatalf("Unexpected value for e. Expected: %#v  Got: %#v", true, e)
	}

	if f != nil {
		t.Fatalf("Unexpected value for f. Expected: %#v  Got: %#v", nil, f)
	}

	nodeVal, err := encoding.Unmarshal(node)
	if err != nil {
		t.Fatalf("Error occurred decoding node: %s", err)
	}
	if nodeVal.(graph.Node).Labels[0] != "FOO" {
		t.Fatalf("Unexpected label for node. Expected: %#v  Got: %#v", "FOO", nodeVal)
	}
	if nodeVal.(graph.Node).Properties["a"] != int64(1) {
		t.Fatalf("Unexpected value for node. Expected: %#v  Got: %#v", int64(1), nodeVal)
	}

	relVal, err := encoding.Unmarshal(rel)
	if err != nil {
		t.Fatalf("Error occurred decoding rel: %s", err)
	}
	if relVal.(graph.Relationship).Type != "BAR" {
		t.Fatalf("Unexpected label for node. Expected: %#v  Got: %#v", "FOO", relVal)
	}

	pathVal, err := encoding.Unmarshal(path)
	if err != nil {
		t.Fatalf("Error occurred decoding path: %s", err)
	}
	if pathVal.(graph.Path).Nodes[0].Labels[0] != "FOO" {
		t.Fatalf("Unexpected label for path node 0. Expected: %#v  Got: %#v", "FOO", pathVal)
	}
	if pathVal.(graph.Path).Nodes[1].Labels[0] != "BAZ" {
		t.Fatalf("Unexpected label for path node 1. Expected: %#v  Got: %#v", "BAZ", pathVal)
	}
	if pathVal.(graph.Path).Relationships[0].Type != "BAR" {
		t.Fatalf("Unexpected label for path relationship 1. Expected: %#v  Got: %#v", "BAR", pathVal)
	}
	if pathVal.(graph.Path).Sequence[0] != 1 {
		t.Fatalf("Unexpected label for path sequence 0. Expected: %#v  Got: %#v", 1, pathVal)
	}

	err = stmt.Close()
	if err != nil {
		t.Fatalf("An error occurred closing statement: %s", err)
	}

	stmt, err = db.Prepare(`MATCH (f:FOO)-[b:BAR]->(c:BAZ) DELETE f, b, c`)

	result, err := stmt.Exec()
	if err != nil {
		t.Fatalf("An error occurred preparing delete statement: %s", err)
	}

	affected, err := result.RowsAffected()
	if err != nil {
		t.Fatalf("An error occurred getting affected rows: %s", err)
	}
	if affected != 3 {
		t.Fatalf("Expected to delete 3 items, got %#v", affected)
	}

	err = stmt.Close()
	if err != nil {
		t.Fatalf("An error occurred closing statement: %s", err)
	}

	err = db.Close()
	if err != nil {
		t.Fatalf("Error closing connection: %s", err)
	}
}