Example #1
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
}
Example #2
0
func (conn *Connection) newStmt() (*Statement, *ODBCError) {
	stmt := &Statement{}

	ret := C.SQLAllocHandle(C.SQL_HANDLE_STMT, conn.Dbc, &stmt.handle)
	if !Success(ret) {
		err := FormatError(C.SQL_HANDLE_DBC, conn.Dbc)
		return nil, err
	}
	return stmt, nil
}
Example #3
0
func initEnv() (err *ODBCError) {
	ret := C.SQLAllocHandle(C.SQL_HANDLE_ENV, nil, &Genv)
	if !Success(ret) {
		err := FormatError(C.SQL_HANDLE_ENV, Genv)
		return err
	}
	ret = C.SQLSetEnvAttr(C.SQLHENV(Genv), C.SQL_ATTR_ODBC_VERSION, C.SQLPOINTER(unsafe.Pointer(uintptr(C.SQL_OV_ODBC3))), C.SQLINTEGER(0))
	if !Success(ret) {
		err := FormatError(C.SQL_HANDLE_ENV, Genv)
		return err
	}
	return nil
}
Example #4
0
func SQLAllocHandle(handleType SQLSMALLINT, inputHandle SQLHANDLE, outputHandle *SQLHANDLE) (ret SQLRETURN) {
	r := C.SQLAllocHandle(C.SQLSMALLINT(handleType), C.SQLHANDLE(inputHandle), (*C.SQLHANDLE)(outputHandle))
	return SQLRETURN(r)
}