1 package mircoders.storage;
7 import freemarker.template.*;
14 * <b>Diese Klasse implementiert die Datenbankverbindung zur MetaObjekt-Tabelle
18 public class DatabaseLinksImcs extends Database
19 implements StorageObject {
20 private static DatabaseLinksImcs instance;
23 * put your documentation comment here
25 * @exception StorageObjectException
27 public static DatabaseLinksImcs getInstance () throws StorageObjectException {
28 if (instance == null) {
29 instance = new DatabaseLinksImcs();
30 instance.myselfDatabase = instance;
36 * put your documentation comment here
38 private DatabaseLinksImcs () throws StorageObjectException
41 //this.cache = new HashMap();
42 this.hasTimestamp = false;
43 this.theTable = "links_imcs";
45 this.theEntityClass = Class.forName("mircoders.entity.EntityLinksImcs");
46 } catch (Exception e) {
47 throw new StorageObjectException(e.toString());
51 public String insert (Entity theEntity) throws StorageObjectException {
52 String returnId = "0";
53 Connection con = null;
54 PreparedStatement pstmt = null;
56 invalidatePopupCache();
58 ArrayList streamedInput = theEntity.streamedInput();
59 StringBuffer f = new StringBuffer();
60 StringBuffer v = new StringBuffer();
61 String aField, aValue;
62 boolean firstField = true;
64 for (int i = 0; i < getFields().size(); i++) {
65 aField = (String)getFields().get(i);
66 if (!aField.equals(thePKeyName)) {
69 if (aField.equals("webdb_create")) {
73 if (streamedInput != null && streamedInput.contains(aField)) {
77 if (theEntity.hasValueForField(aField)) {
78 if (aField.equals("to_parent_id")) {
79 aValue = StringUtil.quote((String)theEntity.getValue(aField));
81 aValue = "'" + StringUtil.quote((String)theEntity.getValue(aField)) + "'";
86 // wenn Wert gegeben, dann einbauen
88 if (firstField == false) {
101 StringBuffer sqlBuf = new StringBuffer("insert into ").append(theTable).append("(").append(f).append(") values (").append(v).append(")");
102 String sql = sqlBuf.toString();
103 theLog.printInfo("INSERT: " + sql);
104 con = getPooledCon();
105 con.setAutoCommit(false);
106 pstmt = con.prepareStatement(sql);
107 if (streamedInput != null) {
108 for (int i = 0; i < streamedInput.size(); i++) {
109 String inputString = (String)theEntity.getValue((String)streamedInput.get(i));
110 pstmt.setBytes(i + 1, inputString.getBytes());
114 pstmt = con.prepareStatement(theAdaptor.getLastInsertSQL((Database)myselfDatabase));
115 ResultSet rs = pstmt.executeQuery();
117 returnId = rs.getString(1);
118 theEntity.setId(returnId);
119 } catch (SQLException sqe) {
120 throwSQLException(sqe, "insert");
123 con.setAutoCommit(true);
124 } catch (Exception e) {
127 freeConnection(con, pstmt);
132 public void update (Entity theEntity) throws StorageObjectException {
133 Connection con = null;
134 PreparedStatement pstmt = null;
135 ArrayList streamedInput = theEntity.streamedInput();
136 String id = theEntity.getId();
138 StringBuffer fv = new StringBuffer();
139 boolean firstField = true;
141 invalidatePopupCache();
142 // build sql statement
143 for (int i = 0; i < getFields().size(); i++) {
144 aField = (String)metadataFields.get(i);
146 if (!(aField.equals(thePKeyName) || aField.equals("webdb_create") ||
147 aField.equals("webdb_lastchange") || (streamedInput != null && streamedInput.contains(aField)))) {
148 if (theEntity.hasValueForField(aField)) {
149 if (firstField == false) {
155 if (aField.equals("to_parent_id")) {
156 fv.append(aField).append("=").append(StringUtil.quote(theEntity.getValue(aField)));
158 fv.append(aField).append("='").append(StringUtil.quote((String)theEntity.getValue(aField))).append("'");
163 StringBuffer sql = new StringBuffer("update ").append(theTable).append(" set ").append(fv);
165 if (metadataFields.contains("webdb_lastchange")) {
166 sql.append(",webdb_lastchange=NOW()");
168 if (streamedInput != null) {
169 for (int i = 0; i < streamedInput.size(); i++) {
170 sql.append(",").append(streamedInput.get(i)).append("=?");
173 sql.append(" where id=").append(id);
174 theLog.printInfo("UPDATE: " + sql);
177 con = getPooledCon();
178 con.setAutoCommit(false);
179 pstmt = con.prepareStatement(sql.toString());
180 if (streamedInput != null) {
181 for (int i = 0; i < streamedInput.size(); i++) {
182 String inputString = (String)theEntity.getValue((String)streamedInput.get(i));
183 pstmt.setBytes(i + 1, inputString.getBytes());
186 pstmt.executeUpdate();
187 } catch (SQLException sqe) {
188 throwSQLException(sqe, "update");
191 con.setAutoCommit(true);
192 } catch (Exception e) {
195 freeConnection(con, pstmt);
200 * put your documentation comment here
203 public SimpleHash getHashData () {
204 return getHashData();
207 public SimpleList getPopupData () {
208 return getPopupData();