1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.dao.orm.common;
16  
17  import com.liferay.portal.kernel.dao.db.DB;
18  import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.util.PropsValues;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  /**
28   * <a href="SQLTransformer.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Brian Wing Shun Chan
31   */
32  public class SQLTransformer {
33  
34      public static String transform(String sql) {
35          return _instance._transform(sql);
36      }
37  
38      private SQLTransformer() {
39          _sqlMap = new HashMap<String, String>();
40  
41          DB db = DBFactoryUtil.getDB();
42  
43          if (db.getType().equals(DB.TYPE_MYSQL)) {
44              _vendorMySQL = true;
45          }
46      }
47  
48      private String _removeLower(String sql) {
49          int x = sql.indexOf(_LOWER_OPEN);
50  
51          if (x == -1) {
52              return sql;
53          }
54  
55          String newSQL = _sqlMap.get(sql);
56  
57          if (newSQL != null) {
58              return newSQL;
59          }
60  
61          StringBuilder sb = new StringBuilder(sql.length());
62  
63          int y = 0;
64  
65          while (true) {
66              sb.append(sql.substring(y, x));
67  
68              y = sql.indexOf(_LOWER_CLOSE, x);
69  
70              if (y == -1) {
71                  sb.append(sql.substring(x));
72  
73                  break;
74              }
75  
76              sb.append(sql.substring(x + _LOWER_OPEN.length(), y));
77  
78              y++;
79  
80              x = sql.indexOf(_LOWER_OPEN, y);
81  
82              if (x == -1) {
83                  sb.append(sql.substring(y));
84  
85                  break;
86              }
87          }
88  
89          newSQL = sb.toString();
90  
91          if (_log.isDebugEnabled()) {
92              _log.debug("Original SQL " + sql);
93              _log.debug("Modified SQL " + newSQL);
94          }
95  
96          _sqlMap.put(sql, newSQL);
97  
98          return newSQL;
99      }
100 
101     private String _transform(String sql) {
102         if (sql == null) {
103             return sql;
104         }
105 
106         if (_vendorMySQL &&
107             !PropsValues.DATABASE_MYSQL_FUNCTION_LOWER_ENABLED) {
108 
109             sql = _removeLower(sql);
110         }
111 
112         return sql;
113     }
114 
115     private static final String _LOWER_CLOSE = StringPool.CLOSE_PARENTHESIS;
116 
117     private static final String _LOWER_OPEN = "lower(";
118 
119     private static Log _log = LogFactoryUtil.getLog(SQLTransformer.class);
120 
121     private static SQLTransformer _instance = new SQLTransformer();
122 
123     private Map<String, String> _sqlMap;
124     private boolean _vendorMySQL;
125 
126 }