>From 20aeb9470d783dd827147136e530fca23cd9810d Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Fri, 8 Apr 2011 14:09:37 +0200 Subject: [PATCH 3/4] dbd-postgres: Allow to execute queries with parameters This only supports the PostgreSQL way of specifying parameters using the $1 $2 $3 parameters. It does not support named bindings or such. This makes going from SQLite to Postgres a lot harder. The parameters must be provided as string, there is no type conversion done on this level. --- packages/dbd-postgresql/ChangeLog | 5 ++++ packages/dbd-postgresql/Connection.st | 38 +++++++++++++++++++++++++++++++++ packages/dbd-postgresql/Statement.st | 21 +++++++++++++---- 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/packages/dbd-postgresql/ChangeLog b/packages/dbd-postgresql/ChangeLog index ea768b5..0ca6d3b 100644 --- a/packages/dbd-postgresql/ChangeLog +++ b/packages/dbd-postgresql/ChangeLog @@ -1,5 +1,10 @@ 2011-04-08 Holger Hans Peter Freyther + * Statements.st: Implement executeWithAll: + * Connection.st: Invoke PQexecParams. + +2011-04-08 Holger Hans Peter Freyther + * Statement.st: New. 2011-01-26 Stefan Schmiedl diff --git a/packages/dbd-postgresql/Connection.st b/packages/dbd-postgresql/Connection.st index f23d3f2..202e604 100644 --- a/packages/dbd-postgresql/Connection.st +++ b/packages/dbd-postgresql/Connection.st @@ -188,6 +188,44 @@ CObject subclass: PQConnection [ exec: aSqlStatement [ ] + + "Executing SQL with params" + exec: aSqlStatement with: params [ + | par | + + "Sanity check before we do memory allocations in C" + (params allSatisfy: [:el | el isString]) ifFalse: [ + ^ self error: 'All parameters must be strings here']. + + "Convert the params into an array of C-Strings." + par := CStringType new: params size. + 1 to: params size do: [:each | + par at: each - 1 put: (params at: each)]. + + [^ self + exec_params:aSqlStatement + n_par: params size + types: nil + values: par + lengths: nil + formats: nil + res: 0 + ] ensure: [ + "Free the memory we allocated" + + 0 to: params size - 1 do: [:each | + (par derefAt: (each * CStringType sizeof) type: CObjectType) free + ]. + + par free. + ] + ] + + exec_params: cmd n_par: parAms types: types values: vals lengths: l formats: f res: r [ + + + ] ] diff --git a/packages/dbd-postgresql/Statement.st b/packages/dbd-postgresql/Statement.st index c9cfaa7..9335989 100644 --- a/packages/dbd-postgresql/Statement.st +++ b/packages/dbd-postgresql/Statement.st @@ -53,6 +53,8 @@ now the ability to execute commands with binding.'> queryString: aSQLQuery [ + "In PostgreSQL one can use $1 for binding parameters with the + executeWithAll:. The parameters must be all strings." queryString := aSQLQuery. ] @@ -63,17 +65,26 @@ now the ability to execute commands with binding.'> ^isSelect ] + checkResult: aRes [ + + self isSelect + ifTrue: [aRes checkStatusForSelect] + ifFalse: [aRes checkStatusForDo]. + ^ aRes + ] + execute [ | res | res := PGResultSet new: (dbHandle exec: queryString). - self isSelect - ifTrue: [res checkStatusForSelect] - ifFalse: [res checkStatusForDo]. - ^ res + ^ self checkResult: res. ] executeWithAll: aParams [ - self notYetImplemented + | res | + "In PostgreSQL one can use $1 for binding parameters with the + executeWithAll:. The parameters must be all strings." + res := PGResultSet new: (dbHandle exec: queryString with: aParams). + ^ self checkResult: res. ] ] -- 1.7.4