FFI, ODBC, and SqlDriverConnect

John R. Pierce john at pierce.name
Tue Mar 30 12:41:16 UTC 2004


Hi all,

There is nothing quite like being the only one to respond to your own email and
questions ;-), but I did find a bug in my DSN-less enhancement to the "ODBC for
Squeak" package.

Here is my fixed DSN-less connection enhancement now officially checked into the
Google SCM system:

ODBCLibrary>>sqlDriverConnect: connectionHandle hwnd: windowHandle connection:
connectionString connectionLength: connectionLengthInteger outConnection:
outConnectionString bufferLength: bufferLengthInteger outConnectionLengthPtr:
outConnectionLengthIntegerPtr driverCompletion: driverCompletionInteger 
	"SQLRETURN	SQLDriverConnect(
     SQLHDBC     ConnectionHandle,
     SQLHWND     WindowHandle,
     SQLCHAR *     InConnectionString,
     SQLSMALLINT     StringLength1,
     SQLCHAR *     OutConnectionString,
     SQLSMALLINT     BufferLength,
     SQLSMALLINT *     StringLength2Ptr,
     SQLUSMALLINT     DriverCompletion);"
	<cdecl: short 'SQLDriverConnect' (SQLHDBC Win32Handle char* short char* short
SQLSmallInteger* ulong)>
	^ self externalCallFailed

ODBCConnection>>connectDsnLess

	| sqlDriverNoPrompt actualOutLengthIntegerPtr fullDsn outString |

	sqlDriverNoPrompt _ 0.

	fullDsn _ String streamContents:
		[:stream |
		stream 
			nextPutAll: dsn;
			nextPutAll: ';UID=';
			nextPutAll: user;
			nextPutAll: ';PWD=';
			nextPutAll: password].

	outString _ String new: 1024 withAll: $ . 
	actualOutLengthIntegerPtr _ SQLSmallInteger new.	
	[self
		checkSQLReturn: (ODBCLibrary default
				sqlDriverConnect: handle
				hwnd: nil
				connection: fullDsn
				connectionLength: fullDsn size
				outConnection: outString
				bufferLength: outString size
				outConnectionLengthPtr: actualOutLengthIntegerPtr
				driverCompletion: sqlDriverNoPrompt)] ensure: [actualOutLengthIntegerPtr
free]

"Override the next message -- essentially a small change"
ODBCConnection>>basicOpen
	"open the connection"
	""
	self registerForFinalization.
	environmentHandle _ SQLHENV new.
	self
		checkSQLReturn: (ODBCLibrary default sqlAllocEnv: environmentHandle).
	""
	handle _ SQLHDBC new.
	self
		checkSQLReturn: (ODBCLibrary default sqlAllocConnectEnvironment:
environmentHandle connection: handle).
	""
	(dsn asLowercase includesSubString: 'driver={')
		ifTrue: [self connectDsnLess]
		ifFalse:
			[self
				checkSQLReturn: (ODBCLibrary default
						sqlConnect: handle
						dsn: dsn
						dsnLength: dsn size
						user: user
						userLength: user size
						authentication: password
						authenticationLength: password size)].
	""
	connected _ true

Enjoy,

John Pierce



More information about the Squeak-dev mailing list