2 * Copyright (C) 2001, 2002 The Mir-coders group
\r
4 * This file is part of Mir.
\r
6 * Mir is free software; you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation; either version 2 of the License, or
\r
9 * (at your option) any later version.
\r
11 * Mir is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with Mir; if not, write to the Free Software
\r
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 * In addition, as a special exception, The Mir-coders gives permission to link
\r
21 * the code of this program with the com.oreilly.servlet library, any library
\r
22 * licensed under the Apache Software License, The Sun (tm) Java Advanced
\r
23 * Imaging library (JAI), The Sun JIMI library (or with modified versions of
\r
24 * the above that use the same license as the above), and distribute linked
\r
25 * combinations including the two. You must obey the GNU General Public
\r
26 * License in all respects for all of the code used other than the above
\r
27 * mentioned libraries. If you modify this file, you may extend this exception
\r
28 * to your version of the file, but you are not obligated to do so. If you do
\r
29 * not wish to do so, delete this exception statement from your version.
\r
32 package mir.storage;
\r
38 import java.text.SimpleDateFormat;
\r
39 import java.text.ParseException;
\r
40 import freemarker.template.*;
\r
41 import com.codestudio.sql.*;
\r
42 import com.codestudio.util.*;
\r
44 import mir.storage.StorageObject;
\r
45 import mir.storage.store.*;
\r
46 import mir.entity.*;
\r
52 * Diese Klasse implementiert die Zugriffsschicht auf die Datenbank.
\r
53 * Alle Projektspezifischen Datenbankklassen erben von dieser Klasse.
\r
54 * In den Unterklassen wird im Minimalfall nur die Tabelle angegeben.
\r
55 * Im Konfigurationsfile findet sich eine Verweis auf den verwendeten
\r
56 * Treiber, Host, User und Passwort, ueber den der Zugriff auf die
\r
57 * Datenbank erfolgt.
\r
59 * @version $Id: Database.java,v 1.28 2002/12/17 19:20:31 zapata Exp $
\r
63 public class Database implements StorageObject {
\r
65 protected String theTable;
\r
66 protected String theCoreTable=null;
\r
67 protected String thePKeyName="id";
\r
68 protected int thePKeyType, thePKeyIndex;
\r
69 protected boolean evaluatedMetaData=false;
\r
70 protected ArrayList metadataFields,metadataLabels,
\r
71 metadataNotNullFields;
\r
72 protected int[] metadataTypes;
\r
73 protected Class theEntityClass;
\r
74 protected StorageObject myselfDatabase;
\r
75 protected SimpleList popupCache=null;
\r
76 protected boolean hasPopupCache = false;
\r
77 protected SimpleHash hashCache=null;
\r
78 protected boolean hasTimestamp=true;
\r
79 private String database_driver, database_url;
\r
80 private int defaultLimit;
\r
81 protected DatabaseAdaptor theAdaptor;
\r
82 protected Logfile theLog;
\r
83 private static Class GENERIC_ENTITY_CLASS=null,
\r
84 STORABLE_OBJECT_ENTITY_CLASS=null;
\r
85 private static SimpleHash POPUP_EMTYLINE=new SimpleHash();
\r
86 protected static final ObjectStore o_store=ObjectStore.getInstance();
\r
87 private SimpleDateFormat _dateFormatterOut =
\r
88 new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
\r
89 private SimpleDateFormat _dateFormatterIn =
\r
90 new SimpleDateFormat("yyyy-MM-dd HH:mm");
\r
91 private Calendar _cal = new GregorianCalendar();
\r
93 private static final int _millisPerHour = 60 * 60 * 1000;
\r
94 private static final int _millisPerMinute = 60 * 1000;
\r
97 // always same object saves a little space
\r
98 POPUP_EMTYLINE.put("key", ""); POPUP_EMTYLINE.put("value", "--");
\r
100 GENERIC_ENTITY_CLASS = Class.forName("mir.entity.StorableObjectEntity");
\r
101 STORABLE_OBJECT_ENTITY_CLASS = Class.forName("mir.entity.StorableObjectEntity");
\r
103 catch (Exception e) {
\r
104 System.err.println("FATAL: Database.java could not initialize" + e.getMessage());
\r
110 * Kontruktor bekommt den Filenamen des Konfigurationsfiles übergeben.
\r
111 * Aus diesem file werden <code>Database.Logfile</code>,
\r
112 * <code>Database.Username</code>,<code>Database.Password</code>,
\r
113 * <code>Database.Host</code> und <code>Database.Adaptor</code>
\r
114 * ausgelesen und ein Broker für die Verbindugen zur Datenbank
\r
117 * @param String confFilename Dateiname der Konfigurationsdatei
\r
119 public Database() throws StorageObjectException {
\r
120 theLog = Logfile.getInstance(MirConfig.getProp("Home")+
\r
121 MirConfig.getProp("Database.Logfile"));
\r
122 String theAdaptorName=MirConfig.getProp("Database.Adaptor");
\r
123 defaultLimit = Integer.parseInt(MirConfig.getProp("Database.Limit"));
\r
125 theEntityClass = GENERIC_ENTITY_CLASS;
\r
126 theAdaptor = (DatabaseAdaptor)Class.forName(theAdaptorName).newInstance();
\r
127 } catch (Exception e){
\r
128 theLog.printError("Error in Database() constructor with "+
\r
129 theAdaptorName + " -- " +e.getMessage());
\r
130 throw new StorageObjectException("Error in Database() constructor with "
\r
133 /*String database_username=MirConfig.getProp("Database.Username");
\r
134 String database_password=MirConfig.getProp("Database.Password");
\r
135 String database_host=MirConfig.getProp("Database.Host");
\r
137 database_driver=theAdaptor.getDriver();
\r
138 database_url=theAdaptor.getURL(database_username,database_password,
\r
140 theLog.printDebugInfo("adding Broker with: " +database_driver+":"+
\r
142 MirConfig.addBroker(database_driver,database_url);
\r
143 //myBroker=MirConfig.getBroker();
\r
148 * Liefert die Entity-Klasse zurück, in der eine Datenbankzeile gewrappt
\r
149 * wird. Wird die Entity-Klasse durch die erbende Klasse nicht überschrieben,
\r
150 * wird eine mir.entity.GenericEntity erzeugt.
\r
152 * @return Class-Objekt der Entity
\r
154 public java.lang.Class getEntityClass () {
\r
155 return theEntityClass;
\r
159 * Liefert die Standardbeschränkung von select-Statements zurück, also
\r
160 * wieviel Datensätze per Default selektiert werden.
\r
162 * @return Standard-Anzahl der Datensätze
\r
164 public int getLimit () {
\r
165 return defaultLimit;
\r
169 * Liefert den Namen des Primary-Keys zurück. Wird die Variable nicht von
\r
170 * der erbenden Klasse überschrieben, so ist der Wert <code>PKEY</code>
\r
171 * @return Name des Primary-Keys
\r
173 public String getIdName () {
\r
174 return thePKeyName;
\r
178 * Liefert den Namen der Tabelle, auf das sich das Datenbankobjekt bezieht.
\r
180 * @return Name der Tabelle
\r
182 public String getTableName () {
\r
187 * Dient dazu vererbte Tabellen bei objectrelationalen DBMS
\r
188 * zu speichern, wenn die id einer Tabelle in der parenttabelle verwaltet
\r
190 * @return liefert theCoreTabel als String zurueck, wenn gesetzt, sonst
\r
194 public String getCoreTable(){
\r
195 if (theCoreTable!=null) return theCoreTable;
\r
196 else return theTable;
\r
200 * Liefert Feldtypen der Felder der Tabelle zurueck (s.a. java.sql.Types)
\r
201 * @return int-Array mit den Typen der Felder
\r
202 * @exception StorageObjectException
\r
204 public int[] getTypes () throws StorageObjectException {
\r
205 if (metadataTypes == null)
\r
207 return metadataTypes;
\r
211 * Liefert eine Liste der Labels der Tabellenfelder
\r
212 * @return ArrayListe mit Labeln
\r
213 * @exception StorageObjectException
\r
215 public ArrayList getLabels () throws StorageObjectException {
\r
216 if (metadataLabels == null)
\r
218 return metadataLabels;
\r
222 * Liefert eine Liste der Felder der Tabelle
\r
223 * @return ArrayList mit Feldern
\r
224 * @exception StorageObjectException
\r
226 public ArrayList getFields () throws StorageObjectException {
\r
227 if (metadataFields == null)
\r
229 return metadataFields;
\r
234 * Gets value out of ResultSet according to type and converts to String
\r
235 * @param inValue Wert aus ResultSet.
\r
236 * @param aType Datenbanktyp.
\r
237 * @return liefert den Wert als String zurueck. Wenn keine Umwandlung moeglich
\r
238 * dann /unsupported value/
\r
240 private String getValueAsString (ResultSet rs, int valueIndex, int aType) throws StorageObjectException {
\r
241 String outValue = null;
\r
245 case java.sql.Types.BIT:
\r
246 outValue = (rs.getBoolean(valueIndex) == true) ? "1" : "0";
\r
248 case java.sql.Types.INTEGER:case java.sql.Types.SMALLINT:case java.sql.Types.TINYINT:case java.sql.Types.BIGINT:
\r
249 int out = rs.getInt(valueIndex);
\r
251 outValue = new Integer(out).toString();
\r
253 case java.sql.Types.NUMERIC:
\r
254 /** @todo Numeric can be float or double depending upon
\r
255 * metadata.getScale() / especially with oracle */
\r
256 long outl = rs.getLong(valueIndex);
\r
258 outValue = new Long(outl).toString();
\r
260 case java.sql.Types.REAL:
\r
261 float tempf = rs.getFloat(valueIndex);
\r
262 if (!rs.wasNull()) {
\r
265 int tempf_int = (int)tempf;
\r
266 tempf = (float)tempf_int;
\r
268 outValue = "" + tempf;
\r
269 outValue = outValue.replace('.', ',');
\r
272 case java.sql.Types.DOUBLE:
\r
273 double tempd = rs.getDouble(valueIndex);
\r
274 if (!rs.wasNull()) {
\r
277 int tempd_int = (int)tempd;
\r
278 tempd = (double)tempd_int;
\r
280 outValue = "" + tempd;
\r
281 outValue = outValue.replace('.', ',');
\r
284 case java.sql.Types.CHAR:case java.sql.Types.VARCHAR:case java.sql.Types.LONGVARCHAR:
\r
285 outValue = rs.getString(valueIndex);
\r
286 //if (outValue != null)
\r
287 //outValue = StringUtil.encodeHtml(StringUtil.unquote(outValue));
\r
289 case java.sql.Types.LONGVARBINARY:
\r
290 outValue = rs.getString(valueIndex);
\r
291 //if (outValue != null)
\r
292 //outValue = StringUtil.encodeHtml(StringUtil.unquote(outValue));
\r
294 case java.sql.Types.TIMESTAMP:
\r
295 // it's important to use Timestamp here as getting it
\r
296 // as a string is undefined and is only there for debugging
\r
297 // according to the API. we can make it a string through formatting.
\r
299 Timestamp timestamp = (rs.getTimestamp(valueIndex));
\r
300 if(!rs.wasNull()) {
\r
301 java.util.Date date = new java.util.Date(timestamp.getTime());
\r
302 outValue = _dateFormatterOut.format(date);
\r
303 _cal.setTime(date);
\r
304 int offset = _cal.get(Calendar.ZONE_OFFSET)+
\r
305 _cal.get(Calendar.DST_OFFSET);
\r
306 String tzOffset = StringUtil.zeroPaddingNumber(
\r
307 offset/_millisPerHour,2,2);
\r
308 outValue = outValue+"+"+tzOffset;
\r
312 outValue = "<unsupported value>";
\r
313 theLog.printWarning("Unsupported Datatype: at " + valueIndex +
\r
314 " (" + aType + ")");
\r
316 } catch (SQLException e) {
\r
317 throw new StorageObjectException("Could not get Value out of Resultset -- "
\r
325 * select-Operator um einen Datensatz zu bekommen.
\r
326 * @param id Primaerschluessel des Datensatzes.
\r
327 * @return liefert EntityObject des gefundenen Datensatzes oder null.
\r
329 public Entity selectById(String id) throws StorageObjectException
\r
331 if (id==null||id.equals(""))
\r
332 throw new StorageObjectException("id war null");
\r
334 // ask object store for object
\r
335 if ( StoreUtil.implementsStorableObject(theEntityClass) ) {
\r
336 String uniqueId = id;
\r
337 if ( theEntityClass.equals(StorableObjectEntity.class) )
\r
338 uniqueId+="@"+theTable;
\r
339 StoreIdentifier search_sid = new StoreIdentifier(theEntityClass, uniqueId);
\r
340 theLog.printDebugInfo("CACHE: (dbg) looking for sid " + search_sid.toString());
\r
341 Entity hit = (Entity)o_store.use(search_sid);
\r
342 if ( hit!=null ) return hit;
\r
345 Statement stmt=null;Connection con=getPooledCon();
\r
346 Entity returnEntity=null;
\r
349 /** @todo better prepared statement */
\r
350 String selectSql = "select * from " + theTable + " where " + thePKeyName + "=" + id;
\r
351 stmt = con.createStatement();
\r
352 rs = executeSql(stmt, selectSql);
\r
354 if (evaluatedMetaData==false) evalMetaData(rs.getMetaData());
\r
356 returnEntity = makeEntityFromResultSet(rs);
\r
357 else theLog.printDebugInfo("Keine daten fuer id: " + id + "in Tabelle" + theTable);
\r
361 theLog.printDebugInfo("No Data for Id " + id + " in Table " + theTable);
\r
364 catch (SQLException sqe){
\r
365 throwSQLException(sqe,"selectById"); return null;
\r
367 catch (NumberFormatException e) {
\r
368 theLog.printError("ID ist keine Zahl: " + id);
\r
370 finally { freeConnection(con,stmt); }
\r
372 /** @todo OS: Entity should be saved in ostore */
\r
373 return returnEntity;
\r
378 * select-Operator um Datensaetze zu bekommen, die key = value erfuellen.
\r
379 * @param key Datenbankfeld der Bedingung.
\r
380 * @param value Wert die der key anehmen muss.
\r
381 * @return EntityList mit den gematchten Entities
\r
383 public EntityList selectByFieldValue(String aField, String aValue)
\r
384 throws StorageObjectException
\r
386 return selectByFieldValue(aField, aValue, 0);
\r
390 * select-Operator um Datensaetze zu bekommen, die key = value erfuellen.
\r
391 * @param key Datenbankfeld der Bedingung.
\r
392 * @param value Wert die der key anehmen muss.
\r
393 * @param offset Gibt an ab welchem Datensatz angezeigt werden soll.
\r
394 * @return EntityList mit den gematchten Entities
\r
396 public EntityList selectByFieldValue(String aField, String aValue, int offset)
\r
397 throws StorageObjectException
\r
399 return selectByWhereClause(aField + "=" + aValue, offset);
\r
404 * select-Operator liefert eine EntityListe mit den gematchten Datensätzen zurück.
\r
405 * Also offset wird der erste Datensatz genommen.
\r
407 * @param wc where-Clause
\r
408 * @return EntityList mit den gematchten Entities
\r
409 * @exception StorageObjectException
\r
411 public EntityList selectByWhereClause(String where)
\r
412 throws StorageObjectException
\r
414 return selectByWhereClause(where, 0);
\r
419 * select-Operator liefert eine EntityListe mit den gematchten Datensätzen zurück.
\r
420 * Als maximale Anzahl wird das Limit auf der Konfiguration genommen.
\r
422 * @param wc where-Clause
\r
423 * @param offset ab welchem Datensatz.
\r
424 * @return EntityList mit den gematchten Entities
\r
425 * @exception StorageObjectException
\r
427 public EntityList selectByWhereClause(String whereClause, int offset)
\r
428 throws StorageObjectException
\r
430 return selectByWhereClause(whereClause, null, offset);
\r
435 * select-Operator liefert eine EntityListe mit den gematchten Datensätzen zurück.
\r
436 * Also offset wird der erste Datensatz genommen.
\r
437 * Als maximale Anzahl wird das Limit auf der Konfiguration genommen.
\r
439 * @param wc where-Clause
\r
440 * @param ob orderBy-Clause
\r
441 * @return EntityList mit den gematchten Entities
\r
442 * @exception StorageObjectException
\r
445 public EntityList selectByWhereClause(String where, String order)
\r
446 throws StorageObjectException {
\r
447 return selectByWhereClause(where, order, 0);
\r
452 * select-Operator liefert eine EntityListe mit den gematchten Datensätzen zurück.
\r
453 * Als maximale Anzahl wird das Limit auf der Konfiguration genommen.
\r
455 * @param wc where-Clause
\r
456 * @param ob orderBy-Clause
\r
457 * @param offset ab welchem Datensatz
\r
458 * @return EntityList mit den gematchten Entities
\r
459 * @exception StorageObjectException
\r
462 public EntityList selectByWhereClause(String whereClause, String orderBy, int offset)
\r
463 throws StorageObjectException {
\r
464 return selectByWhereClause(whereClause, orderBy, offset, defaultLimit);
\r
469 * select-Operator liefert eine EntityListe mit den gematchten Datensätzen zurück.
\r
470 * @param wc where-Clause
\r
471 * @param ob orderBy-Clause
\r
472 * @param offset ab welchem Datensatz
\r
473 * @param limit wieviele Datensätze
\r
474 * @return EntityList mit den gematchten Entities
\r
475 * @exception StorageObjectException
\r
478 public EntityList selectByWhereClause(String wc, String ob, int offset,
\r
480 StorageObjectException {
\r
482 // check o_store for entitylist
\r
483 if (StoreUtil.implementsStorableObject(theEntityClass)) {
\r
484 StoreIdentifier search_sid =
\r
485 new StoreIdentifier(theEntityClass,
\r
486 StoreContainerType.STOC_TYPE_ENTITYLIST,
\r
487 StoreUtil.getEntityListUniqueIdentifierFor(
\r
488 theTable, wc, ob, offset, limit));
\r
489 EntityList hit = (EntityList) o_store.use(search_sid);
\r
491 theLog.printDebugInfo("CACHE (hit): " + search_sid.toString());
\r
497 EntityList theReturnList = null;
\r
498 Connection con = null;
\r
499 Statement stmt = null;
\r
501 int offsetCount = 0, count = 0;
\r
503 // build sql-statement
\r
505 /** @todo count sql string should only be assembled if we really count
\r
506 * see below at the end of method //rk */
\r
508 if (wc != null && wc.length() == 0) {
\r
511 StringBuffer countSql = new StringBuffer("select count(*) from ").append(
\r
513 StringBuffer selectSql = new StringBuffer("select * from ").append(theTable);
\r
515 selectSql.append(" where ").append(wc);
\r
516 countSql.append(" where ").append(wc);
\r
518 if (ob != null && ! (ob.length() == 0)) {
\r
519 selectSql.append(" order by ").append(ob);
\r
521 if (theAdaptor.hasLimit()) {
\r
522 if (limit > -1 && offset > -1) {
\r
523 selectSql.append(" limit ");
\r
524 if (theAdaptor.reverseLimit()) {
\r
525 selectSql.append(limit).append(",").append(offset);
\r
528 selectSql.append(offset).append(",").append(limit);
\r
535 con = getPooledCon();
\r
536 stmt = con.createStatement();
\r
539 rs = executeSql(stmt, selectSql.toString());
\r
541 if (!evaluatedMetaData)
\r
542 evalMetaData(rs.getMetaData());
\r
544 theReturnList = new EntityList();
\r
545 Entity theResultEntity;
\r
546 while (rs.next()) {
\r
547 theResultEntity = makeEntityFromResultSet(rs);
\r
548 theReturnList.add(theResultEntity);
\r
554 // making entitylist infos
\r
555 if (! (theAdaptor.hasLimit()))
\r
556 count = offsetCount;
\r
558 if (theReturnList != null) {
\r
559 // now we decide if we have to know an overall count...
\r
560 count = offsetCount;
\r
561 if (limit > -1 && offset > -1) {
\r
562 if (offsetCount == limit) {
\r
563 /** @todo counting should be deffered to entitylist
\r
564 * getSize() should be used */
\r
565 rs = executeSql(stmt, countSql.toString());
\r
568 count = rs.getInt(1);
\r
572 theLog.printError("Could not count: " + countSql);
\r
575 theReturnList.setCount(count);
\r
576 theReturnList.setOffset(offset);
\r
577 theReturnList.setWhere(wc);
\r
578 theReturnList.setOrder(ob);
\r
579 theReturnList.setStorage(this);
\r
580 theReturnList.setLimit(limit);
\r
581 if (offset >= limit)
\r
582 theReturnList.setPrevBatch(offset - limit);
\r
583 if (offset + offsetCount < count)
\r
584 theReturnList.setNextBatch(offset + limit);
\r
585 if (StoreUtil.implementsStorableObject(theEntityClass)) {
\r
586 StoreIdentifier sid = theReturnList.getStoreIdentifier();
\r
587 theLog.printDebugInfo("CACHE (add): " + sid.toString());
\r
592 catch (SQLException sqe) {
\r
593 throwSQLException(sqe, "selectByWhereClause");
\r
598 freeConnection(con, stmt);
\r
600 catch (Throwable t) {
\r
605 return theReturnList;
\r
610 * Bastelt aus einer Zeile der Datenbank ein EntityObjekt.
\r
612 * @param rs Das ResultSetObjekt.
\r
613 * @return Entity Die Entity.
\r
615 private Entity makeEntityFromResultSet (ResultSet rs)
\r
616 throws StorageObjectException
\r
618 /** @todo OS: get Pkey from ResultSet and consult ObjectStore */
\r
619 HashMap theResultHash = new HashMap();
\r
620 String theResult = null;
\r
622 Entity returnEntity = null;
\r
624 int size = metadataFields.size();
\r
625 for (int i = 0; i < size; i++) {
\r
626 // alle durchlaufen bis nix mehr da
\r
628 theType = metadataTypes[i];
\r
629 if (theType == java.sql.Types.LONGVARBINARY) {
\r
630 InputStreamReader is = (InputStreamReader)rs.getCharacterStream(i + 1);
\r
632 char[] data = new char[32768];
\r
633 StringBuffer theResultString = new StringBuffer();
\r
635 while ((len = is.read(data)) > 0) {
\r
636 theResultString.append(data, 0, len);
\r
639 theResult = theResultString.toString();
\r
646 theResult = getValueAsString(rs, (i + 1), theType);
\r
648 if (theResult != null) {
\r
649 theResultHash.put(metadataFields.get(i), theResult);
\r
652 if (theEntityClass != null) {
\r
653 returnEntity = (Entity)theEntityClass.newInstance();
\r
654 returnEntity.setValues(theResultHash);
\r
655 returnEntity.setStorage(myselfDatabase);
\r
656 if ( returnEntity instanceof StorableObject ) {
\r
657 theLog.printDebugInfo("CACHE: ( in) " + returnEntity.getId() + " :"+theTable);
\r
658 o_store.add(((StorableObject)returnEntity).getStoreIdentifier());
\r
661 throwStorageObjectException("Internal Error: theEntityClass not set!");
\r
664 catch (IllegalAccessException e) {
\r
665 throwStorageObjectException("No access! -- " + e.getMessage());
\r
667 catch (IOException e) {
\r
668 throwStorageObjectException("IOException! -- " + e.getMessage());
\r
670 catch (InstantiationException e) {
\r
671 throwStorageObjectException("No Instatiation! -- " + e.getMessage());
\r
673 catch (SQLException sqe) {
\r
674 throwSQLException(sqe, "makeEntityFromResultSet");
\r
677 return returnEntity;
\r
681 * insert-Operator: fügt eine Entity in die Tabelle ein. Eine Spalte WEBDB_CREATE
\r
682 * wird automatisch mit dem aktuellen Datum gefuellt.
\r
685 * @return der Wert des Primary-keys der eingefügten Entity
\r
687 public String insert (Entity theEntity) throws StorageObjectException {
\r
689 invalidatePopupCache();
\r
691 // invalidating all EntityLists corresponding with theEntityClass
\r
692 if ( StoreUtil.implementsStorableObject(theEntityClass) ) {
\r
693 StoreContainerType stoc_type =
\r
694 StoreContainerType.valueOf( theEntityClass,
\r
695 StoreContainerType.STOC_TYPE_ENTITYLIST);
\r
696 o_store.invalidate(stoc_type);
\r
699 String returnId = null;
\r
700 Connection con = null; PreparedStatement pstmt = null;
\r
703 ArrayList streamedInput = theEntity.streamedInput();
\r
704 StringBuffer f = new StringBuffer();
\r
705 StringBuffer v = new StringBuffer();
\r
706 String aField, aValue;
\r
707 boolean firstField = true;
\r
709 for (int i = 0; i < getFields().size(); i++) {
\r
710 aField = (String)getFields().get(i);
\r
711 if (!aField.equals(thePKeyName)) {
\r
714 if (aField.equals("webdb_create") ||
\r
715 aField.equals("webdb_lastchange")) {
\r
719 if (streamedInput != null && streamedInput.contains(aField)) {
\r
723 if (theEntity.hasValueForField(aField)) {
\r
724 aValue = "'" + JDBCStringRoutines.escapeStringLiteral((String)theEntity.getValue(aField)) + "'";
\r
728 // wenn Wert gegeben, dann einbauen
\r
729 if (aValue != null) {
\r
730 if (firstField == false) {
\r
735 firstField = false;
\r
743 StringBuffer sqlBuf = new StringBuffer("insert into ").append(theTable).append("(").append(f).append(") values (").append(v).append(")");
\r
744 String sql = sqlBuf.toString();
\r
745 theLog.printInfo("INSERT: " + sql);
\r
746 con = getPooledCon();
\r
747 con.setAutoCommit(false);
\r
748 pstmt = con.prepareStatement(sql);
\r
749 if (streamedInput != null) {
\r
750 for (int i = 0; i < streamedInput.size(); i++) {
\r
751 String inputString = (String)theEntity.getValue((String)streamedInput.get(i));
\r
752 pstmt.setBytes(i + 1, inputString.getBytes());
\r
755 int ret = pstmt.executeUpdate();
\r
760 pstmt = con.prepareStatement(theAdaptor.getLastInsertSQL((Database)myselfDatabase));
\r
761 ResultSet rs = pstmt.executeQuery();
\r
763 returnId = rs.getString(1);
\r
764 theEntity.setId(returnId);
\r
765 } catch (SQLException sqe) {
\r
766 throwSQLException(sqe, "insert");
\r
769 con.setAutoCommit(true);
\r
770 } catch (Exception e) {
\r
773 freeConnection(con, pstmt);
\r
775 /** @todo store entity in o_store */
\r
780 * update-Operator: aktualisiert eine Entity. Eine Spalte WEBDB_LASTCHANGE
\r
781 * wird automatisch mit dem aktuellen Datum gefuellt.
\r
785 public void update (Entity theEntity) throws StorageObjectException
\r
787 Connection con = null; PreparedStatement pstmt = null;
\r
788 /** @todo this is stupid: why do we prepare statement, when we
\r
789 * throw it away afterwards. should be regular statement
\r
790 * update/insert could better be one routine called save()
\r
791 * that chooses to either insert or update depending if we
\r
792 * have a primary key in the entity. i don't know if we
\r
793 * still need the streamed input fields. // rk */
\r
795 /** @todo extension: check if Entity did change, otherwise we don't need
\r
796 * the roundtrip to the database */
\r
798 /** invalidating corresponding entitylists in o_store*/
\r
799 if ( StoreUtil.implementsStorableObject(theEntityClass) ) {
\r
800 StoreContainerType stoc_type =
\r
801 StoreContainerType.valueOf( theEntityClass,
\r
802 StoreContainerType.STOC_TYPE_ENTITYLIST);
\r
803 o_store.invalidate(stoc_type);
\r
806 ArrayList streamedInput = theEntity.streamedInput();
\r
807 String id = theEntity.getId();
\r
809 StringBuffer fv = new StringBuffer();
\r
810 boolean firstField = true;
\r
812 invalidatePopupCache();
\r
813 // build sql statement
\r
814 for (int i = 0; i < getFields().size(); i++) {
\r
815 aField = (String)metadataFields.get(i);
\r
816 // only normal cases
\r
817 if (!(aField.equals(thePKeyName) || aField.equals("webdb_create") ||
\r
818 aField.equals("webdb_lastchange") || (streamedInput != null && streamedInput.contains(aField)))) {
\r
819 if (theEntity.hasValueForField(aField)) {
\r
820 if (firstField == false) {
\r
824 firstField = false;
\r
826 fv.append(aField).append("='").append(JDBCStringRoutines.escapeStringLiteral((String) theEntity.getValue(aField))).append("'");
\r
828 // fv.append(aField).append("='").append(StringUtil.quote((String)theEntity.getValue(aField))).append("'");
\r
832 StringBuffer sql = new StringBuffer("update ").append(theTable).append(" set ").append(fv);
\r
834 if (metadataFields.contains("webdb_lastchange")) {
\r
835 sql.append(",webdb_lastchange=NOW()");
\r
837 // special case: the webdb_create requires the field in yyyy-mm-dd HH:mm
\r
838 // format so anything extra will be ignored. -mh
\r
839 if (metadataFields.contains("webdb_create") &&
\r
840 theEntity.hasValueForField("webdb_create")) {
\r
841 // minimum of 10 (yyyy-mm-dd)...
\r
842 if (theEntity.getValue("webdb_create").length() >= 10) {
\r
843 String dateString = theEntity.getValue("webdb_create");
\r
844 // if only 10, then add 00:00 so it doesn't throw a ParseException
\r
845 if (dateString.length() == 10)
\r
846 dateString=dateString+" 00:00";
\r
850 java.util.Date d = _dateFormatterIn.parse(dateString);
\r
851 Timestamp tStamp = new Timestamp(d.getTime());
\r
852 sql.append(",webdb_create='"+tStamp.toString()+"'");
\r
853 } catch (ParseException e) {
\r
854 throw new StorageObjectException(e.getMessage());
\r
858 if (streamedInput != null) {
\r
859 for (int i = 0; i < streamedInput.size(); i++) {
\r
860 sql.append(",").append(streamedInput.get(i)).append("=?");
\r
863 sql.append(" where id=").append(id);
\r
864 theLog.printInfo("UPDATE: " + sql);
\r
867 con = getPooledCon();
\r
868 con.setAutoCommit(false);
\r
869 pstmt = con.prepareStatement(sql.toString());
\r
870 if (streamedInput != null) {
\r
871 for (int i = 0; i < streamedInput.size(); i++) {
\r
872 String inputString = theEntity.getValue((String)streamedInput.get(i));
\r
873 pstmt.setBytes(i + 1, inputString.getBytes());
\r
876 pstmt.executeUpdate();
\r
878 catch (SQLException sqe) {
\r
879 throwSQLException(sqe, "update");
\r
883 con.setAutoCommit(true);
\r
884 } catch (Exception e) {
\r
887 freeConnection(con, pstmt);
\r
893 * @param id des zu loeschenden Datensatzes
\r
894 * @return boolean liefert true zurueck, wenn loeschen erfolgreich war.
\r
896 public boolean delete (String id) throws StorageObjectException {
\r
898 invalidatePopupCache();
\r
899 // ostore send notification
\r
900 if ( StoreUtil.implementsStorableObject(theEntityClass) ) {
\r
901 String uniqueId = id;
\r
902 if ( theEntityClass.equals(StorableObjectEntity.class) )
\r
903 uniqueId+="@"+theTable;
\r
904 theLog.printInfo("CACHE: (del) " + id);
\r
905 StoreIdentifier search_sid =
\r
906 new StoreIdentifier(theEntityClass, StoreContainerType.STOC_TYPE_ENTITY, uniqueId);
\r
907 o_store.invalidate(search_sid);
\r
910 /** @todo could be prepared Statement */
\r
911 Statement stmt = null; Connection con = null;
\r
913 String sql="delete from "+theTable+" where "+thePKeyName+"='"+id+"'";
\r
914 theLog.printInfo("DELETE " + sql);
\r
916 con = getPooledCon(); stmt = con.createStatement();
\r
917 res = stmt.executeUpdate(sql);
\r
919 catch (SQLException sqe) { throwSQLException(sqe, "delete"); }
\r
920 finally { freeConnection(con, stmt); }
\r
922 return (res > 0) ? true : false;
\r
925 /* noch nicht implementiert.
\r
926 * @return immer false
\r
928 public boolean delete (EntityList theEntityList) {
\r
929 invalidatePopupCache();
\r
934 * Diese Methode sollte ueberschrieben werden, wenn fuer die abgeleitete Database-Klasse
\r
935 * eine SimpleList mit Standard-Popupdaten erzeugt werden koennen soll.
\r
938 public SimpleList getPopupData () throws StorageObjectException {
\r
943 * Holt Daten fuer Popups.
\r
944 * @param name Name des Feldes.
\r
945 * @param hasNullValue Wenn true wird eine leerer Eintrag fuer die Popups erzeugt.
\r
946 * @return SimpleList Gibt freemarker.template.SimpleList zurueck.
\r
948 public SimpleList getPopupData (String name, boolean hasNullValue)
\r
949 throws StorageObjectException {
\r
950 return getPopupData(name, hasNullValue, null);
\r
954 * Holt Daten fuer Popups.
\r
955 * @param name Name des Feldes.
\r
956 * @param hasNullValue Wenn true wird eine leerer Eintrag fuer die Popups erzeugt.
\r
957 * @param where Schraenkt die Selektion der Datensaetze ein.
\r
958 * @return SimpleList Gibt freemarker.template.SimpleList zurueck.
\r
960 public SimpleList getPopupData (String name, boolean hasNullValue, String where) throws StorageObjectException {
\r
961 return getPopupData(name, hasNullValue, where, null);
\r
965 * Holt Daten fuer Popups.
\r
966 * @param name Name des Feldes.
\r
967 * @param hasNullValue Wenn true wird eine leerer Eintrag fuer die Popups erzeugt.
\r
968 * @param where Schraenkt die Selektion der Datensaetze ein.
\r
969 * @param order Gibt ein Feld als Sortierkriterium an.
\r
970 * @return SimpleList Gibt freemarker.template.SimpleList zurueck.
\r
972 public SimpleList getPopupData (String name, boolean hasNullValue, String where, String order) throws StorageObjectException {
\r
974 if (hasPopupCache && popupCache != null)
\r
976 SimpleList simpleList = null;
\r
977 Connection con = null;
\r
978 Statement stmt = null;
\r
980 StringBuffer sql = new StringBuffer("select ").append(thePKeyName)
\r
981 .append(",").append(name).append(" from ")
\r
983 if (where != null && !(where.length() == 0))
\r
984 sql.append(" where ").append(where);
\r
985 sql.append(" order by ");
\r
986 if (order != null && !(order.length() == 0))
\r
992 con = getPooledCon();
\r
994 catch (Exception e) {
\r
995 throw new StorageObjectException(e.getMessage());
\r
998 stmt = con.createStatement();
\r
999 ResultSet rs = executeSql(stmt, sql.toString());
\r
1002 if (!evaluatedMetaData) get_meta_data();
\r
1003 simpleList = new SimpleList();
\r
1004 // if popup has null-selector
\r
1005 if (hasNullValue) simpleList.add(POPUP_EMTYLINE);
\r
1007 SimpleHash popupDict;
\r
1008 while (rs.next()) {
\r
1009 popupDict = new SimpleHash();
\r
1010 popupDict.put("key", getValueAsString(rs, 1, thePKeyType));
\r
1011 popupDict.put("value", rs.getString(2));
\r
1012 simpleList.add(popupDict);
\r
1017 catch (Exception e) {
\r
1018 theLog.printError("getPopupData: "+e.getMessage());
\r
1019 throw new StorageObjectException(e.toString());
\r
1022 freeConnection(con, stmt);
\r
1025 if (hasPopupCache) popupCache = simpleList;
\r
1026 return simpleList;
\r
1030 * Liefert alle Daten der Tabelle als SimpleHash zurueck. Dies wird verwandt,
\r
1031 * wenn in den Templates ein Lookup-Table benoetigt wird. Sollte nur bei kleinen
\r
1032 * Tabellen Verwendung finden.
\r
1033 * @return SimpleHash mit den Tabellezeilen.
\r
1035 public SimpleHash getHashData () {
\r
1036 /** @todo dangerous! this should have a flag to be enabled, otherwise
\r
1037 * very big Hashes could be returned */
\r
1038 if (hashCache == null) {
\r
1040 hashCache = HTMLTemplateProcessor.makeSimpleHash(selectByWhereClause("",
\r
1043 catch (StorageObjectException e) {
\r
1044 theLog.printDebugInfo(e.getMessage());
\r
1050 /* invalidates the popupCache
\r
1052 protected void invalidatePopupCache () {
\r
1053 /** @todo invalidates toooo much */
\r
1054 popupCache = null;
\r
1059 * Diese Methode fuehrt den Sqlstring <i>sql</i> aus und timed im Logfile.
\r
1060 * @param stmt Statemnt
\r
1061 * @param sql Sql-String
\r
1062 * @return ResultSet
\r
1063 * @exception StorageObjectException
\r
1065 public ResultSet executeSql (Statement stmt, String sql)
\r
1066 throws StorageObjectException, SQLException
\r
1068 long startTime = System.currentTimeMillis();
\r
1071 rs = stmt.executeQuery(sql);
\r
1072 theLog.printInfo((System.currentTimeMillis() - startTime) + "ms. for: "
\r
1075 catch (SQLException e)
\r
1077 theLog.printDebugInfo("Failed: " + (System.currentTimeMillis()
\r
1078 - startTime) + "ms. for: "+ sql);
\r
1086 * Fuehrt Statement stmt aus und liefert Resultset zurueck. Das SQL-Statment wird
\r
1087 * getimed und geloggt.
\r
1088 * @param stmt PreparedStatement mit der SQL-Anweisung
\r
1089 * @return Liefert ResultSet des Statements zurueck.
\r
1090 * @exception StorageObjectException, SQLException
\r
1092 public ResultSet executeSql (PreparedStatement stmt)
\r
1093 throws StorageObjectException, SQLException {
\r
1095 long startTime = (new java.util.Date()).getTime();
\r
1096 ResultSet rs = stmt.executeQuery();
\r
1097 theLog.printInfo((new java.util.Date().getTime() - startTime) + "ms.");
\r
1102 * returns the number of rows in the table
\r
1104 public int getSize(String where)
\r
1105 throws SQLException,StorageObjectException
\r
1107 long startTime = System.currentTimeMillis();
\r
1108 String sql = "SELECT Count(*) FROM "+ theTable;
\r
1109 if (where != null && !(where.length() == 0))
\r
1110 sql = sql + " where " + where;
\r
1111 Connection con = null;
\r
1112 Statement stmt = null;
\r
1116 con = getPooledCon();
\r
1117 stmt = con.createStatement();
\r
1118 ResultSet rs = executeSql(stmt,sql);
\r
1120 result = rs.getInt(1);
\r
1123 catch (SQLException e) {
\r
1124 theLog.printError(e.getMessage());
\r
1127 freeConnection(con,stmt);
\r
1129 //theLog.printInfo(theTable + " has "+ result +" rows where " + where);
\r
1130 theLog.printInfo((System.currentTimeMillis() - startTime) + "ms. for: "
\r
1135 public int executeUpdate(Statement stmt, String sql)
\r
1136 throws StorageObjectException, SQLException
\r
1139 long startTime = (new java.util.Date()).getTime();
\r
1142 rs = stmt.executeUpdate(sql);
\r
1143 theLog.printInfo((new java.util.Date().getTime() - startTime) + "ms. for: "
\r
1146 catch (SQLException e)
\r
1148 theLog.printDebugInfo("Failed: " + (new java.util.Date().getTime()
\r
1149 - startTime) + "ms. for: "+ sql);
\r
1155 public int executeUpdate(String sql) throws StorageObjectException, SQLException
\r
1158 long startTime = (new java.util.Date()).getTime();
\r
1159 Connection con=null;
\r
1160 PreparedStatement pstmt=null;
\r
1162 con=getPooledCon();
\r
1163 pstmt = con.prepareStatement(sql);
\r
1164 result = pstmt.executeUpdate();
\r
1166 catch (Exception e) {
\r
1167 theLog.printDebugInfo("settimage :: setImage failed: "+e.getMessage());
\r
1168 throw new StorageObjectException("executeUpdate failed: "+e.getMessage());
\r
1171 freeConnection(con,pstmt);
\r
1173 theLog.printInfo((new java.util.Date().getTime() - startTime) + "ms. for: " + sql);
\r
1178 * Wertet ResultSetMetaData aus und setzt interne Daten entsprechend
\r
1179 * @param md ResultSetMetaData
\r
1180 * @exception StorageObjectException
\r
1182 private void evalMetaData (ResultSetMetaData md)
\r
1183 throws StorageObjectException {
\r
1185 this.evaluatedMetaData = true;
\r
1186 this.metadataFields = new ArrayList();
\r
1187 this.metadataLabels = new ArrayList();
\r
1188 this.metadataNotNullFields = new ArrayList();
\r
1190 int numFields = md.getColumnCount();
\r
1191 this.metadataTypes = new int[numFields];
\r
1194 for (int i = 1; i <= numFields; i++) {
\r
1195 aField = md.getColumnName(i);
\r
1196 metadataFields.add(aField);
\r
1197 metadataLabels.add(md.getColumnLabel(i));
\r
1198 aType = md.getColumnType(i);
\r
1199 metadataTypes[i - 1] = aType;
\r
1200 if (aField.equals(thePKeyName)) {
\r
1201 thePKeyType = aType; thePKeyIndex = i;
\r
1203 if (md.isNullable(i) == md.columnNullable) {
\r
1204 metadataNotNullFields.add(aField);
\r
1207 } catch (SQLException e) {
\r
1208 throwSQLException(e, "evalMetaData");
\r
1213 * Wertet die Metadaten eines Resultsets fuer eine Tabelle aus,
\r
1214 * um die alle Columns und Typen einer Tabelle zu ermitteln.
\r
1216 private void get_meta_data () throws StorageObjectException {
\r
1217 Connection con = null;
\r
1218 PreparedStatement pstmt = null;
\r
1219 String sql = "select * from " + theTable + " where 0=1";
\r
1221 con = getPooledCon();
\r
1222 pstmt = con.prepareStatement(sql);
\r
1223 theLog.printInfo("METADATA: " + sql);
\r
1224 ResultSet rs = pstmt.executeQuery();
\r
1225 evalMetaData(rs.getMetaData());
\r
1227 } catch (SQLException e) {
\r
1228 throwSQLException(e, "get_meta_data");
\r
1230 freeConnection(con, pstmt);
\r
1235 public Connection getPooledCon() throws StorageObjectException {
\r
1236 /* @todo , doublecheck but I'm pretty sure that this is unnecessary. -mh
\r
1238 Class.forName("com.codestudio.sql.PoolMan").newInstance();
\r
1239 } catch (Exception e){
\r
1240 throw new StorageObjectException("Could not find the PoolMan Driver"
\r
1243 Connection con = null;
\r
1246 con = SQLManager.getInstance().requestConnection();
\r
1248 catch(SQLException e){
\r
1249 theLog.printError("could not connect to the database "+e.getMessage());
\r
1250 System.err.println("could not connect to the database "+e.getMessage());
\r
1251 throw new StorageObjectException("Could not connect to the database"+ e.getMessage());
\r
1257 public void freeConnection (Connection con, Statement stmt) throws StorageObjectException {
\r
1258 SQLManager.getInstance().closeStatement(stmt);
\r
1259 SQLManager.getInstance().returnConnection(con);
\r
1263 * Wertet SQLException aus und wirft dannach eine StorageObjectException
\r
1264 * @param sqe SQLException
\r
1265 * @param wo Funktonsname, in der die SQLException geworfen wurde
\r
1266 * @exception StorageObjectException
\r
1268 protected void throwSQLException (SQLException sqe, String wo) throws StorageObjectException {
\r
1269 String state = "";
\r
1270 String message = "";
\r
1272 if (sqe != null) {
\r
1273 state = sqe.getSQLState();
\r
1274 message = sqe.getMessage();
\r
1275 vendor = sqe.getErrorCode();
\r
1277 theLog.printError(state + ": " + vendor + " : " + message + " Funktion: "
\r
1279 throw new StorageObjectException((sqe == null) ? "undefined sql exception" :
\r
1280 sqe.getMessage());
\r
1283 protected void _throwStorageObjectException (Exception e, String wo)
\r
1284 throws StorageObjectException {
\r
1287 theLog.printError(e.getMessage()+ wo);
\r
1288 throw new StorageObjectException(wo + e.getMessage());
\r
1291 theLog.printError(wo);
\r
1292 throw new StorageObjectException(wo);
\r
1298 * Loggt Fehlermeldung mit dem Parameter Message und wirft dannach
\r
1299 * eine StorageObjectException
\r
1300 * @param message Nachricht mit dem Fehler
\r
1301 * @exception StorageObjectException
\r
1303 void throwStorageObjectException (String message)
\r
1304 throws StorageObjectException {
\r
1305 _throwStorageObjectException(null, message);
\r