Hmmm, after a little more testing, I fixed a problem with backslashes. New code attached. Tom. On Thu, Aug 30, 2001 at 11:46:16AM +0900, Thomas O'Dowd wrote: > Hi all, > > I found some time this morning to write and test a new EscapeSQL() method. > I didn't make a patch for the driver yet as I'd like to hear some > comments. It's a tad longer than the original code as it only replaces > escape codes which appear in the SQL code and not inside strings. > > It's attached as a separate java program which you can run to test with > various strings. Let me know if you think it is okay. It seems to work > with what I've tested it with. > > Example: > $ /usr/local/java/jdk1.3/bin/java esc "insert into test values ({d '2000-12-01'}, >'string of\\ \' {d mmmm}', {t '12:12:12'})" > insert into test values ( '2000-12-01', 'string of\\ \' {d mmmm}', {t '12:12:12'}) > > Do you think we should expand it to handle the other codes like {t and {ts ? > The old routine only handles {d. > > Tom. -- Thomas O'Dowd. - Nooping - http://nooper.com [EMAIL PROTECTED] - Testing - http://nooper.co.jp/labs
public class esc { private static final short CODE = 0; private static final short STRING = 1; private static final short BACKSLASH =2; private static final short ESC_DATE = 3; public static String EscapeSQL(String sql) { // look through the sql statement for escape codes. Since escape // codes can only appear in SQL CODE, we keep track of if we are // inside or outside a string. StringBuffer newsql = new StringBuffer(); short state = CODE; int i = -1; int len = sql.length(); while(++i < len) { char c = sql.charAt(i); switch(state) { case CODE: if(c == '\'') state = STRING; else if (c == '{') if(i+1 < len && sql.charAt(i+1) == 'd') { state = ESC_DATE; i++; break; // we skip the append. } newsql.append(c); break; case STRING: if(c == '\'') state = CODE; else if(c == '\\') state = BACKSLASH; newsql.append(c); break; case BACKSLASH: state = STRING; newsql.append(c); break; case ESC_DATE: if(c == '}') state = CODE; else newsql.append(c); break; } // end switch } return newsql.toString(); } public static void main(String args[]) { if(args.length != 1) { System.out.println("Usage java esc \"String\""); System.exit(1); } System.out.println(EscapeSQL(args[0])); } }
---------------------------(end of broadcast)--------------------------- TIP 2: you can get off all lists at once with the unregister command (send "unregister YourEmailAddressHere" to [EMAIL PROTECTED])