| 1 16 package org.outerj.daisy.query.model; 17 18 import org.outerj.daisy.repository.RepositoryException; 19 import org.outerj.daisy.repository.query.QueryException; 20 import org.outerj.daisy.query.QueryContext; 21 22 public class SqlUtils { 23 public static String escapeString(String value) { 24 StringBuffer result = new StringBuffer (value.length() + 8); 25 26 for (int i = 0; i < value.length(); i++) { 27 char c = value.charAt(i); 28 if (c == '\'') { 29 result.append("''"); 30 } else { 31 result.append(c); 32 } 33 } 34 35 return result.toString(); 36 } 37 38 public static long parseBranch(String branch, QueryContext context) throws QueryException { 39 if (branch.length() > 0 && (Character.isDigit(branch.charAt(0)) || branch.charAt(0) == '-')) { 40 try { 41 return Long.parseLong(branch); 42 } catch (NumberFormatException e) { 43 throw new QueryException("Invalid branch ID: \"" + branch + "\"."); 44 } 45 } else { 46 try { 47 return context.getBranchByName(branch).getId(); 48 } catch (RepositoryException e) { 49 throw new QueryException("Problem with branch name \"" + branch + "\"."); 50 } 51 } 52 } 53 54 public static long parseLanguage(String language, QueryContext context) throws QueryException { 55 if (language.length() > 0 && (Character.isDigit(language.charAt(0)) || language.charAt(0) == '-')) { 56 try { 57 return Long.parseLong(language); 58 } catch (NumberFormatException e) { 59 throw new QueryException("Invalid language ID: \"" + language + "\"."); 60 } 61 } else { 62 try { 63 return context.getLanguageByName(language).getId(); 64 } catch (RepositoryException e) { 65 throw new QueryException("Problem with language name \"" + language + "\"."); 66 } 67 } 68 } 69 } 70 | Popular Tags |