KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > query > model > SqlUtils


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc escapeString(String JavaDoc value) {
24         StringBuffer JavaDoc result = new StringBuffer JavaDoc(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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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