+ }
+ if (streamedInput != null) {
+ for (int i = 0; i < streamedInput.size(); i++) {
+ sql.append(",").append(streamedInput.get(i)).append("=?");
+ }
+ }
+ sql.append(" where id=").append(id);
+ theLog.printInfo("UPDATE: " + sql);
+ // execute sql
+ try {
+ con = getPooledCon();
+ con.setAutoCommit(false);
+ pstmt = con.prepareStatement(sql.toString());
+ if (streamedInput != null) {
+ for (int i = 0; i < streamedInput.size(); i++) {
+ String inputString = theEntity.getValue((String)streamedInput.get(i));
+ pstmt.setBytes(i + 1, inputString.getBytes());
+ }
+ }
+ pstmt.executeUpdate();
+ } catch (SQLException sqe) {
+ throwSQLException(sqe, "update");
+ } finally {
+ try {
+ con.setAutoCommit(true);
+ } catch (Exception e) {
+ ;
+ }
+ freeConnection(con, pstmt);
+ }
+ }
+
+ /*
+ * delete-Operator
+ * @param id des zu loeschenden Datensatzes
+ * @return boolean liefert true zurueck, wenn loeschen erfolgreich war.
+ */
+ public boolean delete (String id) throws StorageObjectException {
+
+ invalidatePopupCache();
+ // ostore send notification
+ if ( StoreUtil.implementsStorableObject(theEntityClass) ) {
+ String uniqueId = id;
+ if ( theEntityClass.equals(StorableObjectEntity.class) )
+ uniqueId+="@"+theTable;
+ theLog.printInfo("CACHE: (del) " + id);
+ StoreIdentifier search_sid =
+ new StoreIdentifier(theEntityClass, StoreContainerType.STOC_TYPE_ENTITY, uniqueId);
+ o_store.invalidate(search_sid);
+ }
+
+ /** @todo could be prepared Statement */
+ Statement stmt = null; Connection con = null;
+ int res = 0;
+ String sql="delete from "+theTable+" where "+thePKeyName+"='"+id+"'";
+ theLog.printInfo("DELETE " + sql);
+ try {
+ con = getPooledCon(); stmt = con.createStatement();
+ res = stmt.executeUpdate(sql);
+ }
+ catch (SQLException sqe) { throwSQLException(sqe, "delete"); }
+ finally { freeConnection(con, stmt); }
+
+ return (res > 0) ? true : false;
+ }
+
+ /* noch nicht implementiert.
+ * @return immer false
+ */
+ public boolean delete (EntityList theEntityList) {
+ invalidatePopupCache();
+ return false;
+ }
+
+ /**
+ * Diese Methode sollte ueberschrieben werden, wenn fuer die abgeleitete Database-Klasse
+ * eine SimpleList mit Standard-Popupdaten erzeugt werden koennen soll.
+ * @return null
+ */
+ public SimpleList getPopupData () throws StorageObjectException {
+ return null;
+ }
+
+ /**
+ * Holt Daten fuer Popups.
+ * @param name Name des Feldes.
+ * @param hasNullValue Wenn true wird eine leerer Eintrag fuer die Popups erzeugt.
+ * @return SimpleList Gibt freemarker.template.SimpleList zurueck.
+ */
+ public SimpleList getPopupData (String name, boolean hasNullValue)
+ throws StorageObjectException {
+ return getPopupData(name, hasNullValue, null);
+ }
+
+ /**
+ * Holt Daten fuer Popups.
+ * @param name Name des Feldes.
+ * @param hasNullValue Wenn true wird eine leerer Eintrag fuer die Popups erzeugt.
+ * @param where Schraenkt die Selektion der Datensaetze ein.
+ * @return SimpleList Gibt freemarker.template.SimpleList zurueck.
+ */
+ public SimpleList getPopupData (String name, boolean hasNullValue, String where) throws StorageObjectException {
+ return getPopupData(name, hasNullValue, where, null);
+ }
+
+ /**
+ * Holt Daten fuer Popups.
+ * @param name Name des Feldes.
+ * @param hasNullValue Wenn true wird eine leerer Eintrag fuer die Popups erzeugt.
+ * @param where Schraenkt die Selektion der Datensaetze ein.
+ * @param order Gibt ein Feld als Sortierkriterium an.
+ * @return SimpleList Gibt freemarker.template.SimpleList zurueck.
+ */
+ public SimpleList getPopupData (String name, boolean hasNullValue, String where, String order) throws StorageObjectException {
+ // caching
+ if (hasPopupCache && popupCache != null)
+ return popupCache;
+ SimpleList simpleList = null;
+ Connection con = null;
+ Statement stmt = null;
+ // build sql
+ StringBuffer sql = new StringBuffer("select ").append(thePKeyName)
+ .append(",").append(name).append(" from ")
+ .append(theTable);
+ if (where != null && !(where.length() == 0))
+ sql.append(" where ").append(where);
+ sql.append(" order by ");
+ if (order != null && !(order.length() == 0))
+ sql.append(order);
+ else
+ sql.append(name);
+ // execute sql
+ try {
+ con = getPooledCon();
+ } catch (Exception e) {
+ throw new StorageObjectException(e.toString());
+ }
+ try {
+ stmt = con.createStatement();
+ ResultSet rs = executeSql(stmt, sql.toString());
+
+ if (rs != null) {
+ if (!evaluatedMetaData) get_meta_data();
+ simpleList = new SimpleList();
+ // if popup has null-selector
+ if (hasNullValue) simpleList.add(POPUP_EMTYLINE);
+
+ SimpleHash popupDict;
+ while (rs.next()) {
+ popupDict = new SimpleHash();
+ popupDict.put("key", getValueAsString(rs, 1, thePKeyType));
+ popupDict.put("value", rs.getString(2));
+ simpleList.add(popupDict);
+ }
+ rs.close();
+ }
+ } catch (Exception e) {
+ theLog.printError("getPopupData: "+e.toString());
+ throw new StorageObjectException(e.toString());
+ } finally {
+ freeConnection(con, stmt);
+ }
+
+ if (hasPopupCache) popupCache = simpleList;
+ return simpleList;
+ }
+
+ /**
+ * Liefert alle Daten der Tabelle als SimpleHash zurueck. Dies wird verwandt,
+ * wenn in den Templates ein Lookup-Table benoetigt wird. Sollte nur bei kleinen
+ * Tabellen Verwendung finden.
+ * @return SimpleHash mit den Tabellezeilen.
+ */
+ public SimpleHash getHashData () {
+ /** @todo dangerous! this should have a flag to be enabled, otherwise
+ * very big Hashes could be returned */
+ if (hashCache == null) {
+ try {
+ hashCache = HTMLTemplateProcessor.makeSimpleHash(selectByWhereClause("",
+ -1));
+ } catch (StorageObjectException e) {
+ theLog.printDebugInfo(e.toString());
+ }
+ }
+ return hashCache;
+ }
+
+ /* invalidates the popupCache
+ */
+ protected void invalidatePopupCache () {
+ /** @todo invalidates toooo much */
+ popupCache = null;
+ hashCache = null;
+ }
+
+ /**
+ * Diese Methode fuehrt den Sqlstring <i>sql</i> aus und timed im Logfile.
+ * @param stmt Statemnt
+ * @param sql Sql-String
+ * @return ResultSet
+ * @exception StorageObjectException
+ */
+ public ResultSet executeSql (Statement stmt, String sql)
+ throws StorageObjectException, SQLException
+ {
+ long startTime = System.currentTimeMillis();
+ ResultSet rs;
+ try {
+ rs = stmt.executeQuery(sql);
+ theLog.printInfo((System.currentTimeMillis() - startTime) + "ms. for: "
+ + sql);
+ }
+ catch (SQLException e)
+ {
+ theLog.printDebugInfo("Failed: " + (System.currentTimeMillis()
+ - startTime) + "ms. for: "+ sql);
+ throw e;
+ }
+
+ return rs;
+ }
+
+ /**
+ * Fuehrt Statement stmt aus und liefert Resultset zurueck. Das SQL-Statment wird
+ * getimed und geloggt.
+ * @param stmt PreparedStatement mit der SQL-Anweisung
+ * @return Liefert ResultSet des Statements zurueck.
+ * @exception StorageObjectException, SQLException
+ */
+ public ResultSet executeSql (PreparedStatement stmt)
+ throws StorageObjectException, SQLException {
+
+ long startTime = (new java.util.Date()).getTime();
+ ResultSet rs = stmt.executeQuery();
+ theLog.printInfo((new java.util.Date().getTime() - startTime) + "ms.");
+ return rs;
+ }
+
+ /**
+ * returns the number of rows in the table
+ */
+ public int getSize(String where)
+ throws SQLException,StorageObjectException
+ {
+ long startTime = System.currentTimeMillis();
+ String sql = "SELECT count(*) FROM "+ theTable + " where " + where;
+ Connection con = null;
+ Statement stmt = null;
+ int result = 0;
+
+ try {
+ con = getPooledCon();
+ stmt = con.createStatement();
+ ResultSet rs = executeSql(stmt,sql);
+ while(rs.next()){
+ result = rs.getInt(1);
+ }
+ } catch (SQLException e) {
+ theLog.printError(e.toString());
+ } finally {
+ freeConnection(con,stmt);
+ }
+ //theLog.printInfo(theTable + " has "+ result +" rows where " + where);
+ theLog.printInfo((System.currentTimeMillis() - startTime) + "ms. for: "
+ + sql);
+ return result;
+ }
+
+ public int executeUpdate(Statement stmt, String sql)
+ throws StorageObjectException, SQLException
+ {
+ int rs;
+ long startTime = (new java.util.Date()).getTime();
+ try
+ {
+ rs = stmt.executeUpdate(sql);
+ theLog.printInfo((new java.util.Date().getTime() - startTime) + "ms. for: "
+ + sql);
+ }
+ catch (SQLException e)
+ {
+ theLog.printDebugInfo("Failed: " + (new java.util.Date().getTime()
+ - startTime) + "ms. for: "+ sql);
+ throw e;
+ }
+ return rs;
+ }
+
+ public int executeUpdate(String sql)
+ throws StorageObjectException, SQLException
+ {
+ int result=-1;
+ long startTime = (new java.util.Date()).getTime();
+ Connection con=null;PreparedStatement pstmt=null;
+ try {
+ con=getPooledCon();
+ pstmt = con.prepareStatement(sql);
+ result = pstmt.executeUpdate();
+ }
+ catch (Exception e) {
+ theLog.printDebugInfo("settimage :: setImage gescheitert: "+e.toString());
+ throw new StorageObjectException("executeUpdate failed: "+e.toString());
+ }
+ finally { freeConnection(con,pstmt); }
+ theLog.printInfo((new java.util.Date().getTime() - startTime) + "ms. for: "
+ + sql);
+ return result;
+ }
+
+ /**
+ * Wertet ResultSetMetaData aus und setzt interne Daten entsprechend
+ * @param md ResultSetMetaData
+ * @exception StorageObjectException
+ */
+ private void evalMetaData (ResultSetMetaData md)
+ throws StorageObjectException {
+
+ this.evaluatedMetaData = true;
+ this.metadataFields = new ArrayList();
+ this.metadataLabels = new ArrayList();
+ this.metadataNotNullFields = new ArrayList();
+ try {
+ int numFields = md.getColumnCount();
+ this.metadataTypes = new int[numFields];
+ String aField;
+ int aType;
+ for (int i = 1; i <= numFields; i++) {
+ aField = md.getColumnName(i);
+ metadataFields.add(aField);
+ metadataLabels.add(md.getColumnLabel(i));
+ aType = md.getColumnType(i);
+ metadataTypes[i - 1] = aType;
+ if (aField.equals(thePKeyName)) {
+ thePKeyType = aType; thePKeyIndex = i;
+ }
+ if (md.isNullable(i) == md.columnNullable) {
+ metadataNotNullFields.add(aField);
+ }
+ }
+ } catch (SQLException e) {
+ throwSQLException(e, "evalMetaData");
+ }
+ }
+
+ /**
+ * Wertet die Metadaten eines Resultsets fuer eine Tabelle aus,
+ * um die alle Columns und Typen einer Tabelle zu ermitteln.
+ */
+ private void get_meta_data () throws StorageObjectException {
+ Connection con = null;
+ PreparedStatement pstmt = null;
+ String sql = "select * from " + theTable + " where 0=1";
+ try {
+ con = getPooledCon();
+ pstmt = con.prepareStatement(sql);
+ theLog.printInfo("METADATA: " + sql);
+ ResultSet rs = pstmt.executeQuery();
+ evalMetaData(rs.getMetaData());
+ rs.close();
+ } catch (SQLException e) {
+ throwSQLException(e, "get_meta_data");
+ } finally {
+ freeConnection(con, pstmt);
+ }
+ }
+
+
+ public Connection getPooledCon() throws StorageObjectException {
+ /* @todo , doublecheck but I'm pretty sure that this is unnecessary. -mh
+ try{
+ Class.forName("com.codestudio.sql.PoolMan").newInstance();
+ } catch (Exception e){
+ throw new StorageObjectException("Could not find the PoolMan Driver"
+ +e.toString());
+ }*/
+ Connection con = null;
+ try{
+ con = SQLManager.getInstance().requestConnection();
+ } catch(SQLException e){
+ theLog.printError("could not connect to the database "+e.toString());
+ System.err.println("could not connect to the database "+e.toString());
+ throw new StorageObjectException("Could not connect to the database"+
+ e.toString());
+ }
+ return con;
+ }
+
+ public void freeConnection (Connection con, Statement stmt)
+ throws StorageObjectException {
+ SQLManager.getInstance().closeStatement(stmt);
+ SQLManager.getInstance().returnConnection(con);
+ }
+
+ /**
+ * Wertet SQLException aus und wirft dannach eine StorageObjectException
+ * @param sqe SQLException
+ * @param wo Funktonsname, in der die SQLException geworfen wurde
+ * @exception StorageObjectException
+ */
+ protected void throwSQLException (SQLException sqe, String wo)
+ throws StorageObjectException {
+ String state = "";
+ String message = "";
+ int vendor = 0;
+ if (sqe != null) {
+ state = sqe.getSQLState();
+ message = sqe.getMessage();
+ vendor = sqe.getErrorCode();
+ }
+ theLog.printError(state + ": " + vendor + " : " + message + " Funktion: "
+ + wo);
+ throw new StorageObjectException((sqe == null) ? "undefined sql exception" :
+ sqe.toString());
+ }
+
+ protected void _throwStorageObjectException (Exception e, String wo)
+ throws StorageObjectException {
+ if (e != null) {
+ theLog.printError(e.toString()+ wo);
+ throw new StorageObjectException(wo + e.toString());
+ } else {
+ theLog.printError(wo);
+ throw new StorageObjectException(wo);
+ }
+
+ }
+
+ /**
+ * Loggt Fehlermeldung mit dem Parameter Message und wirft dannach
+ * eine StorageObjectException
+ * @param message Nachricht mit dem Fehler
+ * @exception StorageObjectException
+ */
+ void throwStorageObjectException (String message)
+ throws StorageObjectException {
+ _throwStorageObjectException(null, message);
+ }