/*
- * Copyright (C) 2001-2005 The Mir-coders group
+ * Copyright (C) 2001-2006 The Mir-coders group
*
* This file is part of Mir.
*
*
* In addition, as a special exception, The Mir-coders gives permission to link
* the code of this program with any library licensed under the Apache Software License,
- * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
- * (or with modified versions of the above that use the same license as the above),
* and distribute linked combinations including the two. You must obey the
* GNU General Public License in all respects for all of the code used other than
* the above mentioned libraries. If you modify this file, you may extend this
import mir.entity.EntityList;
import mir.entity.StorableObjectEntity;
import mir.log.LoggerWrapper;
-import mir.misc.StringUtil;
import mir.storage.store.*;
import mir.util.JDBCStringRoutines;
import mir.util.StreamCopier;
import org.postgresql.largeobject.LargeObject;
import org.postgresql.largeobject.LargeObjectManager;
-import java.io.*;
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.io.InputStreamReader;
import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
/**
* Implements database access.
*
- * @version $Id: Database.java,v 1.44.2.31 2005/04/16 18:27:31 zapata Exp $
+ * @version $Id: Database.java,v 1.44.2.37 2006/12/25 20:10:22 zapata Exp $
* @author rk
* @author Zapata
*
*/
public class Database {
- private static int DEFAULT_LIMIT = 20;
- private static Class GENERIC_ENTITY_CLASS = mir.entity.StorableObjectEntity.class;
+ private static final int DEFAULT_LIMIT = 20;
+ private static final Class GENERIC_ENTITY_CLASS = StorableObjectEntity.class;
protected static final ObjectStore o_store = ObjectStore.getInstance();
- private static final int _millisPerHour = 60 * 60 * 1000;
protected LoggerWrapper logger;
protected String mainTable;
protected String primaryKeyField = "id";
- protected List fieldNames;
+ private List fieldNames;
private int[] fieldTypes;
private Map fieldNameToType;
//
private Set binaryFields;
- TimeZone timezone;
- SimpleDateFormat internalDateFormat;
- SimpleDateFormat userInputDateFormat;
+ private TimeZone timezone;
+ private SimpleDateFormat userInputDateFormat;
public Database() throws DatabaseFailure {
MirPropertiesConfiguration configuration = MirPropertiesConfiguration.instance();
logger = new LoggerWrapper("Database");
timezone = TimeZone.getTimeZone(configuration.getString("Mir.DefaultTimezone"));
- internalDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- internalDateFormat.setTimeZone(timezone);
userInputDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
userInputDateFormat.setTimeZone(timezone);
binaryFields = new HashSet();
- String theAdaptorName = configuration.getString("Database.Adaptor");
+ String adapterName = configuration.getString("Database.Adaptor");
try {
entityClass = GENERIC_ENTITY_CLASS;
}
catch (Throwable e) {
- logger.error("Error in Database() constructor with " + theAdaptorName + " -- " + e.getMessage());
+ logger.error("Error in Database() constructor with " + adapterName + " -- " + e.getMessage());
throw new DatabaseFailure("Error in Database() constructor.", e);
}
}
break;
case java.sql.Types.NUMERIC:
- /** todo Numeric can be float or double depending upon
- * metadata.getScale() / especially with oracle */
long outl = aResultSet.getLong(aFieldIndex);
if (!aResultSet.wasNull()) {
if (!aResultSet.wasNull()) {
java.util.Date date = new java.util.Date(timestamp.getTime());
-
- Calendar calendar = new GregorianCalendar();
- calendar.setTime(date);
- calendar.setTimeZone(timezone);
- outValue = internalDateFormat.format(date);
-
- int offset = calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET);
- String tzOffset = StringUtil.zeroPaddingNumber(Math.abs(offset) / _millisPerHour, 2, 2);
-
- if (offset<0)
- outValue = outValue + "-";
- else
- outValue = outValue + "+";
- outValue = outValue + tzOffset;
+ outValue = DatabaseHelper.convertDateToInternalRepresenation(date);
}
break;
outValue = "<unsupported value>";
logger.warn("Unsupported Datatype: at " + aFieldIndex + " (" + aType + ")");
}
- } catch (SQLException e) {
+ }
+ catch (SQLException e) {
throw new DatabaseFailure("Could not get Value out of Resultset -- ",
e);
}
}
/**
- * Return an entity specified by id
+ * Return an entity specified by id, or <code>null</code> if no such
+ * entity exists.
*/
public Entity selectById(String anId) throws DatabaseExc {
if ((anId == null) || anId.equals("")) {
anExtraTables=null;
}
- RecordRetriever retriever = new RecordRetriever(mainTable, aMainTablePrefix);
-
// check o_store for entitylist
// only if no relational select
if (anExtraTables==null) {
}
}
+ RecordRetriever retriever = new RecordRetriever(mainTable, aMainTablePrefix);
+
EntityList result = null;
Connection connection = null;
String returnId = null;
Connection con = null;
- PreparedStatement pstmt = null;
try {
String fieldName;
anEntity.setId(returnId);
}
finally {
- try {
- freeConnection(con, pstmt);
- }
- catch (Exception e) {
- }
-
- freeConnection(con, pstmt);
+ freeConnection(con);
}
- /** todo store entity in o_store */
return returnId;
}
* @param theEntity
*/
public void update(Entity theEntity) throws DatabaseFailure {
- Connection connection = null;
-
invalidateStore();
RecordUpdater generator = new RecordUpdater(getTableName(), theEntity.getId());
- String field;
-
// build sql statement
for (int i = 0; i < getFieldNames().size(); i++) {
- field = (String) getFieldNames().get(i);
+ String field = (String) getFieldNames().get(i);
if (!(field.equals(primaryKeyField) ||
- field.equals("webdb_create") ||
- field.equals("webdb_lastchange") ||
+ "webdb_create".equals(field) ||
+ "webdb_lastchange".equals(field) ||
binaryFields.contains(field))) {
if (theEntity.hasFieldValue(field)) {
}
}
}
+ Connection connection = null;
try {
connection = obtainConnection();
public boolean delete(String id) throws DatabaseFailure {
invalidateObject(id);
- /** todo could be prepared Statement */
int resultCode = 0;
Connection connection = obtainConnection();
PreparedStatement statement = null;
aStatement.close();
}
catch (Throwable t) {
- logger.warn("Can't close statemnet: " + t.toString());
+ logger.warn("Can't close statement", t);
}
freeConnection(aConnection);