001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.storage.implementation.database;
011:
012: import org.mmbase.module.core.*;
013: import org.mmbase.storage.StorageException;
014: import org.mmbase.core.CoreField;
015: import org.mmbase.bridge.Field;
016: import org.mmbase.util.logging.Logger;
017: import org.mmbase.util.logging.Logging;
018:
019: /**
020: * A JDBC implementation of a storage manager for relational databases.
021: * @javadoc
022: *
023: * @author Pierre van Rooden
024: * @since MMBase-1.7
025: * @version $Id: RelationalDatabaseStorageManager.java,v 1.12 2008/02/03 17:33:57 nklasens Exp $
026: */
027: public class RelationalDatabaseStorageManager extends
028: DatabaseStorageManager {
029:
030: private static final Logger log = Logging
031: .getLoggerInstance(RelationalDatabaseStorageManager.class);
032:
033: public RelationalDatabaseStorageManager() {
034: }
035:
036: // javadoc is inherited
037: public double getVersion() {
038: return 1.0;
039: }
040:
041: protected boolean tablesInheritFields() {
042: return false;
043: }
044:
045: /**
046: * Adds a node to the passed builder and all its parent builders.
047: * @param node The node to insert. The node already needs to have a (new) number assigned
048: * @param builder the builder to store the node
049: * @throws StorageException if an error occurred during creation
050: */
051: public void create(final MMObjectNode node,
052: final MMObjectBuilder builder) throws StorageException {
053: boolean localTransaction = !inTransaction;
054: if (localTransaction) {
055: beginTransaction();
056: }
057: try {
058: // insert in parent tables (from parents to childs) (especially because foreign keys on object's number may exist)
059: for (MMObjectBuilder b : builder.getAncestors()) {
060: for (CoreField f : b.getFields()) {
061: if (f.getType() == Field.TYPE_BINARY) {
062: // if the value is an inputstream at the moment, convert it to a byte-array, because it must be stored again..
063: node.storeValue(f.getName(),
064: org.mmbase.util.Casting.toByte(node
065: .retrieveValue(f.getName())));
066: }
067: }
068: super .create(node, b);
069: }
070: super .create(node, builder);
071: if (localTransaction)
072: commit();
073: } catch (StorageException se) {
074: if (localTransaction && inTransaction)
075: rollback();
076: throw se;
077: }
078: }
079:
080: /**
081: * Changes a node in the passed builder and all its parent builders
082: * @param node The node to change
083: * @param builder the builder to change the node in
084: * @throws StorageException if an error occurred during change
085: */
086: public void change(MMObjectNode node, MMObjectBuilder builder)
087: throws StorageException {
088: boolean localTransaction = !inTransaction;
089: if (localTransaction) {
090: beginTransaction();
091: }
092: try {
093: do {
094: super .change(node, builder);
095: builder = builder.getParentBuilder();
096: } while (builder != null);
097: if (localTransaction)
098: commit();
099: } catch (StorageException se) {
100: if (localTransaction && inTransaction)
101: rollback();
102: throw se;
103: }
104: }
105:
106: /**
107: * Deletes a node in the passed builder and all its parent builders.
108: * @param node The node to delete
109: * @param builder the builder to delete the node in
110: * @throws StorageException if an error occurred during delete
111: */
112: public void delete(MMObjectNode node, MMObjectBuilder builder)
113: throws StorageException {
114: boolean localTransaction = !inTransaction;
115: if (localTransaction) {
116: beginTransaction();
117: }
118:
119: try {
120: do {
121: super .delete(node, builder);
122: builder = builder.getParentBuilder();
123: } while (builder != null);
124: if (localTransaction)
125: commit();
126: } catch (StorageException se) {
127: if (localTransaction && inTransaction)
128: rollback();
129: throw se;
130: }
131: }
132:
133: }
|