示例#1
0
文件: odbc.go 项目: bwheatley/go-odbc
func (stmt *Statement) GetField(field_index int) (v interface{}, ftype int, flen int, err *ODBCError) {
	var field_type C.SQLINTEGER
	var field_len C.SQLINTEGER
	var ll C.SQLSMALLINT
	ret := C.SQLColAttribute(C.SQLHSTMT(stmt.handle), C.SQLUSMALLINT(field_index+1), C.SQL_DESC_CONCISE_TYPE, C.SQLPOINTER(unsafe.Pointer(uintptr(0))), C.SQLSMALLINT(0), &ll, &field_type)
	if !Success(ret) {
		debugPrint("GetFiled type Error")
	}
	ret = C.SQLColAttribute(C.SQLHSTMT(stmt.handle), C.SQLUSMALLINT(field_index+1), C.SQL_DESC_LENGTH, C.SQLPOINTER(unsafe.Pointer(uintptr(0))), C.SQLSMALLINT(0), &ll, &field_len)
	if !Success(ret) {
		debugPrint("GetFiled len Error")
	}
	var fl C.SQLLEN = C.SQLLEN(field_len)
	switch int(field_type) {
	case C.SQL_BIT, C.SQL_INTEGER, C.SQL_SMALLINT, C.SQL_TINYINT:
		var value C.HANDLE
		ret = C.SQLGetData(C.SQLHSTMT(stmt.handle), C.SQLUSMALLINT(field_index+1), C.SQL_C_LONG, C.SQLPOINTER(unsafe.Pointer(&value)), 0, &fl)
		if fl == -1 {
			v = nil
		} else {
			v = value
		}
	case C.SQL_FLOAT, C.SQL_REAL:
		var value C.double
		ret = C.SQLGetData(C.SQLHSTMT(stmt.handle), C.SQLUSMALLINT(field_index+1), C.SQL_C_DOUBLE, C.SQLPOINTER(unsafe.Pointer(&value)), 0, &fl)
		if fl == -1 {
			v = nil
		} else {
			v = value
		}
	case C.SQL_WCHAR, C.SQL_WVARCHAR, C.SQL_WLONGVARCHAR:
		value := make([]uint16, int(field_len)+8)
		ret = C.SQLGetData(C.SQLHSTMT(stmt.handle), C.SQLUSMALLINT(field_index+1), C.SQL_C_WCHAR, C.SQLPOINTER(unsafe.Pointer(&value[0])), C.SQLINTEGER(int(field_len)+4), &fl)
		s := UTF16ToString(value)
		v = s
	default:
		value := make([]byte, int(fl)+2)
		ret = C.SQLGetData(C.SQLHSTMT(stmt.handle), C.SQLUSMALLINT(field_index+1), C.SQL_C_CHAR, C.SQLPOINTER(unsafe.Pointer(&value[0])), C.SQLINTEGER(int(field_len)+4), &fl)
		s := string(value[0:])
		v = s
		debugPrint("default type", value, fl, s)
	}
	if !Success(ret) {
		err = FormatError(C.SQL_HANDLE_STMT, stmt.handle, int(ret))
		debugPrint("GetFiled Data Error", err)
	}
	return v, int(field_type), int(fl), err
}
示例#2
0
func SQLGetData(statementHandle SQLHSTMT, colOrParamNum SQLUSMALLINT, targetType SQLSMALLINT, targetValuePtr SQLPOINTER, bufferLength SQLLEN, vallen *SQLLEN) (ret SQLRETURN) {
	r := C.SQLGetData(C.SQLHSTMT(statementHandle), C.SQLUSMALLINT(colOrParamNum), C.SQLSMALLINT(targetType), C.SQLPOINTER(targetValuePtr), C.SQLLEN(bufferLength), (*C.SQLLEN)(vallen))
	return SQLRETURN(r)
}
示例#3
0
func (stmt *Statement) GetField(field_index int) (v interface{}, ftype int, flen int, err *ODBCError) {
	var field_type C.int
	var field_len C.SQLLEN
	var ll C.SQLSMALLINT
	ret := C._SQLColAttribute(C.SQLHSTMT(stmt.handle), C.SQLUSMALLINT(field_index+1), C.SQL_DESC_CONCISE_TYPE, C.SQLPOINTER(unsafe.Pointer(uintptr(0))), C.SQLSMALLINT(0), &ll, unsafe.Pointer(&field_type))
	if !Success(ret) {
		// TODO return err
	}
	ret = C._SQLColAttribute(C.SQLHSTMT(stmt.handle), C.SQLUSMALLINT(field_index+1), C.SQL_DESC_LENGTH, C.SQLPOINTER(unsafe.Pointer(uintptr(0))), C.SQLSMALLINT(0), &ll, unsafe.Pointer(&field_len))
	if !Success(ret) {
		// TODO return err
	}
	var fl C.SQLLEN = C.SQLLEN(field_len)
	switch int(field_type) {
	case C.SQL_BIT:
		var value C.BYTE
		ret = C.SQLGetData(C.SQLHSTMT(stmt.handle), C.SQLUSMALLINT(field_index+1), C.SQL_C_BIT, C.SQLPOINTER(unsafe.Pointer(&value)), 0, &fl)
		if fl == -1 {
			v = nil
		} else {
			v = byte(value)
		}
	case C.SQL_INTEGER, C.SQL_SMALLINT, C.SQL_TINYINT:
		var value C.long
		ret = C.SQLGetData(C.SQLHSTMT(stmt.handle), C.SQLUSMALLINT(field_index+1), C.SQL_C_LONG, C.SQLPOINTER(unsafe.Pointer(&value)), 0, &fl)
		if fl == -1 {
			v = nil
		} else {
			v = int(value)
		}
	case C.SQL_BIGINT:
		var value C.longlong
		ret = C.SQLGetData(C.SQLHSTMT(stmt.handle), C.SQLUSMALLINT(field_index+1), C.SQL_C_SBIGINT, C.SQLPOINTER(unsafe.Pointer(&value)), 0, &fl)
		if fl == -1 {
			v = nil
		} else {
			v = int64(value)
		}
	case C.SQL_FLOAT, C.SQL_REAL, C.SQL_DOUBLE:
		var value C.double
		ret = C.SQLGetData(C.SQLHSTMT(stmt.handle), C.SQLUSMALLINT(field_index+1), C.SQL_C_DOUBLE, C.SQLPOINTER(unsafe.Pointer(&value)), 0, &fl)
		if fl == -1 {
			v = nil
		} else {
			v = float64(value)
		}
	case C.SQL_CHAR, C.SQL_VARCHAR, C.SQL_LONGVARCHAR, C.SQL_WCHAR, C.SQL_WVARCHAR, C.SQL_WLONGVARCHAR:
		value := make([]uint16, int(field_len)+8)
		ret = C.SQLGetData(C.SQLHSTMT(stmt.handle), C.SQLUSMALLINT(field_index+1), C.SQL_C_WCHAR, C.SQLPOINTER(unsafe.Pointer(&value[0])), field_len+4, &fl)
		s := UTF16ToString(value)
		v = s
	case C.SQL_TYPE_TIMESTAMP, C.SQL_TYPE_DATE, C.SQL_TYPE_TIME, C.SQL_DATETIME:
		var value C.TIMESTAMP_STRUCT
		ret = C.SQLGetData(C.SQLHSTMT(stmt.handle), C.SQLUSMALLINT(field_index+1), C.SQL_C_TYPE_TIMESTAMP, C.SQLPOINTER(unsafe.Pointer(&value)), C.SQLLEN(unsafe.Sizeof(value)), &fl)
		if fl == -1 {
			v = nil
		} else {
			v = time.Date(int(value.year), time.Month(value.month), int(value.day), int(value.hour), int(value.minute), int(value.second), int(value.fraction), time.UTC)
		}
	case C.SQL_BINARY, C.SQL_VARBINARY, C.SQL_LONGVARBINARY:
		var vv int
		ret = C.SQLGetData(C.SQLHSTMT(stmt.handle), C.SQLUSMALLINT(field_index+1), C.SQL_C_BINARY, C.SQLPOINTER(unsafe.Pointer(&vv)), 0, &fl)
		if fl == -1 {
			v = nil
		} else {
			value := make([]byte, fl)
			ret = C.SQLGetData(C.SQLHSTMT(stmt.handle), C.SQLUSMALLINT(field_index+1), C.SQL_C_BINARY, C.SQLPOINTER(unsafe.Pointer(&value[0])), C.SQLLEN(fl), &fl)
			v = value
		}
	default:
		value := make([]byte, field_len)
		ret = C.SQLGetData(C.SQLHSTMT(stmt.handle), C.SQLUSMALLINT(field_index+1), C.SQL_C_BINARY, C.SQLPOINTER(unsafe.Pointer(&value[0])), field_len, &fl)
		v = value
	}
	if !Success(ret) {
		err = FormatError(C.SQL_HANDLE_STMT, stmt.handle)
	}
	return v, int(field_type), int(fl), err
}