瀏覽代碼

Add bigint encoding

Jonathan Rudenberg 13 年之前
父節點
當前提交
74fc692307
共有 2 個文件被更改,包括 49 次插入0 次删除
  1. 8 0
      convert.go
  2. 41 0
      gocql_test.go

+ 8 - 0
convert.go

@@ -76,6 +76,8 @@ func (e *columnEncoder) ColumnConverter(idx int) ValueConverter {
 	switch e.columnTypes[idx] {
 	case typeInt:
 		return ValueConverter(encInt)
+	case typeBigInt:
+		return ValueConverter(encBigInt)
 	case typeFloat:
 		return ValueConverter(encFloat)
 	case typeDouble:
@@ -121,6 +123,12 @@ func encInt(v interface{}) (driver.Value, error) {
 	return b, nil
 }
 
+func encBigInt(v interface{}) (driver.Value, error) {
+	b := make([]byte, 8)
+	binary.BigEndian.PutUint64(b, uint64(v.(int64)))
+	return b, nil
+}
+
 func encVarchar(v interface{}) (driver.Value, error) {
 	x, err := driver.String.ConvertValue(v)
 	if err != nil {

+ 41 - 0
gocql_test.go

@@ -134,3 +134,44 @@ func TestWiki(t *testing.T) {
 	}
 
 }
+
+func TestTypes(t *testing.T) {
+	db, err := sql.Open("gocql", "localhost:9042 compression=snappy")
+	if err != nil {
+		t.Fatal(err)
+	}
+	db.Exec("DROP KEYSPACE gocql_types")
+	if _, err := db.Exec(`CREATE KEYSPACE gocql_types
+	                      WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 }`); err != nil {
+		t.Fatal(err)
+	}
+	if _, err := db.Exec("USE gocql_types"); err != nil {
+		t.Fatal(err)
+	}
+
+	if _, err := db.Exec(`CREATE TABLE stuff (
+        id bigint,
+		foo text,
+        PRIMARY KEY (id)
+        )`); err != nil {
+		t.Fatal(err)
+	}
+
+	id := int64(-1 << 63)
+
+	if _, err := db.Exec(`INSERT INTO stuff (id, foo) VALUES (?, ?);`, id, "test"); err != nil {
+		t.Fatal(err)
+	}
+
+	var rid int64
+
+	row := db.QueryRow(`SELECT id FROM stuff WHERE id = ?`, id)
+
+	if err := row.Scan(&rid); err != nil {
+		t.Error(err)
+	}
+
+	if id != rid {
+		t.Errorf("expected %v got %v", id, rid)
+	}
+}