Source Code Cross Referenced for EscapedFunctions.java in  » Database-JDBC-Connection-Pool » postgresql » org » postgresql » jdbc2 » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database JDBC Connection Pool » postgresql » org.postgresql.jdbc2 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*-------------------------------------------------------------------------
002:         *
003:         * Copyright (c) 2004-2005, PostgreSQL Global Development Group
004:         *
005:         * IDENTIFICATION
006:         * $PostgreSQL: pgjdbc/org/postgresql/jdbc2/EscapedFunctions.java,v 1.9 2007/04/16 16:16:22 jurka Exp $
007:         *
008:         *-------------------------------------------------------------------------
009:         */
010:        package org.postgresql.jdbc2;
011:
012:        import java.lang.reflect.Method;
013:        import java.sql.SQLException;
014:        import java.util.HashMap;
015:        import java.util.List;
016:        import java.util.Map;
017:
018:        import org.postgresql.util.GT;
019:        import org.postgresql.util.PSQLException;
020:        import org.postgresql.util.PSQLState;
021:
022:        /**
023:         * this class stores supported escaped function
024:         * @author Xavier Poinsard
025:         */
026:        public class EscapedFunctions {
027:            // numeric functions names
028:            public final static String ABS = "abs";
029:            public final static String ACOS = "acos";
030:            public final static String ASIN = "asin";
031:            public final static String ATAN = "atan";
032:            public final static String ATAN2 = "atan2";
033:            public final static String CEILING = "ceiling";
034:            public final static String COS = "cos";
035:            public final static String COT = "cot";
036:            public final static String DEGREES = "degrees";
037:            public final static String EXP = "exp";
038:            public final static String FLOOR = "floor";
039:            public final static String LOG = "log";
040:            public final static String LOG10 = "log10";
041:            public final static String MOD = "mod";
042:            public final static String PI = "pi";
043:            public final static String POWER = "power";
044:            public final static String RADIANS = "radians";
045:            public final static String ROUND = "round";
046:            public final static String SIGN = "sign";
047:            public final static String SIN = "sin";
048:            public final static String SQRT = "sqrt";
049:            public final static String TAN = "tan";
050:            public final static String TRUNCATE = "truncate";
051:
052:            // string function names   
053:            public final static String ASCII = "ascii";
054:            public final static String CHAR = "char";
055:            public final static String CONCAT = "concat";
056:            public final static String INSERT = "insert"; // change arguments order
057:            public final static String LCASE = "lcase";
058:            public final static String LEFT = "left";
059:            public final static String LENGTH = "length";
060:            public final static String LOCATE = "locate"; // the 3 args version duplicate args
061:            public final static String LTRIM = "ltrim";
062:            public final static String REPEAT = "repeat";
063:            public final static String REPLACE = "replace";
064:            public final static String RIGHT = "right"; // duplicate args
065:            public final static String RTRIM = "rtrim";
066:            public final static String SPACE = "space";
067:            public final static String SUBSTRING = "substring";
068:            public final static String UCASE = "ucase";
069:            // soundex is implemented on the server side by 
070:            // the contrib/fuzzystrmatch module.  We provide a translation
071:            // for this in the driver, but since we don't want to bother with run
072:            // time detection of this module's installation we don't report this
073:            // method as supported in DatabaseMetaData.
074:            // difference is currently unsupported entirely.
075:
076:            // date time function names
077:            public final static String CURDATE = "curdate";
078:            public final static String CURTIME = "curtime";
079:            public final static String DAYNAME = "dayname";
080:            public final static String DAYOFMONTH = "dayofmonth";
081:            public final static String DAYOFWEEK = "dayofweek";
082:            public final static String DAYOFYEAR = "dayofyear";
083:            public final static String HOUR = "hour";
084:            public final static String MINUTE = "minute";
085:            public final static String MONTH = "month";
086:            public final static String MONTHNAME = "monthname";
087:            public final static String NOW = "now";
088:            public final static String QUARTER = "quarter";
089:            public final static String SECOND = "second";
090:            public final static String WEEK = "week";
091:            public final static String YEAR = "year";
092:            // for timestampadd and timestampdiff the fractional part of second is not supported
093:            // by the backend
094:            // timestampdiff is very partially supported
095:            public final static String TIMESTAMPADD = "timestampadd";
096:            public final static String TIMESTAMPDIFF = "timestampdiff";
097:
098:            // constants for timestampadd and timestampdiff
099:            public final static String SQL_TSI_ROOT = "SQL_TSI_";
100:            public final static String SQL_TSI_DAY = "DAY";
101:            public final static String SQL_TSI_FRAC_SECOND = "FRAC_SECOND";
102:            public final static String SQL_TSI_HOUR = "HOUR";
103:            public final static String SQL_TSI_MINUTE = "MINUTE";
104:            public final static String SQL_TSI_MONTH = "MONTH";
105:            public final static String SQL_TSI_QUARTER = "QUARTER";
106:            public final static String SQL_TSI_SECOND = "SECOND";
107:            public final static String SQL_TSI_WEEK = "WEEK";
108:            public final static String SQL_TSI_YEAR = "YEAR";
109:
110:            // system functions
111:            public final static String DATABASE = "database";
112:            public final static String IFNULL = "ifnull";
113:            public final static String USER = "user";
114:
115:            /** storage for functions implementations */
116:            private static Map functionMap = null;
117:
118:            /**
119:             * get Method object implementing the given function
120:             * @param functionName name of the searched function
121:             * @return a Method object or null if not found
122:             */
123:            public static Method getFunction(String functionName) {
124:                if (functionMap == null) {
125:                    Method[] arrayMeths = EscapedFunctions.class
126:                            .getDeclaredMethods();
127:                    functionMap = new HashMap(arrayMeths.length * 2);
128:                    for (int i = 0; i < arrayMeths.length; i++) {
129:                        Method meth = arrayMeths[i];
130:                        if (meth.getName().startsWith("sql"))
131:                            functionMap.put(meth.getName().toLowerCase(), meth);
132:                    }
133:                }
134:                return (Method) functionMap.get("sql"
135:                        + functionName.toLowerCase());
136:            }
137:
138:            // ** numeric functions translations **
139:            /** ceiling to ceil translation */
140:            public static String sqlceiling(List parsedArgs)
141:                    throws SQLException {
142:                StringBuffer buf = new StringBuffer();
143:                buf.append("ceil(");
144:                if (parsedArgs.size() != 1) {
145:                    throw new PSQLException(GT.tr(
146:                            "{0} function takes one and only one argument.",
147:                            "ceiling"), PSQLState.SYNTAX_ERROR);
148:                }
149:                buf.append(parsedArgs.get(0));
150:                return buf.append(')').toString();
151:            }
152:
153:            /** log to ln translation */
154:            public static String sqllog(List parsedArgs) throws SQLException {
155:                StringBuffer buf = new StringBuffer();
156:                buf.append("ln(");
157:                if (parsedArgs.size() != 1) {
158:                    throw new PSQLException(GT.tr(
159:                            "{0} function takes one and only one argument.",
160:                            "log"), PSQLState.SYNTAX_ERROR);
161:                }
162:                buf.append(parsedArgs.get(0));
163:                return buf.append(')').toString();
164:            }
165:
166:            /** log10 to log translation */
167:            public static String sqllog10(List parsedArgs) throws SQLException {
168:                StringBuffer buf = new StringBuffer();
169:                buf.append("log(");
170:                if (parsedArgs.size() != 1) {
171:                    throw new PSQLException(GT.tr(
172:                            "{0} function takes one and only one argument.",
173:                            "log10"), PSQLState.SYNTAX_ERROR);
174:                }
175:                buf.append(parsedArgs.get(0));
176:                return buf.append(')').toString();
177:            }
178:
179:            /** power to pow translation */
180:            public static String sqlpower(List parsedArgs) throws SQLException {
181:                StringBuffer buf = new StringBuffer();
182:                buf.append("pow(");
183:                if (parsedArgs.size() != 2) {
184:                    throw new PSQLException(GT.tr(
185:                            "{0} function takes two and only two arguments.",
186:                            "power"), PSQLState.SYNTAX_ERROR);
187:                }
188:                buf.append(parsedArgs.get(0)).append(',').append(
189:                        parsedArgs.get(1));
190:                return buf.append(')').toString();
191:            }
192:
193:            /** truncate to trunc translation */
194:            public static String sqltruncate(List parsedArgs)
195:                    throws SQLException {
196:                StringBuffer buf = new StringBuffer();
197:                buf.append("trunc(");
198:                if (parsedArgs.size() != 2) {
199:                    throw new PSQLException(GT.tr(
200:                            "{0} function takes two and only two arguments.",
201:                            "truncate"), PSQLState.SYNTAX_ERROR);
202:                }
203:                buf.append(parsedArgs.get(0)).append(',').append(
204:                        parsedArgs.get(1));
205:                return buf.append(')').toString();
206:            }
207:
208:            // ** string functions translations **
209:            /** char to chr translation */
210:            public static String sqlchar(List parsedArgs) throws SQLException {
211:                StringBuffer buf = new StringBuffer();
212:                buf.append("chr(");
213:                if (parsedArgs.size() != 1) {
214:                    throw new PSQLException(GT.tr(
215:                            "{0} function takes one and only one argument.",
216:                            "char"), PSQLState.SYNTAX_ERROR);
217:                }
218:                buf.append(parsedArgs.get(0));
219:                return buf.append(')').toString();
220:            }
221:
222:            /** concat translation */
223:            public static String sqlconcat(List parsedArgs) {
224:                StringBuffer buf = new StringBuffer();
225:                buf.append('(');
226:                for (int iArg = 0; iArg < parsedArgs.size(); iArg++) {
227:                    buf.append(parsedArgs.get(iArg));
228:                    if (iArg != (parsedArgs.size() - 1))
229:                        buf.append(" || ");
230:                }
231:                return buf.append(')').toString();
232:            }
233:
234:            /** insert to overlay translation */
235:            public static String sqlinsert(List parsedArgs) throws SQLException {
236:                StringBuffer buf = new StringBuffer();
237:                buf.append("overlay(");
238:                if (parsedArgs.size() != 4) {
239:                    throw new PSQLException(GT.tr(
240:                            "{0} function takes four and only four argument.",
241:                            "insert"), PSQLState.SYNTAX_ERROR);
242:                }
243:                buf.append(parsedArgs.get(0)).append(" placing ").append(
244:                        parsedArgs.get(3));
245:                buf.append(" from ").append(parsedArgs.get(1)).append(" for ")
246:                        .append(parsedArgs.get(2));
247:                return buf.append(')').toString();
248:            }
249:
250:            /** lcase to lower translation */
251:            public static String sqllcase(List parsedArgs) throws SQLException {
252:                StringBuffer buf = new StringBuffer();
253:                buf.append("lower(");
254:                if (parsedArgs.size() != 1) {
255:                    throw new PSQLException(GT.tr(
256:                            "{0} function takes one and only one argument.",
257:                            "lcase"), PSQLState.SYNTAX_ERROR);
258:                }
259:                buf.append(parsedArgs.get(0));
260:                return buf.append(')').toString();
261:            }
262:
263:            /** left to substring translation */
264:            public static String sqlleft(List parsedArgs) throws SQLException {
265:                StringBuffer buf = new StringBuffer();
266:                buf.append("substring(");
267:                if (parsedArgs.size() != 2) {
268:                    throw new PSQLException(GT.tr(
269:                            "{0} function takes two and only two arguments.",
270:                            "left"), PSQLState.SYNTAX_ERROR);
271:                }
272:                buf.append(parsedArgs.get(0)).append(" for ").append(
273:                        parsedArgs.get(1));
274:                return buf.append(')').toString();
275:            }
276:
277:            /** length translation */
278:            public static String sqllength(List parsedArgs) throws SQLException {
279:                StringBuffer buf = new StringBuffer();
280:                buf.append("length(trim(trailing from ");
281:                if (parsedArgs.size() != 1) {
282:                    throw new PSQLException(GT.tr(
283:                            "{0} function takes one and only one argument.",
284:                            "length"), PSQLState.SYNTAX_ERROR);
285:                }
286:                buf.append(parsedArgs.get(0));
287:                return buf.append("))").toString();
288:            }
289:
290:            /** locate translation */
291:            public static String sqllocate(List parsedArgs) throws SQLException {
292:                if (parsedArgs.size() == 2) {
293:                    return "position(" + parsedArgs.get(0) + " in "
294:                            + parsedArgs.get(1) + ")";
295:                } else if (parsedArgs.size() == 3) {
296:                    String tmp = "position(" + parsedArgs.get(0)
297:                            + " in substring(" + parsedArgs.get(1) + " from "
298:                            + parsedArgs.get(2) + "))";
299:                    return "(" + parsedArgs.get(2) + "*sign(" + tmp + ")+"
300:                            + tmp + ")";
301:                } else {
302:                    throw new PSQLException(GT.tr(
303:                            "{0} function takes two or three arguments.",
304:                            "locate"), PSQLState.SYNTAX_ERROR);
305:                }
306:            }
307:
308:            /** ltrim translation */
309:            public static String sqlltrim(List parsedArgs) throws SQLException {
310:                StringBuffer buf = new StringBuffer();
311:                buf.append("trim(leading from ");
312:                if (parsedArgs.size() != 1) {
313:                    throw new PSQLException(GT.tr(
314:                            "{0} function takes one and only one argument.",
315:                            "ltrim"), PSQLState.SYNTAX_ERROR);
316:                }
317:                buf.append(parsedArgs.get(0));
318:                return buf.append(')').toString();
319:            }
320:
321:            /** right to substring translation */
322:            public static String sqlright(List parsedArgs) throws SQLException {
323:                StringBuffer buf = new StringBuffer();
324:                buf.append("substring(");
325:                if (parsedArgs.size() != 2) {
326:                    throw new PSQLException(GT.tr(
327:                            "{0} function takes two and only two arguments.",
328:                            "right"), PSQLState.SYNTAX_ERROR);
329:                }
330:                buf.append(parsedArgs.get(0)).append(" from (length(").append(
331:                        parsedArgs.get(0)).append(")+1-").append(
332:                        parsedArgs.get(1));
333:                return buf.append("))").toString();
334:            }
335:
336:            /** rtrim translation */
337:            public static String sqlrtrim(List parsedArgs) throws SQLException {
338:                StringBuffer buf = new StringBuffer();
339:                buf.append("trim(trailing from ");
340:                if (parsedArgs.size() != 1) {
341:                    throw new PSQLException(GT.tr(
342:                            "{0} function takes one and only one argument.",
343:                            "rtrim"), PSQLState.SYNTAX_ERROR);
344:                }
345:                buf.append(parsedArgs.get(0));
346:                return buf.append(')').toString();
347:            }
348:
349:            /** space translation */
350:            public static String sqlspace(List parsedArgs) throws SQLException {
351:                StringBuffer buf = new StringBuffer();
352:                buf.append("repeat(' ',");
353:                if (parsedArgs.size() != 1) {
354:                    throw new PSQLException(GT.tr(
355:                            "{0} function takes one and only one argument.",
356:                            "space"), PSQLState.SYNTAX_ERROR);
357:                }
358:                buf.append(parsedArgs.get(0));
359:                return buf.append(')').toString();
360:            }
361:
362:            /** substring to substr translation */
363:            public static String sqlsubstring(List parsedArgs)
364:                    throws SQLException {
365:                if (parsedArgs.size() == 2) {
366:                    return "substr(" + parsedArgs.get(0) + ","
367:                            + parsedArgs.get(1) + ")";
368:                } else if (parsedArgs.size() == 3) {
369:                    return "substr(" + parsedArgs.get(0) + ","
370:                            + parsedArgs.get(1) + "," + parsedArgs.get(2) + ")";
371:                } else {
372:                    throw new PSQLException(GT.tr(
373:                            "{0} function takes two or three arguments.",
374:                            "substring"), PSQLState.SYNTAX_ERROR);
375:                }
376:            }
377:
378:            /** ucase to upper translation */
379:            public static String sqlucase(List parsedArgs) throws SQLException {
380:                StringBuffer buf = new StringBuffer();
381:                buf.append("upper(");
382:                if (parsedArgs.size() != 1) {
383:                    throw new PSQLException(GT.tr(
384:                            "{0} function takes one and only one argument.",
385:                            "ucase"), PSQLState.SYNTAX_ERROR);
386:                }
387:                buf.append(parsedArgs.get(0));
388:                return buf.append(')').toString();
389:            }
390:
391:            /** curdate to current_date translation */
392:            public static String sqlcurdate(List parsedArgs)
393:                    throws SQLException {
394:                if (parsedArgs.size() != 0) {
395:                    throw new PSQLException(GT.tr(
396:                            "{0} function doesn''t take any argument.",
397:                            "curdate"), PSQLState.SYNTAX_ERROR);
398:                }
399:                return "current_date";
400:            }
401:
402:            /** curtime to current_time translation */
403:            public static String sqlcurtime(List parsedArgs)
404:                    throws SQLException {
405:                if (parsedArgs.size() != 0) {
406:                    throw new PSQLException(GT.tr(
407:                            "{0} function doesn''t take any argument.",
408:                            "curtime"), PSQLState.SYNTAX_ERROR);
409:                }
410:                return "current_time";
411:            }
412:
413:            /** dayname translation */
414:            public static String sqldayname(List parsedArgs)
415:                    throws SQLException {
416:                if (parsedArgs.size() != 1) {
417:                    throw new PSQLException(GT.tr(
418:                            "{0} function takes one and only one argument.",
419:                            "dayname"), PSQLState.SYNTAX_ERROR);
420:                }
421:                return "to_char(" + parsedArgs.get(0) + ",'Day')";
422:            }
423:
424:            /** dayofmonth translation */
425:            public static String sqldayofmonth(List parsedArgs)
426:                    throws SQLException {
427:                if (parsedArgs.size() != 1) {
428:                    throw new PSQLException(GT.tr(
429:                            "{0} function takes one and only one argument.",
430:                            "dayofmonth"), PSQLState.SYNTAX_ERROR);
431:                }
432:                return "extract(day from " + parsedArgs.get(0) + ")";
433:            }
434:
435:            /** dayofweek translation 
436:             * adding 1 to postgresql function since we expect values from 1 to 7 */
437:            public static String sqldayofweek(List parsedArgs)
438:                    throws SQLException {
439:                if (parsedArgs.size() != 1) {
440:                    throw new PSQLException(GT.tr(
441:                            "{0} function takes one and only one argument.",
442:                            "dayofweek"), PSQLState.SYNTAX_ERROR);
443:                }
444:                return "extract(dow from " + parsedArgs.get(0) + ")+1";
445:            }
446:
447:            /** dayofyear translation */
448:            public static String sqldayofyear(List parsedArgs)
449:                    throws SQLException {
450:                if (parsedArgs.size() != 1) {
451:                    throw new PSQLException(GT.tr(
452:                            "{0} function takes one and only one argument.",
453:                            "dayofyear"), PSQLState.SYNTAX_ERROR);
454:                }
455:                return "extract(doy from " + parsedArgs.get(0) + ")";
456:            }
457:
458:            /** hour translation */
459:            public static String sqlhour(List parsedArgs) throws SQLException {
460:                if (parsedArgs.size() != 1) {
461:                    throw new PSQLException(GT.tr(
462:                            "{0} function takes one and only one argument.",
463:                            "hour"), PSQLState.SYNTAX_ERROR);
464:                }
465:                return "extract(hour from " + parsedArgs.get(0) + ")";
466:            }
467:
468:            /** minute translation */
469:            public static String sqlminute(List parsedArgs) throws SQLException {
470:                if (parsedArgs.size() != 1) {
471:                    throw new PSQLException(GT.tr(
472:                            "{0} function takes one and only one argument.",
473:                            "minute"), PSQLState.SYNTAX_ERROR);
474:                }
475:                return "extract(minute from " + parsedArgs.get(0) + ")";
476:            }
477:
478:            /** month translation */
479:            public static String sqlmonth(List parsedArgs) throws SQLException {
480:                if (parsedArgs.size() != 1) {
481:                    throw new PSQLException(GT.tr(
482:                            "{0} function takes one and only one argument.",
483:                            "month"), PSQLState.SYNTAX_ERROR);
484:                }
485:                return "extract(month from " + parsedArgs.get(0) + ")";
486:            }
487:
488:            /** monthname translation */
489:            public static String sqlmonthname(List parsedArgs)
490:                    throws SQLException {
491:                if (parsedArgs.size() != 1) {
492:                    throw new PSQLException(GT.tr(
493:                            "{0} function takes one and only one argument.",
494:                            "monthname"), PSQLState.SYNTAX_ERROR);
495:                }
496:                return "to_char(" + parsedArgs.get(0) + ",'Month')";
497:            }
498:
499:            /** quarter translation */
500:            public static String sqlquarter(List parsedArgs)
501:                    throws SQLException {
502:                if (parsedArgs.size() != 1) {
503:                    throw new PSQLException(GT.tr(
504:                            "{0} function takes one and only one argument.",
505:                            "quarter"), PSQLState.SYNTAX_ERROR);
506:                }
507:                return "extract(quarter from " + parsedArgs.get(0) + ")";
508:            }
509:
510:            /** second translation */
511:            public static String sqlsecond(List parsedArgs) throws SQLException {
512:                if (parsedArgs.size() != 1) {
513:                    throw new PSQLException(GT.tr(
514:                            "{0} function takes one and only one argument.",
515:                            "second"), PSQLState.SYNTAX_ERROR);
516:                }
517:                return "extract(second from " + parsedArgs.get(0) + ")";
518:            }
519:
520:            /** week translation */
521:            public static String sqlweek(List parsedArgs) throws SQLException {
522:                if (parsedArgs.size() != 1) {
523:                    throw new PSQLException(GT.tr(
524:                            "{0} function takes one and only one argument.",
525:                            "week"), PSQLState.SYNTAX_ERROR);
526:                }
527:                return "extract(week from " + parsedArgs.get(0) + ")";
528:            }
529:
530:            /** year translation */
531:            public static String sqlyear(List parsedArgs) throws SQLException {
532:                if (parsedArgs.size() != 1) {
533:                    throw new PSQLException(GT.tr(
534:                            "{0} function takes one and only one argument.",
535:                            "year"), PSQLState.SYNTAX_ERROR);
536:                }
537:                return "extract(year from " + parsedArgs.get(0) + ")";
538:            }
539:
540:            /** time stamp add */
541:            public static String sqltimestampadd(List parsedArgs)
542:                    throws SQLException {
543:                if (parsedArgs.size() != 3) {
544:                    throw new PSQLException(
545:                            GT
546:                                    .tr(
547:                                            "{0} function takes three and only three arguments.",
548:                                            "timestampadd"),
549:                            PSQLState.SYNTAX_ERROR);
550:                }
551:                String interval = EscapedFunctions.constantToInterval(
552:                        parsedArgs.get(0).toString(), parsedArgs.get(1)
553:                                .toString());
554:                StringBuffer buf = new StringBuffer();
555:                buf.append("(interval ").append(interval).append("+").append(
556:                        parsedArgs.get(2)).append(")");
557:                return buf.toString();
558:            }
559:
560:            private final static String constantToInterval(String type,
561:                    String value) throws SQLException {
562:                if (!type.startsWith(SQL_TSI_ROOT))
563:                    throw new PSQLException(GT.tr(
564:                            "Interval {0} not yet implemented", type),
565:                            PSQLState.SYNTAX_ERROR);
566:                String shortType = type.substring(SQL_TSI_ROOT.length());
567:                if (SQL_TSI_DAY.equalsIgnoreCase(shortType))
568:                    return "'" + value + " day'";
569:                else if (SQL_TSI_SECOND.equalsIgnoreCase(shortType))
570:                    return "'" + value + " second'";
571:                else if (SQL_TSI_HOUR.equalsIgnoreCase(shortType))
572:                    return "'" + value + " hour'";
573:                else if (SQL_TSI_MINUTE.equalsIgnoreCase(shortType))
574:                    return "'" + value + " minute'";
575:                else if (SQL_TSI_MONTH.equalsIgnoreCase(shortType))
576:                    return "'" + value + " month'";
577:                else if (SQL_TSI_QUARTER.equalsIgnoreCase(shortType))
578:                    return "'" + Integer.valueOf(value).intValue() * 3
579:                            + " month'";
580:                else if (SQL_TSI_WEEK.equalsIgnoreCase(shortType))
581:                    return "'" + value + " week'";
582:                else if (SQL_TSI_YEAR.equalsIgnoreCase(shortType))
583:                    return "'" + value + " year'";
584:                else if (SQL_TSI_FRAC_SECOND.equalsIgnoreCase(shortType))
585:                    throw new PSQLException(GT.tr(
586:                            "Interval {0} not yet implemented",
587:                            "SQL_TSI_FRAC_SECOND"), PSQLState.SYNTAX_ERROR);
588:                else
589:                    throw new PSQLException(GT.tr(
590:                            "Interval {0} not yet implemented", type),
591:                            PSQLState.SYNTAX_ERROR);
592:            }
593:
594:            /** time stamp diff */
595:            public static String sqltimestampdiff(List parsedArgs)
596:                    throws SQLException {
597:                if (parsedArgs.size() != 3) {
598:                    throw new PSQLException(
599:                            GT
600:                                    .tr(
601:                                            "{0} function takes three and only three arguments.",
602:                                            "timestampdiff"),
603:                            PSQLState.SYNTAX_ERROR);
604:                }
605:                String datePart = EscapedFunctions
606:                        .constantToDatePart(parsedArgs.get(0).toString());
607:                StringBuffer buf = new StringBuffer();
608:                buf.append("extract( ").append(datePart).append(" from (")
609:                        .append(parsedArgs.get(2)).append("-").append(
610:                                parsedArgs.get(1)).append("))");
611:                return buf.toString();
612:            }
613:
614:            private final static String constantToDatePart(String type)
615:                    throws SQLException {
616:                if (!type.startsWith(SQL_TSI_ROOT))
617:                    throw new PSQLException(GT.tr(
618:                            "Interval {0} not yet implemented", type),
619:                            PSQLState.SYNTAX_ERROR);
620:                String shortType = type.substring(SQL_TSI_ROOT.length());
621:                if (SQL_TSI_DAY.equalsIgnoreCase(shortType))
622:                    return "day";
623:                else if (SQL_TSI_SECOND.equalsIgnoreCase(shortType))
624:                    return "second";
625:                else if (SQL_TSI_HOUR.equalsIgnoreCase(shortType))
626:                    return "hour";
627:                else if (SQL_TSI_MINUTE.equalsIgnoreCase(shortType))
628:                    return "minute";
629:                /*else if (SQL_TSI_MONTH.equalsIgnoreCase(shortType))
630:                	return "month";
631:                else if (SQL_TSI_QUARTER.equalsIgnoreCase(shortType))
632:                	return "quarter";
633:                else if (SQL_TSI_WEEK.equalsIgnoreCase(shortType))
634:                	return "week";
635:                else if (SQL_TSI_YEAR.equalsIgnoreCase(shortType))
636:                	return "year";*/
637:                else if (SQL_TSI_FRAC_SECOND.equalsIgnoreCase(shortType))
638:                    throw new PSQLException(GT.tr(
639:                            "Interval {0} not yet implemented",
640:                            "SQL_TSI_FRAC_SECOND"), PSQLState.SYNTAX_ERROR);
641:                else
642:                    throw new PSQLException(GT.tr(
643:                            "Interval {0} not yet implemented", type),
644:                            PSQLState.SYNTAX_ERROR);
645:            }
646:
647:            /** database translation */
648:            public static String sqldatabase(List parsedArgs)
649:                    throws SQLException {
650:                if (parsedArgs.size() != 0) {
651:                    throw new PSQLException(GT.tr(
652:                            "{0} function doesn''t take any argument.",
653:                            "database"), PSQLState.SYNTAX_ERROR);
654:                }
655:                return "current_database()";
656:            }
657:
658:            /** ifnull translation */
659:            public static String sqlifnull(List parsedArgs) throws SQLException {
660:                if (parsedArgs.size() != 2) {
661:                    throw new PSQLException(GT.tr(
662:                            "{0} function takes two and only two arguments.",
663:                            "ifnull"), PSQLState.SYNTAX_ERROR);
664:                }
665:                return "coalesce(" + parsedArgs.get(0) + ","
666:                        + parsedArgs.get(1) + ")";
667:            }
668:
669:            /** user translation */
670:            public static String sqluser(List parsedArgs) throws SQLException {
671:                if (parsedArgs.size() != 0) {
672:                    throw new PSQLException(
673:                            GT.tr("{0} function doesn''t take any argument.",
674:                                    "user"), PSQLState.SYNTAX_ERROR);
675:                }
676:                return "user";
677:            }
678:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.