示例#1
0
文件: oci8.go 项目: mattn/go-oci8
func (d *OCI8Driver) Open(dsnString string) (connection driver.Conn, err error) {
	var (
		conn OCI8Conn
		dsn  *DSN
	)

	if dsn, err = ParseDSN(dsnString); err != nil {
		return nil, err
	}

	if rv := C.WrapOCIEnvCreate(
		C.OCI_DEFAULT|C.OCI_THREADED,
		0); rv.rv != C.OCI_SUCCESS && rv.rv != C.OCI_SUCCESS_WITH_INFO {
		// TODO: error handle not yet allocated, we can't get string error from oracle
		return nil, errors.New("can't OCIEnvCreate")
	} else {
		conn.env = rv.ptr
	}

	if rv := C.WrapOCIHandleAlloc(
		conn.env,
		C.OCI_HTYPE_ERROR,
		0); rv.rv != C.OCI_SUCCESS {
		return nil, errors.New("cant allocate error handle")
	} else {
		conn.err = rv.ptr
	}

	phost := C.CString(dsn.Connect)
	defer C.free(unsafe.Pointer(phost))
	puser := C.CString(dsn.Username)
	defer C.free(unsafe.Pointer(puser))
	ppass := C.CString(dsn.Password)
	defer C.free(unsafe.Pointer(ppass))

	if rv := C.WrapOCILogon(
		(*C.OCIEnv)(conn.env),
		(*C.OCIError)(conn.err),
		(*C.OraText)(unsafe.Pointer(puser)),
		C.ub4(len(dsn.Username)),
		(*C.OraText)(unsafe.Pointer(ppass)),
		C.ub4(len(dsn.Password)),
		(*C.OraText)(unsafe.Pointer(phost)),
		C.ub4(len(dsn.Connect))); rv.rv != C.OCI_SUCCESS && rv.rv != C.OCI_SUCCESS_WITH_INFO {
		return nil, ociGetError(rv.rv, conn.err)
	} else {
		conn.svc = rv.ptr
	}
	conn.location = dsn.Location
	conn.transactionMode = dsn.transactionMode
	conn.prefetch_rows = dsn.prefetch_rows
	conn.prefetch_memory = dsn.prefetch_memory
	conn.enableQMPlaceholders = dsn.enableQMPlaceholders
	return &conn, nil
}
示例#2
0
文件: oci8.go 项目: AsamQi/go-oci8
func (d *OCI8Driver) Open(dsnString string) (connection driver.Conn, err error) {
	var (
		conn OCI8Conn
		dsn  *DSN
	)

	if dsn, err = ParseDSN(dsnString); err != nil {
		return nil, err
	}

	// set safe defaults
	conn.attrs = make(Values)
	conn.attrs.Set("prefetch_rows", 10)
	conn.attrs.Set("prefetch_memory", int64(0))

	for k, v := range parseEnviron(os.Environ()) {
		conn.attrs.Set(k, v)
	}

	if rv := C.WrapOCIEnvCreate(
		C.OCI_DEFAULT|C.OCI_THREADED,
		0); rv.rv != C.OCI_SUCCESS && rv.rv != C.OCI_SUCCESS_WITH_INFO {
		// TODO: error handle not yet allocated, we can't get string error from oracle
		return nil, errors.New("can't OCIEnvCreate")
	} else {
		conn.env = rv.ptr
	}

	if rv := C.WrapOCIHandleAlloc(
		conn.env,
		C.OCI_HTYPE_ERROR,
		0); rv.rv != C.OCI_SUCCESS {
		return nil, errors.New("cant  allocate error handle")
	} else {
		conn.err = rv.ptr
	}

	var host string
	if dsn.Host != "" && dsn.SID != "" {
		host = fmt.Sprintf("%s:%d/%s", dsn.Host, dsn.Port, dsn.SID)
	} else {
		host = dsn.SID
	}
	phost := C.CString(host)
	defer C.free(unsafe.Pointer(phost))
	puser := C.CString(dsn.Username)
	defer C.free(unsafe.Pointer(puser))
	ppass := C.CString(dsn.Password)
	defer C.free(unsafe.Pointer(ppass))

	if rv := C.WrapOCILogon(
		(*C.OCIEnv)(conn.env),
		(*C.OCIError)(conn.err),
		(*C.OraText)(unsafe.Pointer(puser)),
		C.ub4(len(dsn.Username)),
		(*C.OraText)(unsafe.Pointer(ppass)),
		C.ub4(len(dsn.Password)),
		(*C.OraText)(unsafe.Pointer(phost)),
		C.ub4(len(host))); rv.rv != C.OCI_SUCCESS {
		return nil, ociGetError(conn.err)
	} else {
		conn.svc = rv.ptr
	}
	conn.location = dsn.Location
	conn.transactionMode = dsn.transactionMode
	c := &conn
	runtime.SetFinalizer(c, (*OCI8Conn).Close)
	return c, nil
}