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 the com.oreilly.servlet library, any library
22 * licensed under the Apache Software License, The Sun (tm) Java Advanced
23 * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24 * the above that use the same license as the above), and distribute linked
25 * combinations including the two. You must obey the GNU General Public
26 * License in all respects for all of the code used other than the above
27 * mentioned libraries. If you modify this file, you may extend this exception
28 * to your version of the file, but you are not obligated to do so. If you do
29 * not wish to do so, delete this exception statement from your version.
38 import java.text.SimpleDateFormat;
39 import java.text.ParseException;
40 import freemarker.template.*;
41 import com.codestudio.sql.*;
42 import com.codestudio.util.*;
44 import mir.storage.StorageObject;
45 import mir.storage.store.*;
51 * Diese Klasse implementiert die Zugriffsschicht auf die Datenbank.
52 * Alle Projektspezifischen Datenbankklassen erben von dieser Klasse.
53 * In den Unterklassen wird im Minimalfall nur die Tabelle angegeben.
54 * Im Konfigurationsfile findet sich eine Verweis auf den verwendeten
55 * Treiber, Host, User und Passwort, ueber den der Zugriff auf die
58 * @version $Revision: 1.21.2.2 $ $Date: 2002/11/26 01:52:55 $
59 * @author $Author: mh $
62 public class Database implements StorageObject {
64 protected String theTable;
65 protected String theCoreTable=null;
66 protected String thePKeyName="id";
67 protected int thePKeyType, thePKeyIndex;
68 protected boolean evaluatedMetaData=false;
69 protected ArrayList metadataFields,metadataLabels,
70 metadataNotNullFields;
71 protected int[] metadataTypes;
72 protected Class theEntityClass;
73 protected StorageObject myselfDatabase;
74 protected SimpleList popupCache=null;
75 protected boolean hasPopupCache = false;
76 protected SimpleHash hashCache=null;
77 protected boolean hasTimestamp=true;
78 private String database_driver, database_url;
79 private int defaultLimit;
80 protected DatabaseAdaptor theAdaptor;
81 protected Logfile theLog;
82 private static Class GENERIC_ENTITY_CLASS=null,
83 STORABLE_OBJECT_ENTITY_CLASS=null;
84 private static SimpleHash POPUP_EMTYLINE=new SimpleHash();
85 protected static final ObjectStore o_store=ObjectStore.getInstance();
86 private SimpleDateFormat _dateFormatterOut =
87 new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
88 private SimpleDateFormat _dateFormatterIn =
89 new SimpleDateFormat("yyyy-MM-dd HH:mm");
90 private Calendar _cal = new GregorianCalendar();
92 private static final int _millisPerHour = 60 * 60 * 1000;
93 private static final int _millisPerMinute = 60 * 1000;
96 // always same object saves a little space
97 POPUP_EMTYLINE.put("key", ""); POPUP_EMTYLINE.put("value", "--");
99 GENERIC_ENTITY_CLASS = Class.forName("mir.entity.StorableObjectEntity");
100 STORABLE_OBJECT_ENTITY_CLASS = Class.forName("mir.entity.StorableObjectEntity");
102 catch (Exception e) {
103 System.err.println("FATAL: Database.java could not initialize" + e.toString());
109 * Kontruktor bekommt den Filenamen des Konfigurationsfiles übergeben.
110 * Aus diesem file werden <code>Database.Logfile</code>,
111 * <code>Database.Username</code>,<code>Database.Password</code>,
112 * <code>Database.Host</code> und <code>Database.Adaptor</code>
113 * ausgelesen und ein Broker für die Verbindugen zur Datenbank
116 * @param String confFilename Dateiname der Konfigurationsdatei
118 public Database() throws StorageObjectException {
119 theLog = Logfile.getInstance(MirConfig.getProp("Home")+
120 MirConfig.getProp("Database.Logfile"));
121 String theAdaptorName=MirConfig.getProp("Database.Adaptor");
122 defaultLimit = Integer.parseInt(MirConfig.getProp("Database.Limit"));
124 theEntityClass = GENERIC_ENTITY_CLASS;
125 theAdaptor = (DatabaseAdaptor)Class.forName(theAdaptorName).newInstance();
126 } catch (Exception e){
127 theLog.printError("Error in Database() constructor with "+
128 theAdaptorName + " -- " +e.toString());
129 throw new StorageObjectException("Error in Database() constructor with "
132 /*String database_username=MirConfig.getProp("Database.Username");
133 String database_password=MirConfig.getProp("Database.Password");
134 String database_host=MirConfig.getProp("Database.Host");
136 database_driver=theAdaptor.getDriver();
137 database_url=theAdaptor.getURL(database_username,database_password,
139 theLog.printDebugInfo("adding Broker with: " +database_driver+":"+
141 MirConfig.addBroker(database_driver,database_url);
142 //myBroker=MirConfig.getBroker();
147 * Liefert die Entity-Klasse zurück, in der eine Datenbankzeile gewrappt
148 * wird. Wird die Entity-Klasse durch die erbende Klasse nicht überschrieben,
149 * wird eine mir.entity.GenericEntity erzeugt.
151 * @return Class-Objekt der Entity
153 public java.lang.Class getEntityClass () {
154 return theEntityClass;
158 * Liefert die Standardbeschränkung von select-Statements zurück, also
159 * wieviel Datensätze per Default selektiert werden.
161 * @return Standard-Anzahl der Datensätze
163 public int getLimit () {
168 * Liefert den Namen des Primary-Keys zurück. Wird die Variable nicht von
169 * der erbenden Klasse überschrieben, so ist der Wert <code>PKEY</code>
170 * @return Name des Primary-Keys
172 public String getIdName () {
177 * Liefert den Namen der Tabelle, auf das sich das Datenbankobjekt bezieht.
179 * @return Name der Tabelle
181 public String getTableName () {
186 * Dient dazu vererbte Tabellen bei objectrelationalen DBMS
187 * zu speichern, wenn die id einer Tabelle in der parenttabelle verwaltet
189 * @return liefert theCoreTabel als String zurueck, wenn gesetzt, sonst
193 public String getCoreTable(){
194 if (theCoreTable!=null) return theCoreTable;
195 else return theTable;
199 * Liefert Feldtypen der Felder der Tabelle zurueck (s.a. java.sql.Types)
200 * @return int-Array mit den Typen der Felder
201 * @exception StorageObjectException
203 public int[] getTypes () throws StorageObjectException {
204 if (metadataTypes == null)
206 return metadataTypes;
210 * Liefert eine Liste der Labels der Tabellenfelder
211 * @return ArrayListe mit Labeln
212 * @exception StorageObjectException
214 public ArrayList getLabels () throws StorageObjectException {
215 if (metadataLabels == null)
217 return metadataLabels;
221 * Liefert eine Liste der Felder der Tabelle
222 * @return ArrayList mit Feldern
223 * @exception StorageObjectException
225 public ArrayList getFields () throws StorageObjectException {
226 if (metadataFields == null)
228 return metadataFields;
233 * Gets value out of ResultSet according to type and converts to String
234 * @param inValue Wert aus ResultSet.
235 * @param aType Datenbanktyp.
236 * @return liefert den Wert als String zurueck. Wenn keine Umwandlung moeglich
237 * dann /unsupported value/
239 private String getValueAsString (ResultSet rs, int valueIndex, int aType) throws StorageObjectException {
240 String outValue = null;
244 case java.sql.Types.BIT:
245 outValue = (rs.getBoolean(valueIndex) == true) ? "1" : "0";
247 case java.sql.Types.INTEGER:case java.sql.Types.SMALLINT:case java.sql.Types.TINYINT:case java.sql.Types.BIGINT:
248 int out = rs.getInt(valueIndex);
250 outValue = new Integer(out).toString();
252 case java.sql.Types.NUMERIC:
253 /** @todo Numeric can be float or double depending upon
254 * metadata.getScale() / especially with oracle */
255 long outl = rs.getLong(valueIndex);
257 outValue = new Long(outl).toString();
259 case java.sql.Types.REAL:
260 float tempf = rs.getFloat(valueIndex);
264 int tempf_int = (int)tempf;
265 tempf = (float)tempf_int;
267 outValue = "" + tempf;
268 outValue = outValue.replace('.', ',');
271 case java.sql.Types.DOUBLE:
272 double tempd = rs.getDouble(valueIndex);
276 int tempd_int = (int)tempd;
277 tempd = (double)tempd_int;
279 outValue = "" + tempd;
280 outValue = outValue.replace('.', ',');
283 case java.sql.Types.CHAR:case java.sql.Types.VARCHAR:case java.sql.Types.LONGVARCHAR:
284 outValue = rs.getString(valueIndex);
285 //if (outValue != null)
286 //outValue = StringUtil.encodeHtml(StringUtil.unquote(outValue));
288 case java.sql.Types.LONGVARBINARY:
289 outValue = rs.getString(valueIndex);
290 //if (outValue != null)
291 //outValue = StringUtil.encodeHtml(StringUtil.unquote(outValue));
293 case java.sql.Types.TIMESTAMP:
294 // it's important to use Timestamp here as getting it
295 // as a string is undefined and is only there for debugging
296 // according to the API. we can make it a string through formatting.
298 Timestamp timestamp = (rs.getTimestamp(valueIndex));
300 java.util.Date date = new java.util.Date(timestamp.getTime());
301 outValue = _dateFormatterOut.format(date);
303 int offset = _cal.get(Calendar.ZONE_OFFSET)+
304 _cal.get(Calendar.DST_OFFSET);
305 String tzOffset = StringUtil.zeroPaddingNumber(
306 offset/_millisPerHour,2,2);
307 outValue = outValue+"+"+tzOffset;
311 outValue = "<unsupported value>";
312 theLog.printWarning("Unsupported Datatype: at " + valueIndex +
315 } catch (SQLException e) {
316 throw new StorageObjectException("Could not get Value out of Resultset -- "
324 * select-Operator um einen Datensatz zu bekommen.
325 * @param id Primaerschluessel des Datensatzes.
326 * @return liefert EntityObject des gefundenen Datensatzes oder null.
328 public Entity selectById(String id) throws StorageObjectException
330 if (id==null||id.equals(""))
331 throw new StorageObjectException("id war null");
333 // ask object store for object
334 if ( StoreUtil.implementsStorableObject(theEntityClass) ) {
335 String uniqueId = id;
336 if ( theEntityClass.equals(StorableObjectEntity.class) )
337 uniqueId+="@"+theTable;
338 StoreIdentifier search_sid = new StoreIdentifier(theEntityClass, uniqueId);
339 theLog.printDebugInfo("CACHE: (dbg) looking for sid " + search_sid.toString());
340 Entity hit = (Entity)o_store.use(search_sid);
341 if ( hit!=null ) return hit;
344 Statement stmt=null;Connection con=getPooledCon();
345 Entity returnEntity=null;
348 /** @todo better prepared statement */
349 String selectSql = "select * from " + theTable + " where " + thePKeyName + "=" + id;
350 stmt = con.createStatement();
351 rs = executeSql(stmt, selectSql);
353 if (evaluatedMetaData==false) evalMetaData(rs.getMetaData());
355 returnEntity = makeEntityFromResultSet(rs);
356 else theLog.printDebugInfo("Keine daten fuer id: " + id + "in Tabelle" + theTable);
360 theLog.printDebugInfo("No Data for Id " + id + " in Table " + theTable);
363 catch (SQLException sqe){
364 throwSQLException(sqe,"selectById"); return null;
366 catch (NumberFormatException e) {
367 theLog.printError("ID ist keine Zahl: " + id);
369 finally { freeConnection(con,stmt); }
371 /** @todo OS: Entity should be saved in ostore */
377 * select-Operator um Datensaetze zu bekommen, die key = value erfuellen.
378 * @param key Datenbankfeld der Bedingung.
379 * @param value Wert die der key anehmen muss.
380 * @return EntityList mit den gematchten Entities
382 public EntityList selectByFieldValue(String aField, String aValue)
383 throws StorageObjectException
385 return selectByFieldValue(aField, aValue, 0);
389 * select-Operator um Datensaetze zu bekommen, die key = value erfuellen.
390 * @param key Datenbankfeld der Bedingung.
391 * @param value Wert die der key anehmen muss.
392 * @param offset Gibt an ab welchem Datensatz angezeigt werden soll.
393 * @return EntityList mit den gematchten Entities
395 public EntityList selectByFieldValue(String aField, String aValue, int offset)
396 throws StorageObjectException
398 return selectByWhereClause(aField + "=" + aValue, offset);
403 * select-Operator liefert eine EntityListe mit den gematchten Datensätzen zurück.
404 * Also offset wird der erste Datensatz genommen.
406 * @param wc where-Clause
407 * @return EntityList mit den gematchten Entities
408 * @exception StorageObjectException
410 public EntityList selectByWhereClause(String where)
411 throws StorageObjectException
413 return selectByWhereClause(where, 0);
418 * select-Operator liefert eine EntityListe mit den gematchten Datensätzen zurück.
419 * Als maximale Anzahl wird das Limit auf der Konfiguration genommen.
421 * @param wc where-Clause
422 * @param offset ab welchem Datensatz.
423 * @return EntityList mit den gematchten Entities
424 * @exception StorageObjectException
426 public EntityList selectByWhereClause(String whereClause, int offset)
427 throws StorageObjectException
429 return selectByWhereClause(whereClause, null, offset);
434 * select-Operator liefert eine EntityListe mit den gematchten Datensätzen zurück.
435 * Also offset wird der erste Datensatz genommen.
436 * Als maximale Anzahl wird das Limit auf der Konfiguration genommen.
438 * @param wc where-Clause
439 * @param ob orderBy-Clause
440 * @return EntityList mit den gematchten Entities
441 * @exception StorageObjectException
444 public EntityList selectByWhereClause(String where, String order)
445 throws StorageObjectException {
446 return selectByWhereClause(where, order, 0);
451 * select-Operator liefert eine EntityListe mit den gematchten Datensätzen zurück.
452 * Als maximale Anzahl wird das Limit auf der Konfiguration genommen.
454 * @param wc where-Clause
455 * @param ob orderBy-Clause
456 * @param offset ab welchem Datensatz
457 * @return EntityList mit den gematchten Entities
458 * @exception StorageObjectException
461 public EntityList selectByWhereClause(String whereClause, String orderBy, int offset)
462 throws StorageObjectException {
463 return selectByWhereClause(whereClause, orderBy, offset, defaultLimit);
468 * select-Operator liefert eine EntityListe mit den gematchten Datensätzen zurück.
469 * @param wc where-Clause
470 * @param ob orderBy-Clause
471 * @param offset ab welchem Datensatz
472 * @param limit wieviele Datensätze
473 * @return EntityList mit den gematchten Entities
474 * @exception StorageObjectException
477 public EntityList selectByWhereClause(String wc, String ob, int offset, int limit)
478 throws StorageObjectException
481 // check o_store for entitylist
482 if ( StoreUtil.implementsStorableObject(theEntityClass) ) {
483 StoreIdentifier search_sid =
484 new StoreIdentifier( theEntityClass,
485 StoreContainerType.STOC_TYPE_ENTITYLIST,
486 StoreUtil.getEntityListUniqueIdentifierFor(theTable,wc,ob,offset,limit) );
487 EntityList hit = (EntityList)o_store.use(search_sid);
489 theLog.printDebugInfo("CACHE (hit): " + search_sid.toString());
495 EntityList theReturnList=null;
496 Connection con=null; Statement stmt=null;
498 int offsetCount = 0, count=0;
500 // build sql-statement
502 /** @todo count sql string should only be assembled if we really count
503 * see below at the end of method //rk */
505 if (wc != null && wc.length() == 0) {
508 StringBuffer countSql = new StringBuffer("select count(*) from ").append(theTable);
509 StringBuffer selectSql = new StringBuffer("select * from ").append(theTable);
511 selectSql.append(" where ").append(wc);
512 countSql.append(" where ").append(wc);
514 if (ob != null && !(ob.length() == 0)) {
515 selectSql.append(" order by ").append(ob);
517 if (theAdaptor.hasLimit()) {
518 if (limit > -1 && offset > -1) {
519 selectSql.append(" limit ");
520 if (theAdaptor.reverseLimit()) {
521 selectSql.append(limit).append(",").append(offset);
524 selectSql.append(offset).append(",").append(limit);
531 con = getPooledCon();
532 stmt = con.createStatement();
535 rs = executeSql(stmt, selectSql.toString());
537 if (!evaluatedMetaData) evalMetaData(rs.getMetaData());
539 theReturnList = new EntityList();
540 Entity theResultEntity;
542 theResultEntity = makeEntityFromResultSet(rs);
543 theReturnList.add(theResultEntity);
549 // making entitylist infos
550 if (!(theAdaptor.hasLimit())) count = offsetCount;
552 if (theReturnList != null) {
553 // now we decide if we have to know an overall count...
555 if (limit > -1 && offset > -1) {
556 if (offsetCount==limit) {
557 /** @todo counting should be deffered to entitylist
558 * getSize() should be used */
559 rs = executeSql(stmt, countSql.toString());
561 if ( rs.next() ) count = rs.getInt(1);
564 else theLog.printError("Could not count: " + countSql);
567 theReturnList.setCount(count);
568 theReturnList.setOffset(offset);
569 theReturnList.setWhere(wc);
570 theReturnList.setOrder(ob);
571 theReturnList.setStorage(this);
572 theReturnList.setLimit(limit);
573 if ( offset >= limit )
574 theReturnList.setPrevBatch(offset - limit);
575 if ( offset+offsetCount < count )
576 theReturnList.setNextBatch(offset + limit);
577 if ( StoreUtil.implementsStorableObject(theEntityClass) ) {
578 StoreIdentifier sid=theReturnList.getStoreIdentifier();
579 theLog.printDebugInfo("CACHE (add): " + sid.toString());
584 catch (SQLException sqe) { throwSQLException(sqe, "selectByWhereClause"); }
585 finally { freeConnection(con, stmt); }
587 return theReturnList;
592 * Bastelt aus einer Zeile der Datenbank ein EntityObjekt.
594 * @param rs Das ResultSetObjekt.
595 * @return Entity Die Entity.
597 private Entity makeEntityFromResultSet (ResultSet rs)
598 throws StorageObjectException
600 /** @todo OS: get Pkey from ResultSet and consult ObjectStore */
601 HashMap theResultHash = new HashMap();
602 String theResult = null;
604 Entity returnEntity = null;
606 int size = metadataFields.size();
607 for (int i = 0; i < size; i++) {
608 // alle durchlaufen bis nix mehr da
609 theType = metadataTypes[i];
610 if (theType == java.sql.Types.LONGVARBINARY) {
611 InputStreamReader is = (InputStreamReader)rs.getCharacterStream(i + 1);
613 char[] data = new char[32768];
614 StringBuffer theResultString = new StringBuffer();
616 while ((len = is.read(data)) > 0) {
617 theResultString.append(data, 0, len);
620 theResult = theResultString.toString();
627 theResult = getValueAsString(rs, (i + 1), theType);
629 if (theResult != null) {
630 theResultHash.put(metadataFields.get(i), theResult);
633 if (theEntityClass != null) {
634 returnEntity = (Entity)theEntityClass.newInstance();
635 returnEntity.setValues(theResultHash);
636 returnEntity.setStorage(myselfDatabase);
637 if ( returnEntity instanceof StorableObject ) {
638 theLog.printDebugInfo("CACHE: ( in) " + returnEntity.getId() + " :"+theTable);
639 o_store.add(((StorableObject)returnEntity).getStoreIdentifier());
642 throwStorageObjectException("Internal Error: theEntityClass not set!");
644 } catch (IllegalAccessException e) {
645 throwStorageObjectException("Kein Zugriff! -- " + e.toString());
646 } catch (IOException e) {
647 throwStorageObjectException("IOException! -- " + e.toString());
648 } catch (InstantiationException e) {
649 throwStorageObjectException("Keine Instantiiierung! -- " + e.toString());
650 } catch (SQLException sqe) {
651 throwSQLException(sqe, "makeEntityFromResultSet");
658 * insert-Operator: fügt eine Entity in die Tabelle ein. Eine Spalte WEBDB_CREATE
659 * wird automatisch mit dem aktuellen Datum gefuellt.
662 * @return der Wert des Primary-keys der eingefügten Entity
664 public String insert (Entity theEntity) throws StorageObjectException {
666 invalidatePopupCache();
668 // invalidating all EntityLists corresponding with theEntityClass
669 if ( StoreUtil.implementsStorableObject(theEntityClass) ) {
670 StoreContainerType stoc_type =
671 StoreContainerType.valueOf( theEntityClass,
672 StoreContainerType.STOC_TYPE_ENTITYLIST);
673 o_store.invalidate(stoc_type);
676 String returnId = null;
677 Connection con = null; PreparedStatement pstmt = null;
680 ArrayList streamedInput = theEntity.streamedInput();
681 StringBuffer f = new StringBuffer();
682 StringBuffer v = new StringBuffer();
683 String aField, aValue;
684 boolean firstField = true;
686 for (int i = 0; i < getFields().size(); i++) {
687 aField = (String)getFields().get(i);
688 if (!aField.equals(thePKeyName)) {
691 if (aField.equals("webdb_create") ||
692 aField.equals("webdb_lastchange")) {
696 if (streamedInput != null && streamedInput.contains(aField)) {
700 if (theEntity.hasValueForField(aField)) {
701 aValue = "'" + StringUtil.quote((String)theEntity.getValue(aField))
706 // wenn Wert gegeben, dann einbauen
707 if (aValue != null) {
708 if (firstField == false) {
721 StringBuffer sqlBuf = new StringBuffer("insert into ").append(theTable).append("(").append(f).append(") values (").append(v).append(")");
722 String sql = sqlBuf.toString();
723 theLog.printInfo("INSERT: " + sql);
724 con = getPooledCon();
725 con.setAutoCommit(false);
726 pstmt = con.prepareStatement(sql);
727 if (streamedInput != null) {
728 for (int i = 0; i < streamedInput.size(); i++) {
729 String inputString = (String)theEntity.getValue((String)streamedInput.get(i));
730 pstmt.setBytes(i + 1, inputString.getBytes());
733 int ret = pstmt.executeUpdate();
738 pstmt = con.prepareStatement(theAdaptor.getLastInsertSQL((Database)myselfDatabase));
739 ResultSet rs = pstmt.executeQuery();
741 returnId = rs.getString(1);
742 theEntity.setId(returnId);
743 } catch (SQLException sqe) {
744 throwSQLException(sqe, "insert");
747 con.setAutoCommit(true);
748 } catch (Exception e) {
751 freeConnection(con, pstmt);
753 /** @todo store entity in o_store */
758 * update-Operator: aktualisiert eine Entity. Eine Spalte WEBDB_LASTCHANGE
759 * wird automatisch mit dem aktuellen Datum gefuellt.
763 public void update (Entity theEntity) throws StorageObjectException
765 Connection con = null; PreparedStatement pstmt = null;
766 /** @todo this is stupid: why do we prepare statement, when we
767 * throw it away afterwards. should be regular statement
768 * update/insert could better be one routine called save()
769 * that chooses to either insert or update depending if we
770 * have a primary key in the entity. i don't know if we
771 * still need the streamed input fields. // rk */
773 /** @todo extension: check if Entity did change, otherwise we don't need
774 * the roundtrip to the database */
776 /** invalidating corresponding entitylists in o_store*/
777 if ( StoreUtil.implementsStorableObject(theEntityClass) ) {
778 StoreContainerType stoc_type =
779 StoreContainerType.valueOf( theEntityClass,
780 StoreContainerType.STOC_TYPE_ENTITYLIST);
781 o_store.invalidate(stoc_type);
784 ArrayList streamedInput = theEntity.streamedInput();
785 String id = theEntity.getId();
787 StringBuffer fv = new StringBuffer();
788 boolean firstField = true;
790 invalidatePopupCache();
791 // build sql statement
792 for (int i = 0; i < getFields().size(); i++) {
793 aField = (String)metadataFields.get(i);
795 if (!(aField.equals(thePKeyName) || aField.equals("webdb_create") ||
796 aField.equals("webdb_lastchange") || (streamedInput != null && streamedInput.contains(aField)))) {
797 if (theEntity.hasValueForField(aField)) {
798 if (firstField == false) {
804 fv.append(aField).append("='").append(StringUtil.quote((String)theEntity.getValue(aField))).append("'");
808 StringBuffer sql = new StringBuffer("update ").append(theTable).append(" set ").append(fv);
810 if (metadataFields.contains("webdb_lastchange")) {
811 sql.append(",webdb_lastchange=NOW()");
813 // special case: the webdb_create requires the field in yyyy-mm-dd HH:mm
814 // format so anything extra will be ignored. -mh
815 if (metadataFields.contains("webdb_create") &&
816 theEntity.hasValueForField("webdb_create")) {
817 // minimum of 10 (yyyy-mm-dd)...
818 if (theEntity.getValue("webdb_create").length() >= 10) {
819 String dateString = theEntity.getValue("webdb_create");
820 // if only 10, then add 00:00 so it doesn't throw a ParseException
821 if (dateString.length() == 10)
822 dateString=dateString+" 00:00";
826 java.util.Date d = _dateFormatterIn.parse(dateString);
827 Timestamp tStamp = new Timestamp(d.getTime());
828 sql.append(",webdb_create='"+tStamp.toString()+"'");
829 } catch (ParseException e) {
830 throw new StorageObjectException(e.toString());
834 if (streamedInput != null) {
835 for (int i = 0; i < streamedInput.size(); i++) {
836 sql.append(",").append(streamedInput.get(i)).append("=?");
839 sql.append(" where id=").append(id);
840 theLog.printInfo("UPDATE: " + sql);
843 con = getPooledCon();
844 con.setAutoCommit(false);
845 pstmt = con.prepareStatement(sql.toString());
846 if (streamedInput != null) {
847 for (int i = 0; i < streamedInput.size(); i++) {
848 String inputString = theEntity.getValue((String)streamedInput.get(i));
849 pstmt.setBytes(i + 1, inputString.getBytes());
852 pstmt.executeUpdate();
853 } catch (SQLException sqe) {
854 throwSQLException(sqe, "update");
857 con.setAutoCommit(true);
858 } catch (Exception e) {
861 freeConnection(con, pstmt);
867 * @param id des zu loeschenden Datensatzes
868 * @return boolean liefert true zurueck, wenn loeschen erfolgreich war.
870 public boolean delete (String id) throws StorageObjectException {
872 invalidatePopupCache();
873 // ostore send notification
874 if ( StoreUtil.implementsStorableObject(theEntityClass) ) {
875 String uniqueId = id;
876 if ( theEntityClass.equals(StorableObjectEntity.class) )
877 uniqueId+="@"+theTable;
878 theLog.printInfo("CACHE: (del) " + id);
879 StoreIdentifier search_sid =
880 new StoreIdentifier(theEntityClass, StoreContainerType.STOC_TYPE_ENTITY, uniqueId);
881 o_store.invalidate(search_sid);
884 /** @todo could be prepared Statement */
885 Statement stmt = null; Connection con = null;
887 String sql="delete from "+theTable+" where "+thePKeyName+"='"+id+"'";
888 theLog.printInfo("DELETE " + sql);
890 con = getPooledCon(); stmt = con.createStatement();
891 res = stmt.executeUpdate(sql);
893 catch (SQLException sqe) { throwSQLException(sqe, "delete"); }
894 finally { freeConnection(con, stmt); }
896 return (res > 0) ? true : false;
899 /* noch nicht implementiert.
900 * @return immer false
902 public boolean delete (EntityList theEntityList) {
903 invalidatePopupCache();
908 * Diese Methode sollte ueberschrieben werden, wenn fuer die abgeleitete Database-Klasse
909 * eine SimpleList mit Standard-Popupdaten erzeugt werden koennen soll.
912 public SimpleList getPopupData () throws StorageObjectException {
917 * Holt Daten fuer Popups.
918 * @param name Name des Feldes.
919 * @param hasNullValue Wenn true wird eine leerer Eintrag fuer die Popups erzeugt.
920 * @return SimpleList Gibt freemarker.template.SimpleList zurueck.
922 public SimpleList getPopupData (String name, boolean hasNullValue)
923 throws StorageObjectException {
924 return getPopupData(name, hasNullValue, null);
928 * Holt Daten fuer Popups.
929 * @param name Name des Feldes.
930 * @param hasNullValue Wenn true wird eine leerer Eintrag fuer die Popups erzeugt.
931 * @param where Schraenkt die Selektion der Datensaetze ein.
932 * @return SimpleList Gibt freemarker.template.SimpleList zurueck.
934 public SimpleList getPopupData (String name, boolean hasNullValue, String where) throws StorageObjectException {
935 return getPopupData(name, hasNullValue, where, null);
939 * Holt Daten fuer Popups.
940 * @param name Name des Feldes.
941 * @param hasNullValue Wenn true wird eine leerer Eintrag fuer die Popups erzeugt.
942 * @param where Schraenkt die Selektion der Datensaetze ein.
943 * @param order Gibt ein Feld als Sortierkriterium an.
944 * @return SimpleList Gibt freemarker.template.SimpleList zurueck.
946 public SimpleList getPopupData (String name, boolean hasNullValue, String where, String order) throws StorageObjectException {
948 if (hasPopupCache && popupCache != null)
950 SimpleList simpleList = null;
951 Connection con = null;
952 Statement stmt = null;
954 StringBuffer sql = new StringBuffer("select ").append(thePKeyName)
955 .append(",").append(name).append(" from ")
957 if (where != null && !(where.length() == 0))
958 sql.append(" where ").append(where);
959 sql.append(" order by ");
960 if (order != null && !(order.length() == 0))
966 con = getPooledCon();
967 } catch (Exception e) {
968 throw new StorageObjectException(e.toString());
971 stmt = con.createStatement();
972 ResultSet rs = executeSql(stmt, sql.toString());
975 if (!evaluatedMetaData) get_meta_data();
976 simpleList = new SimpleList();
977 // if popup has null-selector
978 if (hasNullValue) simpleList.add(POPUP_EMTYLINE);
980 SimpleHash popupDict;
982 popupDict = new SimpleHash();
983 popupDict.put("key", getValueAsString(rs, 1, thePKeyType));
984 popupDict.put("value", rs.getString(2));
985 simpleList.add(popupDict);
989 } catch (Exception e) {
990 theLog.printError("getPopupData: "+e.toString());
991 throw new StorageObjectException(e.toString());
993 freeConnection(con, stmt);
996 if (hasPopupCache) popupCache = simpleList;
1001 * Liefert alle Daten der Tabelle als SimpleHash zurueck. Dies wird verwandt,
1002 * wenn in den Templates ein Lookup-Table benoetigt wird. Sollte nur bei kleinen
1003 * Tabellen Verwendung finden.
1004 * @return SimpleHash mit den Tabellezeilen.
1006 public SimpleHash getHashData () {
1007 /** @todo dangerous! this should have a flag to be enabled, otherwise
1008 * very big Hashes could be returned */
1009 if (hashCache == null) {
1011 hashCache = HTMLTemplateProcessor.makeSimpleHash(selectByWhereClause("",
1013 } catch (StorageObjectException e) {
1014 theLog.printDebugInfo(e.toString());
1020 /* invalidates the popupCache
1022 protected void invalidatePopupCache () {
1023 /** @todo invalidates toooo much */
1029 * Diese Methode fuehrt den Sqlstring <i>sql</i> aus und timed im Logfile.
1030 * @param stmt Statemnt
1031 * @param sql Sql-String
1033 * @exception StorageObjectException
1035 public ResultSet executeSql (Statement stmt, String sql)
1036 throws StorageObjectException, SQLException
1038 long startTime = System.currentTimeMillis();
1041 rs = stmt.executeQuery(sql);
1042 theLog.printInfo((System.currentTimeMillis() - startTime) + "ms. for: "
1045 catch (SQLException e)
1047 theLog.printDebugInfo("Failed: " + (System.currentTimeMillis()
1048 - startTime) + "ms. for: "+ sql);
1056 * Fuehrt Statement stmt aus und liefert Resultset zurueck. Das SQL-Statment wird
1057 * getimed und geloggt.
1058 * @param stmt PreparedStatement mit der SQL-Anweisung
1059 * @return Liefert ResultSet des Statements zurueck.
1060 * @exception StorageObjectException, SQLException
1062 public ResultSet executeSql (PreparedStatement stmt)
1063 throws StorageObjectException, SQLException {
1065 long startTime = (new java.util.Date()).getTime();
1066 ResultSet rs = stmt.executeQuery();
1067 theLog.printInfo((new java.util.Date().getTime() - startTime) + "ms.");
1072 * returns the number of rows in the table
1074 public int getSize(String where)
1075 throws SQLException,StorageObjectException
1077 long startTime = System.currentTimeMillis();
1078 String sql = "SELECT count(*) FROM "+ theTable + " where " + where;
1079 Connection con = null;
1080 Statement stmt = null;
1084 con = getPooledCon();
1085 stmt = con.createStatement();
1086 ResultSet rs = executeSql(stmt,sql);
1088 result = rs.getInt(1);
1090 } catch (SQLException e) {
1091 theLog.printError(e.toString());
1093 freeConnection(con,stmt);
1095 //theLog.printInfo(theTable + " has "+ result +" rows where " + where);
1096 theLog.printInfo((System.currentTimeMillis() - startTime) + "ms. for: "
1101 public int executeUpdate(Statement stmt, String sql)
1102 throws StorageObjectException, SQLException
1105 long startTime = (new java.util.Date()).getTime();
1108 rs = stmt.executeUpdate(sql);
1109 theLog.printInfo((new java.util.Date().getTime() - startTime) + "ms. for: "
1112 catch (SQLException e)
1114 theLog.printDebugInfo("Failed: " + (new java.util.Date().getTime()
1115 - startTime) + "ms. for: "+ sql);
1121 public int executeUpdate(String sql)
1122 throws StorageObjectException, SQLException
1125 long startTime = (new java.util.Date()).getTime();
1126 Connection con=null;PreparedStatement pstmt=null;
1129 pstmt = con.prepareStatement(sql);
1130 result = pstmt.executeUpdate();
1132 catch (Exception e) {
1133 theLog.printDebugInfo("settimage :: setImage gescheitert: "+e.toString());
1134 throw new StorageObjectException("executeUpdate failed: "+e.toString());
1136 finally { freeConnection(con,pstmt); }
1137 theLog.printInfo((new java.util.Date().getTime() - startTime) + "ms. for: "
1143 * Wertet ResultSetMetaData aus und setzt interne Daten entsprechend
1144 * @param md ResultSetMetaData
1145 * @exception StorageObjectException
1147 private void evalMetaData (ResultSetMetaData md)
1148 throws StorageObjectException {
1150 this.evaluatedMetaData = true;
1151 this.metadataFields = new ArrayList();
1152 this.metadataLabels = new ArrayList();
1153 this.metadataNotNullFields = new ArrayList();
1155 int numFields = md.getColumnCount();
1156 this.metadataTypes = new int[numFields];
1159 for (int i = 1; i <= numFields; i++) {
1160 aField = md.getColumnName(i);
1161 metadataFields.add(aField);
1162 metadataLabels.add(md.getColumnLabel(i));
1163 aType = md.getColumnType(i);
1164 metadataTypes[i - 1] = aType;
1165 if (aField.equals(thePKeyName)) {
1166 thePKeyType = aType; thePKeyIndex = i;
1168 if (md.isNullable(i) == md.columnNullable) {
1169 metadataNotNullFields.add(aField);
1172 } catch (SQLException e) {
1173 throwSQLException(e, "evalMetaData");
1178 * Wertet die Metadaten eines Resultsets fuer eine Tabelle aus,
1179 * um die alle Columns und Typen einer Tabelle zu ermitteln.
1181 private void get_meta_data () throws StorageObjectException {
1182 Connection con = null;
1183 PreparedStatement pstmt = null;
1184 String sql = "select * from " + theTable + " where 0=1";
1186 con = getPooledCon();
1187 pstmt = con.prepareStatement(sql);
1188 theLog.printInfo("METADATA: " + sql);
1189 ResultSet rs = pstmt.executeQuery();
1190 evalMetaData(rs.getMetaData());
1192 } catch (SQLException e) {
1193 throwSQLException(e, "get_meta_data");
1195 freeConnection(con, pstmt);
1200 public Connection getPooledCon() throws StorageObjectException {
1201 /* @todo , doublecheck but I'm pretty sure that this is unnecessary. -mh
1203 Class.forName("com.codestudio.sql.PoolMan").newInstance();
1204 } catch (Exception e){
1205 throw new StorageObjectException("Could not find the PoolMan Driver"
1208 Connection con = null;
1210 con = SQLManager.getInstance().requestConnection();
1211 } catch(SQLException e){
1212 theLog.printError("could not connect to the database "+e.toString());
1213 System.err.println("could not connect to the database "+e.toString());
1214 throw new StorageObjectException("Could not connect to the database"+
1220 public void freeConnection (Connection con, Statement stmt)
1221 throws StorageObjectException {
1222 SQLManager.getInstance().closeStatement(stmt);
1223 SQLManager.getInstance().returnConnection(con);
1227 * Wertet SQLException aus und wirft dannach eine StorageObjectException
1228 * @param sqe SQLException
1229 * @param wo Funktonsname, in der die SQLException geworfen wurde
1230 * @exception StorageObjectException
1232 protected void throwSQLException (SQLException sqe, String wo)
1233 throws StorageObjectException {
1235 String message = "";
1238 state = sqe.getSQLState();
1239 message = sqe.getMessage();
1240 vendor = sqe.getErrorCode();
1242 theLog.printError(state + ": " + vendor + " : " + message + " Funktion: "
1244 throw new StorageObjectException((sqe == null) ? "undefined sql exception" :
1248 protected void _throwStorageObjectException (Exception e, String wo)
1249 throws StorageObjectException {
1251 theLog.printError(e.toString()+ wo);
1252 throw new StorageObjectException(wo + e.toString());
1254 theLog.printError(wo);
1255 throw new StorageObjectException(wo);
1261 * Loggt Fehlermeldung mit dem Parameter Message und wirft dannach
1262 * eine StorageObjectException
1263 * @param message Nachricht mit dem Fehler
1264 * @exception StorageObjectException
1266 void throwStorageObjectException (String message)
1267 throws StorageObjectException {
1268 _throwStorageObjectException(null, message);