Example #1
0
func (stmt *statement) bindByteArray(index int, value []byte, direction ParameterDirection) error {
	// Store both value and lenght, because we need a pointer to the lenght in
	// the last parameter of SQLBindParamter. Otherwise the data is assumed to
	// be a null terminated string.
	bindVal := &struct {
		value  []byte
		length int
	}{
		value,
		len(value),
	}
	sqlType := odbc.SQL_VARBINARY
	if bindVal.length > 4000 {
		sqlType = odbc.SQL_LONGVARBINARY
	}

	// Protect against index out of range on &bindVal.value[0] when value is zero-length.
	// We can't pass NULL to SQLBindParameter so this is needed, it will still
	// write a zero length value to the database since the length parameter is
	// zero.
	if bindVal.length == 0 {
		bindVal.value = []byte{'\x00'}
	}

	ret := odbc.SQLBindParameter(stmt.handle, odbc.SQLUSMALLINT(index), direction.SQLBindParameterType(), odbc.SQL_C_BINARY, sqlType, odbc.SQLULEN(bindVal.length), 0, odbc.SQLPOINTER(unsafe.Pointer(&bindVal.value[0])), 0, (*odbc.SQLLEN)(unsafe.Pointer(&bindVal.length)))
	if isError(ret) {
		return errorStatement(stmt.handle, fmt.Sprintf("Bind index: %v, Value: %v", index, value))
	}
	return nil
}
Example #2
0
func (stmt *statement) bindString(index int, value string, length int, direction ParameterDirection) error {
	if length == 0 {
		length = len(value)
	}
	stmt.bindValues[index] = syscall.StringToUTF16(value)
	var sqlType odbc.SQLDataType
	if length < 4000 {
		sqlType = odbc.SQL_VARCHAR
	} else {
		sqlType = odbc.SQL_LONGVARCHAR
	}
	ret := odbc.SQLBindParameter(stmt.handle, odbc.SQLUSMALLINT(index), direction.SQLBindParameterType(), odbc.SQL_C_WCHAR, sqlType, odbc.SQLULEN(length), 0, odbc.SQLPOINTER(unsafe.Pointer(&stmt.bindValues[index].([]uint16)[0])), 0, nil)
	if isError(ret) {
		return errorStatement(stmt.handle, fmt.Sprintf("Bind index: %v, Value: %v", index, value))
	}
	return nil
}