Esempio n. 1
0
func (bnd *bndStringSlice) bind(values []string, nullInds []C.sb2, position int, stmt *Stmt) (err error) {
	bnd.stmt = stmt
	if nullInds == nil {
		nullInds = make([]C.sb2, len(values))
	}
	alenp := make([]C.ACTUAL_LENGTH_TYPE, len(values))
	rcodep := make([]C.ub2, len(values))
	var maxLen int
	for _, str := range values {
		strLen := len(str)
		if strLen > maxLen {
			maxLen = strLen
		}
	}
	for m := 0; m < len(values); m++ {
		_, err = bnd.buf.WriteString(values[m])
		if err != nil {
			return err
		}
		// pad to make equal to max len if necessary
		padLen := maxLen - len(values[m])
		for n := 0; n < padLen; n++ {
			_, err = bnd.buf.WriteRune('0')
			if err != nil {
				return err
			}
		}
		alenp[m] = C.ACTUAL_LENGTH_TYPE(len(values[m]))
	}
	bnd.bytes = bnd.buf.Bytes()
	r := C.OCIBINDBYPOS(
		bnd.stmt.ocistmt,              //OCIStmt      *stmtp,
		(**C.OCIBind)(&bnd.ocibnd),    //OCIBind      **bindpp,
		bnd.stmt.ses.srv.env.ocierr,   //OCIError     *errhp,
		C.ub4(position),               //ub4          position,
		unsafe.Pointer(&bnd.bytes[0]), //void         *valuep,
		C.LENGTH_TYPE(maxLen),         //sb8          value_sz,
		C.SQLT_CHR,                    //ub2          dty,
		unsafe.Pointer(&nullInds[0]),  //void         *indp,
		&alenp[0],                     //ub4          *alenp,
		&rcodep[0],                    //ub2          *rcodep,
		0,                             //ub4          maxarr_len,
		nil,                           //ub4          *curelep,
		C.OCI_DEFAULT)                 //ub4          mode );
	if r == C.OCI_ERROR {
		return bnd.stmt.ses.srv.env.ociError()
	}
	r = C.OCIBindArrayOfStruct(
		bnd.ocibnd,
		bnd.stmt.ses.srv.env.ocierr,
		C.ub4(maxLen),       //ub4         pvskip,
		C.ub4(C.sizeof_sb2), //ub4         indskip,
		C.ub4(C.sizeof_ub4), //ub4         alskip,
		C.ub4(C.sizeof_ub2)) //ub4         rcskip
	if r == C.OCI_ERROR {
		return bnd.stmt.ses.srv.env.ociError()
	}
	return nil
}
Esempio n. 2
0
func (bnd *bndBinSlice) bind(values [][]byte, nullInds []C.sb2, position int, lobBufferSize int, stmt *Stmt) error {
	bnd.stmt = stmt
	if nullInds == nil {
		nullInds = make([]C.sb2, len(values))
	}
	alenp := make([]C.ACTUAL_LENGTH_TYPE, len(values))
	rcodep := make([]C.ub2, len(values))
	var maxLen int
	for _, b := range values {
		if len(b) > maxLen {
			maxLen = len(b)
		}
	}
	n := maxLen * len(values)
	if cap(bnd.buf) < n {
		bnd.buf = make([]byte, n)
	} else {
		bnd.buf = bnd.buf[:n]
		// reset buffer
		for i := range bnd.buf {
			bnd.buf[i] = 0
		}
	}
	for i, b := range values {
		copy(bnd.buf[i*maxLen:], b)
		alenp[i] = C.ACTUAL_LENGTH_TYPE(len(b))
	}
	r := C.OCIBINDBYPOS(
		bnd.stmt.ocistmt,             //OCIStmt      *stmtp,
		(**C.OCIBind)(&bnd.ocibnd),   //OCIBind      **bindpp,
		bnd.stmt.ses.srv.env.ocierr,  //OCIError     *errhp,
		C.ub4(position),              //ub4          position,
		unsafe.Pointer(&bnd.buf[0]),  //void         *valuep,
		C.LENGTH_TYPE(maxLen),        //sb8          value_sz,
		C.SQLT_LBI,                   //ub2          dty,
		unsafe.Pointer(&nullInds[0]), //void         *indp,
		&alenp[0],                    //ub4          *alenp,
		&rcodep[0],                   //ub2          *rcodep,
		0,                            //ub4          maxarr_len,
		nil,                          //ub4          *curelep,
		C.OCI_DEFAULT)                //ub4          mode );
	if r == C.OCI_ERROR {
		return bnd.stmt.ses.srv.env.ociError()
	}
	r = C.OCIBindArrayOfStruct(
		bnd.ocibnd,
		bnd.stmt.ses.srv.env.ocierr,
		C.ub4(maxLen),       //ub4         pvskip,
		C.ub4(C.sizeof_sb2), //ub4         indskip,
		C.ub4(C.sizeof_ub4), //ub4         alskip,
		C.ub4(C.sizeof_ub2)) //ub4         rcskip
	if r == C.OCI_ERROR {
		return bnd.stmt.ses.srv.env.ociError()
	}
	return nil
}
Esempio n. 3
0
func (bnd *bndBoolSlice) bind(values []bool, nullInds []C.sb2, position int, falseRune rune, trueRune rune, stmt *Stmt) (err error) {
	bnd.stmt = stmt
	if nullInds == nil {
		nullInds = make([]C.sb2, len(values))
	}
	alenp := make([]C.ACTUAL_LENGTH_TYPE, len(values))
	rcodep := make([]C.ub2, len(values))
	var maxLen int = 1
	for n, bValue := range values {
		if bValue {
			_, err = bnd.buf.WriteRune(trueRune)
			if err != nil {
				return err
			}
		} else {
			_, err = bnd.buf.WriteRune(falseRune)
			if err != nil {
				return err
			}
		}
		alenp[n] = 1
	}
	bnd.bytes = bnd.buf.Bytes()

	r := C.OCIBINDBYPOS(
		bnd.stmt.ocistmt,              //OCIStmt      *stmtp,
		(**C.OCIBind)(&bnd.ocibnd),    //OCIBind      **bindpp,
		bnd.stmt.ses.srv.env.ocierr,   //OCIError     *errhp,
		C.ub4(position),               //ub4          position,
		unsafe.Pointer(&bnd.bytes[0]), //void         *valuep,
		C.LENGTH_TYPE(maxLen),         //sb8          value_sz,
		C.SQLT_CHR,                    //ub2          dty,
		unsafe.Pointer(&nullInds[0]),  //void         *indp,
		&alenp[0],                     //ub4          *alenp,
		&rcodep[0],                    //ub2          *rcodep,
		0,                             //ub4          maxarr_len,
		nil,                           //ub4          *curelep,
		C.OCI_DEFAULT)                 //ub4          mode );
	if r == C.OCI_ERROR {
		return bnd.stmt.ses.srv.env.ociError()
	}

	r = C.OCIBindArrayOfStruct(
		bnd.ocibnd,                  //OCIBind     *bindp,
		bnd.stmt.ses.srv.env.ocierr, //OCIError    *errhp,
		C.ub4(maxLen),               //ub4         pvskip,
		C.ub4(C.sizeof_sb2),         //ub4         indskip,
		C.ub4(C.sizeof_ub4),         //ub4         alskip,
		C.ub4(C.sizeof_ub2))         //ub4         rcskip
	if r == C.OCI_ERROR {
		return bnd.stmt.ses.srv.env.ociError()
	}

	return nil
}
Esempio n. 4
0
func (bnd *bndInt16Slice) bind(values []int16, nullInds []C.sb2, position int, stmt *Stmt) error {
	bnd.stmt = stmt
	if nullInds == nil {
		nullInds = make([]C.sb2, len(values))
	}
	alenp := make([]C.ACTUAL_LENGTH_TYPE, len(values))
	rcodep := make([]C.ub2, len(values))
	bnd.ociNumbers = make([]C.OCINumber, len(values))
	for n := range values {
		alenp[n] = C.ACTUAL_LENGTH_TYPE(C.sizeof_OCINumber)
		r := C.OCINumberFromInt(
			bnd.stmt.ses.srv.env.ocierr, //OCIError            *err,
			unsafe.Pointer(&values[n]),  //const void          *inum,
			2,                   //uword               inum_length,
			C.OCI_NUMBER_SIGNED, //uword               inum_s_flag,
			&bnd.ociNumbers[n])  //OCINumber           *number );
		if r == C.OCI_ERROR {
			return bnd.stmt.ses.srv.env.ociError()
		}
	}
	r := C.OCIBINDBYPOS(
		bnd.stmt.ocistmt,                   //OCIStmt      *stmtp,
		(**C.OCIBind)(&bnd.ocibnd),         //OCIBind      **bindpp,
		bnd.stmt.ses.srv.env.ocierr,        //OCIError     *errhp,
		C.ub4(position),                    //ub4          position,
		unsafe.Pointer(&bnd.ociNumbers[0]), //void         *valuep,
		C.LENGTH_TYPE(C.sizeof_OCINumber),  //sb8          value_sz,
		C.SQLT_VNU,                         //ub2          dty,
		unsafe.Pointer(&nullInds[0]),       //void         *indp,
		&alenp[0],                          //ub4          *alenp,
		&rcodep[0],                         //ub2          *rcodep,
		0,                                  //ub4          maxarr_len,
		nil,                                //ub4          *curelep,
		C.OCI_DEFAULT)                      //ub4          mode );
	if r == C.OCI_ERROR {
		return bnd.stmt.ses.srv.env.ociError()
	}
	r = C.OCIBindArrayOfStruct(
		bnd.ocibnd,
		bnd.stmt.ses.srv.env.ocierr,
		C.ub4(C.sizeof_OCINumber), //ub4         pvskip,
		C.ub4(C.sizeof_sb2),       //ub4         indskip,
		C.ub4(C.sizeof_ub4),       //ub4         alskip,
		C.ub4(C.sizeof_ub2))       //ub4         rcskip
	if r == C.OCI_ERROR {
		return bnd.stmt.ses.srv.env.ociError()
	}
	return nil
}
Esempio n. 5
0
func (bnd *bndIntervalDSSlice) bind(values []IntervalDS, position int, stmt *Stmt) error {
	bnd.stmt = stmt
	bnd.ociIntervals = make([]*C.OCIInterval, len(values))
	nullInds := make([]C.sb2, len(values))
	alenp := make([]C.ACTUAL_LENGTH_TYPE, len(values))
	rcodep := make([]C.ub2, len(values))
	for n, value := range values {
		r := C.OCIDescriptorAlloc(
			unsafe.Pointer(bnd.stmt.ses.srv.env.ocienv),             //CONST dvoid   *parenth,
			(*unsafe.Pointer)(unsafe.Pointer(&bnd.ociIntervals[n])), //dvoid         **descpp,
			C.OCI_DTYPE_INTERVAL_DS,                                 //ub4           type,
			0,   //size_t        xtramem_sz,
			nil) //dvoid         **usrmempp);
		if r == C.OCI_ERROR {
			return bnd.stmt.ses.srv.env.ociError()
		} else if r == C.OCI_INVALID_HANDLE {
			return errNew("unable to allocate oci interval handle during bind")
		}
		r = C.OCIIntervalSetDaySecond(
			unsafe.Pointer(bnd.stmt.ses.srv.env.ocienv), //void               *hndl,
			bnd.stmt.ses.srv.env.ocierr,                 //OCIError           *err,
			C.sb4(value.Day),                            //sb4                dy,
			C.sb4(value.Hour),                           //sb4                hr,
			C.sb4(value.Minute),                         //sb4                mm,
			C.sb4(value.Second),                         //sb4                ss,
			C.sb4(value.Nanosecond),                     //sb4                fsec,
			bnd.ociIntervals[n])                         //OCIInterval        *result );
		if r == C.OCI_ERROR {
			return bnd.stmt.ses.srv.env.ociError()
		}
		if values[n].IsNull {
			nullInds[n] = C.sb2(-1)
		} else {
			nullInds[n] = C.sb2(0)
		}
		alenp[n] = C.ACTUAL_LENGTH_TYPE(unsafe.Sizeof(bnd.ociIntervals[n]))
	}
	r := C.OCIBINDBYPOS(
		bnd.stmt.ocistmt,                                  //OCIStmt      *stmtp,
		(**C.OCIBind)(&bnd.ocibnd),                        //OCIBind      **bindpp,
		bnd.stmt.ses.srv.env.ocierr,                       //OCIError     *errhp,
		C.ub4(position),                                   //ub4          position,
		unsafe.Pointer(&bnd.ociIntervals[0]),              //void         *valuep,
		C.LENGTH_TYPE(unsafe.Sizeof(bnd.ociIntervals[0])), //sb8          value_sz,
		C.SQLT_INTERVAL_DS,                                //ub2          dty,
		unsafe.Pointer(&nullInds[0]),                      //void         *indp,
		&alenp[0],                                         //ub2          *alenp,
		&rcodep[0],                                        //ub2          *rcodep,
		0,                                                 //ub4          maxarr_len,
		nil,                                               //ub4          *curelep,
		C.OCI_DEFAULT)                                     //ub4          mode );
	if r == C.OCI_ERROR {
		return bnd.stmt.ses.srv.env.ociError()
	}
	r = C.OCIBindArrayOfStruct(
		bnd.ocibnd,
		bnd.stmt.ses.srv.env.ocierr,
		C.ub4(unsafe.Sizeof(bnd.ociIntervals[0])), //ub4         pvskip,
		C.ub4(C.sizeof_sb2),                       //ub4         indskip,
		C.ub4(C.sizeof_ub4),                       //ub4         alskip,
		C.ub4(C.sizeof_ub2))                       //ub4         rcskip
	if r == C.OCI_ERROR {
		return bnd.stmt.ses.srv.env.ociError()
	}
	return nil
}
Esempio n. 6
0
func (bnd *bndTimeSlice) bind(values []time.Time, nullInds []C.sb2, position int, stmt *Stmt) error {
	bnd.stmt = stmt
	bnd.ociDateTimes = make([]*C.OCIDateTime, len(values))
	if nullInds == nil {
		nullInds = make([]C.sb2, len(values))
	}
	alenp := make([]C.ACTUAL_LENGTH_TYPE, len(values))
	rcodep := make([]C.ub2, len(values))
	for n, timeValue := range values {
		timezoneStr := zoneOffset(timeValue, &bnd.zoneBuf)
		cTimezoneStr := C.CString(timezoneStr)
		defer func() {
			C.free(unsafe.Pointer(cTimezoneStr))
		}()
		r := C.OCIDescriptorAlloc(
			unsafe.Pointer(bnd.stmt.ses.srv.env.ocienv),             //CONST dvoid   *parenth,
			(*unsafe.Pointer)(unsafe.Pointer(&bnd.ociDateTimes[n])), //dvoid         **descpp,
			C.OCI_DTYPE_TIMESTAMP_TZ,                                //ub4           type,
			0,   //size_t        xtramem_sz,
			nil) //dvoid         **usrmempp);
		if r == C.OCI_ERROR {
			return bnd.stmt.ses.srv.env.ociError()
		} else if r == C.OCI_INVALID_HANDLE {
			return errNew("unable to allocate oci timestamp handle during bind")
		}
		r = C.OCIDateTimeConstruct(
			unsafe.Pointer(bnd.stmt.ses.srv.env.ocienv), //dvoid         *hndl,
			bnd.stmt.ses.srv.env.ocierr,                 //OCIError      *err,
			bnd.ociDateTimes[n],                         //OCIDateTime   *datetime,
			C.sb2(timeValue.Year()),                     //sb2           year,
			C.ub1(int32(timeValue.Month())),             //ub1           month,
			C.ub1(timeValue.Day()),                      //ub1           day,
			C.ub1(timeValue.Hour()),                     //ub1           hour,
			C.ub1(timeValue.Minute()),                   //ub1           min,
			C.ub1(timeValue.Second()),                   //ub1           sec,
			C.ub4(timeValue.Nanosecond()),               //ub4           fsec,
			(*C.OraText)(unsafe.Pointer(cTimezoneStr)),  //OraText       *timezone,
			C.size_t(len(timezoneStr)))                  //size_t        timezone_length );
		if r == C.OCI_ERROR {
			return bnd.stmt.ses.srv.env.ociError()
		}
		alenp[n] = C.ACTUAL_LENGTH_TYPE(unsafe.Sizeof(bnd.ociDateTimes[n]))
	}

	r := C.OCIBINDBYPOS(
		bnd.stmt.ocistmt,                                  //OCIStmt      *stmtp,
		(**C.OCIBind)(&bnd.ocibnd),                        //OCIBind      **bindpp,
		bnd.stmt.ses.srv.env.ocierr,                       //OCIError     *errhp,
		C.ub4(position),                                   //ub4          position,
		unsafe.Pointer(&bnd.ociDateTimes[0]),              //void         *valuep,
		C.LENGTH_TYPE(unsafe.Sizeof(bnd.ociDateTimes[0])), //sb8          value_sz,
		C.SQLT_TIMESTAMP_TZ,                               //ub2          dty,
		unsafe.Pointer(&nullInds[0]),                      //void         *indp,
		&alenp[0],                                         //ub2          *alenp,
		&rcodep[0],                                        //ub2          *rcodep,
		0,                                                 //ub4          maxarr_len,
		nil,                                               //ub4          *curelep,
		C.OCI_DEFAULT)                                     //ub4          mode );
	if r == C.OCI_ERROR {
		return bnd.stmt.ses.srv.env.ociError()
	}
	r = C.OCIBindArrayOfStruct(
		bnd.ocibnd,
		bnd.stmt.ses.srv.env.ocierr,
		C.ub4(unsafe.Sizeof(bnd.ociDateTimes[0])), //ub4         pvskip,
		C.ub4(C.sizeof_sb2),                       //ub4         indskip,
		C.ub4(C.sizeof_ub4),                       //ub4         alskip,
		C.ub4(C.sizeof_ub2))                       //ub4         rcskip
	if r == C.OCI_ERROR {
		return bnd.stmt.ses.srv.env.ociError()
	}
	return nil
}
Esempio n. 7
0
func (bnd *bndLobSlice) bindReaders(
	values []io.Reader,
	nullInds []C.sb2,
	position int,
	lobBufferSize int,
	stmt *Stmt,
) (
	err error,
) {
	bnd.stmt = stmt
	bnd.ociLobLocators = make([]*C.OCILobLocator, len(values))
	if nullInds == nil {
		nullInds = make([]C.sb2, len(values))
	}
	alenp := make([]C.ACTUAL_LENGTH_TYPE, len(values))
	rcodep := make([]C.ub2, len(values))
	if len(bnd.buf) < lobBufferSize {
		bnd.buf = make([]byte, lobBufferSize)
	}
	finishers := make([]func(), len(values))
	defer func() {
		if err == nil {
			return
		}
		for _, finish := range finishers {
			if finish == nil {
				continue
			}
			finish()
		}
	}()

	for i, r := range values {
		bnd.ociLobLocators[i], finishers[i], err = allocTempLob(bnd.stmt)
		if err != nil {
			return err
		}

		alenp[i] = C.ACTUAL_LENGTH_TYPE(unsafe.Sizeof(bnd.ociLobLocators[i]))
		if nullInds[i] <= C.sb2(-1) {
			continue
		}
		if err = writeLob(bnd.ociLobLocators[i], bnd.stmt, r, lobBufferSize); err != nil {
			bnd.stmt.ses.Break()
			return err
		}
	}

	r := C.OCIBINDBYPOS(
		bnd.stmt.ocistmt,                                    //OCIStmt      *stmtp,
		(**C.OCIBind)(&bnd.ocibnd),                          //OCIBind      **bindpp,
		bnd.stmt.ses.srv.env.ocierr,                         //OCIError     *errhp,
		C.ub4(position),                                     //ub4          position,
		unsafe.Pointer(&bnd.ociLobLocators[0]),              //void         *valuep,
		C.LENGTH_TYPE(unsafe.Sizeof(bnd.ociLobLocators[0])), //sb8          value_sz,
		C.SQLT_BLOB,                  //ub2          dty,
		unsafe.Pointer(&nullInds[0]), //void         *indp,
		&alenp[0],                    //ub4          *alenp,
		&rcodep[0],                   //ub2          *rcodep,
		0,                            //ub4          maxarr_len,
		nil,                          //ub4          *curelep,
		C.OCI_DEFAULT)                //ub4          mode );
	if r == C.OCI_ERROR {
		return bnd.stmt.ses.srv.env.ociError()
	}

	r = C.OCIBindArrayOfStruct(
		bnd.ocibnd,
		bnd.stmt.ses.srv.env.ocierr,
		C.ub4(unsafe.Sizeof(bnd.ociLobLocators[0])), //ub4         pvskip,
		C.ub4(C.sizeof_sb2),                         //ub4         indskip,
		C.ub4(C.sizeof_ub4),                         //ub4         alskip,
		C.ub4(C.sizeof_ub2))                         //ub4         rcskip
	if r == C.OCI_ERROR {
		return bnd.stmt.ses.srv.env.ociError()
	}

	return nil
}