コード例 #1
0
func (c *ConnectionOracle) NewStatement(sql string) (s *StatementOracle, err int) {

	s = new(StatementOracle)
	pStatement := unsafe.Pointer(&s.hStatement)
	res := C.OCIHandleAlloc(unsafe.Pointer(c.driver.hEnv), (*unsafe.Pointer)(pStatement), C.OCI_HTYPE_STMT, 0, nil)
	if C.OCI_SUCCESS != res /*&& C.OCI_STILL_EXECUTING != res*/ {
		fmt.Printf("Result hSession=%v\n", res)
		C.AuxOCIErrorGet(c.driver.hError)
		return nil, -1
	}

	cSql := C.CString(sql)
	defer C.free(unsafe.Pointer(cSql))
	var osql *C.OraText = C.Get_OraText(cSql)
	//fmt.Printf( "%v:%v\n", C.strlen(sql), C.GoString(sql))
	res = C.OCIStmtPrepare(s.hStatement, c.driver.hError, osql, C.ub4(C.strlen(cSql)), C.OCI_NTV_SYNTAX, C.OCI_DEFAULT)
	if C.OCI_SUCCESS != res /*&& C.OCI_STILL_EXECUTING != res*/ {
		fmt.Printf("Resultado OCIStmtPrepare=%v\n", res)
		C.AuxOCIErrorGet(c.driver.hError)
		return nil, -2
	}
	s.fetchDone = false
	s.con = c
	return s, 0
}
コード例 #2
0
func (s *StatementOracle) Next() (err int) {

	if !s.fetchDone {
		s.hDefine = new(C.OCIDefine)
		res := C.OCIStmtExecute(s.con.driver.hService, s.hStatement, s.con.driver.hError, 0, 0, nil, nil, C.OCI_DEFAULT)
		if C.OCI_SUCCESS != res && C.OCI_STILL_EXECUTING != res {
			fmt.Printf("Resultado OCIStmtExecute=%v\n", res)
			C.AuxOCIErrorGet(s.con.driver.hError)
			return -1
		}
		s.fetchDone = true
		/*
		    var idValue int
		    res = C.OCIDefineByPos( s.hStatement, &s.hDefine, s.con.driver.hError, 1 /*pos* /, unsafe.Pointer(&idValue), C.sb4(unsafe.Sizeof(idValue)), C.SQLT_INT, nil, nil, nil, C.OCI_DEFAULT);
		    if (C.OCI_SUCCESS != res /*&& C.OCI_STILL_EXECUTING != res* /) {
		 		fmt.Printf( "Resultado OCIDefineByPos=%v\n", res)
		     	C.AuxOCIErrorGet( s.con.driver.hError);
		     	return -2
		    }
		    var surnameValue [40]C.char
		    res = C.OCIDefineByPos( s.hStatement, &s.hDefine, s.con.driver.hError, 2 /*pos* /, unsafe.Pointer(&surnameValue[0]), C.sb4(unsafe.Sizeof(surnameValue)), C.SQLT_STR, nil, nil, nil, C.OCI_DEFAULT);
		    if (C.OCI_SUCCESS != res /*&& C.OCI_STILL_EXECUTING != res* /) {
		 		fmt.Printf( "Resultado OCIDefineByPos=%v\n", res)
		     	C.AuxOCIErrorGet( s.con.driver.hError);
		     	return -3
		    }
		*/
		res = C.OCIAttrGet(unsafe.Pointer(s.hStatement), C.OCI_HTYPE_STMT, unsafe.Pointer(&s.numColumns), nil, C.OCI_ATTR_PARAM_COUNT, s.con.driver.hError)
		if C.OCI_SUCCESS != res /*&& C.OCI_STILL_EXECUTING != res*/ {
			fmt.Printf("Resultado OCIAttrGet=%v\n", res)
			C.AuxOCIErrorGet(s.con.driver.hError)
			return -3
		}
		fmt.Printf("numColumns=%v\n", s.numColumns)

		s.columns = make([]*ColumnOracle, 0, 50)
		var hColumn C.ub4
		pColumn := unsafe.Pointer(&hColumn)
		for i := 1; i <= s.numColumns; i++ {
			/* get parameter for column i */
			res = C.OCIParamGet(unsafe.Pointer(s.hStatement), C.OCI_HTYPE_STMT, s.con.driver.hError, &pColumn, C.ub4(i))
			if C.OCI_SUCCESS != res /*&& C.OCI_STILL_EXECUTING != res*/ {
				fmt.Printf("Resultado OCIParamGet=%v\n", res)
				C.AuxOCIErrorGet(s.con.driver.hError)
				return -4
			}

			/* get data-type of column i */
			var columnType C.ub2
			pType := unsafe.Pointer(&columnType)
			res = C.OCIAttrGet(pColumn, C.OCI_DTYPE_PARAM, pType, nil, C.OCI_ATTR_DATA_TYPE, s.con.driver.hError)
			if C.OCI_SUCCESS != res /*&& C.OCI_STILL_EXECUTING != res*/ {
				fmt.Printf("Resultado OCIParamGet=%v\n", res)
				C.AuxOCIErrorGet(s.con.driver.hError)
				return -4
			}

			size := C.ub4(100)
			var column_name_tmp *C.char
			res = C.OCIAttrGet(pColumn, C.OCI_DTYPE_PARAM, unsafe.Pointer(&column_name_tmp), nil, C.OCI_ATTR_NAME, s.con.driver.hError)
			fmt.Printf("coumnName (size=%v) = %v\n", size, column_name_tmp)
			//column_name_tmp[size] = 0
			columnName := C.GoString(column_name_tmp)
			var columnSize int
			res = C.OCIAttrGet(pColumn, C.OCI_DTYPE_PARAM, unsafe.Pointer(&columnSize), nil, C.OCI_ATTR_DATA_SIZE, s.con.driver.hError)

			fmt.Printf("Column:%v Name:%v Size:%v Type:%v\n", i, columnName, columnSize, columnType)

			switch columnType {
			case C.SQLT_CHR:
				var columnValue = make([]C.char, columnSize+1)

				res = C.OCIDefineByPos(s.hStatement, &s.hDefine, s.con.driver.hError, C.ub4(i), /*pos*/
					unsafe.Pointer(&columnValue[0]), C.sb4(columnSize), columnType, nil, nil, nil, C.OCI_DEFAULT)
				if C.OCI_SUCCESS != res /*&& C.OCI_STILL_EXECUTING != res*/ {
					fmt.Printf("Resultado OCIDefineByPos=%v\n", res)
					C.AuxOCIErrorGet(s.con.driver.hError)
					return -2
				}
				s.columns = append(s.columns, NewColumnChrOracle(s, columnName, int(columnSize), columnValue))

			case C.SQLT_NUM:
				var columnValue int

				res = C.OCIDefineByPos(s.hStatement, &s.hDefine, s.con.driver.hError, C.ub4(i), /*pos*/
					unsafe.Pointer(&columnValue), C.sb4(4 /*sizeof(columnValue)*/), columnType, nil, nil, nil, C.OCI_DEFAULT)
				if C.OCI_SUCCESS != res /*&& C.OCI_STILL_EXECUTING != res*/ {
					fmt.Printf("Resultado OCIDefineByPos=%v\n", res)
					C.AuxOCIErrorGet(s.con.driver.hError)
					return -2
				}
				s.columns = append(s.columns, NewColumnNumberOracle(s, columnName, int(columnSize), columnValue))

			//case C.SQLT_LNG:
			//case C.SQLT_RID:
			//case C.SQLT_DAT:
			//case C.SQLT_BIN:
			//case C.SQLT_LBI:
			//case C.SQLT_AFC:
			//case C.SQLT_REF:
			//case C.SQLT_CLOB:
			//case C.SQLT_BLOB:
			//case C.SQLT_BFILEE:
			//case C.SQLT_TIMESTAMP:
			//case C.SQLT_TIME_TZ:
			//case C.SQLT_INTERVAL_YM:
			//case C.SQLT_INTERVAL_DS:
			//case C.SQLT_TIMESTAMP_LTZ:
			//case C.SQLT_RDD:
			default:
				s.columns = append(s.columns, NewColumnUnknownOracle(s, columnName, int(columnSize), int(columnType)))
			}
			fmt.Printf("Next len(stmt.columns)=%v\n", len(s.columns))
		}
		//return 0
	}

	res := C.OCIStmtFetch2(s.hStatement, s.con.driver.hError, 1 /*nrows*/, C.OCI_FETCH_NEXT, 0, C.OCI_DEFAULT)
	if C.OCI_SUCCESS != res && C.OCI_NO_DATA != res {
		fmt.Printf("Resultado OCIStmtFetch2=%v\n", res)
		C.AuxOCIErrorGet(s.con.driver.hError)
		return -4
	} else if C.OCI_NO_DATA == res {
		//fmt.Printf( "The result is:%v-%v\n", idValue, C.GoString(&surnameValue[0]))
		return 0
	}
	return 1
}
コード例 #3
0
ファイル: DriverOracle.go プロジェクト: reanjar/go-db-oracle
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
}