Beispiel #1
0
func (conn *Connection) ClientInfo() (string, string, string, *ODBCError) {
	var info_len C.SQLSMALLINT
	p := make([]byte, INFO_BUFFER_LEN)
	ret := C.SQLGetInfo(C.SQLHDBC(conn.Dbc), C.SQL_DRIVER_NAME, C.SQLPOINTER(unsafe.Pointer(&p[0])), INFO_BUFFER_LEN, &info_len)
	if !Success(ret) {
		err := FormatError(C.SQL_HANDLE_DBC, conn.Dbc, int(ret))
		debugPrint("SQLGetInfo ERROR=", err)
		return "", "", "", err
	}
	drv_name := string(p[0:info_len])
	ret = C.SQLGetInfo(C.SQLHDBC(conn.Dbc), C.SQL_DRIVER_ODBC_VER, C.SQLPOINTER(unsafe.Pointer(&p[0])), INFO_BUFFER_LEN, &info_len)
	if !Success(ret) {
		err := FormatError(C.SQL_HANDLE_DBC, conn.Dbc, int(ret))
		debugPrint("SQLGetInfo ERROR=", err)
		return "", "", "", err
	}
	drv_odbc_ver := string(p[0:info_len])
	ret = C.SQLGetInfo(C.SQLHDBC(conn.Dbc), C.SQL_DRIVER_VER, C.SQLPOINTER(unsafe.Pointer(&p[0])), INFO_BUFFER_LEN, &info_len)
	if !Success(ret) {
		err := FormatError(C.SQL_HANDLE_DBC, conn.Dbc, int(ret))
		debugPrint("SQLGetInfo ERROR=", err)
		return "", "", "", err
	}
	drv_ver := string(p[0:info_len])
	return drv_name, drv_odbc_ver, drv_ver, nil
}
Beispiel #2
0
func (conn *Connection) ServerInfo() (string, string, string, *ODBCError) {
	var info_len C.SQLSMALLINT
	p := make([]byte, INFO_BUFFER_LEN)
	ret := C.SQLGetInfo(C.SQLHDBC(conn.Dbc), C.SQL_DATABASE_NAME, C.SQLPOINTER(unsafe.Pointer(&p[0])), INFO_BUFFER_LEN, &info_len)
	if !Success(ret) {
		err := FormatError(C.SQL_HANDLE_DBC, conn.Dbc, int(ret))
		debugPrint("SQLGetInfo ERROR=", err)
		return "", "", "", err
	}
	db := string(p[0:info_len])
	ret = C.SQLGetInfo(C.SQLHDBC(conn.Dbc), C.SQL_DBMS_VER, C.SQLPOINTER(unsafe.Pointer(&p[0])), INFO_BUFFER_LEN, &info_len)
	if !Success(ret) {
		err := FormatError(C.SQL_HANDLE_DBC, conn.Dbc, int(ret))
		debugPrint("SQLGetInfo ERROR=", err)
		return db, "", "", err
	}
	ver := string(p[0:info_len])
	ret = C.SQLGetInfo(C.SQLHDBC(conn.Dbc), C.SQL_SERVER_NAME, C.SQLPOINTER(unsafe.Pointer(&p[0])), INFO_BUFFER_LEN, &info_len)
	if !Success(ret) {
		err := FormatError(C.SQL_HANDLE_DBC, conn.Dbc, int(ret))
		debugPrint("SQLGetInfo ERROR=", err)
		return db, ver, "", err
	}
	server := string(p[0:info_len])
	return db, ver, server, nil
}
Beispiel #3
0
func (conn *Connection) BeginTransaction() (err *ODBCError) {
	ret := C.SQLSetConnectAttr(C.SQLHDBC(conn.Dbc), C.SQL_ATTR_AUTOCOMMIT, C.SQLPOINTER(unsafe.Pointer(uintptr(C.SQL_AUTOCOMMIT_OFF))), C.SQL_IS_UINTEGER)
	if !Success(ret) {
		err = FormatError(C.SQL_HANDLE_DBC, conn.Dbc)
	}
	return
}
Beispiel #4
0
func Connect(dsn string, params ...interface{}) (conn *Connection, err *ODBCError) {
	var h C.SQLHANDLE
	ret := C.SQLAllocHandle(C.SQL_HANDLE_DBC, Genv, &h)
	if !Success(ret) {
		err := FormatError(C.SQL_HANDLE_DBC, h)
		return nil, err
	}

	var stringLength2 C.SQLSMALLINT
	outBuf := make([]byte, BUFFER_SIZE*2)
	outConnectionString := (*C.SQLWCHAR)(unsafe.Pointer(&outBuf[0]))

	ret = C.SQLDriverConnectW(C.SQLHDBC(h),
		C.SQLHWND(unsafe.Pointer(uintptr(0))),
		(*C.SQLWCHAR)(unsafe.Pointer(StringToUTF16Ptr(dsn))),
		C.SQL_NTS,
		outConnectionString,
		BUFFER_SIZE,
		&stringLength2,
		C.SQL_DRIVER_NOPROMPT)

	if !Success(ret) {
		err := FormatError(C.SQL_HANDLE_DBC, h)
		return nil, err
	}
	return &Connection{Dbc: h, connected: true}, nil
}
Beispiel #5
0
func (conn *Connection) AutoCommit(b bool) (err *ODBCError) {
	var n C.int
	if b {
		n = C.SQL_AUTOCOMMIT_ON
	} else {
		n = C.SQL_AUTOCOMMIT_OFF
	}
	ret := C.SQLSetConnectAttr(C.SQLHDBC(conn.Dbc), C.SQL_ATTR_AUTOCOMMIT, C.SQLPOINTER(unsafe.Pointer(uintptr(n))), C.SQL_IS_UINTEGER)
	if !Success(ret) {
		err = FormatError(C.SQL_HANDLE_DBC, conn.Dbc)
	}
	return
}
Beispiel #6
0
func (conn *Connection) Close() *ODBCError {
	if conn.connected {
		ret := C.SQLDisconnect(C.SQLHDBC(conn.Dbc))
		if !Success(ret) {
			err := FormatError(C.SQL_HANDLE_DBC, conn.Dbc)
			return err
		}
		ret = C.SQLFreeHandle(C.SQL_HANDLE_DBC, conn.Dbc)
		if !Success(ret) {
			err := FormatError(C.SQL_HANDLE_DBC, conn.Dbc)
			return err
		}
		conn.connected = false
	}
	return nil
}
Beispiel #7
0
func SQLDriverConnect(connectionHandle SQLHDBC, windowHandle SQLHWND, inConnectionString *SQLWCHAR, stringLength1 SQLSMALLINT, outConnectionString *SQLWCHAR, bufferLength SQLSMALLINT, stringLength2Ptr *SQLSMALLINT, driverCompletion SQLUSMALLINT) (ret SQLRETURN) {
	r := C.SQLDriverConnectW(C.SQLHDBC(connectionHandle), C.SQLHWND(windowHandle), (*C.SQLWCHAR)(unsafe.Pointer(inConnectionString)), C.SQLSMALLINT(stringLength1), (*C.SQLWCHAR)(unsafe.Pointer(outConnectionString)), C.SQLSMALLINT(bufferLength), (*C.SQLSMALLINT)(stringLength2Ptr), C.SQLUSMALLINT(driverCompletion))
	return SQLRETURN(r)
}
Beispiel #8
0
func SQLDisconnect(connectionHandle SQLHDBC) (ret SQLRETURN) {
	r := C.SQLDisconnect(C.SQLHDBC(connectionHandle))
	return SQLRETURN(r)
}
Beispiel #9
0
func SQLSetConnectAttr(connectionHandle SQLHDBC, attribute SQLINTEGER, valuePtr SQLPOINTER, stringLength SQLINTEGER) (ret SQLRETURN) {
	r := C.SQLSetConnectAttrW(C.SQLHDBC(connectionHandle), C.SQLINTEGER(attribute), C.SQLPOINTER(valuePtr), C.SQLINTEGER(stringLength))
	return SQLRETURN(r)
}
Beispiel #10
0
func SQLSetConnectUIntPtrAttr(connectionHandle SQLHDBC, attribute SQLINTEGER, valuePtr uintptr, stringLength SQLINTEGER) (ret SQLRETURN) {
	r := C.sqlSetConnectUIntPtrAttr(C.SQLHDBC(connectionHandle), C.SQLINTEGER(attribute), C.uintptr_t(valuePtr), C.SQLINTEGER(stringLength))
	return SQLRETURN(r)
}