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;
50 import com.codestudio.util.SQLManager;
52 import freemarker.template.SimpleHash;
53 import freemarker.template.SimpleList;
55 import mir.config.MirPropertiesConfiguration;
56 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
57 import mir.entity.Entity;
58 import mir.entity.EntityList;
59 import mir.entity.StorableObjectEntity;
60 import mir.log.LoggerWrapper;
61 import mir.misc.HTMLTemplateProcessor;
62 import mir.misc.StringUtil;
63 import mir.storage.store.ObjectStore;
64 import mir.storage.store.StorableObject;
65 import mir.storage.store.StoreContainerType;
66 import mir.storage.store.StoreIdentifier;
67 import mir.storage.store.StoreUtil;
68 import mir.util.JDBCStringRoutines;
72 * Diese Klasse implementiert die Zugriffsschicht auf die Datenbank.
73 * Alle Projektspezifischen Datenbankklassen erben von dieser Klasse.
74 * In den Unterklassen wird im Minimalfall nur die Tabelle angegeben.
75 * Im Konfigurationsfile findet sich eine Verweis auf den verwendeten
76 * Treiber, Host, User und Passwort, ueber den der Zugriff auf die
79 * @version $Id: Database.java,v 1.44.2.3 2003/05/26 03:29:46 zapata Exp $
83 public class Database implements StorageObject {
84 private static Class GENERIC_ENTITY_CLASS = mir.entity.StorableObjectEntity.class;
85 private static Class STORABLE_OBJECT_ENTITY_CLASS = mir.entity.StorableObjectEntity.class;
88 private static SimpleHash POPUP_EMPTYLINE = new SimpleHash();
89 protected static final ObjectStore o_store = ObjectStore.getInstance();
90 private static final int _millisPerHour = 60 * 60 * 1000;
91 private static final int _millisPerMinute = 60 * 1000;
94 // always same object saves a little space
95 POPUP_EMPTYLINE.put("key", "");
96 POPUP_EMPTYLINE.put("value", "--");
99 protected LoggerWrapper logger;
100 protected MirPropertiesConfiguration configuration;
101 protected String theTable;
102 protected String theCoreTable = null;
103 protected String thePKeyName = "id";
104 protected int thePKeyType;
105 protected int thePKeyIndex;
106 protected boolean evaluatedMetaData = false;
107 protected ArrayList metadataFields;
108 protected ArrayList metadataLabels;
109 protected ArrayList metadataNotNullFields;
110 protected int[] metadataTypes;
111 protected Class theEntityClass;
112 protected SimpleList popupCache = null;
113 protected boolean hasPopupCache = false;
114 protected SimpleHash hashCache = null;
115 protected boolean hasTimestamp = true;
116 private String database_driver;
117 private String database_url;
118 private int defaultLimit;
119 protected DatabaseAdaptor theAdaptor;
120 private SimpleDateFormat _dateFormatterOut =
121 new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
122 private SimpleDateFormat _dateFormatterIn =
123 new SimpleDateFormat("yyyy-MM-dd HH:mm");
124 private Calendar _cal = new GregorianCalendar();
127 * Kontruktor bekommt den Filenamen des Konfigurationsfiles ?bergeben.
128 * Aus diesem file werden <code>Database.Logfile</code>,
129 * <code>Database.Username</code>,<code>Database.Password</code>,
130 * <code>Database.Host</code> und <code>Database.Adaptor</code>
131 * ausgelesen und ein Broker f?r die Verbindugen zur Datenbank
134 * @param String confFilename Dateiname der Konfigurationsdatei
136 public Database() throws StorageObjectFailure {
138 configuration = MirPropertiesConfiguration.instance();
140 catch (PropertiesConfigExc e) {
141 throw new StorageObjectFailure(e);
143 logger = new LoggerWrapper("Database");
145 String theAdaptorName = configuration.getString("Database.Adaptor");
146 defaultLimit = Integer.parseInt(configuration.getString("Database.Limit"));
149 theEntityClass = GENERIC_ENTITY_CLASS;
150 theAdaptor = (DatabaseAdaptor) Class.forName(theAdaptorName).newInstance();
152 catch (Throwable e) {
153 logger.error("Error in Database() constructor with " + theAdaptorName + " -- " + e.getMessage());
154 throw new StorageObjectFailure("Error in Database() constructor.", e);
159 * Liefert die Entity-Klasse zur?ck, in der eine Datenbankzeile gewrappt
160 * wird. Wird die Entity-Klasse durch die erbende Klasse nicht ?berschrieben,
161 * wird eine mir.entity.GenericEntity erzeugt.
163 * @return Class-Objekt der Entity
165 public java.lang.Class getEntityClass() {
166 return theEntityClass;
170 * Liefert die Standardbeschr?nkung von select-Statements zur?ck, also
171 * wieviel Datens?tze per Default selektiert werden.
173 * @return Standard-Anzahl der Datens?tze
175 public int getLimit() {
180 * Liefert den Namen des Primary-Keys zur?ck. Wird die Variable nicht von
181 * der erbenden Klasse ?berschrieben, so ist der Wert <code>PKEY</code>
182 * @return Name des Primary-Keys
184 public String getIdName() {
189 * Liefert den Namen der Tabelle, auf das sich das Datenbankobjekt bezieht.
191 * @return Name der Tabelle
193 public String getTableName() {
198 * Dient dazu vererbte Tabellen bei objectrelationalen DBMS
199 * zu speichern, wenn die id einer Tabelle in der parenttabelle verwaltet
201 * @return liefert theCoreTabel als String zurueck, wenn gesetzt, sonst
204 public String getCoreTable() {
205 if (theCoreTable != null) {
214 * Liefert Feldtypen der Felder der Tabelle zurueck (s.a. java.sql.Types)
215 * @return int-Array mit den Typen der Felder
216 * @exception StorageObjectException
218 public int[] getTypes() throws StorageObjectFailure {
219 if (metadataTypes == null) {
223 return metadataTypes;
227 * Liefert eine Liste der Labels der Tabellenfelder
228 * @return ArrayListe mit Labeln
229 * @exception StorageObjectException
231 public List getLabels() throws StorageObjectFailure {
232 if (metadataLabels == null) {
236 return metadataLabels;
240 * Liefert eine Liste der Felder der Tabelle
241 * @return ArrayList mit Feldern
242 * @exception StorageObjectException
244 public List getFields() throws StorageObjectFailure {
245 if (metadataFields == null) {
249 return metadataFields;
253 * Gets value out of ResultSet according to type and converts to String
254 * @param inValue Wert aus ResultSet.
255 * @param aType Datenbanktyp.
256 * @return liefert den Wert als String zurueck. Wenn keine Umwandlung moeglich
257 * dann /unsupported value/
259 private String getValueAsString(ResultSet rs, int valueIndex, int aType)
260 throws StorageObjectFailure {
261 String outValue = null;
266 case java.sql.Types.BIT:
267 outValue = (rs.getBoolean(valueIndex) == true) ? "1" : "0";
271 case java.sql.Types.INTEGER:
272 case java.sql.Types.SMALLINT:
273 case java.sql.Types.TINYINT:
274 case java.sql.Types.BIGINT:
276 int out = rs.getInt(valueIndex);
279 outValue = new Integer(out).toString();
284 case java.sql.Types.NUMERIC:
286 /** @todo Numeric can be float or double depending upon
287 * metadata.getScale() / especially with oracle */
288 long outl = rs.getLong(valueIndex);
291 outValue = new Long(outl).toString();
296 case java.sql.Types.REAL:
298 float tempf = rs.getFloat(valueIndex);
304 int tempf_int = (int) tempf;
305 tempf = (float) tempf_int;
307 outValue = "" + tempf;
308 outValue = outValue.replace('.', ',');
313 case java.sql.Types.DOUBLE:
315 double tempd = rs.getDouble(valueIndex);
321 int tempd_int = (int) tempd;
322 tempd = (double) tempd_int;
324 outValue = "" + tempd;
325 outValue = outValue.replace('.', ',');
330 case java.sql.Types.CHAR:
331 case java.sql.Types.VARCHAR:
332 case java.sql.Types.LONGVARCHAR:
333 outValue = rs.getString(valueIndex);
337 case java.sql.Types.LONGVARBINARY:
338 outValue = rs.getString(valueIndex);
342 case java.sql.Types.TIMESTAMP:
344 // it's important to use Timestamp here as getting it
345 // as a string is undefined and is only there for debugging
346 // according to the API. we can make it a string through formatting.
348 Timestamp timestamp = (rs.getTimestamp(valueIndex));
351 java.util.Date date = new java.util.Date(timestamp.getTime());
352 outValue = _dateFormatterOut.format(date);
356 _cal.get(Calendar.ZONE_OFFSET) + _cal.get(Calendar.DST_OFFSET);
358 StringUtil.zeroPaddingNumber(offset / _millisPerHour, 2, 2);
359 outValue = outValue + "+" + tzOffset;
365 outValue = "<unsupported value>";
366 logger.warn("Unsupported Datatype: at " + valueIndex + " (" + aType + ")");
368 } catch (SQLException e) {
369 throw new StorageObjectFailure("Could not get Value out of Resultset -- ",
378 * select-Operator um einen Datensatz zu bekommen.
379 * @param id Primaerschluessel des Datensatzes.
380 * @return liefert EntityObject des gefundenen Datensatzes oder null.
382 public Entity selectById(String id) throws StorageObjectExc {
383 if ((id == null) || id.equals("")) {
384 throw new StorageObjectExc("Database.selectById: Missing id");
387 // ask object store for object
388 if (StoreUtil.implementsStorableObject(theEntityClass)) {
389 String uniqueId = id;
391 if (theEntityClass.equals(StorableObjectEntity.class)) {
392 uniqueId += ("@" + theTable);
395 StoreIdentifier search_sid = new StoreIdentifier(theEntityClass, uniqueId);
396 logger.debug("CACHE: (dbg) looking for sid " + search_sid.toString());
398 Entity hit = (Entity) o_store.use(search_sid);
405 Statement stmt = null;
406 Connection con = getPooledCon();
407 Entity returnEntity = null;
412 /** @todo better prepared statement */
414 "select * from " + theTable + " where " + thePKeyName + "=" + id;
415 stmt = con.createStatement();
416 rs = executeSql(stmt, selectSql);
419 if (evaluatedMetaData == false) {
420 evalMetaData(rs.getMetaData());
424 returnEntity = makeEntityFromResultSet(rs);
427 logger.debug("No data for id: " + id + " in table " + theTable);
433 logger.debug("No Data for Id " + id + " in Table " + theTable);
436 catch (SQLException sqe) {
437 throwSQLException(sqe, "selectById");
440 catch (NumberFormatException e) {
441 logger.error("ID is no number: " + id);
444 freeConnection(con, stmt);
451 * select-Operator um Datensaetze zu bekommen, die key = value erfuellen.
452 * @param key Datenbankfeld der Bedingung.
453 * @param value Wert die der key anehmen muss.
454 * @return EntityList mit den gematchten Entities
456 public EntityList selectByFieldValue(String aField, String aValue) throws StorageObjectFailure {
457 return selectByFieldValue(aField, aValue, 0);
461 * select-Operator um Datensaetze zu bekommen, die key = value erfuellen.
462 * @param key Datenbankfeld der Bedingung.
463 * @param value Wert die der key anehmen muss.
464 * @param offset Gibt an ab welchem Datensatz angezeigt werden soll.
465 * @return EntityList mit den gematchten Entities
467 public EntityList selectByFieldValue(String aField, String aValue, int offset) throws StorageObjectFailure {
468 return selectByWhereClause(aField + "=" + aValue, offset);
472 * select-Operator liefert eine EntityListe mit den gematchten Datens?tzen zur?ck.
473 * Also offset wird der erste Datensatz genommen.
475 * @param wc where-Clause
476 * @return EntityList mit den gematchten Entities
477 * @exception StorageObjectException
479 public EntityList selectByWhereClause(String where) throws StorageObjectFailure {
480 return selectByWhereClause(where, 0);
484 * select-Operator liefert eine EntityListe mit den gematchten Datens?tzen zur?ck.
485 * Als maximale Anzahl wird das Limit auf der Konfiguration genommen.
487 * @param wc where-Clause
488 * @param offset ab welchem Datensatz.
489 * @return EntityList mit den gematchten Entities
490 * @exception StorageObjectException
492 public EntityList selectByWhereClause(String whereClause, int offset) throws StorageObjectFailure {
493 return selectByWhereClause(whereClause, null, offset);
497 * select-Operator liefert eine EntityListe mit den gematchten Datens?tzen zur?ck.
498 * Also offset wird der erste Datensatz genommen.
499 * Als maximale Anzahl wird das Limit auf der Konfiguration genommen.
501 * @param wc where-Clause
502 * @param ob orderBy-Clause
503 * @return EntityList mit den gematchten Entities
504 * @exception StorageObjectException
506 public EntityList selectByWhereClause(String where, String order) throws StorageObjectFailure {
507 return selectByWhereClause(where, order, 0);
511 * select-Operator liefert eine EntityListe mit den gematchten Datens?tzen zur?ck.
512 * Als maximale Anzahl wird das Limit auf der Konfiguration genommen.
514 * @param wc where-Clause
515 * @param ob orderBy-Clause
516 * @param offset ab welchem Datensatz
517 * @return EntityList mit den gematchten Entities
518 * @exception StorageObjectException
520 public EntityList selectByWhereClause(String whereClause, String orderBy, int offset) throws StorageObjectFailure {
521 return selectByWhereClause(whereClause, orderBy, offset, defaultLimit);
525 * select-Operator liefert eine EntityListe mit den gematchten Datens?tzen zur?ck.
526 * @param aWhereClause where-Clause
527 * @param anOrderByClause orderBy-Clause
528 * @param offset ab welchem Datensatz
529 * @param limit wieviele Datens?tze
530 * @return EntityList mit den gematchten Entities
531 * @exception StorageObjectException
533 public EntityList selectByWhereClause(String aWhereClause, String anOrderByClause,
534 int offset, int limit) throws StorageObjectFailure {
536 // check o_store for entitylist
537 if (StoreUtil.implementsStorableObject(theEntityClass)) {
538 StoreIdentifier search_sid =
540 theEntityClass, StoreContainerType.STOC_TYPE_ENTITYLIST,
541 StoreUtil.getEntityListUniqueIdentifierFor(theTable, aWhereClause, anOrderByClause, offset, limit));
542 EntityList hit = (EntityList) o_store.use(search_sid);
545 logger.debug("CACHE (hit): " + search_sid.toString());
552 EntityList theReturnList = null;
553 Connection con = null;
554 Statement stmt = null;
559 // build sql-statement
561 /** @todo count sql string should only be assembled if we really count
562 * see below at the end of method //rk */
563 if ((aWhereClause != null) && (aWhereClause.trim().length() == 0)) {
567 StringBuffer countSql =
568 new StringBuffer("select count(*) from ").append(theTable);
569 StringBuffer selectSql =
570 new StringBuffer("select * from ").append(theTable);
572 if (aWhereClause != null) {
573 selectSql.append(" where ").append(aWhereClause);
574 countSql.append(" where ").append(aWhereClause);
577 if ((anOrderByClause != null) && !(anOrderByClause.trim().length() == 0)) {
578 selectSql.append(" order by ").append(anOrderByClause);
581 if (theAdaptor.hasLimit()) {
582 if ((limit > -1) && (offset > -1)) {
583 selectSql.append(" LIMIT ").append(limit).append(" OFFSET ").append(offset);
589 con = getPooledCon();
590 stmt = con.createStatement();
593 rs = executeSql(stmt, selectSql.toString());
596 if (!evaluatedMetaData) {
597 evalMetaData(rs.getMetaData());
600 theReturnList = new EntityList();
602 Entity theResultEntity;
605 theResultEntity = makeEntityFromResultSet(rs);
606 theReturnList.add(theResultEntity);
613 // making entitylist infos
614 if (!(theAdaptor.hasLimit())) {
618 if (theReturnList != null) {
619 // now we decide if we have to know an overall count...
622 if ((limit > -1) && (offset > -1)) {
623 if (offsetCount == limit) {
624 /** @todo counting should be deffered to entitylist
625 * getSize() should be used */
626 rs = executeSql(stmt, countSql.toString());
630 count = rs.getInt(1);
636 logger.error("Could not count: " + countSql);
641 theReturnList.setCount(count);
642 theReturnList.setOffset(offset);
643 theReturnList.setWhere(aWhereClause);
644 theReturnList.setOrder(anOrderByClause);
645 theReturnList.setStorage(this);
646 theReturnList.setLimit(limit);
648 if (offset >= limit) {
649 theReturnList.setPrevBatch(offset - limit);
652 if ((offset + offsetCount) < count) {
653 theReturnList.setNextBatch(offset + limit);
656 if (StoreUtil.implementsStorableObject(theEntityClass)) {
657 StoreIdentifier sid = theReturnList.getStoreIdentifier();
658 logger.debug("CACHE (add): " + sid.toString());
663 catch (SQLException sqe) {
664 throwSQLException(sqe, "selectByWhereClause");
669 freeConnection(con, stmt);
671 } catch (Throwable t) {
675 return theReturnList;
679 * Bastelt aus einer Zeile der Datenbank ein EntityObjekt.
681 * @param rs Das ResultSetObjekt.
682 * @return Entity Die Entity.
684 private Entity makeEntityFromResultSet(ResultSet rs)
685 throws StorageObjectFailure {
686 /** @todo OS: get Pkey from ResultSet and consult ObjectStore */
687 Map theResultHash = new HashMap();
688 String theResult = null;
690 Entity returnEntity = null;
693 int size = metadataFields.size();
695 for (int i = 0; i < size; i++) {
696 // alle durchlaufen bis nix mehr da
697 theType = metadataTypes[i];
699 if (theType == java.sql.Types.LONGVARBINARY) {
700 InputStreamReader is =
701 (InputStreamReader) rs.getCharacterStream(i + 1);
704 char[] data = new char[32768];
705 StringBuffer theResultString = new StringBuffer();
708 while ((len = is.read(data)) > 0) {
709 theResultString.append(data, 0, len);
713 theResult = theResultString.toString();
718 theResult = getValueAsString(rs, (i + 1), theType);
721 if (theResult != null) {
722 theResultHash.put(metadataFields.get(i), theResult);
726 if (theEntityClass != null) {
727 returnEntity = (Entity) theEntityClass.newInstance();
728 returnEntity.setStorage(this);
729 returnEntity.setValues(theResultHash);
731 if (returnEntity instanceof StorableObject) {
732 logger.debug("CACHE: ( in) " + returnEntity.getId() + " :" + theTable);
733 o_store.add(((StorableObject) returnEntity).getStoreIdentifier());
736 throwStorageObjectException("Internal Error: theEntityClass not set!");
739 catch (IllegalAccessException e) {
740 throwStorageObjectException("No access! -- " + e.getMessage());
742 catch (IOException e) {
743 throwStorageObjectException("IOException! -- " + e.getMessage());
745 catch (InstantiationException e) {
746 throwStorageObjectException("No Instatiation! -- " + e.getMessage());
748 catch (SQLException sqe) {
749 throwSQLException(sqe, "makeEntityFromResultSet");
758 * Inserts an entity into the database.
761 * @return der Wert des Primary-keys der eingef?gten Entity
763 public String insert(Entity theEntity) throws StorageObjectFailure {
765 invalidatePopupCache();
767 // invalidating all EntityLists corresponding with theEntityClass
768 if (StoreUtil.implementsStorableObject(theEntityClass)) {
769 StoreContainerType stoc_type =
770 StoreContainerType.valueOf(theEntityClass,
771 StoreContainerType.STOC_TYPE_ENTITYLIST);
772 o_store.invalidate(stoc_type);
775 String returnId = null;
776 Connection con = null;
777 PreparedStatement pstmt = null;
780 List streamedInput = theEntity.streamedInput();
781 StringBuffer f = new StringBuffer();
782 StringBuffer v = new StringBuffer();
785 boolean firstField = true;
788 for (int i = 0; i < getFields().size(); i++) {
789 aField = (String) getFields().get(i);
791 if (!aField.equals(thePKeyName)) {
795 if (!theEntity.hasValueForField(aField) && (
796 aField.equals("webdb_create") ||
797 aField.equals("webdb_lastchange"))) {
801 if ((streamedInput != null) && streamedInput.contains(aField)) {
805 if (theEntity.hasValueForField(aField)) {
808 JDBCStringRoutines.escapeStringLiteral((String) theEntity.getValue(aField)) + "'";
813 // wenn Wert gegeben, dann einbauen
814 if (aValue != null) {
815 if (firstField == false) {
831 StringBuffer sqlBuf =
832 new StringBuffer("insert into ").append(theTable).append("(").append(f)
833 .append(") values (").append(v).append(")");
834 String sql = sqlBuf.toString();
836 logger.debug("INSERT: " + sql);
837 con = getPooledCon();
838 con.setAutoCommit(false);
839 pstmt = con.prepareStatement(sql);
841 if (streamedInput != null) {
842 for (int i = 0; i < streamedInput.size(); i++) {
844 (String) theEntity.getValue((String) streamedInput.get(i));
845 pstmt.setBytes(i + 1, inputString.getBytes());
849 int ret = pstmt.executeUpdate();
856 pstmt = con.prepareStatement(theAdaptor.getLastInsertSQL(this));
858 ResultSet rs = pstmt.executeQuery();
860 returnId = rs.getString(1);
861 theEntity.setId(returnId);
863 catch (SQLException sqe) {
864 throwSQLException(sqe, "insert");
868 con.setAutoCommit(true);
870 catch (Exception e) {
873 freeConnection(con, pstmt);
876 /** @todo store entity in o_store */
881 * Updates an entity in the database
885 public void update(Entity theEntity) throws StorageObjectFailure {
886 Connection con = null;
887 PreparedStatement pstmt = null;
889 /** @todo this is stupid: why do we prepare statement, when we
890 * throw it away afterwards. should be regular statement
891 * update/insert could better be one routine called save()
892 * that chooses to either insert or update depending if we
893 * have a primary key in the entity. i don't know if we
894 * still need the streamed input fields. // rk */
895 /** @todo extension: check if Entity did change, otherwise we don't need
896 * the roundtrip to the database */
897 /** invalidating corresponding entitylists in o_store*/
898 if (StoreUtil.implementsStorableObject(theEntityClass)) {
899 StoreContainerType stoc_type =
900 StoreContainerType.valueOf(theEntityClass,
901 StoreContainerType.STOC_TYPE_ENTITYLIST);
902 o_store.invalidate(stoc_type);
905 List streamedInput = theEntity.streamedInput();
906 String id = theEntity.getId();
908 StringBuffer fv = new StringBuffer();
909 boolean firstField = true;
912 invalidatePopupCache();
914 // build sql statement
915 for (int i = 0; i < getFields().size(); i++) {
916 aField = (String) metadataFields.get(i);
919 if ( !(aField.equals(thePKeyName) ||
920 aField.equals("webdb_create") ||
921 aField.equals("webdb_lastchange") ||
922 ((streamedInput != null) && streamedInput.contains(aField)))) {
923 if (theEntity.hasValueForField(aField)) {
924 if (firstField == false) {
931 fv.append(aField).append("='").append(JDBCStringRoutines.escapeStringLiteral((String) theEntity.getValue(aField))).append("'");
933 // fv.append(aField).append("='").append(StringUtil.quote((String)theEntity.getValue(aField))).append("'");
939 new StringBuffer("update ").append(theTable).append(" set ").append(fv);
942 if (metadataFields.contains("webdb_lastchange")) {
943 sql.append(",webdb_lastchange=NOW()");
946 // special case: the webdb_create requires the field in yyyy-mm-dd HH:mm
947 // format so anything extra will be ignored. -mh
948 if (metadataFields.contains("webdb_create") &&
949 theEntity.hasValueForField("webdb_create")) {
950 // minimum of 10 (yyyy-mm-dd)...
951 if (theEntity.getValue("webdb_create").length() >= 10) {
952 String dateString = theEntity.getValue("webdb_create");
954 // if only 10, then add 00:00 so it doesn't throw a ParseException
955 if (dateString.length() == 10) {
956 dateString = dateString + " 00:00";
961 java.util.Date d = _dateFormatterIn.parse(dateString);
962 Timestamp tStamp = new Timestamp(d.getTime());
963 sql.append(",webdb_create='" + tStamp.toString() + "'");
964 } catch (ParseException e) {
965 throw new StorageObjectFailure(e);
970 if (streamedInput != null) {
971 for (int i = 0; i < streamedInput.size(); i++) {
972 sql.append(",").append(streamedInput.get(i)).append("=?");
976 sql.append(" where id=").append(id);
977 logger.debug("UPDATE: " + sql);
980 con = getPooledCon();
981 con.setAutoCommit(false);
982 pstmt = con.prepareStatement(sql.toString());
984 if (streamedInput != null) {
985 for (int i = 0; i < streamedInput.size(); i++) {
987 theEntity.getValue((String) streamedInput.get(i));
988 pstmt.setBytes(i + 1, inputString.getBytes());
992 pstmt.executeUpdate();
994 catch (SQLException sqe) {
995 throwSQLException(sqe, "update");
999 con.setAutoCommit(true);
1001 catch (Exception e) {
1005 freeConnection(con, pstmt);
1011 * @param id des zu loeschenden Datensatzes
1012 * @return boolean liefert true zurueck, wenn loeschen erfolgreich war.
1014 public boolean delete(String id) throws StorageObjectFailure {
1015 invalidatePopupCache();
1017 // ostore send notification
1018 if (StoreUtil.implementsStorableObject(theEntityClass)) {
1019 String uniqueId = id;
1021 if (theEntityClass.equals(StorableObjectEntity.class)) {
1022 uniqueId += ("@" + theTable);
1025 logger.debug("CACHE: (del) " + id);
1027 StoreIdentifier search_sid =
1028 new StoreIdentifier(theEntityClass,
1029 StoreContainerType.STOC_TYPE_ENTITY, uniqueId);
1030 o_store.invalidate(search_sid);
1033 /** @todo could be prepared Statement */
1034 Statement stmt = null;
1035 Connection con = null;
1038 "delete from " + theTable + " where " + thePKeyName + "='" + id + "'";
1040 //theLog.printInfo("DELETE " + sql);
1042 con = getPooledCon();
1043 stmt = con.createStatement();
1044 res = stmt.executeUpdate(sql);
1045 } catch (SQLException sqe) {
1046 throwSQLException(sqe, "delete");
1048 freeConnection(con, stmt);
1051 return (res > 0) ? true : false;
1055 * Deletes entities based on a where clause
1057 * @param aWhereClause
1059 * @throws StorageObjectFailure
1061 public int deleteByWhereClause(String aWhereClause) throws StorageObjectFailure {
1062 invalidatePopupCache();
1063 if (StoreUtil.implementsStorableObject(theEntityClass)) {
1064 StoreContainerType stoc_type = StoreContainerType.valueOf(theEntityClass, StoreContainerType.STOC_TYPE_ENTITYLIST);
1065 o_store.invalidate(stoc_type);
1068 Statement stmt = null;
1069 Connection con = null;
1072 "delete from " + theTable + " where " + aWhereClause;
1074 //theLog.printInfo("DELETE " + sql);
1076 con = getPooledCon();
1077 stmt = con.createStatement();
1078 res = stmt.executeUpdate(sql);
1080 catch (SQLException sqe) {
1081 throwSQLException(sqe, "delete");
1084 freeConnection(con, stmt);
1090 /* noch nicht implementiert.
1091 * @return immer false
1093 public boolean delete(EntityList theEntityList) {
1094 invalidatePopupCache();
1100 * Diese Methode sollte ueberschrieben werden, wenn fuer die abgeleitete Database-Klasse
1101 * eine SimpleList mit Standard-Popupdaten erzeugt werden koennen soll.
1104 public SimpleList getPopupData() throws StorageObjectFailure {
1109 * Holt Daten fuer Popups.
1110 * @param name Name des Feldes.
1111 * @param hasNullValue Wenn true wird eine leerer Eintrag fuer die Popups erzeugt.
1112 * @return SimpleList Gibt freemarker.template.SimpleList zurueck.
1114 public SimpleList getPopupData(String name, boolean hasNullValue)
1115 throws StorageObjectFailure {
1116 return getPopupData(name, hasNullValue, null);
1120 * Holt Daten fuer Popups.
1121 * @param name Name des Feldes.
1122 * @param hasNullValue Wenn true wird eine leerer Eintrag fuer die Popups erzeugt.
1123 * @param where Schraenkt die Selektion der Datensaetze ein.
1124 * @return SimpleList Gibt freemarker.template.SimpleList zurueck.
1126 public SimpleList getPopupData(String name, boolean hasNullValue, String where)
1127 throws StorageObjectFailure {
1128 return getPopupData(name, hasNullValue, where, null);
1132 * Holt Daten fuer Popups.
1133 * @param name Name des Feldes.
1134 * @param hasNullValue Wenn true wird eine leerer Eintrag fuer die Popups erzeugt.
1135 * @param where Schraenkt die Selektion der Datensaetze ein.
1136 * @param order Gibt ein Feld als Sortierkriterium an.
1137 * @return SimpleList Gibt freemarker.template.SimpleList zurueck.
1139 public SimpleList getPopupData(String name, boolean hasNullValue,
1140 String where, String order) throws StorageObjectFailure {
1142 if (hasPopupCache && (popupCache != null)) {
1146 SimpleList simpleList = null;
1147 Connection con = null;
1148 Statement stmt = null;
1152 new StringBuffer("select ").append(thePKeyName).append(",").append(name)
1153 .append(" from ").append(theTable);
1155 if ((where != null) && !(where.length() == 0)) {
1156 sql.append(" where ").append(where);
1159 sql.append(" order by ");
1161 if ((order != null) && !(order.length() == 0)) {
1169 con = getPooledCon();
1170 } catch (Exception e) {
1171 throw new StorageObjectFailure(e);
1175 stmt = con.createStatement();
1177 ResultSet rs = executeSql(stmt, sql.toString());
1180 if (!evaluatedMetaData) {
1184 simpleList = new SimpleList();
1186 // if popup has null-selector
1188 simpleList.add(POPUP_EMPTYLINE);
1191 SimpleHash popupDict;
1194 popupDict = new SimpleHash();
1195 popupDict.put("key", getValueAsString(rs, 1, thePKeyType));
1196 popupDict.put("value", rs.getString(2));
1197 simpleList.add(popupDict);
1203 catch (Exception e) {
1204 logger.error("getPopupData: " + e.getMessage());
1205 throw new StorageObjectFailure(e);
1207 freeConnection(con, stmt);
1210 if (hasPopupCache) {
1211 popupCache = simpleList;
1218 * Liefert alle Daten der Tabelle als SimpleHash zurueck. Dies wird verwandt,
1219 * wenn in den Templates ein Lookup-Table benoetigt wird. Sollte nur bei kleinen
1220 * Tabellen Verwendung finden.
1221 * @return SimpleHash mit den Tabellezeilen.
1223 public SimpleHash getHashData() {
1224 /** @todo dangerous! this should have a flag to be enabled, otherwise
1225 * very big Hashes could be returned */
1226 if (hashCache == null) {
1229 HTMLTemplateProcessor.makeSimpleHash(selectByWhereClause("", -1));
1231 catch (StorageObjectFailure e) {
1232 logger.debug(e.getMessage());
1239 /* invalidates the popupCache
1241 protected void invalidatePopupCache() {
1242 /** @todo invalidates toooo much */
1248 * Diese Methode fuehrt den Sqlstring <i>sql</i> aus und timed im Logfile.
1249 * @param stmt Statemnt
1250 * @param sql Sql-String
1252 * @exception StorageObjectException
1254 public ResultSet executeSql(Statement stmt, String sql)
1255 throws StorageObjectFailure, SQLException {
1257 long startTime = System.currentTimeMillis();
1260 rs = stmt.executeQuery(sql);
1262 logger.debug((System.currentTimeMillis() - startTime) + "ms. for: " + sql);
1264 catch (SQLException e) {
1265 logger.error(e.getMessage() +"\n" + (System.currentTimeMillis() - startTime) + "ms. for: " + sql);
1272 public ResultSet executeSql(String sql) throws StorageObjectFailure, SQLException {
1273 long startTime = System.currentTimeMillis();
1274 Connection connection = null;
1275 Statement statement = null;
1278 connection = getPooledCon();
1279 statement = connection.createStatement();
1282 result = statement.executeQuery(sql);
1284 logger.debug((System.currentTimeMillis() - startTime) + "ms. for: " + sql);
1287 catch (Throwable e) {
1288 logger.error(e.getMessage() +"\n" + (System.currentTimeMillis() - startTime) + "ms. for: " + sql);
1289 throw new StorageObjectFailure(e);
1292 if (connection!=null) {
1293 freeConnection(connection, statement);
1298 private Map processRow(ResultSet aResultSet) throws StorageObjectFailure, StorageObjectExc {
1300 Map result = new HashMap();
1301 ResultSetMetaData metaData = aResultSet.getMetaData();
1302 int nrColumns = metaData.getColumnCount();
1303 for (int i=0; i<nrColumns; i++) {
1304 result.put(metaData.getColumnName(i+1), getValueAsString(aResultSet, i+1, metaData.getColumnType(i+1)));
1309 catch (Throwable e) {
1310 throw new StorageObjectFailure(e);
1314 public List executeFreeSql(String sql, int aLimit) throws StorageObjectFailure, StorageObjectExc {
1315 Connection connection = null;
1316 Statement statement = null;
1318 List result = new Vector();
1319 connection = getPooledCon();
1320 statement = connection.createStatement();
1321 ResultSet resultset = executeSql(statement, sql);
1323 while (resultset.next() && result.size() < aLimit) {
1324 result.add(processRow(resultset));
1333 catch (Throwable e) {
1334 throw new StorageObjectFailure(e);
1337 if (connection!=null) {
1338 freeConnection(connection, statement);
1343 public Map executeFreeSingleRowSql(String anSqlStatement) throws StorageObjectFailure, StorageObjectExc {
1345 List resultList = executeFreeSql(anSqlStatement, 1);
1347 if (resultList.size()>0)
1348 return (Map) resultList.get(0);
1355 catch (Throwable t) {
1356 throw new StorageObjectFailure(t);
1360 public String executeFreeSingleValueSql(String sql) throws StorageObjectFailure, StorageObjectExc {
1361 Map row = executeFreeSingleRowSql(sql);
1366 Iterator i = row.values().iterator();
1368 return (String) i.next();
1374 * returns the number of rows in the table
1376 public int getSize(String where) throws SQLException, StorageObjectFailure {
1377 long startTime = System.currentTimeMillis();
1378 String sql = "SELECT Count(*) FROM " + theTable;
1380 if ((where != null) && (where.length() != 0)) {
1381 sql = sql + " where " + where;
1384 Connection con = null;
1385 Statement stmt = null;
1389 con = getPooledCon();
1390 stmt = con.createStatement();
1392 ResultSet rs = executeSql(stmt, sql);
1395 result = rs.getInt(1);
1398 catch (SQLException e) {
1399 logger.error("Database.getSize: " + e.getMessage());
1402 freeConnection(con, stmt);
1405 //theLog.printInfo(theTable + " has "+ result +" rows where " + where);
1406 logger.debug((System.currentTimeMillis() - startTime) + "ms. for: " + sql);
1411 public int executeUpdate(Statement stmt, String sql)
1412 throws StorageObjectFailure, SQLException {
1414 long startTime = System.currentTimeMillis();
1417 rs = stmt.executeUpdate(sql);
1419 logger.debug((System.currentTimeMillis() - startTime) + "ms. for: " + sql);
1421 catch (SQLException e) {
1422 logger.error("Failed: " + (System.currentTimeMillis() - startTime) + "ms. for: " + sql);
1429 public int executeUpdate(String sql)
1430 throws StorageObjectFailure, SQLException {
1432 long startTime = System.currentTimeMillis();
1433 Connection con = null;
1434 PreparedStatement pstmt = null;
1437 con = getPooledCon();
1438 pstmt = con.prepareStatement(sql);
1439 result = pstmt.executeUpdate();
1441 catch (Throwable e) {
1442 logger.error("Database.executeUpdate(" + sql + "): " + e.getMessage());
1443 throw new StorageObjectFailure("Database.executeUpdate(" + sql + "): " + e.getMessage(), e);
1446 freeConnection(con, pstmt);
1449 logger.debug((System.currentTimeMillis() - startTime) + "ms. for: " + sql);
1454 * Wertet ResultSetMetaData aus und setzt interne Daten entsprechend
1455 * @param md ResultSetMetaData
1456 * @exception StorageObjectException
1458 private void evalMetaData(ResultSetMetaData md) throws StorageObjectFailure {
1459 this.evaluatedMetaData = true;
1460 this.metadataFields = new ArrayList();
1461 this.metadataLabels = new ArrayList();
1462 this.metadataNotNullFields = new ArrayList();
1465 int numFields = md.getColumnCount();
1466 this.metadataTypes = new int[numFields];
1471 for (int i = 1; i <= numFields; i++) {
1472 aField = md.getColumnName(i);
1473 metadataFields.add(aField);
1474 metadataLabels.add(md.getColumnLabel(i));
1475 aType = md.getColumnType(i);
1476 metadataTypes[i - 1] = aType;
1478 if (aField.equals(thePKeyName)) {
1479 thePKeyType = aType;
1483 if (md.isNullable(i) == ResultSetMetaData.columnNullable) {
1484 metadataNotNullFields.add(aField);
1488 catch (SQLException e) {
1489 throwSQLException(e, "evalMetaData");
1494 * Wertet die Metadaten eines Resultsets fuer eine Tabelle aus,
1495 * um die alle Columns und Typen einer Tabelle zu ermitteln.
1497 private void get_meta_data() throws StorageObjectFailure {
1498 Connection con = null;
1499 PreparedStatement pstmt = null;
1500 String sql = "select * from " + theTable + " where 0=1";
1503 con = getPooledCon();
1504 pstmt = con.prepareStatement(sql);
1506 logger.debug("METADATA: " + sql);
1507 ResultSet rs = pstmt.executeQuery();
1508 evalMetaData(rs.getMetaData());
1511 catch (SQLException e) {
1512 throwSQLException(e, "get_meta_data");
1515 freeConnection(con, pstmt);
1519 public Connection getPooledCon() throws StorageObjectFailure {
1520 Connection con = null;
1523 con = SQLManager.getInstance().requestConnection();
1525 catch (SQLException e) {
1526 logger.error("could not connect to the database " + e.getMessage());
1528 throw new StorageObjectFailure("Could not connect to the database", e);
1534 public void freeConnection(Connection con, Statement stmt)
1535 throws StorageObjectFailure {
1536 SQLManager.closeStatement(stmt);
1537 SQLManager.getInstance().returnConnection(con);
1541 * Wertet SQLException aus und wirft dannach eine StorageObjectException
1542 * @param sqe SQLException
1543 * @param wo Funktonsname, in der die SQLException geworfen wurde
1544 * @exception StorageObjectException
1546 protected void throwSQLException(SQLException sqe, String aFunction) throws StorageObjectFailure {
1548 String message = "";
1552 state = sqe.getSQLState();
1553 message = sqe.getMessage();
1554 vendor = sqe.getErrorCode();
1557 String information =
1560 ", vendor= " + vendor +
1561 ", message=" + message +
1562 ", function= " + aFunction;
1564 logger.error(information);
1566 throw new StorageObjectFailure(information, sqe);
1569 protected void _throwStorageObjectException(Exception e, String aFunction)
1570 throws StorageObjectFailure {
1573 logger.error(e.getMessage() + aFunction);
1574 throw new StorageObjectFailure(aFunction, e);
1579 * Loggt Fehlermeldung mit dem Parameter Message und wirft dannach
1580 * eine StorageObjectException
1581 * @param message Nachricht mit dem Fehler
1582 * @exception StorageObjectException
1584 void throwStorageObjectException(String aMessage) throws StorageObjectFailure {
1585 logger.error(aMessage);
1586 throw new StorageObjectFailure(aMessage, null);