dbTalk Databases Forums  

Processing DynamicSQL with single quotes in the data

comp.databases.ingres comp.databases.ingres


Discuss Processing DynamicSQL with single quotes in the data in the comp.databases.ingres forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Colin Hay
 
Posts: n/a

Default Processing DynamicSQL with single quotes in the data - 03-26-2009 , 04:48 PM






I have an ESQLC program that handles SQL statements as strings.
It executes all non-SELECT statements with an EXEC SQL EXECUTE
IMMEDIATE :SQLStmt statement.

This works fine except for strings that include single quotes

e.g.

name2 = 'O''MALLEY';

SQLStmt = 'UPDATE pepl SET name2 = ' + :name2 + ' WHERE ....'

results in

'UPDATE pepl SET name2 = 'O'MALLEY' WHERE ....'

which will fail

Has anyone out there ever come up with an algorithm for processing a
string like this to escape the correct single quote that will work
with ANY sql statement ?


Colin Hay


Reply With Quote
  #2  
Old   
nikosv
 
Posts: n/a

Default Re: Processing DynamicSQL with single quotes in the data - 03-27-2009 , 12:10 AM






On Mar 27, 12:48*am, Colin Hay <wobb... (AT) hotmail (DOT) com> wrote:
Quote:
I have an ESQLC program that handles SQL statements as strings.
It executes all non-SELECT statements with an EXEC SQL EXECUTE
IMMEDIATE :SQLStmt statement.

This works fine except for strings that include single quotes

e.g.

name2 = 'O''MALLEY';

SQLStmt = 'UPDATE pepl SET name2 = ' + :name2 + ' WHERE ....'

results in

'UPDATE pepl SET name2 = 'O'MALLEY' WHERE ....'

which will fail

Has anyone out there ever come up with an algorithm for processing a
string like this to escape the correct single quote that will work
with ANY sql statement ?

Colin Hay
Hi Colin,
I used the single quotes' ASCII representation X'27' to produce the
following :

name2='O'+X'27'+X'27'+X'27'+X'27'+'MALLEY';
SQLStmt='UPDATE mypal SET epwnymo = '+X'27'+ :name2 + X'27'+ '
WHERE .......';

which interpolated is: UPDATE mypal SET epwnymo = 'O''''MALLEY' WHERE
and inserts O''MALLEY in the table



Reply With Quote
  #3  
Old   
Bodo
 
Posts: n/a

Default Re: Processing DynamicSQL with single quotes in the data - 03-27-2009 , 10:17 AM



On Mar 26, 11:48*pm, Colin Hay <wobb... (AT) hotmail (DOT) com> wrote:
Quote:
I have an ESQLC program that handles SQL statements as strings.
It executes all non-SELECT statements with an EXEC SQL EXECUTE
IMMEDIATE :SQLStmt statement.

This works fine except for strings that include single quotes

e.g.

name2 = 'O''MALLEY';

SQLStmt = 'UPDATE pepl SET name2 = ' + :name2 + ' WHERE ....'

results in

'UPDATE pepl SET name2 = 'O'MALLEY' WHERE ....'

which will fail

Has anyone out there ever come up with an algorithm for processing a
string like this to escape the correct single quote that will work
with ANY sql statement ?

Colin Hay
Basically you have to replace each occurance of a single quote in
variable name2 by two single quotes.

As I know you are programming with OpenROAD here is an example:

SQLStmt = 'UPDATE pepl SET name2 = ' +
ReplaceText(text = name2, match='''', replacement = '''''') + '
WHERE ....';

This uses 4GL Procedure "ReplaceText" having the following code:

procedure ReplaceText(
text = varchar(2000) not null,
match = varchar(256) not null,
replacement = varchar(256) not null
)=
DECLARE
pos = integer not null;
v = varchar(2000) not null;
newtext = varchar(2000) not null;
ENDDECLARE
BEGIN
v = text;
while (length(v) > 0) do
pos = locate(v, match);
newtext = newtext + left(v, pos-1);
if pos <= length(v) then
newtext = newtext+replacement;
endif;
v = shift(v, -(pos+length(match)-1));
endwhile;
return newtext;
END

HTH,
Bodo.


Reply With Quote
  #4  
Old   
Bodo
 
Posts: n/a

Default Re: Processing DynamicSQL with single quotes in the data - 03-27-2009 , 10:22 AM



On Mar 27, 5:17*pm, Bodo <Bodo.Bergm... (AT) ingres (DOT) com> wrote:
Quote:
On Mar 26, 11:48*pm, Colin Hay <wobb... (AT) hotmail (DOT) com> wrote:





I have an ESQLC program that handles SQL statements as strings.
It executes all non-SELECT statements with an EXEC SQL EXECUTE
IMMEDIATE :SQLStmt statement.

This works fine except for strings that include single quotes

e.g.

name2 = 'O''MALLEY';

SQLStmt = 'UPDATE pepl SET name2 = ' + :name2 + ' WHERE ....'

results in

'UPDATE pepl SET name2 = 'O'MALLEY' WHERE ....'

which will fail

Has anyone out there ever come up with an algorithm for processing a
string like this to escape the correct single quote that will work
with ANY sql statement ?

Colin Hay

Basically you have to replace each occurance of a single quote in
variable name2 by two single quotes.

As I know you are programming with OpenROAD here is an example:

SQLStmt = 'UPDATE pepl SET name2 = ' +
* * ReplaceText(text = name2, match='''', replacement = '''''')+ '
WHERE ....';

This uses 4GL Procedure "ReplaceText" having the following code:

procedure ReplaceText(
* * text = varchar(2000) not null,
* * match = varchar(256) not null,
* * replacement = varchar(256) not null
)=
DECLARE
* * pos = integer not null;
* * v = varchar(2000) not null;
* * newtext = varchar(2000) not null;
ENDDECLARE
BEGIN
* * v = text;
* * while (length(v) > 0) do
* * * * pos = locate(v, match);
* * * * newtext = newtext + left(v, pos-1);
* * * * if pos <= length(v) then
* * * * * * newtext = newtext+replacement;
* * * * endif;
* * * * v = shift(v, -(pos+length(match)-1));
* * endwhile;
* * return newtext;
END

HTH,
Bodo.- Hide quoted text -

- Show quoted text -
Slight correction - should set the single quotes around the name
(was also missing in your original example).:

SQLStmt = 'UPDATE pepl SET name2 = ''' +
ReplaceText(text = name2, match='''', replacement = '''''') +
''' WHERE ....';





Reply With Quote
  #5  
Old   
Colin Hay
 
Posts: n/a

Default Re: Processing DynamicSQL with single quotes in the data - 03-27-2009 , 10:49 PM



This is fine if I know only pepl.name2 can have single quotes and only need to code explicitly for that column

However any varchar could have a quote in it - i.e. descriptions, addresses etc

Every SQL statement is being executed in C from a common handler OpenROAD procedure so
ideally what I want is a routine in that procedure that takes any SQL statement string, does some some semantic checking, adding the extra quote where deemed necessary

My first attempt counted the number of quotes delimited by a comma or '='
Where I can count up to 3 quotes I escape the 2nd so 'O'MALLEY' becomes 'O''MALLEY'
the problem then was that 'O'MALLEY, SEAMUS' doesn't work because my algorithm doesn't realise that the comma is part of the string ...

I guess I should then delimit by ' followed by any number of spaces followed by ,
I was just wondering if anyone had had this problem before and had a working solution before I spend any more time on this

The alternative is to have to scan the existing data in every char column and narrow it down to a list of possible problem columns
and explicitly code them ...


"Bodo" <Bodo.Bergmann (AT) ingres (DOT) com> wrote

Quote:
On Mar 26, 11:48 pm, Colin Hay <wobb... (AT) hotmail (DOT) com> wrote:
I have an ESQLC program that handles SQL statements as strings.
It executes all non-SELECT statements with an EXEC SQL EXECUTE
IMMEDIATE :SQLStmt statement.

This works fine except for strings that include single quotes

e.g.

name2 = 'O''MALLEY';

SQLStmt = 'UPDATE pepl SET name2 = ' + :name2 + ' WHERE ....'

results in

'UPDATE pepl SET name2 = 'O'MALLEY' WHERE ....'

which will fail

Has anyone out there ever come up with an algorithm for processing a
string like this to escape the correct single quote that will work
with ANY sql statement ?

Colin Hay

Basically you have to replace each occurance of a single quote in
variable name2 by two single quotes.

As I know you are programming with OpenROAD here is an example:

SQLStmt = 'UPDATE pepl SET name2 = ' +
ReplaceText(text = name2, match='''', replacement = '''''') + '
WHERE ....';

This uses 4GL Procedure "ReplaceText" having the following code:

procedure ReplaceText(
text = varchar(2000) not null,
match = varchar(256) not null,
replacement = varchar(256) not null
)=
DECLARE
pos = integer not null;
v = varchar(2000) not null;
newtext = varchar(2000) not null;
ENDDECLARE
BEGIN
v = text;
while (length(v) > 0) do
pos = locate(v, match);
newtext = newtext + left(v, pos-1);
if pos <= length(v) then
newtext = newtext+replacement;
endif;
v = shift(v, -(pos+length(match)-1));
endwhile;
return newtext;
END

HTH,
Bodo.

Reply With Quote
  #6  
Old   
nikosv
 
Posts: n/a

Default Re: Processing DynamicSQL with single quotes in the data - 03-29-2009 , 11:19 PM



In that case you can you regular expressions to substitute each quote
with its counterpart to the extend that you can substitute all quotes
except the first and last one.
You can use an external C regex library such as the Perl compatible
one PCRE
(www.pcre.org).
Depending on the platform you could fire up a shell and use sed or tr
or their GNU counterparts
to processes the string but that would require to pass the resulting
string back to your program so it might be better to do the processing
from within your C program by using the lib


Ο συντάκτης Colin Hay έγραψε:
Quote:
This is fine if I know only pepl.name2 can have single quotes and only need to code explicitly for that column

However any varchar could have a quote in it - i.e. descriptions, addresses etc

Every SQL statement is being executed in C from a common handler OpenROADprocedure so
ideally what I want is a routine in that procedure that takes any SQL statement string, does some some semantic checking, adding the extra quote where deemed necessary

My first attempt counted the number of quotes delimited by a comma or '='
Where I can count up to 3 quotes I escape the 2nd so 'O'MALLEY' becomes 'O''MALLEY'
the problem then was that 'O'MALLEY, SEAMUS' doesn't work because my algorithm doesn't realise that the comma is part of the string ...

I guess I should then delimit by ' followed by any number of spaces followed by ,
I was just wondering if anyone had had this problem before and had a working solution before I spend any more time on this

The alternative is to have to scan the existing data in every char columnand narrow it down to a list of possible problem columns
and explicitly code them ...


"Bodo" <Bodo.Bergmann (AT) ingres (DOT) com> wrote

On Mar 26, 11:48 pm, Colin Hay <wobb... (AT) hotmail (DOT) com> wrote:
I have an ESQLC program that handles SQL statements as strings.
It executes all non-SELECT statements with an EXEC SQL EXECUTE
IMMEDIATE :SQLStmt statement.

This works fine except for strings that include single quotes

e.g.

name2 = 'O''MALLEY';

SQLStmt = 'UPDATE pepl SET name2 = ' + :name2 + ' WHERE ....'

results in

'UPDATE pepl SET name2 = 'O'MALLEY' WHERE ....'

which will fail

Has anyone out there ever come up with an algorithm for processing a
string like this to escape the correct single quote that will work
with ANY sql statement ?

Colin Hay

Basically you have to replace each occurance of a single quote in
variable name2 by two single quotes.

As I know you are programming with OpenROAD here is an example:

SQLStmt = 'UPDATE pepl SET name2 = ' +
ReplaceText(text = name2, match='''', replacement = '''''') + '
WHERE ....';

This uses 4GL Procedure "ReplaceText" having the following code:

procedure ReplaceText(
text = varchar(2000) not null,
match = varchar(256) not null,
replacement = varchar(256) not null
)=
DECLARE
pos = integer not null;
v = varchar(2000) not null;
newtext = varchar(2000) not null;
ENDDECLARE
BEGIN
v = text;
while (length(v) > 0) do
pos = locate(v, match);
newtext = newtext + left(v, pos-1);
if pos <= length(v) then
newtext = newtext+replacement;
endif;
v = shift(v, -(pos+length(match)-1));
endwhile;
return newtext;
END

HTH,
Bodo.

Reply With Quote
  #7  
Old   
Kristoff
 
Posts: n/a

Default Re: Processing DynamicSQL with single quotes in the data - 03-30-2009 , 02:11 AM



You could use use prepared queries instead, i.e.

strcpy(name2,"O'MALLEY");
....
EXEC SQL prepare updatestmt from 'UPDATE pepl SET name2 = ? where ...
";
EXEC SQL execute updatestmt using :name;

In this way there is need to look for any quotes in the values.

Regards
Kristoff



On Mar 27, 12:48*am, Colin Hay <wobb... (AT) hotmail (DOT) com> wrote:
Quote:
I have an ESQLC program that handles SQL statements as strings.
It executes all non-SELECT statements with an EXEC SQL EXECUTE
IMMEDIATE :SQLStmt statement.

This works fine except for strings that include single quotes

e.g.

name2 = 'O''MALLEY';

SQLStmt = 'UPDATE pepl SET name2 = ' + :name2 + ' WHERE ....'

results in

'UPDATE pepl SET name2 = 'O'MALLEY' WHERE ....'

which will fail

Has anyone out there ever come up with an algorithm for processing a
string like this to escape the correct single quote that will work
with ANY sql statement ?

Colin Hay


Reply With Quote
  #8  
Old   
Bodo
 
Posts: n/a

Default Re: Processing DynamicSQL with single quotes in the data - 04-06-2009 , 11:16 AM



On Mar 28, 6:49*am, "Colin Hay" <colin... (AT) bigpond (DOT) net.au> wrote:
Quote:
This is fine if I know only pepl.name2 can have single quotes and only need to code explicitly for that column

However any varchar could have a quote in it - i.e. descriptions, addresses etc

Every SQL statement is being executed in C from a common handler OpenROADprocedure so
ideally what I want is a routine in that procedure that takes any SQL statement string, does some some semantic checking, adding the extra quote where deemed necessary

My first attempt counted the number of quotes delimited by a comma or '='
Where I can count up to 3 quotes I escape the 2nd so 'O'MALLEY' becomes 'O''MALLEY'
the problem then was that 'O'MALLEY, SEAMUS' doesn't work because my algorithm doesn't realise that the comma is part of the string ...

I guess I should then delimit by ' followed by any number of spaces followed by ,
I was just wondering if anyone had had this problem before and had a working solution before I spend any more time on this

The alternative is to have to scan the existing data in every char columnand narrow it down to a list of possible problem columns
and explicitly code them ...



"Bodo" <Bodo.Bergm... (AT) ingres (DOT) com> wrote in messagenews:b465552a-df4e-420b-adf6-37a19f2aa7f2 (AT) n17g2000vba (DOT) googlegroups.com...
On Mar 26, 11:48 pm, Colin Hay <wobb... (AT) hotmail (DOT) com> wrote:
I have an ESQLC program that handles SQL statements as strings.
It executes all non-SELECT statements with an EXEC SQL EXECUTE
IMMEDIATE :SQLStmt statement.

This works fine except for strings that include single quotes

e.g.

name2 = 'O''MALLEY';

SQLStmt = 'UPDATE pepl SET name2 = ' + :name2 + ' WHERE ....'

results in

'UPDATE pepl SET name2 = 'O'MALLEY' WHERE ....'

which will fail

Has anyone out there ever come up with an algorithm for processing a
string like this to escape the correct single quote that will work
with ANY sql statement ?

Colin Hay

Basically you have to replace each occurance of a single quote in
variable name2 by two single quotes.

As I know you are programming with OpenROAD here is an example:

SQLStmt = 'UPDATE pepl SET name2 = ' +
* *ReplaceText(text = name2, match='''', replacement = '''''') + '
WHERE ....';

This uses 4GL Procedure "ReplaceText" having the following code:

procedure ReplaceText(
* *text = varchar(2000) not null,
* *match = varchar(256) not null,
* *replacement = varchar(256) not null
)=
DECLARE
* *pos = integer not null;
* *v = varchar(2000) not null;
* *newtext = varchar(2000) not null;
ENDDECLARE
BEGIN
* *v = text;
* *while (length(v) > 0) do
pos = locate(v, match);
newtext = newtext + left(v, pos-1);
if pos <= length(v) then
* * newtext = newtext+replacement;
endif;
v = shift(v, -(pos+length(match)-1));
* *endwhile;
* *return newtext;
END

HTH,
Bodo.- Hide quoted text -

- Show quoted text -
Colin,
why can't you use the ReplaceText() function on any varchar value
(descriptions, addresses etc )when adding it to the query?


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.