some notes in preparation of ObjectStore and other things
[mir.git] / source / mir / storage / Database.java
1 /*
2  * put your module comment here
3  */
4 package mir.storage;
5
6 import  java.sql.*;
7 import  java.lang.*;
8 import  java.io.*;
9 import  java.util.*;
10 import  freemarker.template.*;
11 import  mir.storage.StorageObject;
12 import  mir.entity.*;
13 import  mir.misc.*;
14 import com.codestudio.sql.*;
15 import com.codestudio.util.*;
16
17
18 /**
19  * Diese Klasse implementiert die Zugriffsschicht auf die Datenbank.
20  * Alle Projektspezifischen Datenbankklassen erben von dieser Klasse.
21  * In den Unterklassen wird im Minimalfall nur die Tabelle angegeben.
22  * Im Konfigurationsfile findet sich eine Verweis auf den verwendeten
23  * Treiber, Host, User und Passwort, ueber den der Zugriff auf die
24  * Datenbank erfolgt.
25  *
26  * @author RK
27  * @version 16.7.1999
28  */
29 public class Database implements StorageObject {
30
31         protected String                    theTable;
32         protected String                    theCoreTable=null;
33         protected String                    thePKeyName="id";
34         protected int                       thePKeyType;
35         protected boolean                   evaluatedMetaData=false;
36         protected ArrayList                 metadataFields,metadataLabels,
37                                                                                                                                                         metadataNotNullFields;
38         protected int[]                     metadataTypes;
39         protected Class                     theEntityClass;
40         protected StorageObject             myselfDatabase;
41
42         /** @todo DatabaseCache will soon be replaced by
43          *  ObjectStore. Points to interweave Database with
44          *  ObjectStore are marked "todo: OS:" below */
45         protected DatabaseCache             cache;
46
47         protected SimpleList                popupCache=null;
48         protected boolean                   hasPopupCache = false;
49         protected SimpleHash                hashCache=null;
50         protected boolean                   hasTimestamp=true;
51         private       String                database_driver;
52         private       String                database_url;
53         private int                         defaultLimit;
54         protected DatabaseAdaptor           theAdaptor;
55         protected Logfile                   theLog;
56         protected static final SimpleHash   POPUP_EMTYLINE=new SimpleHash();
57
58         static {
59                 // always same object saves a little space
60                 POPUP_EMTYLINE.put("key", ""); POPUP_EMTYLINE.put("value", "--");
61
62                 /** @todo OS: init ObjectStore*/
63         }
64
65         /**
66          * Kontruktor bekommt den Filenamen des Konfigurationsfiles übergeben.
67          * Aus diesem file werden <code>Database.Logfile</code>,
68          * <code>Database.Username</code>,<code>Database.Password</code>,
69          * <code>Database.Host</code> und <code>Database.Adaptor</code>
70          * ausgelesen und ein Broker für die Verbindugen zur Datenbank
71          * erzeugt.
72          *
73          * @param   String confFilename Dateiname der Konfigurationsdatei
74          */
75         public Database() throws StorageObjectException {
76                 theLog = Logfile.getInstance(MirConfig.getProp("Home")+
77                                                                                                                                 MirConfig.getProp("Database.Logfile"));
78                 String theAdaptorName=MirConfig.getProp("Database.Adaptor");
79                 defaultLimit = Integer.parseInt(MirConfig.getProp("Database.Limit"));
80                 try {
81                         theEntityClass = Class.forName("mir.entity.GenericEntity");
82                         theAdaptor = (DatabaseAdaptor)Class.forName(theAdaptorName).newInstance();
83                 } catch (Exception e){
84                         theLog.printError("Error in Database() constructor with "+
85                                                                                                 theAdaptorName + " -- " +e.toString());
86                         throw new StorageObjectException("Error in Database() constructor with "
87                                                                                                                                                          +e.toString());
88                 }
89                 /*String database_username=MirConfig.getProp("Database.Username");
90                 String database_password=MirConfig.getProp("Database.Password");
91                 String database_host=MirConfig.getProp("Database.Host");
92                 try {
93                         database_driver=theAdaptor.getDriver();
94                         database_url=theAdaptor.getURL(database_username,database_password,
95                                                                                                                                                 database_host);
96                         theLog.printDebugInfo("adding Broker with: " +database_driver+":"+
97                                                                                                                 database_url  );
98                         MirConfig.addBroker(database_driver,database_url);
99                         //myBroker=MirConfig.getBroker();
100                 }*/
101         }
102
103         /**
104          * Liefert die Entity-Klasse zurück, in der eine Datenbankzeile gewrappt
105          * wird. Wird die Entity-Klasse durch die erbende Klasse nicht überschrieben,
106          * wird eine mir.entity.GenericEntity erzeugt.
107          *
108          * @return Class-Objekt der Entity
109          */
110         public java.lang.Class getEntityClass () {
111                 return  theEntityClass;
112         }
113
114         /**
115          * Liefert die Standardbeschränkung von select-Statements zurück, also
116          * wieviel Datensätze per Default selektiert werden.
117          *
118          * @return Standard-Anzahl der Datensätze
119          */
120         public int getLimit () {
121                 return  defaultLimit;
122         }
123
124         /**
125          * Liefert den Namen des Primary-Keys zurück. Wird die Variable nicht von
126          * der erbenden Klasse überschrieben, so ist der Wert <code>PKEY</code>
127          * @return Name des Primary-Keys
128          */
129         public String getIdName () {
130                 return  thePKeyName;
131         }
132
133         /**
134          * Liefert den Namen der Tabelle, auf das sich das Datenbankobjekt bezieht.
135          *
136          * @return Name der Tabelle
137          */
138         public String getTableName () {
139                 return  theTable;
140         }
141
142         /*
143          *   Dient dazu vererbte Tabellen bei objectrelationalen DBMS
144          *   zu speichern, wenn die id einer Tabelle in der parenttabelle verwaltet
145          *   wird.
146          *   @return liefert theCoreTabel als String zurueck, wenn gesetzt, sonst
147          *    the Table
148          */
149
150         public String getCoreTable(){
151                 if (theCoreTable!=null) return theCoreTable;
152                 else return theTable;
153         }
154
155         /**
156          * Liefert Feldtypen der Felder der Tabelle zurueck (s.a. java.sql.Types)
157          * @return int-Array mit den Typen der Felder
158          * @exception StorageObjectException
159          */
160         public int[] getTypes () throws StorageObjectException {
161                 if (metadataTypes == null)
162                         get_meta_data();
163                 return  metadataTypes;
164         }
165
166         /**
167          * Liefert eine Liste der Labels der Tabellenfelder
168          * @return ArrayListe mit Labeln
169          * @exception StorageObjectException
170          */
171         public ArrayList getLabels () throws StorageObjectException {
172                 if (metadataLabels == null)
173                         get_meta_data();
174                 return  metadataLabels;
175         }
176
177         /**
178          * Liefert eine Liste der Felder der Tabelle
179          * @return ArrayList mit Feldern
180          * @exception StorageObjectException
181          */
182         public ArrayList getFields () throws StorageObjectException {
183                 if (metadataFields == null)
184                         get_meta_data();
185                 return  metadataFields;
186         }
187
188
189         /*
190          *   Gets value out of ResultSet according to type and converts to String
191          *   @param inValue  Wert aus ResultSet.
192          *   @param aType  Datenbanktyp.
193          *   @return liefert den Wert als String zurueck. Wenn keine Umwandlung moeglich
194          *           dann /unsupported value/
195          */
196         private String getValueAsString (ResultSet rs, int valueIndex, int aType) throws StorageObjectException {
197                 String outValue = null;
198                 if (rs != null) {
199                         try {
200                                 switch (aType) {
201                                         case java.sql.Types.BIT:
202                                                 outValue = (rs.getBoolean(valueIndex) == true) ? "1" : "0";
203                                                 break;
204                                         case java.sql.Types.INTEGER:case java.sql.Types.SMALLINT:case java.sql.Types.TINYINT:case java.sql.Types.BIGINT:
205                                                 int out = rs.getInt(valueIndex);
206                                                 if (!rs.wasNull())
207                                                         outValue = new Integer(out).toString();
208                                                 break;
209                                         case java.sql.Types.NUMERIC:
210                                                 long outl = rs.getLong(valueIndex);
211                                                 if (!rs.wasNull())
212                                                         outValue = new Long(outl).toString();
213                                                 break;
214                                         case java.sql.Types.REAL:
215                                                 float tempf = rs.getFloat(valueIndex);
216                                                 if (!rs.wasNull()) {
217                                                         tempf *= 10;
218                                                         tempf += 0.5;
219                                                         int tempf_int = (int)tempf;
220                                                         tempf = (float)tempf_int;
221                                                         tempf /= 10;
222                                                         outValue = "" + tempf;
223                                                         outValue = outValue.replace('.', ',');
224                                                 }
225                                                 break;
226                                         case java.sql.Types.DOUBLE:
227                                                 double tempd = rs.getDouble(valueIndex);
228                                                 if (!rs.wasNull()) {
229                                                         tempd *= 10;
230                                                         tempd += 0.5;
231                                                         int tempd_int = (int)tempd;
232                                                         tempd = (double)tempd_int;
233                                                         tempd /= 10;
234                                                         outValue = "" + tempd;
235                                                         outValue = outValue.replace('.', ',');
236                                                 }
237                                                 break;
238                                         case java.sql.Types.CHAR:case java.sql.Types.VARCHAR:case java.sql.Types.LONGVARCHAR:
239                                                 outValue = rs.getString(valueIndex);
240                                                 //if (outValue != null)
241                                                         //outValue = StringUtil.encodeHtml(StringUtil.unquote(outValue));
242                                                 break;
243                                         case java.sql.Types.LONGVARBINARY:
244                                                 outValue = rs.getString(valueIndex);
245                                                 //if (outValue != null)
246                                                         //outValue = StringUtil.encodeHtml(StringUtil.unquote(outValue));
247                                                 break;
248                                         case java.sql.Types.TIMESTAMP:
249                                                 Timestamp timestamp = (rs.getTimestamp(valueIndex));
250                                                 if (!rs.wasNull()) {
251                                                         outValue = timestamp.toString();
252                                                 }
253                                                 break;
254                                         default:
255                                                 outValue = "<unsupported value>";
256                                                 theLog.printWarning("Unsupported Datatype: at " + valueIndex +
257                                                                 " (" + aType + ")");
258                                 }
259                         } catch (SQLException e) {
260                                 throw  new StorageObjectException("Could not get Value out of Resultset -- "
261                                                 + e.toString());
262                         }
263                 }
264                 return  outValue;
265         }
266
267         /*
268          *   select-Operator um einen Datensatz zu bekommen.
269          *   @param id Primaerschluessel des Datensatzes.
270          *   @return liefert EntityObject des gefundenen Datensatzes oder null.
271          */
272         public Entity selectById(String id)
273                 throws StorageObjectException {
274
275                 if (id==null||id.equals(""))
276                         throw new StorageObjectException("id war null");
277
278                 /** @todo OS: build StoreIdenfier and check ObjectStore for StoreIdentifier */
279                 if (cache != null && (cache.containsKey(id) > -1))
280                         return (Entity)cache.get(id);  // wenn cache gesetzt, evtl. kein roundtrip zur Datenbank
281
282                 Statement stmt=null;Connection con=getPooledCon();
283                 Entity returnEntity=null;
284                 try {
285                         ResultSet rs;
286                         /** @todo better prepared statement */
287                         String selectSql = "select * from " + theTable + " where " + thePKeyName + "=" + id;
288                         stmt = con.createStatement();
289                         rs = executeSql(stmt, selectSql);
290                         if (rs != null) {
291                                 if (evaluatedMetaData==false) evalMetaData(rs.getMetaData());
292                                 if (rs.next())
293                                         returnEntity = makeEntityFromResultSet(rs);
294                                 else theLog.printDebugInfo("Keine daten fuer id: " + id + "in Tabelle" + theTable);
295                                 rs.close();
296                         }
297                         else {
298                                 theLog.printDebugInfo("No Data for Id " + id + " in Table " + theTable);
299                         }
300                 }
301                 catch (SQLException sqe){
302                         throwSQLException(sqe,"selectById"); return null;
303                 }
304                 catch (NumberFormatException e) {
305                         theLog.printError("ID ist keine Zahl: " + id);
306                 }
307                 finally { freeConnection(con,stmt); }
308
309                 /** @todo OS: Entity should be saved in ostore */
310                 return returnEntity;
311         }
312
313
314         /**
315          *   select-Operator um Datensaetze zu bekommen, die key = value erfuellen.
316          *   @param key  Datenbankfeld der Bedingung.
317          *   @param value  Wert die der key anehmen muss.
318          *   @return EntityList mit den gematchten Entities
319          */
320         public EntityList selectByFieldValue(String aField, String aValue)
321                 throws StorageObjectException
322         {
323                 return selectByFieldValue(aField, aValue, 0);
324         }
325
326         /**
327          *   select-Operator um Datensaetze zu bekommen, die key = value erfuellen.
328          *   @param key  Datenbankfeld der Bedingung.
329          *   @param value  Wert die der key anehmen muss.
330          *   @param offset  Gibt an ab welchem Datensatz angezeigt werden soll.
331          *   @return EntityList mit den gematchten Entities
332          */
333         public EntityList selectByFieldValue(String aField, String aValue, int offset)
334                 throws StorageObjectException
335         {
336                 return selectByWhereClause(aField + "=" + aValue, offset);
337         }
338
339
340         /**
341          * select-Operator liefert eine EntityListe mit den gematchten Datensätzen zurück.
342          * Also offset wird der erste Datensatz genommen.
343          *
344          * @param wc where-Clause
345          * @return EntityList mit den gematchten Entities
346          * @exception StorageObjectException
347          */
348         public EntityList selectByWhereClause(String where)
349                 throws StorageObjectException
350         {
351                 return selectByWhereClause(where, 0);
352         }
353
354
355         /**
356          * select-Operator liefert eine EntityListe mit den gematchten Datensätzen zurück.
357          * Als maximale Anzahl wird das Limit auf der Konfiguration genommen.
358          *
359          * @param wc where-Clause
360          * @param offset ab welchem Datensatz.
361          * @return EntityList mit den gematchten Entities
362          * @exception StorageObjectException
363          */
364         public EntityList selectByWhereClause(String whereClause, int offset)
365                 throws StorageObjectException
366         {
367                 return selectByWhereClause(whereClause, null, offset);
368         }
369
370
371         /**
372          * select-Operator liefert eine EntityListe mit den gematchten Datensätzen zurück.
373          * Also offset wird der erste Datensatz genommen.
374          * Als maximale Anzahl wird das Limit auf der Konfiguration genommen.
375          *
376          * @param wc where-Clause
377          * @param ob orderBy-Clause
378          * @return EntityList mit den gematchten Entities
379          * @exception StorageObjectException
380          */
381
382         public EntityList selectByWhereClause(String where, String order)
383                 throws StorageObjectException {
384                 return selectByWhereClause(where, order, 0);
385         }
386
387
388         /**
389          * select-Operator liefert eine EntityListe mit den gematchten Datensätzen zurück.
390          * Als maximale Anzahl wird das Limit auf der Konfiguration genommen.
391          *
392          * @param wc where-Clause
393          * @param ob orderBy-Clause
394          * @param offset ab welchem Datensatz
395          * @return EntityList mit den gematchten Entities
396          * @exception StorageObjectException
397          */
398
399         public EntityList selectByWhereClause(String whereClause, String orderBy, int offset)
400                 throws StorageObjectException {
401                 return selectByWhereClause(whereClause, orderBy, offset, defaultLimit);
402         }
403
404
405         /**
406          * select-Operator liefert eine EntityListe mit den gematchten Datensätzen zurück.
407          * @param wc where-Clause
408          * @param ob orderBy-Clause
409          * @param offset ab welchem Datensatz
410          * @param limit wieviele Datensätze
411          * @return EntityList mit den gematchten Entities
412          * @exception StorageObjectException
413          */
414
415         public EntityList selectByWhereClause(String wc, String ob, int offset, int limit)
416                 throws StorageObjectException   {
417
418                 // local
419                 EntityList    theReturnList=null;
420                 Connection    con=null;
421                 Statement     stmt=null;
422                 ResultSet     rs;
423                 int           offsetCount = 0, count=0;
424
425                 // build sql-statement
426
427                 /** @todo count sql string should only be assembled if we really count
428                  *  see below at the end of method //rk */
429
430                 if (wc != null && wc.length() == 0) {
431                         wc = null;
432                 }
433                 StringBuffer countSql = new StringBuffer("select count(*) from ").append(theTable);
434                 StringBuffer selectSql = new StringBuffer("select * from ").append(theTable);
435                 if (wc != null) {
436                         selectSql.append(" where ").append(wc);
437                         countSql.append(" where ").append(wc);
438                 }
439                 if (ob != null && !(ob.length() == 0)) {
440                         selectSql.append(" order by ").append(ob);
441                 }
442                 if (theAdaptor.hasLimit()) {
443                         if (limit > -1 && offset > -1) {
444                                 selectSql.append(" limit ");
445                                 if (theAdaptor.reverseLimit()) {
446                                         selectSql.append(limit).append(",").append(offset);
447                                 }
448                                 else {
449                                         selectSql.append(offset).append(",").append(limit);
450                                 }
451                         }
452                 }
453
454                 /** @todo OS: make StoreIdentifier and ask Ostore*/
455
456                 // execute sql
457                 try {
458                         con = getPooledCon();
459                         stmt = con.createStatement();
460
461                         // selecting...
462                         rs = executeSql(stmt, selectSql.toString());
463                         if (rs != null) {
464                                 if (!evaluatedMetaData) evalMetaData(rs.getMetaData());
465
466                                 theReturnList = new EntityList();
467                                 Entity theResultEntity;
468                                 while (rs.next()) {
469                                         theResultEntity = makeEntityFromResultSet(rs);
470                                         theReturnList.add(theResultEntity);
471                                         offsetCount++;
472                                 }
473                                 rs.close();
474                         }
475
476                         // making entitylist infos
477                         if (!(theAdaptor.hasLimit())) count = offsetCount;
478
479                         if (theReturnList != null) {
480                                 // now we decide if we have to know an overall count...
481                                 count=offsetCount;
482                                 if (limit > -1 && offset > -1) {
483                                         if (offsetCount==limit) {
484                                                 /** @todo counting should be deffered to entitylist
485                                                  *  getSize() should be used */
486                                                 rs = executeSql(stmt, countSql.toString());
487                                                 if (rs != null) {
488                                                         if ( rs.next() ) count = rs.getInt(1);
489                                                         rs.close();
490                                                 }
491                                                 else theLog.printError("Could not count: " + countSql);
492                                         }
493                                 }
494                                 theReturnList.setCount(count);
495                                 theReturnList.setOffset(offset);
496                                 theReturnList.setWhere(wc);
497                                 theReturnList.setOrder(ob);
498                                 if (offset >= limit) {
499                                         theReturnList.setPrevBatch(offset - limit);
500                                 }
501                                 if (offset + offsetCount < count) {
502                                         theReturnList.setNextBatch(offset + limit);
503                                 }
504                         }
505                 }
506                 catch (SQLException sqe) {
507                         throwSQLException(sqe, "selectByWhereClause");
508                 }
509                 finally {
510                         freeConnection(con, stmt);
511                 }
512
513                 return  theReturnList;
514         }
515
516
517         /**
518          *  Bastelt aus einer Zeile der Datenbank ein EntityObjekt.
519          *
520          *  @param rs Das ResultSetObjekt.
521          *  @return Entity Die Entity.
522          */
523         private Entity makeEntityFromResultSet (ResultSet rs)
524                 throws StorageObjectException
525         {
526                 /** @todo OS: get Pkey from ResultSet and consult ObjectStore */
527                 HashMap theResultHash = new HashMap();
528                 String theResult = null;
529                 int theType;
530                 Entity returnEntity = null;
531                 try {
532                         int size = metadataFields.size();
533                         for (int i = 0; i < size; i++) {
534                                 // alle durchlaufen bis nix mehr da
535                                 theType = metadataTypes[i];
536                                 if (theType == java.sql.Types.LONGVARBINARY) {
537                                         InputStream us = rs.getAsciiStream(i + 1);
538                                         if (us != null) {
539                                                 InputStreamReader is = new InputStreamReader(us);
540                                                 char[] data = new char[32768];
541                                                 StringBuffer theResultString = new StringBuffer();
542                                                 int len;
543                                                 while ((len = is.read(data)) > 0) {
544                                                         theResultString.append(data, 0, len);
545                                                 }
546                                                 is.close();
547                                                 theResult = theResultString.toString();
548                                         }
549                                         else {
550                                                 theResult = null;
551                                         }
552                                 }
553                                 else {
554                                         theResult = getValueAsString(rs, (i + 1), theType);
555                                 }
556                                 if (theResult != null) {
557                                         theResultHash.put(metadataFields.get(i), theResult);
558                                 }
559                         }
560
561                         /** @todo why fetching all data (above) and then consulting the
562                          *  cache? we should fetch pkeyname first // rk  */
563                         if (cache != null && theResultHash.containsKey(thePKeyName) &&
564                                         (cache.containsKey((String)theResultHash.get(thePKeyName)) > -1)) {
565                                 returnEntity = (Entity)cache.get((String)theResultHash.get(thePKeyName));
566                         } else {
567                                 if (theEntityClass != null) {
568                                         returnEntity = (Entity)theEntityClass.newInstance();
569                                         returnEntity.setValues(theResultHash);
570                                         returnEntity.setStorage(myselfDatabase);
571                                         if (cache != null) {
572                                                 //theLog.printDebugInfo("CACHE: ( in) " + returnEntity.getId() + " :"+theTable);
573                                                 /** @todo put element into ObjectStore */
574                                                 cache.put(returnEntity.getId(), returnEntity);
575                                         }
576                                 } else {
577                                         throwStorageObjectException("Internal Error: theEntityClass not set!");
578                                 }
579                         }
580                 } catch (IllegalAccessException e) {
581                         throwStorageObjectException("Kein Zugriff! -- " + e.toString());
582                 } catch (IOException e) {
583                         throwStorageObjectException("IOException! -- " + e.toString());
584                 } catch (InstantiationException e) {
585                         throwStorageObjectException("Keine Instantiiierung! -- " + e.toString());
586                 } catch (SQLException sqe) {
587                         throwSQLException(sqe, "makeEntityFromResultSet");
588                         return  null;
589                 }
590                 return  returnEntity;
591         }
592
593         /**
594          * insert-Operator: fügt eine Entity in die Tabelle ein. Eine Spalte WEBDB_CREATE
595          * wird automatisch mit dem aktuellen Datum gefuellt.
596          *
597          * @param theEntity
598          * @return der Wert des Primary-keys der eingefügten Entity
599          */
600         public String insert (Entity theEntity) throws StorageObjectException {
601                 String returnId = null;
602                 Connection con = null;
603                 PreparedStatement pstmt = null;
604                 //cache
605                 invalidatePopupCache();
606                 /** @todo OS: if Entity is StorableObject, invalidate in ObjectStore
607                  *  careful: Entity has no id yet! */
608                 try {
609                         ArrayList streamedInput = theEntity.streamedInput();
610                         StringBuffer f = new StringBuffer();
611                         StringBuffer v = new StringBuffer();
612                         String aField, aValue;
613                         boolean firstField = true;
614                         // make sql-string
615                         for (int i = 0; i < getFields().size(); i++) {
616                                 aField = (String)getFields().get(i);
617                                 if (!aField.equals(thePKeyName)) {
618                                         aValue = null;
619                                         // sonderfaelle
620                                         if (aField.equals("webdb_create")) {
621                                                 aValue = "NOW()";
622                                         }
623                                         else {
624                                                 if (streamedInput != null && streamedInput.contains(aField)) {
625                                                         aValue = "?";
626                                                 }
627                                                 else {
628                                                         if (theEntity.hasValueForField(aField)) {
629                                                                 aValue = "'" + StringUtil.quote((String)theEntity.getValue(aField))
630                                                                                 + "'";
631                                                         }
632                                                 }
633                                         }
634                                         // wenn Wert gegeben, dann einbauen
635                                         if (aValue != null) {
636                                                 if (firstField == false) {
637                                                         f.append(",");
638                                                         v.append(",");
639                                                 }
640                                                 else {
641                                                         firstField = false;
642                                                 }
643                                                 f.append(aField);
644                                                 v.append(aValue);
645                                         }
646                                 }
647                         }         // end for
648                         // insert into db
649                         StringBuffer sqlBuf = new StringBuffer("insert into ").append(theTable).append("(").append(f).append(") values (").append(v).append(")");
650                         String sql = sqlBuf.toString();
651                         theLog.printInfo("INSERT: " + sql);
652                         con = getPooledCon();
653                         con.setAutoCommit(false);
654                         pstmt = con.prepareStatement(sql);
655                         if (streamedInput != null) {
656                                 for (int i = 0; i < streamedInput.size(); i++) {
657                                         String inputString = (String)theEntity.getValue((String)streamedInput.get(i));
658                                         pstmt.setBytes(i + 1, inputString.getBytes());
659                                 }
660                         }
661                         int ret = pstmt.executeUpdate();
662                         if(ret == 0){
663                                 //insert failed
664                                 return null;
665                         }
666                         pstmt = con.prepareStatement(theAdaptor.getLastInsertSQL((Database)myselfDatabase));
667                         ResultSet rs = pstmt.executeQuery();
668                         rs.next();
669                         returnId = rs.getString(1);
670                         theEntity.setId(returnId);
671                 } catch (SQLException sqe) {
672                         throwSQLException(sqe, "insert");
673                 } finally {
674                         try {
675                                 con.setAutoCommit(true);
676                         } catch (Exception e) {
677                                 ;
678                         }
679                         freeConnection(con, pstmt);
680                 }
681                 return  returnId;
682         }
683
684         /**
685          * update-Operator: aktualisiert eine Entity. Eine Spalte WEBDB_LASTCHANGE
686          * wird automatisch mit dem aktuellen Datum gefuellt.
687          *
688          * @param theEntity
689          */
690         public void update (Entity theEntity) throws StorageObjectException {
691                 Connection con = null;
692                 /** @todo this is stupid: why do we prepare statement, when we
693                  *  throw it away afterwards. should be regular statement
694                  *  update/insert could better be one routine called save()
695                  *  that chooses to either insert or update depending if we
696                  *  have a primary key in the entity. i don't know if we
697                  *  still need the streamed input fields. // rk  */
698                 /** @todo extension: check if Entity did change, otherwise we don't need */
699                 /** @todo OS: invalidate in Ostore if Entity is StorableObject */
700                 PreparedStatement pstmt = null;
701                 ArrayList streamedInput = theEntity.streamedInput();
702                 String id = theEntity.getId();
703                 String aField;
704                 StringBuffer fv = new StringBuffer();
705                 boolean firstField = true;
706                 //cache
707                 invalidatePopupCache();
708                 // build sql statement
709                 for (int i = 0; i < getFields().size(); i++) {
710                         aField = (String)metadataFields.get(i);
711                         // only normal cases
712                         if (!(aField.equals(thePKeyName) || aField.equals("webdb_create") ||
713                                         aField.equals("webdb_lastchange") || (streamedInput != null && streamedInput.contains(aField)))) {
714                                 if (theEntity.hasValueForField(aField)) {
715                                         if (firstField == false) {
716                                                 fv.append(", ");
717                                         }
718                                         else {
719                                                 firstField = false;
720                                         }
721                                         fv.append(aField).append("='").append(StringUtil.quote((String)theEntity.getValue(aField))).append("'");
722                                 }
723                         }
724                 }
725                 StringBuffer sql = new StringBuffer("update ").append(theTable).append(" set ").append(fv);
726                 // exceptions
727                 if (metadataFields.contains("webdb_lastchange")) {
728                         sql.append(",webdb_lastchange=NOW()");
729                 }
730                 if (streamedInput != null) {
731                         for (int i = 0; i < streamedInput.size(); i++) {
732                                 sql.append(",").append(streamedInput.get(i)).append("=?");
733                         }
734                 }
735                 sql.append(" where id=").append(id);
736                 theLog.printInfo("UPDATE: " + sql);
737                 // execute sql
738                 try {
739                         con = getPooledCon();
740                         con.setAutoCommit(false);
741                         pstmt = con.prepareStatement(sql.toString());
742                         if (streamedInput != null) {
743                                 for (int i = 0; i < streamedInput.size(); i++) {
744                                         String inputString = theEntity.getValue((String)streamedInput.get(i));
745                                         pstmt.setBytes(i + 1, inputString.getBytes());
746                                 }
747                         }
748                         pstmt.executeUpdate();
749                 } catch (SQLException sqe) {
750                         throwSQLException(sqe, "update");
751                 } finally {
752                         try {
753                                 con.setAutoCommit(true);
754                         } catch (Exception e) {
755                                 ;
756                         }
757                         freeConnection(con, pstmt);
758                 }
759         }
760
761         /*
762          *   delete-Operator
763          *   @param id des zu loeschenden Datensatzes
764          *   @return boolean liefert true zurueck, wenn loeschen erfolgreich war.
765          */
766         public boolean delete (String id) throws StorageObjectException {
767                 Statement stmt = null;
768                 Connection con = null;
769                 String sql;
770                 int res = 0;
771                 // loeschen des caches
772                 invalidatePopupCache();
773                 /** @todo OS: invalidate if StorableObject */
774                 /** @todo could be prepared Statement */
775                 sql = "delete from " + theTable + " where " + thePKeyName + "='" + id +
776                                 "'";
777                 theLog.printInfo("DELETE " + sql);
778                 try {
779                         con = getPooledCon();
780                         stmt = con.createStatement();
781                         res = stmt.executeUpdate(sql);
782                 } catch (SQLException sqe) {
783                         throwSQLException(sqe, "delete");
784                 } finally {
785                         freeConnection(con, stmt);
786                 }
787                 /** @todo should take place before we delete */
788                 if (cache != null) {
789                         theLog.printInfo("CACHE: deleted " + id);
790                         cache.remove(id);
791                 }
792                 return  (res > 0) ? true : false;
793         }
794
795         /* noch nicht implementiert.
796          * @return immer false
797          */
798         public boolean delete (EntityList theEntityList) {
799                 invalidatePopupCache();
800                 return  false;
801         }
802
803         /**
804          * Diese Methode sollte ueberschrieben werden, wenn fuer die abgeleitete Database-Klasse
805          * eine SimpleList mit Standard-Popupdaten erzeugt werden koennen soll.
806          * @return null
807          */
808         public SimpleList getPopupData () throws StorageObjectException {
809                 return  null;
810         }
811
812         /**
813          *  Holt Daten fuer Popups.
814          *  @param name  Name des Feldes.
815          *  @param hasNullValue  Wenn true wird eine leerer  Eintrag fuer die Popups erzeugt.
816          *  @return SimpleList Gibt freemarker.template.SimpleList zurueck.
817          */
818         public SimpleList getPopupData (String name, boolean hasNullValue)
819                 throws StorageObjectException {
820                 return  getPopupData(name, hasNullValue, null);
821         }
822
823         /**
824          *  Holt Daten fuer Popups.
825          *  @param name  Name des Feldes.
826          *  @param hasNullValue  Wenn true wird eine leerer  Eintrag fuer die Popups erzeugt.
827          *  @param where  Schraenkt die Selektion der Datensaetze ein.
828          *  @return SimpleList Gibt freemarker.template.SimpleList zurueck.
829          */
830         public SimpleList getPopupData (String name, boolean hasNullValue, String where) throws StorageObjectException {
831          return  getPopupData(name, hasNullValue, where, null);
832         }
833
834         /**
835          *  Holt Daten fuer Popups.
836          *  @param name  Name des Feldes.
837          *  @param hasNullValue  Wenn true wird eine leerer  Eintrag fuer die Popups erzeugt.
838          *  @param where  Schraenkt die Selektion der Datensaetze ein.
839          *  @param order  Gibt ein Feld als Sortierkriterium an.
840          *  @return SimpleList Gibt freemarker.template.SimpleList zurueck.
841          */
842         public SimpleList getPopupData (String name, boolean hasNullValue, String where, String order) throws StorageObjectException {
843                 // caching
844                 if (hasPopupCache && popupCache != null)
845                         return  popupCache;
846                 SimpleList simpleList = null;
847                 Connection con = null;
848                 Statement stmt = null;
849                 // build sql
850                 StringBuffer sql = new StringBuffer("select ").append(thePKeyName)
851                                                                                                                                                                 .append(",").append(name).append(" from ")
852                                                                                                                                                                 .append(theTable);
853                 if (where != null && !(where.length() == 0))
854                         sql.append(" where ").append(where);
855                 sql.append(" order by ");
856                 if (order != null && !(order.length() == 0))
857                         sql.append(order);
858                 else
859                         sql.append(name);
860                 // execute sql
861                 try {
862                         con = getPooledCon();
863                 } catch (Exception e) {
864                         throw new StorageObjectException(e.toString());
865                 }
866                 try {
867                         stmt = con.createStatement();
868                         ResultSet rs = executeSql(stmt, sql.toString());
869
870                         if (rs != null) {
871                                 if (!evaluatedMetaData) get_meta_data();
872                                 simpleList = new SimpleList();
873                                 // if popup has null-selector
874                                 if (hasNullValue) simpleList.add(POPUP_EMTYLINE);
875
876                                 SimpleHash popupDict;
877                                 while (rs.next()) {
878                                         popupDict = new SimpleHash();
879                                         popupDict.put("key", getValueAsString(rs, 1, thePKeyType));
880                                         popupDict.put("value", rs.getString(2));
881                                         simpleList.add(popupDict);
882                                 }
883                                 rs.close();
884                         }
885                 } catch (Exception e) {
886                         theLog.printError("getPopupData: "+e.toString());
887                         throw new StorageObjectException(e.toString());
888                 } finally {
889                         freeConnection(con, stmt);
890                 }
891
892                 if (hasPopupCache) popupCache = simpleList;
893                 return  simpleList;
894         }
895
896         /**
897          * Liefert alle Daten der Tabelle als SimpleHash zurueck. Dies wird verwandt,
898          * wenn in den Templates ein Lookup-Table benoetigt wird. Sollte nur bei kleinen
899          * Tabellen Verwendung finden.
900          * @return SimpleHash mit den Tabellezeilen.
901          */
902         public SimpleHash getHashData () {
903                 /** @todo dangerous! this should have a flag to be enabled, otherwise
904                  *  very big Hashes could be returned */
905                 if (hashCache == null) {
906                         try {
907                                 hashCache = HTMLTemplateProcessor.makeSimpleHash(selectByWhereClause("",
908                                                 -1));
909                         } catch (StorageObjectException e) {
910                                 theLog.printDebugInfo(e.toString());
911                         }
912                 }
913                 return  hashCache;
914         }
915
916         /* invalidates the popupCache
917          */
918         protected void invalidatePopupCache () {
919                 /** @todo  invalidates toooo much */
920                 popupCache = null;
921                 hashCache = null;
922         }
923
924         /**
925          * Diese Methode fuehrt den Sqlstring <i>sql</i> aus und timed im Logfile.
926          * @param stmt Statemnt
927          * @param sql Sql-String
928          * @return ResultSet
929          * @exception StorageObjectException
930          */
931         public ResultSet executeSql (Statement stmt, String sql)
932                 throws StorageObjectException, SQLException
933         {
934                 long startTime = System.currentTimeMillis();
935                 ResultSet rs;
936                 try {
937                         rs = stmt.executeQuery(sql);
938                         theLog.printInfo((System.currentTimeMillis() - startTime) + "ms. for: "
939                                 + sql);
940                 }
941                 catch (SQLException e)
942                 {
943                         theLog.printDebugInfo("Failed: " + (System.currentTimeMillis()
944                                                                                                                 - startTime) + "ms. for: "+ sql);
945                         throw e;
946                 }
947
948                 return  rs;
949         }
950
951         /**
952          * Fuehrt Statement stmt aus und liefert Resultset zurueck. Das SQL-Statment wird
953          * getimed und geloggt.
954          * @param stmt PreparedStatement mit der SQL-Anweisung
955          * @return Liefert ResultSet des Statements zurueck.
956          * @exception StorageObjectException, SQLException
957          */
958         public ResultSet executeSql (PreparedStatement stmt)
959                 throws StorageObjectException, SQLException {
960
961                 long startTime = (new java.util.Date()).getTime();
962                 ResultSet rs = stmt.executeQuery();
963                 theLog.printInfo((new java.util.Date().getTime() - startTime) + "ms.");
964                 return  rs;
965         }
966
967                 /**
968          * returns the number of rows in the table
969          */
970         public int getSize(String where)
971                 throws SQLException,StorageObjectException
972         {
973                 long  startTime = System.currentTimeMillis();
974                 String sql = "SELECT count(*) FROM "+ theTable + " where " + where;
975                 Connection con = null;
976                 Statement stmt = null;
977                 int result = 0;
978
979                 try {
980                         con = getPooledCon();
981                         stmt = con.createStatement();
982                         ResultSet rs = executeSql(stmt,sql);
983                         while(rs.next()){
984                                 result = rs.getInt(1);
985                         }
986                 } catch (SQLException e) {
987                         theLog.printError(e.toString());
988                 } finally {
989                         freeConnection(con,stmt);
990                 }
991                 //theLog.printInfo(theTable + " has "+ result +" rows where " + where);
992                 theLog.printInfo((System.currentTimeMillis() - startTime) + "ms. for: "
993                                                                                 + sql);
994                 return result;
995         }
996
997         public int executeUpdate(Statement stmt, String sql)
998                 throws StorageObjectException, SQLException
999         {
1000                 int rs;
1001                 long  startTime = (new java.util.Date()).getTime();
1002                 try
1003                 {
1004                         rs = stmt.executeUpdate(sql);
1005                         theLog.printInfo((new java.util.Date().getTime() - startTime) + "ms. for: "
1006                                                                                                 + sql);
1007                 }
1008                 catch (SQLException e)
1009                 {
1010                         theLog.printDebugInfo("Failed: " + (new java.util.Date().getTime()
1011                                                                                                                 - startTime) + "ms. for: "+ sql);
1012                         throw e;
1013                 }
1014                 return rs;
1015         }
1016
1017         public int executeUpdate(String sql)
1018                 throws StorageObjectException, SQLException
1019         {
1020                 int result=-1;
1021                 long  startTime = (new java.util.Date()).getTime();
1022                 Connection con=null;PreparedStatement pstmt=null;
1023                 try {
1024                         con=getPooledCon();
1025                         pstmt = con.prepareStatement(sql);
1026                         result = pstmt.executeUpdate();
1027                 }
1028                 catch (Exception e) {
1029                         theLog.printDebugInfo("settimage :: setImage gescheitert: "+e.toString());
1030                         throw new StorageObjectException("executeUpdate failed: "+e.toString());
1031                 }
1032                 finally { freeConnection(con,pstmt); }
1033                 theLog.printInfo((new java.util.Date().getTime() - startTime) + "ms. for: "
1034                                                                                 + sql);
1035                 return result;
1036         }
1037
1038         /**
1039          * Wertet ResultSetMetaData aus und setzt interne Daten entsprechend
1040          * @param md ResultSetMetaData
1041          * @exception StorageObjectException
1042          */
1043         private void evalMetaData (ResultSetMetaData md)
1044                 throws StorageObjectException {
1045
1046                 this.evaluatedMetaData = true;
1047                 this.metadataFields = new ArrayList();
1048                 this.metadataLabels = new ArrayList();
1049                 this.metadataNotNullFields = new ArrayList();
1050                 try {
1051                         int numFields = md.getColumnCount();
1052                         this.metadataTypes = new int[numFields];
1053                         String aField;
1054                         int aType;
1055                         for (int i = 1; i <= numFields; i++) {
1056                                 aField = md.getColumnName(i);
1057                                 metadataFields.add(aField);
1058                                 metadataLabels.add(md.getColumnLabel(i));
1059                                 aType = md.getColumnType(i);
1060                                 metadataTypes[i - 1] = aType;
1061                                 if (aField.equals(thePKeyName)) {
1062                                         thePKeyType = aType;
1063                                 }
1064                                 if (md.isNullable(i) == md.columnNullable) {
1065                                         metadataNotNullFields.add(aField);
1066                                 }
1067                         }
1068                 } catch (SQLException e) {
1069                         throwSQLException(e, "evalMetaData");
1070                 }
1071         }
1072
1073         /**
1074          *  Wertet die Metadaten eines Resultsets fuer eine Tabelle aus,
1075          *  um die alle Columns und Typen einer Tabelle zu ermitteln.
1076          */
1077         private void get_meta_data () throws StorageObjectException {
1078                 Connection con = null;
1079                 PreparedStatement pstmt = null;
1080                 String sql = "select * from " + theTable + " where 0=1";
1081                 try {
1082                         con = getPooledCon();
1083                         pstmt = con.prepareStatement(sql);
1084                         theLog.printInfo("METADATA: " + sql);
1085                         ResultSet rs = pstmt.executeQuery();
1086                         evalMetaData(rs.getMetaData());
1087                         rs.close();
1088                 } catch (SQLException e) {
1089                         throwSQLException(e, "get_meta_data");
1090                 } finally {
1091                         freeConnection(con, pstmt);
1092                 }
1093         }
1094
1095
1096         public Connection getPooledCon() throws StorageObjectException {
1097                 /* @todo , doublecheck but I'm pretty sure that this is unnecessary. -mh
1098                         try{
1099                         Class.forName("com.codestudio.sql.PoolMan").newInstance();
1100                 } catch (Exception e){
1101                         throw new StorageObjectException("Could not find the PoolMan Driver"
1102                                                                                                                                                                 +e.toString());
1103                 }*/
1104                 Connection con = null;
1105                 try{
1106                         con = SQLManager.getInstance().requestConnection();
1107                 } catch(SQLException e){
1108                         theLog.printError("could not connect to the database "+e.toString());
1109                         System.err.println("could not connect to the database "+e.toString());
1110                         throw new StorageObjectException("Could not connect to the database"+
1111                                                                                                                                                                 e.toString());
1112                 }
1113                 return con;
1114         }
1115
1116         public void freeConnection (Connection con, Statement stmt)
1117                 throws StorageObjectException {
1118                 SQLManager.getInstance().closeStatement(stmt);
1119                 SQLManager.getInstance().returnConnection(con);
1120         }
1121
1122         /**
1123          * Wertet SQLException aus und wirft dannach eine StorageObjectException
1124          * @param sqe SQLException
1125          * @param wo Funktonsname, in der die SQLException geworfen wurde
1126          * @exception StorageObjectException
1127          */
1128         protected void throwSQLException (SQLException sqe, String wo)
1129                 throws StorageObjectException {
1130                 String state = "";
1131                 String message = "";
1132                 int vendor = 0;
1133                 if (sqe != null) {
1134                         state = sqe.getSQLState();
1135                         message = sqe.getMessage();
1136                         vendor = sqe.getErrorCode();
1137                 }
1138                 theLog.printError(state + ": " + vendor + " : " + message + " Funktion: "
1139                                 + wo);
1140                 throw  new StorageObjectException((sqe == null) ? "undefined sql exception" :
1141                                 sqe.toString());
1142         }
1143
1144         protected void _throwStorageObjectException (Exception e, String wo)
1145                 throws StorageObjectException {
1146                 if (e != null) {
1147                                 theLog.printError(e.toString()+ wo);
1148                                 throw  new StorageObjectException(wo + e.toString());
1149                 } else {
1150                                 theLog.printError(wo);
1151                                 throw  new StorageObjectException(wo);
1152                 }
1153
1154         }
1155
1156         /**
1157          * Loggt Fehlermeldung mit dem Parameter Message und wirft dannach
1158          * eine StorageObjectException
1159          * @param message Nachricht mit dem Fehler
1160          * @exception StorageObjectException
1161          */
1162         void throwStorageObjectException (String message)
1163                 throws StorageObjectException {
1164                 _throwStorageObjectException(null, message);
1165         }
1166
1167 }
1168
1169
1170