1 6 7 package org.jdesktop.dataset.provider.sql; 8 import java.sql.PreparedStatement ; 9 import java.sql.ResultSet ; 10 import java.sql.SQLException ; 11 import java.util.ArrayList ; 12 import java.util.HashMap ; 13 import java.util.HashSet ; 14 import java.util.List ; 15 import java.util.Map ; 16 import java.util.Set ; 17 import org.jdesktop.dataset.DataCommand; 18 import org.jdesktop.dataset.DataRow; 19 20 24 public abstract class AbstractSqlCommand extends DataCommand { 25 26 29 protected abstract PreparedStatement getSelectStatement(JDBCDataConnection conn) throws Exception ; 30 protected abstract PreparedStatement getInsertStatement(JDBCDataConnection conn, DataRow row) throws Exception ; 31 protected abstract PreparedStatement getUpdateStatement(JDBCDataConnection conn, DataRow row) throws Exception ; 32 protected abstract PreparedStatement getDeleteStatement(JDBCDataConnection conn, DataRow row) throws Exception ; 33 34 protected String constructSql(String sql, Map <String ,List <Integer >> indexes) { 35 StringBuilder buffer = new StringBuilder (sql); 41 int paramIndex = 0; 44 45 boolean inSingleQuote = false; 49 boolean inDoubleQuote = false; 50 for (int i=0; i<buffer.length(); i++) { 51 char c = buffer.charAt(i); 52 if (c == '\'') { 53 inSingleQuote = !inSingleQuote; 54 } else if (c == '\"') { 55 inDoubleQuote = !inDoubleQuote; 56 } else if (c == ':' && !inSingleQuote && !inDoubleQuote) { 57 61 int firstCharIndex = i; 62 i++; 63 boolean found = false; 64 while (!found) { 65 if (i >= buffer.length()) { 66 found = true; 69 } else { 70 char next = buffer.charAt(i); 71 if (next == ' ' || next == '\n' || next == '\t' || next == '\r' || next == ',' || next == ')') { 72 found = true; 73 } 74 } 75 i++; 76 } 77 78 String paramName = buffer.substring(firstCharIndex+1, i-1); 80 81 buffer.replace(firstCharIndex, i-1, "?"); 84 if (!indexes.containsKey(paramName)) { 85 indexes.put(paramName, new ArrayList <Integer >()); 86 } 87 List <Integer > list = indexes.get(paramName); 88 list.add(paramIndex++); 89 90 i = firstCharIndex + 1; 93 } 94 } 95 return buffer.toString(); 96 } 97 98 116 public String [] getParameterNames(String [] statements) { 117 StringBuilder buffer = new StringBuilder (); 120 for (String s : statements) { 121 buffer.append(s); 122 buffer.append("\n"); 123 } 124 Set <String > names = new HashSet <String >(); 125 126 boolean inSingleQuote = false; 130 boolean inDoubleQuote = false; 131 for (int i=0; i<buffer.length(); i++) { 132 char c = buffer.charAt(i); 133 if (c == '\'') { 134 inSingleQuote = !inSingleQuote; 135 } else if (c == '\"') { 136 inDoubleQuote = !inDoubleQuote; 137 } else if (c == ':' && !inSingleQuote && !inDoubleQuote) { 138 142 int firstCharIndex = i; 143 i++; 144 boolean found = false; 145 while (!found) { 146 if (i >= buffer.length()) { 147 found = true; 150 } else { 151 char next = buffer.charAt(i); 152 if (next == ' ' || next == '\n' || next == '\t' || next == '\r' || next == ',' || next == ')') { 153 found = true; 154 } 155 } 156 i++; 157 } 158 159 String paramName = buffer.substring(firstCharIndex+1, i-1); 161 names.add(paramName); 162 } 163 } 164 String [] results = new String [names.size()]; 165 return names.toArray(results); 166 } 167 } 168 | Popular Tags |