| 1 |
package br.usp.ime.ccsl.kalibro.database.daos; |
| 2 |
|
| 3 |
import static br.usp.ime.ccsl.kalibro.core.utilities.ReflectionUtils.*; |
| 4 |
|
| 5 |
import br.usp.ime.ccsl.kalibro.core.types.KalibroType; |
| 6 |
import br.usp.ime.ccsl.kalibro.database.DatabaseConnector; |
| 7 |
|
| 8 |
import java.awt.Color; |
| 9 |
import java.lang.reflect.Method; |
| 10 |
import java.sql.ResultSet; |
| 11 |
import java.sql.SQLException; |
| 12 |
import java.util.Date; |
| 13 |
|
| 14 |
/** |
| 15 |
* Parent class of all Kalibro data access objects. Contains utility data access methods. |
| 16 |
* |
| 17 |
* @author Carlos Morais |
| 18 |
*/ |
| 19 |
|
| 20 |
public abstract class AbstractDao { |
| 21 |
|
| 22 |
protected DatabaseConnector connector; |
| 23 |
|
| 24 |
public AbstractDao() throws SQLException { |
| 25 |
this(new DatabaseConnector()); |
| 26 |
} |
| 27 |
|
| 28 |
public AbstractDao(DatabaseConnector connector) { |
| 29 |
this.connector = connector; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Returns the string quoted (with simple quotes). |
| 34 |
|
| 35 |
* A simple quote is also added before any simple quote present in the original string. |
| 36 |
|
| 37 |
* This method should be used for preventing SQL injection. |
| 38 |
* |
| 39 |
|
| 40 |
|
| 41 |
* Example: "Joana d'Arc" becomes "'Joana d''Arc'" |
| 42 |
*/ |
| 43 |
protected String quote(Object object) { |
| 44 |
if (new Double(Double.POSITIVE_INFINITY).equals(object)) |
| 45 |
return "" + Double.MAX_VALUE; |
| 46 |
if (new Double(Double.NEGATIVE_INFINITY).equals(object)) |
| 47 |
return "" + Double.MIN_VALUE; |
| 48 |
if (object instanceof Color) |
| 49 |
return "" + ((Color) object).getRGB(); |
| 50 |
if (object instanceof Date) |
| 51 |
return "" + ((Date) object).getTime(); |
| 52 |
return (object == null) ? "null" : "'" + object.toString().replaceAll("'", "''") + "'"; |
| 53 |
} |
| 54 |
|
| 55 |
private String prepare(String skeleton, KalibroType object) { |
| 56 |
String result = skeleton; |
| 57 |
for (Method method : object.getClass().getMethods()) |
| 58 |
result = result.replace("$" + method.getName(), quote(getValue(method.getName(), object))); |
| 59 |
return result; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Replaces the occurrences of "$" + fieldName with the values of the field for the specified object. For |
| 64 |
* example, "name = $name" becomes "name = 'Sample'" if object.name = "Sample". For more than one object, |
| 65 |
* prepare(skeleton, object1, object2) id equivalent to |
| 66 |
* prepare(prepare(skeleton, object1), object2). |
| 67 |
*/ |
| 68 |
protected String prepare(String skeleton, KalibroType... objects) { |
| 69 |
String result = skeleton; |
| 70 |
for (KalibroType object : objects) |
| 71 |
result = prepare(result, object); |
| 72 |
return result; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Does what ResultSet.getDouble() does, but when the field content is null, returns a null |
| 77 |
* Double object instead of 0.0 |
| 78 |
*/ |
| 79 |
protected Double getDouble(ResultSet resultSet, String column) throws SQLException { |
| 80 |
double value = resultSet.getDouble(column); |
| 81 |
return resultSet.wasNull() ? null : value; |
| 82 |
} |
| 83 |
} |
| 84 |
|