Esempio n. 1
0
func (statement *Statement) Bind(args ...interface{}) error {
	var err C.CassError = C.CASS_OK

	for i, v := range args {

		switch v := v.(type) {

		case nil:
			err = C.cass_statement_bind_null(statement.cptr, C.cass_size_t(i))

		case int32:
			err = C.cass_statement_bind_int32(statement.cptr, C.cass_size_t(i), C.cass_int32_t(v))

		case int64:
			err = C.cass_statement_bind_int64(statement.cptr, C.cass_size_t(i), C.cass_int64_t(v))

		case float32:
			err = C.cass_statement_bind_float(statement.cptr, C.cass_size_t(i), C.cass_float_t(v))

		case float64:
			err = C.cass_statement_bind_double(statement.cptr, C.cass_size_t(i), C.cass_double_t(v))

		case bool:
			if v {
				err = C.cass_statement_bind_bool(statement.cptr, C.cass_size_t(i), 1)
			} else {
				err = C.cass_statement_bind_bool(statement.cptr, C.cass_size_t(i), 0)
			}

		case string:
			var str C.CassString
			str.data = C.CString(v)
			str.length = C.cass_size_t(len(v))
			defer C.free(unsafe.Pointer(str.data))
			err = C.cass_statement_bind_string(statement.cptr, C.cass_size_t(i), str)

		case []byte:
			var bytes C.CassBytes
			bytes.data = (*C.cass_byte_t)(unsafe.Pointer(&v))
			bytes.size = C.cass_size_t(len(v))
			err = C.cass_statement_bind_bytes(statement.cptr, C.cass_size_t(i), bytes)

		case *Uuid:
			C.cass_statement_bind_uuid(statement.cptr, C.cass_size_t(i), *v.cptr)
		}

	}

	if err != C.CASS_OK {
		return errors.New(C.GoString(C.cass_error_desc(err)))
	}

	return nil
}
Esempio n. 2
0
func (result *CassResult) Scan(args ...interface{}) error {

	if result.ColumnCount() != uint64(len(args)) {
		errors.New("invalid argument count")
	}

	row := C.cass_iterator_get_row(result.iter)

	var err C.CassError = C.CASS_OK

	for i, v := range args {
		value := C.cass_row_get_column(row, C.cass_size_t(i))

		switch v := v.(type) {

		case *string:
			var str C.CassString
			err = C.cass_value_get_string(value, &str)
			if err != C.CASS_OK {
				return errors.New(C.GoString(C.cass_error_desc(err)))
			}
			*v = C.GoStringN(str.data, C.int(str.length))

		case *[]byte:
			var b C.CassBytes
			err = C.cass_value_get_bytes(value, &b)
			if err != C.CASS_OK {
				return errors.New(C.GoString(C.cass_error_desc(err)))
			}
			*v = C.GoBytes(unsafe.Pointer(b.data), C.int(b.size))

		case *int32:
			var i32 C.cass_int32_t
			err = C.cass_value_get_int32(value, &i32)
			if err != C.CASS_OK {
				return errors.New(C.GoString(C.cass_error_desc(err)))
			}
			*v = int32(i32)

		case *int64:
			var i64 C.cass_int64_t
			err = C.cass_value_get_int64(value, &i64)
			if err != C.CASS_OK {
				return errors.New(C.GoString(C.cass_error_desc(err)))
			}
			*v = int64(i64)

		case *float32:
			var f32 C.cass_float_t
			err = C.cass_value_get_float(value, &f32)
			if err != C.CASS_OK {
				return errors.New(C.GoString(C.cass_error_desc(err)))
			}
			*v = float32(f32)

		case *float64:
			var f64 C.cass_double_t
			err = C.cass_value_get_double(value, &f64)
			if err != C.CASS_OK {
				return errors.New(C.GoString(C.cass_error_desc(err)))
			}
			*v = float64(f64)

		case *bool:
			var b C.cass_bool_t
			err = C.cass_value_get_bool(value, &b)
			if err != C.CASS_OK {
				return errors.New(C.GoString(C.cass_error_desc(err)))
			}
			*v = bool(b != 0)

		default:
			return errors.New("unsupported type in Scan: " + reflect.TypeOf(v).String())
		}
	}

	return nil
}