2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
6 * Mir is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Mir is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Mir; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * In addition, as a special exception, The Mir-coders gives permission to link
21 * the code of this program with any library licensed under the Apache Software License,
22 * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23 * (or with modified versions of the above that use the same license as the above),
24 * and distribute linked combinations including the two. You must obey the
25 * GNU General Public License in all respects for all of the code used other than
26 * the above mentioned libraries. If you modify this file, you may extend this
27 * exception to your version of the file, but you are not obligated to do so.
28 * If you do not wish to do so, delete this exception statement from your version.
32 import java.io.IOException;
33 import java.io.InputStreamReader;
34 import java.sql.Connection;
35 import java.sql.PreparedStatement;
36 import java.sql.ResultSet;
37 import java.sql.ResultSetMetaData;
38 import java.sql.SQLException;
39 import java.sql.Statement;
40 import java.sql.Timestamp;
41 import java.text.ParseException;
42 import java.text.SimpleDateFormat;
43 import java.util.ArrayList;
44 import java.util.Calendar;
45 import java.util.GregorianCalendar;
46 import java.util.HashMap;
47 import java.util.Iterator;
48 import java.util.List;
50 import java.util.TimeZone;
51 import java.util.Vector;
53 import mir.config.MirPropertiesConfiguration;
54 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
55 import mir.entity.Entity;
56 import mir.entity.EntityList;
57 import mir.entity.StorableObjectEntity;
58 import mir.log.LoggerWrapper;
59 import mir.misc.StringUtil;
60 import mir.storage.store.ObjectStore;
61 import mir.storage.store.StorableObject;
62 import mir.storage.store.StoreContainerType;
63 import mir.storage.store.StoreIdentifier;
64 import mir.storage.store.StoreUtil;
65 import mir.util.JDBCStringRoutines;
67 import com.codestudio.util.SQLManager;
71 * Diese Klasse implementiert die Zugriffsschicht auf die Datenbank.
72 * Alle Projektspezifischen Datenbankklassen erben von dieser Klasse.
73 * In den Unterklassen wird im Minimalfall nur die Tabelle angegeben.
74 * Im Konfigurationsfile findet sich eine Verweis auf den verwendeten
75 * Treiber, Host, User und Passwort, ueber den der Zugriff auf die
78 * @version $Id: Database.java,v 1.44.2.22 2004/02/08 21:05:01 zapata Exp $
82 public class Database implements StorageObject {
83 private static Class GENERIC_ENTITY_CLASS = mir.entity.StorableObjectEntity.class;
84 protected static final ObjectStore o_store = ObjectStore.getInstance();
85 private static final int _millisPerHour = 60 * 60 * 1000;
87 protected LoggerWrapper logger;
88 protected MirPropertiesConfiguration configuration;
89 protected String mainTable;
90 protected String primaryKeySequence = null;
91 protected String primaryKeyField = "id";
93 protected boolean evaluatedMetaData = false;
94 protected ArrayList metadataFields;
95 protected ArrayList metadataLabels;
96 protected ArrayList metadataNotNullFields;
97 protected int[] metadataTypes;
98 protected Class theEntityClass;
99 protected boolean hasTimestamp = true;
100 private int defaultLimit;
103 SimpleDateFormat internalDateFormat;
104 SimpleDateFormat userInputDateFormat;
107 * Kontruktor bekommt den Filenamen des Konfigurationsfiles ?bergeben.
108 * Aus diesem file werden <code>Database.Logfile</code>,
109 * <code>Database.Username</code>,<code>Database.Password</code>,
110 * <code>Database.Host</code> und <code>Database.Adaptor</code>
111 * ausgelesen und ein Broker f?r die Verbindugen zur Datenbank
114 public Database() throws StorageObjectFailure {
116 configuration = MirPropertiesConfiguration.instance();
118 catch (PropertiesConfigExc e) {
119 throw new StorageObjectFailure(e);
121 logger = new LoggerWrapper("Database");
122 timezone = TimeZone.getTimeZone(configuration.getString("Mir.DefaultTimezone"));
123 internalDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
124 internalDateFormat.setTimeZone(timezone);
126 userInputDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
127 userInputDateFormat.setTimeZone(timezone);
129 String theAdaptorName = configuration.getString("Database.Adaptor");
130 defaultLimit = Integer.parseInt(configuration.getString("Database.Limit"));
133 theEntityClass = GENERIC_ENTITY_CLASS;
135 catch (Throwable e) {
136 logger.error("Error in Database() constructor with " + theAdaptorName + " -- " + e.getMessage());
137 throw new StorageObjectFailure("Error in Database() constructor.", e);
142 * Liefert die Entity-Klasse zur?ck, in der eine Datenbankzeile gewrappt
143 * wird. Wird die Entity-Klasse durch die erbende Klasse nicht ?berschrieben,
144 * wird eine mir.entity.GenericEntity erzeugt.
146 * @return Class-Objekt der Entity
148 public java.lang.Class getEntityClass() {
149 return theEntityClass;
153 * Liefert die Standardbeschr?nkung von select-Statements zur?ck, also
154 * wieviel Datens?tze per Default selektiert werden.
156 * @return Standard-Anzahl der Datens?tze
158 public int getLimit() {
163 * Liefert den Namen des Primary-Keys zur?ck. Wird die Variable nicht von
164 * der erbenden Klasse ?berschrieben, so ist der Wert <code>PKEY</code>
165 * @return Name des Primary-Keys
167 public String getIdName() {
168 return primaryKeyField;
172 * Liefert den Namen der Tabelle, auf das sich das Datenbankobjekt bezieht.
174 * @return Name der Tabelle
176 public String getTableName() {
181 * Returns the id that was most recently added to the database
183 private String getLatestInsertedId(Connection aConnection) throws SQLException {
184 if (primaryKeySequence==null)
185 primaryKeySequence = mainTable+"_id_seq";
187 PreparedStatement statement = aConnection.prepareStatement("select currval('" + primaryKeySequence + "')");
189 ResultSet rs = statement.executeQuery();
191 return rs.getString(1);
195 * Liefert Feldtypen der Felder der Tabelle zurueck (s.a. java.sql.Types)
196 * @return int-Array mit den Typen der Felder
197 * @exception StorageObjectFailure
199 public int[] getTypes() throws StorageObjectFailure {
200 if (metadataTypes == null) {
204 return metadataTypes;
208 * Liefert eine Liste der Labels der Tabellenfelder
210 public List getLabels() throws StorageObjectFailure {
211 if (metadataLabels == null) {
215 return metadataLabels;
219 * Liefert eine Liste der Felder der Tabelle
220 * @return ArrayList mit Feldern
222 public List getFields() throws StorageObjectFailure {
223 if (metadataFields == null) {
227 return metadataFields;
231 * Gets value out of ResultSet according to type and converts to String
232 * @param rs ResultSet.
233 * @param aType a type from java.sql.Types.*
234 * @param valueIndex index in ResultSet
235 * @return returns the value as String. If no conversion is possible
236 * /unsupported value/ is returned
238 private String getValueAsString(ResultSet rs, int valueIndex, int aType)
239 throws StorageObjectFailure {
240 String outValue = null;
245 case java.sql.Types.BIT:
246 outValue = (rs.getBoolean(valueIndex) == true) ? "1" : "0";
250 case java.sql.Types.INTEGER:
251 case java.sql.Types.SMALLINT:
252 case java.sql.Types.TINYINT:
253 case java.sql.Types.BIGINT:
255 int out = rs.getInt(valueIndex);
258 outValue = new Integer(out).toString();
263 case java.sql.Types.NUMERIC:
265 /** todo Numeric can be float or double depending upon
266 * metadata.getScale() / especially with oracle */
267 long outl = rs.getLong(valueIndex);
270 outValue = new Long(outl).toString();
275 case java.sql.Types.REAL:
277 float tempf = rs.getFloat(valueIndex);
283 int tempf_int = (int) tempf;
284 tempf = (float) tempf_int;
286 outValue = "" + tempf;
287 outValue = outValue.replace('.', ',');
292 case java.sql.Types.DOUBLE:
294 double tempd = rs.getDouble(valueIndex);
300 int tempd_int = (int) tempd;
301 tempd = (double) tempd_int;
303 outValue = "" + tempd;
304 outValue = outValue.replace('.', ',');
309 case java.sql.Types.CHAR:
310 case java.sql.Types.VARCHAR:
311 case java.sql.Types.LONGVARCHAR:
312 outValue = rs.getString(valueIndex);
316 case java.sql.Types.LONGVARBINARY:
317 outValue = rs.getString(valueIndex);
321 case java.sql.Types.TIMESTAMP:
323 // it's important to use Timestamp here as getting it
324 // as a string is undefined and is only there for debugging
325 // according to the API. we can make it a string through formatting.
327 Timestamp timestamp = (rs.getTimestamp(valueIndex));
330 java.util.Date date = new java.util.Date(timestamp.getTime());
332 Calendar calendar = new GregorianCalendar();
333 calendar.setTime(date);
334 calendar.setTimeZone(timezone);
335 outValue = internalDateFormat.format(date);
337 int offset = calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET);
338 String tzOffset = StringUtil.zeroPaddingNumber(Math.abs(offset) / _millisPerHour, 2, 2);
341 outValue = outValue + "-";
343 outValue = outValue + "+";
344 outValue = outValue + tzOffset;
350 outValue = "<unsupported value>";
351 logger.warn("Unsupported Datatype: at " + valueIndex + " (" + aType + ")");
353 } catch (SQLException e) {
354 throw new StorageObjectFailure("Could not get Value out of Resultset -- ",
363 * select-Operator um einen Datensatz zu bekommen.
364 * @param id Primaerschluessel des Datensatzes.
365 * @return liefert EntityObject des gefundenen Datensatzes oder null.
367 public Entity selectById(String id) throws StorageObjectExc {
368 if ((id == null) || id.equals("")) {
369 throw new StorageObjectExc("Database.selectById: Missing id");
372 // ask object store for object
373 if (StoreUtil.extendsStorableEntity(theEntityClass)) {
374 String uniqueId = id;
376 if (theEntityClass.equals(StorableObjectEntity.class)) {
377 uniqueId += ("@" + mainTable);
380 StoreIdentifier search_sid = new StoreIdentifier(theEntityClass, uniqueId);
381 logger.debug("CACHE: (dbg) looking for sid " + search_sid.toString());
383 Entity hit = (Entity) o_store.use(search_sid);
390 Statement stmt = null;
391 Connection con = getPooledCon();
392 Entity returnEntity = null;
397 /** todo better prepared statement */
399 "select * from " + mainTable + " where " + primaryKeyField + "=" + id;
400 stmt = con.createStatement();
401 rs = executeSql(stmt, selectSql);
404 if (evaluatedMetaData == false) {
405 evalMetaData(rs.getMetaData());
409 returnEntity = makeEntityFromResultSet(rs);
412 logger.warn("No data for id: " + id + " in table " + mainTable);
418 logger.warn("No Data for Id " + id + " in Table " + mainTable);
421 catch (SQLException sqe) {
422 throwSQLException(sqe, "selectById");
425 catch (NumberFormatException e) {
426 logger.error("ID is no number: " + id);
429 freeConnection(con, stmt);
436 * This method makes it possible to make selects across multiple tables
438 * @param mainTablePrefix prefix for the mainTable
439 * @param extraTables a vector of tables for relational select
440 * @param aWhereClause whereClause
441 * @return EntityList of selected Objects
442 * @throws StorageObjectFailure
445 public EntityList selectByWhereClauseWithExtraTables(String mainTablePrefix,
446 List extraTables, String aWhereClause )
447 throws StorageObjectFailure {
448 return selectByWhereClause( mainTablePrefix, extraTables, aWhereClause, "", 0, defaultLimit);
452 * select-Operator um Datensaetze zu bekommen, die key = value erfuellen.
453 * @param aField Datenbankfeld der Bedingung.
454 * @param aValue Wert die der key anehmen muss.
455 * @return EntityList mit den gematchten Entities
457 public EntityList selectByFieldValue(String aField, String aValue) throws StorageObjectFailure {
458 return selectByFieldValue(aField, aValue, 0);
462 * select-Operator um Datensaetze zu bekommen, die key = value erfuellen.
463 * @param aField Datenbankfeld der Bedingung.
464 * @param aValue Wert die der key anehmen muss.
465 * @param offset Gibt an ab welchem Datensatz angezeigt werden soll.
466 * @return EntityList mit den gematchten Entities
468 public EntityList selectByFieldValue(String aField, String aValue, int offset) throws StorageObjectFailure {
469 return selectByWhereClause(aField + "=" + aValue, offset);
473 * select-Operator liefert eine EntityListe mit den gematchten Datens?tzen zur?ck.
474 * Also offset wird der erste Datensatz genommen.
476 * @param where where-Clause
477 * @return EntityList mit den gematchten Entities
478 * @exception StorageObjectFailure
480 public EntityList selectByWhereClause(String where) throws StorageObjectFailure {
481 return selectByWhereClause(where, 0);
485 * select-Operator liefert eine EntityListe mit den gematchten Datens?tzen zur?ck.
486 * Als maximale Anzahl wird das Limit auf der Konfiguration genommen.
488 * @param whereClause where-Clause
489 * @param offset ab welchem Datensatz.
490 * @return EntityList mit den gematchten Entities
491 * @exception StorageObjectFailure
493 public EntityList selectByWhereClause(String whereClause, int offset) throws StorageObjectFailure {
494 return selectByWhereClause(whereClause, null, offset);
498 * select-Operator liefert eine EntityListe mit den gematchten Datens?tzen zur?ck.
499 * Also offset wird der erste Datensatz genommen.
500 * Als maximale Anzahl wird das Limit auf der Konfiguration genommen.
502 * @param where where-Clause
503 * @param order orderBy-Clause
504 * @return EntityList mit den gematchten Entities
505 * @exception StorageObjectFailure
507 public EntityList selectByWhereClause(String where, String order) throws StorageObjectFailure {
508 return selectByWhereClause(where, order, 0);
511 public EntityList selectByWhereClause(String mainTablePrefix, List extraTables, String where, String order) throws StorageObjectFailure {
512 return selectByWhereClause(mainTablePrefix, extraTables, where, order, 0, defaultLimit);
516 * select-Operator liefert eine EntityListe mit den gematchten Datens?tzen zur?ck.
517 * Als maximale Anzahl wird das Limit auf der Konfiguration genommen.
519 * @param whereClause where-Clause
520 * @param orderBy orderBy-Clause
521 * @param offset ab welchem Datensatz
522 * @return EntityList mit den gematchten Entities
523 * @exception StorageObjectFailure
525 public EntityList selectByWhereClause(String whereClause, String orderBy, int offset) throws StorageObjectFailure {
526 return selectByWhereClause(whereClause, orderBy, offset, defaultLimit);
530 * select-Operator returns EntityList with matching rows in Database.
531 * @param aWhereClause where-Clause
532 * @param anOrderByClause orderBy-Clause
533 * @param offset ab welchem Datensatz
534 * @param limit wieviele Datens?tze
535 * @return EntityList mit den gematchten Entities
536 * @exception StorageObjectFailure
538 public EntityList selectByWhereClause(String aWhereClause, String anOrderByClause,
539 int offset, int limit) throws StorageObjectFailure {
540 return selectByWhereClause("", null, aWhereClause, anOrderByClause, offset, limit);
545 * select-Operator returns EntityList with matching rows in Database.
546 * @param aWhereClause where-Clause
547 * @param anOrderByClause orderBy-Clause
548 * @param offset ab welchem Datensatz
549 * @param limit wieviele Datens?tze
550 * @return EntityList mit den gematchten Entities
551 * @exception StorageObjectFailure
553 public EntityList selectByWhereClause(String mainTablePrefix, List extraTables,
554 String aWhereClause, String anOrderByClause,
555 int offset, int limit) throws StorageObjectFailure {
557 // TODO get rid of emtpy Strings in extraTables
558 // make extraTables null, if single empty String in it
559 // cause StringUtil.splitString puts in emptyString
560 if (extraTables != null && ((String)extraTables.get(0)).trim().equals(""))
562 logger.debug("+++ made extraTables to null!");
566 String useTable = mainTable;
567 String selectStar = "*";
568 if (mainTablePrefix!=null && mainTablePrefix.trim().length()>0) {
569 useTable+=" "+mainTablePrefix;
570 selectStar=mainTablePrefix.trim() + ".*";
573 // check o_store for entitylist
574 // only if no relational select
575 if (extraTables==null) {
576 if (StoreUtil.extendsStorableEntity(theEntityClass)) {
577 StoreIdentifier searchSid = new StoreIdentifier(theEntityClass,
578 StoreContainerType.STOC_TYPE_ENTITYLIST,
579 StoreUtil.getEntityListUniqueIdentifierFor(mainTable,
580 aWhereClause, anOrderByClause, offset, limit));
581 EntityList hit = (EntityList) o_store.use(searchSid);
590 EntityList theReturnList = null;
591 Connection con = null;
592 Statement stmt = null;
597 // build sql-statement
599 if ((aWhereClause != null) && (aWhereClause.trim().length() == 0)) {
603 StringBuffer countSql =
604 new StringBuffer("select count(*) from ").append(useTable);
605 StringBuffer selectSql =
606 new StringBuffer("select "+selectStar+" from ").append(useTable);
608 // append extratables, if necessary
609 if (extraTables!=null) {
610 for (int i=0;i < extraTables.size();i++) {
611 if (!extraTables.get(i).equals("")) {
612 countSql.append( ", " + extraTables.get(i));
613 selectSql.append( ", " + extraTables.get(i));
618 if (aWhereClause != null) {
619 selectSql.append(" where ").append(aWhereClause);
620 countSql.append(" where ").append(aWhereClause);
623 if ((anOrderByClause != null) && !(anOrderByClause.trim().length() == 0)) {
624 selectSql.append(" order by ").append(anOrderByClause);
627 if ((limit > -1) && (offset > -1)) {
628 selectSql.append(" LIMIT ").append(limit).append(" OFFSET ").append(offset);
633 con = getPooledCon();
634 stmt = con.createStatement();
637 rs = executeSql(stmt, selectSql.toString());
640 if (!evaluatedMetaData) {
641 evalMetaData(rs.getMetaData());
644 theReturnList = new EntityList();
645 Entity theResultEntity;
647 theResultEntity = makeEntityFromResultSet(rs);
648 theReturnList.add(theResultEntity);
654 // making entitylist infos
657 if (theReturnList != null) {
658 // now we decide if we have to know an overall count...
661 if ((limit > -1) && (offset > -1)) {
662 if (offsetCount == limit) {
663 rs = executeSql(stmt, countSql.toString());
667 count = rs.getInt(1);
673 logger.error("Could not count: " + countSql);
678 theReturnList.setCount(count);
679 theReturnList.setOffset(offset);
680 theReturnList.setWhere(aWhereClause);
681 theReturnList.setOrder(anOrderByClause);
682 theReturnList.setStorage(this);
683 theReturnList.setLimit(limit);
685 if (offset >= limit) {
686 theReturnList.setPrevBatch(offset - limit);
689 if ((offset + offsetCount) < count) {
690 theReturnList.setNextBatch(offset + limit);
693 if (extraTables==null && StoreUtil.extendsStorableEntity(theEntityClass)) {
694 StoreIdentifier sid = theReturnList.getStoreIdentifier();
695 logger.debug("CACHE (add): " + sid.toString());
700 catch (SQLException sqe) {
701 throwSQLException(sqe, "selectByWhereClause");
706 freeConnection(con, stmt);
708 } catch (Throwable t) {
712 return theReturnList;
716 * Bastelt aus einer Zeile der Datenbank ein EntityObjekt.
718 * @param rs Das ResultSetObjekt.
719 * @return Entity Die Entity.
721 private Entity makeEntityFromResultSet(ResultSet rs)
722 throws StorageObjectFailure {
723 Map theResultHash = new HashMap();
724 String theResult = null;
726 Entity returnEntity = null;
729 if (StoreUtil.extendsStorableEntity(theEntityClass)) {
730 StoreIdentifier searchSid = StorableObjectEntity.getStoreIdentifier(this,
732 Entity hit = (Entity) o_store.use(searchSid);
733 if (hit != null) return hit;
737 int size = metadataFields.size();
739 for (int i = 0; i < size; i++) {
740 // alle durchlaufen bis nix mehr da
741 theType = metadataTypes[i];
743 if (theType == java.sql.Types.LONGVARBINARY) {
744 InputStreamReader is =
745 (InputStreamReader) rs.getCharacterStream(i + 1);
748 char[] data = new char[32768];
749 StringBuffer theResultString = new StringBuffer();
752 while ((len = is.read(data)) > 0) {
753 theResultString.append(data, 0, len);
757 theResult = theResultString.toString();
762 theResult = getValueAsString(rs, (i + 1), theType);
765 if (theResult != null) {
766 theResultHash.put(metadataFields.get(i), theResult);
770 if (theEntityClass != null) {
771 returnEntity = (Entity) theEntityClass.newInstance();
772 returnEntity.setStorage(this);
773 returnEntity.setFieldValues(theResultHash);
775 if (returnEntity instanceof StorableObject) {
776 logger.debug("CACHE: ( in) " + returnEntity.getId() + " :" + mainTable);
777 o_store.add(((StorableObject) returnEntity).getStoreIdentifier());
780 throwStorageObjectException("Internal Error: theEntityClass not set!");
783 catch (IllegalAccessException e) {
784 throwStorageObjectException("No access! -- " + e.getMessage());
786 catch (IOException e) {
787 throwStorageObjectException("IOException! -- " + e.getMessage());
789 catch (InstantiationException e) {
790 throwStorageObjectException("No Instatiation! -- " + e.getMessage());
792 catch (SQLException sqe) {
793 throwSQLException(sqe, "makeEntityFromResultSet");
802 * Inserts an entity into the database.
805 * @return der Wert des Primary-keys der eingef?gten Entity
807 public String insert(Entity theEntity) throws StorageObjectFailure {
809 invalidatePopupCache();
811 // invalidating all EntityLists corresponding with theEntityClass
812 if (StoreUtil.extendsStorableEntity(theEntityClass)) {
813 StoreContainerType stoc_type =
814 StoreContainerType.valueOf(theEntityClass,
815 StoreContainerType.STOC_TYPE_ENTITYLIST);
816 o_store.invalidate(stoc_type);
819 String returnId = null;
820 Connection con = null;
821 PreparedStatement pstmt = null;
824 StringBuffer f = new StringBuffer();
825 StringBuffer v = new StringBuffer();
828 boolean firstField = true;
831 for (int i = 0; i < getFields().size(); i++) {
832 aField = (String) getFields().get(i);
834 if (!aField.equals(primaryKeyField)) {
838 if (!theEntity.hasFieldValue(aField) && (
839 aField.equals("webdb_create") ||
840 aField.equals("webdb_lastchange"))) {
844 if (theEntity.hasFieldValue(aField)) {
847 JDBCStringRoutines.escapeStringLiteral(theEntity.getFieldValue(aField)) + "'";
851 // wenn Wert gegeben, dann einbauen
852 if (aValue != null) {
853 if (firstField == false) {
869 StringBuffer sqlBuf =
870 new StringBuffer("insert into ").append(mainTable).append("(").append(f)
871 .append(") values (").append(v).append(")");
872 String sql = sqlBuf.toString();
874 logger.info("INSERT: " + sql);
875 con = getPooledCon();
876 con.setAutoCommit(false);
877 pstmt = con.prepareStatement(sql);
879 int ret = pstmt.executeUpdate();
886 // pstmt = con.prepareStatement("select currval('" + + "_id_seq')");
888 returnId = getLatestInsertedId(con);
889 theEntity.setId(returnId);
891 catch (SQLException sqe) {
892 throwSQLException(sqe, "insert");
896 con.setAutoCommit(true);
898 catch (Exception e) {
901 freeConnection(con, pstmt);
904 /** todo store entity in o_store */
909 * Updates an entity in the database
913 public void update(Entity theEntity) throws StorageObjectFailure {
914 Connection con = null;
915 PreparedStatement pstmt = null;
917 /** todo this is stupid: why do we prepare statement, when we
918 * throw it away afterwards. should be regular statement
919 * update/insert could better be one routine called save()
920 * that chooses to either insert or update depending if we
921 * have a primary key in the entity. i don't know if we
922 * still need the streamed input fields. // rk */
923 /** todo extension: check if Entity did change, otherwise we don't need
924 * the roundtrip to the database */
925 /** invalidating corresponding entitylists in o_store*/
926 if (StoreUtil.extendsStorableEntity(theEntityClass)) {
927 StoreContainerType stoc_type =
928 StoreContainerType.valueOf(theEntityClass,
929 StoreContainerType.STOC_TYPE_ENTITYLIST);
930 o_store.invalidate(stoc_type);
933 String id = theEntity.getId();
935 StringBuffer fv = new StringBuffer();
936 boolean firstField = true;
939 invalidatePopupCache();
941 // build sql statement
942 for (int i = 0; i < getFields().size(); i++) {
943 aField = (String) metadataFields.get(i);
946 // todo if entity.hasFieldValue returns false, then the value should be stored as null
947 if (!(aField.equals(primaryKeyField) ||
948 aField.equals("webdb_create") ||
949 aField.equals("webdb_lastchange"))) {
950 if (theEntity.hasFieldValue(aField)) {
951 if (firstField == false) {
958 fv.append(aField).append("='").append(JDBCStringRoutines.escapeStringLiteral(theEntity.getFieldValue(aField))).append("'");
960 // fv.append(aField).append("='").append(StringUtil.quote((String)theEntity.getFieldValue(aField))).append("'");
966 new StringBuffer("update ").append(mainTable).append(" set ").append(fv);
969 if (metadataFields.contains("webdb_lastchange")) {
970 sql.append(",webdb_lastchange=NOW()");
973 // special case: the webdb_create requires the field in yyyy-mm-dd HH:mm
974 // format so anything extra will be ignored. -mh
975 if (metadataFields.contains("webdb_create") &&
976 theEntity.hasFieldValue("webdb_create")) {
977 // minimum of 10 (yyyy-mm-dd)...
978 if (theEntity.getFieldValue("webdb_create").length() >= 10) {
979 String dateString = theEntity.getFieldValue("webdb_create");
981 // if only 10, then add 00:00 so it doesn't throw a ParseException
982 if (dateString.length() == 10) {
983 dateString = dateString + " 00:00";
988 java.util.Date d = userInputDateFormat.parse(dateString);
989 // Timestamp tStamp = new Timestamp(d.getTime());
990 sql.append(",webdb_create='" + JDBCStringRoutines.formatDate(d) + "'");
992 catch (ParseException e) {
993 throw new StorageObjectFailure(e);
998 sql.append(" where id=").append(id);
999 logger.info("UPDATE: " + sql);
1002 con = getPooledCon();
1003 con.setAutoCommit(false);
1004 pstmt = con.prepareStatement(sql.toString());
1006 pstmt.executeUpdate();
1008 catch (SQLException sqe) {
1009 throwSQLException(sqe, "update");
1013 con.setAutoCommit(true);
1015 catch (Exception e) {
1019 freeConnection(con, pstmt);
1025 * @param id des zu loeschenden Datensatzes
1026 * @return boolean liefert true zurueck, wenn loeschen erfolgreich war.
1028 public boolean delete(String id) throws StorageObjectFailure {
1029 invalidatePopupCache();
1031 // ostore send notification
1032 if (StoreUtil.extendsStorableEntity(theEntityClass)) {
1033 String uniqueId = id;
1035 if (theEntityClass.equals(StorableObjectEntity.class)) {
1036 uniqueId += ("@" + mainTable);
1039 logger.debug("CACHE: (del) " + id);
1041 StoreIdentifier search_sid =
1042 new StoreIdentifier(theEntityClass,
1043 StoreContainerType.STOC_TYPE_ENTITY, uniqueId);
1044 o_store.invalidate(search_sid);
1047 /** todo could be prepared Statement */
1048 Statement stmt = null;
1049 Connection con = null;
1052 "delete from " + mainTable + " where " + primaryKeyField + "='" + id + "'";
1054 //theLog.printInfo("DELETE " + sql);
1056 con = getPooledCon();
1057 stmt = con.createStatement();
1058 res = stmt.executeUpdate(sql);
1059 } catch (SQLException sqe) {
1060 throwSQLException(sqe, "delete");
1062 freeConnection(con, stmt);
1065 return (res > 0) ? true : false;
1069 * Deletes entities based on a where clause
1071 * @param aWhereClause
1073 * @throws StorageObjectFailure
1075 public int deleteByWhereClause(String aWhereClause) throws StorageObjectFailure {
1076 invalidatePopupCache();
1077 if (StoreUtil.extendsStorableEntity(theEntityClass)) {
1078 StoreContainerType stoc_type = StoreContainerType.valueOf(theEntityClass, StoreContainerType.STOC_TYPE_ENTITYLIST);
1079 o_store.invalidate(stoc_type);
1082 Statement stmt = null;
1083 Connection con = null;
1086 "delete from " + mainTable + " where " + aWhereClause;
1088 //theLog.printInfo("DELETE " + sql);
1090 con = getPooledCon();
1091 stmt = con.createStatement();
1092 res = stmt.executeUpdate(sql);
1094 catch (SQLException sqe) {
1095 throwSQLException(sqe, "delete");
1098 freeConnection(con, stmt);
1104 /* noch nicht implementiert.
1105 * @return immer false
1107 public boolean delete(EntityList theEntityList) {
1108 invalidatePopupCache();
1113 protected void invalidatePopupCache() {
1114 /** todo invalidates toooo much */
1118 * Diese Methode fuehrt den Sqlstring <i>sql</i> aus und timed im Logfile.
1119 * @param stmt Statemnt
1120 * @param sql Sql-String
1122 public ResultSet executeSql(Statement stmt, String sql)
1123 throws StorageObjectFailure, SQLException {
1125 long startTime = System.currentTimeMillis();
1128 rs = stmt.executeQuery(sql);
1130 logger.info((System.currentTimeMillis() - startTime) + "ms. for: " + sql);
1132 catch (SQLException e) {
1133 logger.error(e.getMessage() +"\n" + (System.currentTimeMillis() - startTime) + "ms. for: " + sql);
1140 private Map processRow(ResultSet aResultSet) throws StorageObjectFailure {
1142 Map result = new HashMap();
1143 ResultSetMetaData metaData = aResultSet.getMetaData();
1144 int nrColumns = metaData.getColumnCount();
1145 for (int i=0; i<nrColumns; i++) {
1146 result.put(metaData.getColumnName(i+1), getValueAsString(aResultSet, i+1, metaData.getColumnType(i+1)));
1151 catch (Throwable e) {
1152 throw new StorageObjectFailure(e);
1156 public List executeFreeSql(String sql, int aLimit) throws StorageObjectFailure, StorageObjectExc {
1157 Connection connection = null;
1158 Statement statement = null;
1160 List result = new Vector();
1161 connection = getPooledCon();
1162 statement = connection.createStatement();
1163 ResultSet resultset = executeSql(statement, sql);
1165 while (resultset.next() && result.size() < aLimit) {
1166 result.add(processRow(resultset));
1175 catch (Throwable e) {
1176 throw new StorageObjectFailure(e);
1179 if (connection!=null) {
1180 freeConnection(connection, statement);
1185 public Map executeFreeSingleRowSql(String anSqlStatement) throws StorageObjectFailure, StorageObjectExc {
1187 List resultList = executeFreeSql(anSqlStatement, 1);
1189 if (resultList.size()>0)
1190 return (Map) resultList.get(0);
1197 catch (Throwable t) {
1198 throw new StorageObjectFailure(t);
1202 public String executeFreeSingleValueSql(String sql) throws StorageObjectFailure, StorageObjectExc {
1203 Map row = executeFreeSingleRowSql(sql);
1208 Iterator i = row.values().iterator();
1210 return (String) i.next();
1215 public int getSize(String where) throws SQLException, StorageObjectFailure {
1216 return getSize("", null, where);
1219 * returns the number of rows in the table
1221 public int getSize(String mainTablePrefix, List extraTables, String where) throws SQLException, StorageObjectFailure {
1223 long startTime = System.currentTimeMillis();
1225 String useTable = mainTable;
1226 if (mainTablePrefix!=null && mainTablePrefix.trim().length()>0) {
1227 useTable+=" "+mainTablePrefix;
1229 StringBuffer countSql =
1230 new StringBuffer("select count(*) from ").append(useTable);
1231 // append extratables, if necessary
1232 if (extraTables!=null) {
1233 for (int i=0;i < extraTables.size();i++) {
1234 if (!extraTables.get(i).equals("")) {
1235 countSql.append( ", " + extraTables.get(i));
1240 if ((where != null) && (where.length() != 0)) {
1241 countSql.append( " where " + where);
1244 Connection con = null;
1245 Statement stmt = null;
1249 con = getPooledCon();
1250 stmt = con.createStatement();
1252 ResultSet rs = executeSql(stmt, countSql.toString());
1255 result = rs.getInt(1);
1258 catch (SQLException e) {
1259 logger.error("Database.getSize: " + e.getMessage());
1262 freeConnection(con, stmt);
1264 logger.info((System.currentTimeMillis() - startTime) + "ms. for: " + countSql);
1269 public int executeUpdate(Statement stmt, String sql)
1270 throws StorageObjectFailure, SQLException {
1272 long startTime = System.currentTimeMillis();
1275 rs = stmt.executeUpdate(sql);
1277 logger.info((System.currentTimeMillis() - startTime) + "ms. for: " + sql);
1279 catch (SQLException e) {
1280 logger.error("Failed: " + (System.currentTimeMillis() - startTime) + "ms. for: " + sql);
1287 public int executeUpdate(String sql)
1288 throws StorageObjectFailure, SQLException {
1290 long startTime = System.currentTimeMillis();
1291 Connection con = null;
1292 PreparedStatement pstmt = null;
1295 con = getPooledCon();
1296 pstmt = con.prepareStatement(sql);
1297 result = pstmt.executeUpdate();
1299 catch (Throwable e) {
1300 logger.error("Database.executeUpdate(" + sql + "): " + e.getMessage());
1301 throw new StorageObjectFailure("Database.executeUpdate(" + sql + "): " + e.getMessage(), e);
1304 freeConnection(con, pstmt);
1307 logger.info((System.currentTimeMillis() - startTime) + "ms. for: " + sql);
1312 * Wertet ResultSetMetaData aus und setzt interne Daten entsprechend
1313 * @param md ResultSetMetaData
1315 private void evalMetaData(ResultSetMetaData md) throws StorageObjectFailure {
1316 this.evaluatedMetaData = true;
1317 this.metadataFields = new ArrayList();
1318 this.metadataLabels = new ArrayList();
1319 this.metadataNotNullFields = new ArrayList();
1322 int numFields = md.getColumnCount();
1323 this.metadataTypes = new int[numFields];
1328 for (int i = 1; i <= numFields; i++) {
1329 aField = md.getColumnName(i);
1330 metadataFields.add(aField);
1331 metadataLabels.add(md.getColumnLabel(i));
1332 aType = md.getColumnType(i);
1333 metadataTypes[i - 1] = aType;
1335 if (aField.equals(primaryKeyField)) {
1338 if (md.isNullable(i) == ResultSetMetaData.columnNullable) {
1339 metadataNotNullFields.add(aField);
1343 catch (SQLException e) {
1344 throwSQLException(e, "evalMetaData");
1349 * Wertet die Metadaten eines Resultsets fuer eine Tabelle aus,
1350 * um die alle Columns und Typen einer Tabelle zu ermitteln.
1352 private void get_meta_data() throws StorageObjectFailure {
1353 Connection con = null;
1354 PreparedStatement pstmt = null;
1355 String sql = "select * from " + mainTable + " where 0=1";
1358 con = getPooledCon();
1359 pstmt = con.prepareStatement(sql);
1361 logger.debug("METADATA: " + sql);
1362 ResultSet rs = pstmt.executeQuery();
1363 evalMetaData(rs.getMetaData());
1366 catch (SQLException e) {
1367 throwSQLException(e, "get_meta_data");
1370 freeConnection(con, pstmt);
1374 public Connection getPooledCon() throws StorageObjectFailure {
1375 Connection con = null;
1378 con = SQLManager.getInstance().requestConnection();
1380 catch (SQLException e) {
1381 logger.error("could not connect to the database " + e.getMessage());
1383 throw new StorageObjectFailure("Could not connect to the database", e);
1389 public void freeConnection(Connection con, Statement stmt)
1390 throws StorageObjectFailure {
1391 SQLManager.closeStatement(stmt);
1392 SQLManager.getInstance().returnConnection(con);
1396 * Wertet SQLException aus und wirft dannach eine StorageObjectException
1397 * @param sqe SQLException
1398 * @param aFunction Funktonsname, in der die SQLException geworfen wurde
1400 protected void throwSQLException(SQLException sqe, String aFunction) throws StorageObjectFailure {
1402 String message = "";
1406 state = sqe.getSQLState();
1407 message = sqe.getMessage();
1408 vendor = sqe.getErrorCode();
1411 String information =
1414 ", vendor= " + vendor +
1415 ", message=" + message +
1416 ", function= " + aFunction;
1418 logger.error(information);
1420 throw new StorageObjectFailure(information, sqe);
1423 protected void _throwStorageObjectException(Exception e, String aFunction)
1424 throws StorageObjectFailure {
1427 logger.error(e.getMessage() + aFunction);
1428 throw new StorageObjectFailure(aFunction, e);
1433 * Loggt Fehlermeldung mit dem Parameter Message und wirft dannach
1434 * eine StorageObjectException
1435 * @param aMessage Nachricht mit dem Fehler
1436 * @exception StorageObjectFailure
1438 void throwStorageObjectException(String aMessage) throws StorageObjectFailure {
1439 logger.error(aMessage);
1440 throw new StorageObjectFailure(aMessage, null);