-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQLObjects.java
77 lines (68 loc) · 1.9 KB
/
SQLObjects.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package bbtrial.nl.logicgate.ace;
import java.sql.Timestamp;
/**
* Converts the objects returned by
* SQL queries to various data types.
* Returns null if the object is null.
* @author skmedlock
*
*/
public class SQLObjects {
/**
* Just like calling .toString(), except returns null if the object itself is null
* @param object
* @return string the toString() or null
*/
public String objectToString(Object o) {
if(o==null){
return null;
} else {
return o.toString().trim();
}
}
/**
* Accepts any object with a toString that returns an appropriate
* SQL string, and returns it with single quotes. If the object is
* null, returns a null. Escapes quote characters.
* @param Object o Any object where 'toString()' is an acceptable SQL value
* @return String ' + toString() + '
*/
public String objectToSQLValue(Object o){
if(o==null){
return null;
} else {
String t = o.toString().trim().replaceAll("'", "''");
return "'" + t + "'";
}
}
/**
* if object is a Timestamp, returns an RCalendar. Otherwise returns null.
* @param object - any object, but preferably a java.sql.Timestamp object
* @return RCalendar
*/
public RCalendar sqlTimestampToRCalendar(Object object) {
if(Timestamp.class.isInstance(object)){
return new RCalendar((Timestamp) object);
}
return null;
}
/**
* Takes any object and tries to make an integer out of it.
* Works best if you give it objects that resemble integers,
* otherwise returns zero.
* @param o
* @return the integer represented by the object, or zero.
*/
public int objectToint(Object o) {
if(o==null) return 0;
try{
if(Integer.class.isInstance(o)){
Integer integer = (Integer) o;
return integer.intValue();
}
return Integer.parseInt(o.toString().trim());
} catch(Exception e){
return 0;
}
}
}