1 pjaol 1.1
2 /**
3 * Title: <p>
4 * Description: <p>
5 * Copyright: Copyright (c) Troy Thompson, Bob Byron<p>
6 * Company: JavaUnderground<p>
7 * @author Troy Thompson, Bob Byron
8 * @version 1.1
9 */
10 package com.javaunderground.jdbc;
11
12 import java.util.Calendar;
13 import java.math.BigDecimal;
14 import java.io.*;
15 import java.sql.*;
16
17 /**
18 * OracleSqlFormatter formats Oracle specific types. These include
19 * Calendar, Date, Time, and TimeStamps. Generic types are handled
20 * by SqlFormatter.
21 */
22 pjaol 1.1 public class OracleSqlFormatter extends SqlFormatter{
23
24 /**
25 * Format of Oracle date: 'YYYY-MM-DD HH24:MI:SS.#'
26 */
27 final String ymd24="'YYYY-MM-DD HH24:MI:SS.#'";
28
29 /**
30 * Formats Calendar object into Oracle TO_DATE String.
31 * @param cal Calendar to be formatted
32 * @return formatted TO_DATE function
33 */
34 private String format(Calendar cal){
35 return "TO_DATE('" + new java.sql.Timestamp(cal.getTime().getTime()) + "',"+ymd24+")";
36 }
37
38 /**
39 * Formats Date object into Oracle TO_DATE String.
40 * @param date Date to be formatted
41 * @return formatted TO_DATE function
42 */
43 pjaol 1.1 private String format(java.sql.Date date){
44 return "TO_DATE('" + new java.sql.Timestamp(date.getTime()) + "',"+ymd24+")";
45 }
46
47 /**
48 * Formats Time object into Oracle TO_DATE String.
49 * @param time Time to be formatted
50 * @return formatted TO_DATE function
51 */
52 private String format(java.sql.Time time){
53 Calendar cal = Calendar.getInstance();
54 cal.setTime(new java.util.Date(time.getTime()));
55 return "TO_DATE('" + cal.get(Calendar.HOUR_OF_DAY) + ":" +
56 cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND) + "." +
57 cal.get(Calendar.MILLISECOND) + "','HH24:MI:SS.#')";
58 }
59
60 /**
61 * Formats Timestamp object into Oracle TO_DATE String.
62 * @param timestamp Timestamp to be formatted
63 * @return formatted TO_DATE function
64 pjaol 1.1 */
65 private String format(java.sql.Timestamp timestamp){
66 return "TO_DATE('" + timestamp.toString() + "',"+ymd24+")";
67 }
68
69
70 /**
71 * Formats object to an Oracle specific formatted function.
72 * @param o Object to be formatted.
73 * @return formatted Oracle function or "NULL" if o is null.
74 * @exception SqlException
75 */
76 public String format(Object o) throws SQLException{
77 if (o == null) return "NULL";
78 if (o instanceof Calendar) return format((Calendar)o);
79 if (o instanceof Date) return format((Date)o);
80 if (o instanceof Time) return format((Time)o);
81 if (o instanceof Timestamp) return format((Timestamp)o);
82 //if object not in one of our overridden methods, send to super class
83 return super.format(o);
84
85 pjaol 1.1 } }
|