Пример #1
0
// Version returns the Oracle database server version.
//
// Version requires the server have at least one open session.
func (srv *Srv) Version() (ver string, err error) {
	srv.mu.Lock()
	defer srv.mu.Unlock()
	srv.log(_drv.cfg.Log.Srv.Version)
	err = srv.checkClosed()
	if err != nil {
		return "", errE(err)
	}
	var buf [512]C.char
	r := C.OCIServerVersion(
		unsafe.Pointer(srv.ocisrv),            //void         *hndlp,
		srv.env.ocierr,                        //OCIError     *errhp,
		(*C.OraText)(unsafe.Pointer(&buf[0])), //OraText      *bufp,
		C.ub4(len(buf)),                       //ub4          bufsz
		C.OCI_HTYPE_SERVER)                    //ub1          hndltype );
	if r == C.OCI_ERROR {
		return "", errE(srv.env.ociError())
	}
	return C.GoString(&buf[0]), nil
}
Пример #2
0
func (d *DriverOracle) NewConnection(tns string, username string, password string) (c *ConnectionOracle, err int) {
	fmt.Printf("username=%v, password=%v, tns=%v\n", username, password, tns)
	fmt.Println("Connecting to Oracle...")

	c = new(ConnectionOracle)
	pServer := unsafe.Pointer(&c.hServer)
	res := C.OCIHandleAlloc(unsafe.Pointer(d.hEnv), (*unsafe.Pointer)(pServer), C.OCI_HTYPE_SERVER, 0, nil)
	if C.OCI_SUCCESS != res && C.OCI_STILL_EXECUTING != res {
		fmt.Printf("Resultado3=%v\n", res)
		return nil, 4
	}
	pTns := C.CString(tns)
	defer C.free(unsafe.Pointer(pTns))
	//    C.Printw (pTns);
	res = C.AuxOCIServerAttach(c.hServer, d.hError, pTns)
	if C.OCI_SUCCESS != res && C.OCI_STILL_EXECUTING != res {
		fmt.Printf("Resultado OCIServerAttach=%v\n", res)
		C.AuxOCIErrorGet(d.hError)
		return nil, 5
	}

	res = C.OCIAttrSet(unsafe.Pointer(d.hService), C.OCI_HTYPE_SVCCTX, unsafe.Pointer(c.hServer), 0, C.OCI_ATTR_SERVER, d.hError)
	if C.OCI_SUCCESS != res && C.OCI_STILL_EXECUTING != res {
		fmt.Printf("Resultado OCIAttrSet SERVICE-SERVER=%v\n", res)
		C.AuxOCIErrorGet(d.hError)
		return nil, 6
	}

	var test [1000]C.OraText
	res = C.OCIServerVersion(unsafe.Pointer(c.hServer), d.hError, &test[0], C.ub4(len(test)), C.OCI_HTYPE_SERVER)
	if C.OCI_SUCCESS != res && C.OCI_STILL_EXECUTING != res {
		fmt.Printf("Resultado OCIServerVersion=%v\n", res)
		C.AuxOCIErrorGet(d.hError)
		return nil, 7
	}
	fmt.Printf("Version=%s\n", test)

	pSession := unsafe.Pointer(&c.hSession)
	res = C.OCIHandleAlloc(unsafe.Pointer(d.hEnv), (*unsafe.Pointer)(pSession), C.OCI_HTYPE_SESSION, 0, nil)
	if C.OCI_SUCCESS != res && C.OCI_STILL_EXECUTING != res {
		fmt.Printf("Resultado hSession=%v\n", res)
		return nil, 8
	}
	pUsername := unsafe.Pointer(C.CString(username))
	defer C.free(pUsername)
	res = C.OCIAttrSet(unsafe.Pointer(c.hSession), C.OCI_HTYPE_SESSION, pUsername, C.ub4(len(username)), C.OCI_ATTR_USERNAME, d.hError)
	if C.OCI_SUCCESS != res && C.OCI_STILL_EXECUTING != res {
		fmt.Printf("Resultado OCIAttrSet USERNAME=%v\n", res)
		return nil, 9
	}
	cPassword := C.CString(password)
	//    pPassword := unsafe.Pointer(C.CString(password))
	pPassword := unsafe.Pointer(cPassword)
	defer C.free(pPassword)
	res = C.OCIAttrSet(unsafe.Pointer(c.hSession), C.OCI_HTYPE_SESSION, pPassword, C.ub4(len(password)), C.OCI_ATTR_PASSWORD, d.hError)
	if C.OCI_SUCCESS != res && C.OCI_STILL_EXECUTING != res {
		fmt.Printf("Resultado OCIAttrSet PASSWORD=%v\n", res)
		return nil, 10
	}
	res = C.OCISessionBegin(d.hService, d.hError, c.hSession, C.OCI_CRED_RDBMS, C.OCI_DEFAULT)
	if C.OCI_SUCCESS != res && C.OCI_STILL_EXECUTING != res {
		fmt.Printf("Resultado OCIAttrSet SessionBegin=%v\n", res)
		C.AuxOCIErrorGet(d.hError)
		return nil, 11
	}
	res = C.OCIAttrSet(unsafe.Pointer(d.hService), C.OCI_HTYPE_SVCCTX, unsafe.Pointer(c.hSession), 0, C.OCI_ATTR_SESSION, d.hError)
	if C.OCI_SUCCESS != res && C.OCI_STILL_EXECUTING != res {
		fmt.Printf("Resultado OCIAttrSet Service<-Session=%v\n", res)
		C.AuxOCIErrorGet(d.hError)
		return nil, 12
	}
	fmt.Printf("End NewConnection\n")

	c.driver = d
	return c, 0
}