Project

General

Profile

« Previous | Next » 

Revision 174

Added by bojilova over 24 years ago

for safe nodeid generation

View differences:

src/edu/ucsb/nceas/metacat/DBSAXNode.java
33 33
    super(tagname);
34 34
    this.conn = conn;
35 35
    writeChildNodeToDB("DOCUMENT", tagname, null);
36
    setNodeID(getAssignedNodeID());
36
    //setNodeID(getAssignedNodeID());
37 37
    setRootNodeID(getNodeID());
38 38
  }
39 39

  
......
54 54
    setNodeIndex(parentNode.incChildNum());
55 55
    this.conn = conn;
56 56
    writeChildNodeToDB("ELEMENT", getTagName(), null);
57
    setNodeID(getAssignedNodeID());
57
    //setNodeID(getAssignedNodeID());
58 58
  }
59 59
    
60 60
  /** creates SQL code and inserts new node into DB connection */
......
66 66
        pstmt = conn.prepareStatement(
67 67
            "INSERT INTO xml_nodes " +
68 68
            "(nodeid, nodetype, nodename) " +
69
            "VALUES (null, ?, ?)");
69
            "VALUES (?, ?, ?)");
70 70
      } else {
71 71
        pstmt = conn.prepareStatement(
72 72
            "INSERT INTO xml_nodes " +
73 73
            "(nodeid, nodetype, nodename, " +
74 74
            "parentnodeid, rootnodeid, docid, nodedata, nodeindex) " +
75
            "VALUES (null, ?, ?, ?, ?, ?, ?, ?)");
75
            "VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
76 76
      }
77 77

  
78 78
      // Bind the values to the query
79
      pstmt.setString(1, nodetype);
80
      pstmt.setString(2, nodename);
79
      long nid = genNodeID();
80
      pstmt.setLong(1, nid);
81
      pstmt.setString(2, nodetype);
82
      pstmt.setString(3, nodename);
81 83
      if (nodetype != "DOCUMENT") {
82 84
        if (nodetype == "ELEMENT") {
83
          pstmt.setLong(3, getParentID());
84
          pstmt.setLong(4, getRootNodeID());
85
          pstmt.setString(5, getDocID());
86
          pstmt.setString(6, data);
87
          pstmt.setInt(7, getNodeIndex());
85
          pstmt.setLong(4, getParentID());
86
          pstmt.setLong(5, getRootNodeID());
87
          pstmt.setString(6, getDocID());
88
          pstmt.setString(7, data);
89
          pstmt.setInt(8, getNodeIndex());
88 90
        } else {
89
          pstmt.setLong(3, getNodeID());
90
          pstmt.setLong(4, getRootNodeID());
91
          pstmt.setString(5, getDocID());
92
          pstmt.setString(6, data);
93
          pstmt.setInt(7, incChildNum());
91
          pstmt.setLong(4, getNodeID());
92
          pstmt.setLong(5, getRootNodeID());
93
          pstmt.setString(6, getDocID());
94
          pstmt.setString(7, data);
95
          pstmt.setInt(8, incChildNum());
94 96
        }
95 97
      }
96 98
      // Do the insertion
97 99
      pstmt.execute();
98 100
      pstmt.close();
101
      
102
      if (nodetype == "DOCUMENT" || nodetype == "ELEMENT")
103
        setNodeID(nid);
104
      
99 105
    } catch (SQLException e) {
100 106
      System.err.println("Error inserting node: (" + nodetype + ", " +
101 107
                                                     nodename + ", " + 
......
177 183
        pstmt.close();
178 184
      } catch (SQLException e) {
179 185
        System.out.println(e.getMessage());
180
      }
186
      }   
181 187
  }
182 188

  
183
  /** look up the assigned node id from DB connection */
184
  private long getAssignedNodeID() {
185
      long assigned_id=0;
189
  /** get next node id from DB connection */
190
  private long genNodeID() {
191
      long nid=0;
186 192
      Statement stmt;
187 193
      try {
188 194
        stmt = conn.createStatement();
189
        stmt.execute("SELECT xml_nodes_id_seq.currval FROM dual");
195
        stmt.execute("SELECT xml_nodes_id_seq.nextval FROM dual");
190 196
        ResultSet rs = stmt.getResultSet();
191 197
        boolean tableHasRows = rs.next();
192 198
        if (tableHasRows) {
193
          assigned_id = rs.getLong(1);
199
          nid = rs.getLong(1);
194 200
        }
195 201
        stmt.close();
196 202
      } catch (SQLException e) {
197 203
        System.out.println("Error getting id: " + e.getMessage());
198 204
      }
199 205

  
200
      // assign the new ID number
201
      return assigned_id;
206
      return nid;
202 207
  }
203 208

  
204 209
  /** Add a new attribute to this node, or set its value */

Also available in: Unified diff