Project

General

Profile

« Previous | Next » 

Revision 758

Added by bojilova over 23 years ago

Changed the order of generating and getting Unique ID for
xml_nodes.nodeid and for uniqueid part of the accession#
because of SQl Server capabilities:
first let the db to generate unique id on insert (by db trigger and sequence or by IDENTITY key),
then get that unique id for further use by the application.
Used to be the opposite for these 2 unique id generations.

View differences:

src/edu/ucsb/nceas/metacat/DBSAXNode.java
57 57
    this.conn = conn;
58 58
    this.parentNode = null;
59 59
    writeChildNodeToDB("DOCUMENT", null, null, docid);
60
    updateRootNodeID(getNodeID());
60 61
  }
61 62

  
62 63
  /** 
......
91 92
      if (nodetype == "DOCUMENT") {
92 93
        pstmt = conn.prepareStatement(
93 94
            "INSERT INTO xml_nodes " +
94
            "(nodeid, nodetype, nodename, docid, rootnodeid) " +
95
            "VALUES (?, ?, ?, ?, ?)");
95
            "(nodetype, nodename, docid) " +
96
            "VALUES (?, ?, ?)");
96 97
        MetaCatUtil.debugMessage("INSERTING DOCNAME: " + nodename);
97 98
      } else {
98 99
        pstmt = conn.prepareStatement(
99 100
            "INSERT INTO xml_nodes " +
100
            "(nodeid, nodetype, nodename, docid, " +
101
            "(nodetype, nodename, docid, " +
101 102
            "rootnodeid, parentnodeid, nodedata, nodeindex) " +
102
            "VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
103
            "VALUES (?, ?, ?, ?, ?, ?, ?)");
103 104
      }
104 105

  
105 106
      // Bind the values to the query
106 107
      // NOT USED; USED DBAdapter.getUniqueID() instead
107 108
      //long nid = generateNodeID();
108
      long nid = dbAdapter.getUniqueID(conn, "xml_nodes");
109
      pstmt.setLong(1, nid);
110
      pstmt.setString(2, nodetype);
111
      pstmt.setString(3, nodename);
112
      pstmt.setString(4, docid);
109
      // NOT USED
110
      //let the db generate it: using identity key or through 
111
      //the db trigger xml_nodes_before_insert using sequence
112
      //long nid = dbAdapter.getUniqueID(conn, "xml_nodes");
113
      //pstmt.setLong(1, nid); 
114
      pstmt.setString(1, nodetype);
115
      pstmt.setString(2, nodename);
116
      pstmt.setString(3, docid);
113 117
      if (nodetype == "DOCUMENT") {
114
        pstmt.setLong(5, nid);
118
        // moved it to separate method updateRootNodeID
119
        //pstmt.setLong(4, nid);
115 120
      } else {
116 121
        if (nodetype == "ELEMENT") {
117
          pstmt.setLong(5, getRootNodeID());
118
          pstmt.setLong(6, getParentID());
119
          pstmt.setString(7, data);
120
          pstmt.setInt(8, getNodeIndex());
122
          pstmt.setLong(4, getRootNodeID());
123
          pstmt.setLong(5, getParentID());
124
          pstmt.setString(6, data);
125
          pstmt.setInt(7, getNodeIndex());
121 126
        } else {
122
          pstmt.setLong(5, getRootNodeID());
123
          pstmt.setLong(6, getNodeID());
127
          pstmt.setLong(4, getRootNodeID());
128
          pstmt.setLong(5, getNodeID());
124 129
          if ( nodetype == "TEXT" && getTagName().equals("meta_file_id") ) {
125
            pstmt.setString(7, getDocID());
130
            pstmt.setString(6, getDocID());
126 131
          } else {
127
            pstmt.setString(7, data);
132
            pstmt.setString(6, data);
128 133
          }  
129
          pstmt.setInt(8, incChildNum());
134
          pstmt.setInt(7, incChildNum());
130 135
        }
131 136
      }
132 137
      // Do the insertion
133 138
      pstmt.execute();
134 139
      pstmt.close();
140

  
141
      // get the generated unique id afterward
142
      long nid = dbAdapter.getUniqueID(conn, "xml_nodes");
135 143
      
136 144
      if (nodetype.equals("DOCUMENT")) {
137 145
        // Record the root node id that was generated from the database
......
159 167
  }
160 168

  
161 169
  /** 
170
   * update rootnodeid=nodeid for 'DOCUMENT' type of nodes only
171
   */
172
  public void updateRootNodeID(long nodeid) throws SAXException {
173
      try {
174
        PreparedStatement pstmt;
175
        pstmt = conn.prepareStatement(
176
              "UPDATE xml_nodes set rootnodeid = ? " +
177
              "WHERE nodeid = ?");
178

  
179
        // Bind the values to the query
180
        pstmt.setLong(1, nodeid);
181
        pstmt.setLong(2, nodeid);
182
        // Do the update
183
        pstmt.execute();
184
        pstmt.close();
185
      } catch (SQLException e) {
186
        System.out.println("Error in DBSaxNode.updateRootNodeID: " + 
187
                           e.getMessage());
188
        throw new SAXException(e.getMessage());
189
      }
190
  }
191

  
192
  /** 
162 193
   * creates SQL code to put nodename for the document node 
163 194
   * into DB connection 
164 195
   */
src/edu/ucsb/nceas/metacat/AccessionNumber.java
108 108
        // get a new docid
109 109
        if ( docid == null ) {
110 110
          String sitecode = getSitecode();
111
          // NOT USED; USED DBAdapter.getUniqueID() instead
112
          //String uniqueid = getUniqueID();
113
          String uniqueid = "" + dbAdapter.getUniqueID(conn,"xml_documents");
111
          String uniqueid = getUniqueID(sitecode);
114 112

  
115 113
          return sitecode + sep + uniqueid;
116 114

  
......
167 165
  }
168 166

  
169 167
  // get Unique ID from DB sequence
170
  private String getUniqueID () throws SQLException
168
  private String getUniqueID (String sitecode) throws SQLException
171 169
  {
172 170
    String uniqueid;
171
    String sysdate = dbAdapter.getDateTimeFunction();
173 172
    
174 173
    try {
175 174
      PreparedStatement pstmt;
176 175
      pstmt = conn.prepareStatement
177
              ("SELECT accnum_uniqueid_seq.nextval FROM dual");
176
              ("INSERT INTO accession_number (site_code, date_created) " +
177
               "VALUES (?, " + sysdate + ")");
178
      pstmt.setString(1, sitecode);
178 179
      pstmt.execute();
179

  
180
      ResultSet rs = pstmt.getResultSet();
181
      boolean hasRow = rs.next();
182
      uniqueid = rs.getString(1);
183 180
      pstmt.close();
184 181
      
182
      uniqueid = "" + dbAdapter.getUniqueID(conn, "accession_number");
183

  
185 184
    } catch (SQLException e) {
186 185
      throw new 
187 186
      SQLException("Error on AccessionNumber.getUniqueID(): "+e.getMessage());

Also available in: Unified diff