Project

General

Profile

« Previous | Next » 

Revision 19

Added by Matt Jones over 24 years ago

added attribute persistence to DB code

View differences:

src/xmltables.sql
11 11
 */
12 12

  
13 13
/*
14
 * Drop all of the tables in proper order
14
 * Drop all of the objects in proper order
15 15
 */
16
DROP SEQUENCE xml_attributes_id_seq;
17
DROP TRIGGER xml_attributes_before_insert;
18
DROP SEQUENCE xml_elements_id_seq;
19
DROP TRIGGER xml_elements_before_insert;
16 20
DROP TABLE xml_documents;
17 21
DROP TABLE xml_attributes;
18 22
DROP TABLE xml_elements;
......
32 36
		FOREIGN KEY (parentnodeid) REFERENCES xml_elements
33 37
);
34 38

  
39
CREATE SEQUENCE xml_elements_id_seq;
40

  
41
CREATE TRIGGER xml_elements_before_insert
42
BEFORE INSERT ON xml_elements FOR EACH ROW
43
BEGIN
44
  SELECT xml_elements_id_seq.nextval
45
    INTO :new.nodeid
46
    FROM dual;
47
END;
48
/
49

  
35 50
/* 
36 51
 * Documents -- table to store XML document catalog
37 52
 */
......
54 69
	attributeid	NUMBER(20),
55 70
	nodeid		NUMBER(20),
56 71
	attributenumber	NUMBER(20),
57
	attributename	VARCHAR2(2000),
72
	attributename	VARCHAR2(256),
58 73
	attributevalue	VARCHAR2(2000),
59 74
	date_created	DATE,
60 75
	date_updated	DATE,
61 76
   CONSTRAINT xml_attributes_pk PRIMARY KEY (attributeid),
62 77
   CONSTRAINT xml_attributes_parent_node FOREIGN KEY (nodeid) 
63
		REFERENCES xml_elements
78
		REFERENCES xml_elements,
79
   CONSTRAINT xml_attributes_uniq_attribute UNIQUE (nodeid,attributename) 
64 80
);
81

  
82
CREATE SEQUENCE xml_attributes_id_seq;
83

  
84
CREATE TRIGGER xml_attributes_before_insert
85
BEFORE INSERT ON xml_attributes FOR EACH ROW
86
BEGIN
87
  SELECT xml_attributes_id_seq.nextval
88
    INTO :new.attributeid
89
    FROM dual;
90
END;
91
/
92

  
src/edu/ucsb/nceas/metacat/DBSAXNode.java
27 27
    public DBSAXElement (Connection conn, String tagname, 
28 28
                         long parent_id) {
29 29
      this.conn = conn;
30
      this.element_id = assignElementID();
31 30

  
32 31
      this.tagname = tagname;
33 32
      this.parent_id = parent_id;
......
36 35
      writeElementToDB();
37 36
    };
38 37
    
39
    private long assignElementID() {
40
        long maxid=0;
38
    /** creates SQL code and inserts new element into DB connection */
39
    private void writeElementToDB() {
40
        try {
41
          conn.setAutoCommit(false);
42
          PreparedStatement pstmt;
43
          if (parent_id != 0) {
44
            pstmt = conn.prepareStatement(
45
                "INSERT INTO xml_elements(nodeid, nodename, parentnodeid) " +
46
                "VALUES (null, ?, ?)");
47
          } else {
48
            pstmt = conn.prepareStatement(
49
                "INSERT INTO xml_elements(nodeid, nodename) " +
50
                "VALUES (null, ?)");
51
          }
52

  
53
          // Bind the values to the query
54
          pstmt.setString(1, getTagName());// The second ? is for NODENAME
55
          if (parent_id != 0) {
56
            pstmt.setLong(2, parent_id);
57
          }
58
          // Do the insertion
59
          pstmt.execute();
60
          pstmt.close();
61
          this.element_id = getAssignedElementID();
62
          conn.commit();
63
          conn.setAutoCommit(true);
64
        } catch (SQLException e) {
65
          System.out.println(e.getMessage());
66
        }
67
    }
68

  
69
    /** look up the assigned element id from DB connection */
70
    private long getAssignedElementID() {
71
        long assigned_id=0;
41 72
        Statement stmt;
42 73
        try {
43 74
          stmt = conn.createStatement();
44
          stmt.execute("SELECT MAX(nodeid) FROM xml_elements");
75
          stmt.execute("SELECT xml_elements_id_seq.currval FROM dual");
45 76
          try {
46 77
            ResultSet rs = stmt.getResultSet();
47 78
            try {
48 79
              boolean tableHasRows = rs.next();
49 80
              if (tableHasRows) {
50 81
                try {
51
                  maxid = rs.getInt(1);
82
                  assigned_id = rs.getLong(1);
52 83
                } catch (SQLException e) {
53 84
                  System.out.println("Error with getInt: " + e.getMessage());
54 85
                }
......
64 95
          System.out.println("Error getting id: " + e.getMessage());
65 96
        }
66 97

  
67
        // assign a new ID number
68
        return (maxid + 1);
98
        // assign the new ID number
99
        return assigned_id;
69 100
    }
70 101

  
71
    private void writeElementToDB() {
72
        try {
73
          PreparedStatement pstmt;
74
          if (parent_id != 0) {
75
            pstmt = conn.prepareStatement(
76
                "INSERT INTO xml_elements(nodeid, nodename, parentnodeid) " +
77
                "VALUES (?, ?, ?)");
78
          } else {
79
            pstmt = conn.prepareStatement(
80
                "INSERT INTO xml_elements(nodeid, nodename) " +
81
                "VALUES (?, ?)");
82
          }
83

  
84
          // Bind the values to the query
85
          pstmt.setLong(1, element_id); // The first ? is for NODEID
86
          pstmt.setString(2, getTagName());// The second ? is for NODENAME
87
          if (parent_id != 0) {
88
            pstmt.setLong(3, parent_id);
89
          }
90
          // Do the insertion
91
          pstmt.execute();
92
          pstmt.close();
93

  
94
        } catch (SQLException e) {
95
          System.out.println(e.getMessage());
96
        }
97
    }
98

  
99 102
    // used by JTree to display this node
100 103
    public String toString ()
101 104
    {
......
131 134
    /** Add a new attribute to this element, or set its value */
132 135
    public void setAttribute(String attName, String attValue) {
133 136
      if (attName != null) {
137
        // Enter the attribute in the hash table
134 138
        attributes.put(attName, attValue);
139
 
140
        // And enter the attribute in the database
141
        try {
142
          PreparedStatement pstmt = conn.prepareStatement(
143
              "INSERT INTO xml_attributes(attributeid, nodeid, " +
144
              "attributename, attributevalue) " +
145
              "VALUES (null, ?, ?, ?)");
146

  
147
          // Bind the values to the query
148
          pstmt.setLong(1, getElementID());
149
          pstmt.setString(2, attName);
150
          pstmt.setString(3, attValue);
151
 
152
          // Do the insertion
153
          pstmt.execute();
154
          pstmt.close();
155
        } catch (SQLException e) {
156
          System.out.println(e.getMessage());
157
        }
158

  
135 159
      } else {
136 160
        System.err.println("Attribute name must not be null!");
137 161
      }
src/edu/ucsb/nceas/metacat/DBSAXElement.java
27 27
    public DBSAXElement (Connection conn, String tagname, 
28 28
                         long parent_id) {
29 29
      this.conn = conn;
30
      this.element_id = assignElementID();
31 30

  
32 31
      this.tagname = tagname;
33 32
      this.parent_id = parent_id;
......
36 35
      writeElementToDB();
37 36
    };
38 37
    
39
    private long assignElementID() {
40
        long maxid=0;
38
    /** creates SQL code and inserts new element into DB connection */
39
    private void writeElementToDB() {
40
        try {
41
          conn.setAutoCommit(false);
42
          PreparedStatement pstmt;
43
          if (parent_id != 0) {
44
            pstmt = conn.prepareStatement(
45
                "INSERT INTO xml_elements(nodeid, nodename, parentnodeid) " +
46
                "VALUES (null, ?, ?)");
47
          } else {
48
            pstmt = conn.prepareStatement(
49
                "INSERT INTO xml_elements(nodeid, nodename) " +
50
                "VALUES (null, ?)");
51
          }
52

  
53
          // Bind the values to the query
54
          pstmt.setString(1, getTagName());// The second ? is for NODENAME
55
          if (parent_id != 0) {
56
            pstmt.setLong(2, parent_id);
57
          }
58
          // Do the insertion
59
          pstmt.execute();
60
          pstmt.close();
61
          this.element_id = getAssignedElementID();
62
          conn.commit();
63
          conn.setAutoCommit(true);
64
        } catch (SQLException e) {
65
          System.out.println(e.getMessage());
66
        }
67
    }
68

  
69
    /** look up the assigned element id from DB connection */
70
    private long getAssignedElementID() {
71
        long assigned_id=0;
41 72
        Statement stmt;
42 73
        try {
43 74
          stmt = conn.createStatement();
44
          stmt.execute("SELECT MAX(nodeid) FROM xml_elements");
75
          stmt.execute("SELECT xml_elements_id_seq.currval FROM dual");
45 76
          try {
46 77
            ResultSet rs = stmt.getResultSet();
47 78
            try {
48 79
              boolean tableHasRows = rs.next();
49 80
              if (tableHasRows) {
50 81
                try {
51
                  maxid = rs.getInt(1);
82
                  assigned_id = rs.getLong(1);
52 83
                } catch (SQLException e) {
53 84
                  System.out.println("Error with getInt: " + e.getMessage());
54 85
                }
......
64 95
          System.out.println("Error getting id: " + e.getMessage());
65 96
        }
66 97

  
67
        // assign a new ID number
68
        return (maxid + 1);
98
        // assign the new ID number
99
        return assigned_id;
69 100
    }
70 101

  
71
    private void writeElementToDB() {
72
        try {
73
          PreparedStatement pstmt;
74
          if (parent_id != 0) {
75
            pstmt = conn.prepareStatement(
76
                "INSERT INTO xml_elements(nodeid, nodename, parentnodeid) " +
77
                "VALUES (?, ?, ?)");
78
          } else {
79
            pstmt = conn.prepareStatement(
80
                "INSERT INTO xml_elements(nodeid, nodename) " +
81
                "VALUES (?, ?)");
82
          }
83

  
84
          // Bind the values to the query
85
          pstmt.setLong(1, element_id); // The first ? is for NODEID
86
          pstmt.setString(2, getTagName());// The second ? is for NODENAME
87
          if (parent_id != 0) {
88
            pstmt.setLong(3, parent_id);
89
          }
90
          // Do the insertion
91
          pstmt.execute();
92
          pstmt.close();
93

  
94
        } catch (SQLException e) {
95
          System.out.println(e.getMessage());
96
        }
97
    }
98

  
99 102
    // used by JTree to display this node
100 103
    public String toString ()
101 104
    {
......
131 134
    /** Add a new attribute to this element, or set its value */
132 135
    public void setAttribute(String attName, String attValue) {
133 136
      if (attName != null) {
137
        // Enter the attribute in the hash table
134 138
        attributes.put(attName, attValue);
139
 
140
        // And enter the attribute in the database
141
        try {
142
          PreparedStatement pstmt = conn.prepareStatement(
143
              "INSERT INTO xml_attributes(attributeid, nodeid, " +
144
              "attributename, attributevalue) " +
145
              "VALUES (null, ?, ?, ?)");
146

  
147
          // Bind the values to the query
148
          pstmt.setLong(1, getElementID());
149
          pstmt.setString(2, attName);
150
          pstmt.setString(3, attValue);
151
 
152
          // Do the insertion
153
          pstmt.execute();
154
          pstmt.close();
155
        } catch (SQLException e) {
156
          System.out.println(e.getMessage());
157
        }
158

  
135 159
      } else {
136 160
        System.err.println("Attribute name must not be null!");
137 161
      }
xmltables.sql
11 11
 */
12 12

  
13 13
/*
14
 * Drop all of the tables in proper order
14
 * Drop all of the objects in proper order
15 15
 */
16
DROP SEQUENCE xml_attributes_id_seq;
17
DROP TRIGGER xml_attributes_before_insert;
18
DROP SEQUENCE xml_elements_id_seq;
19
DROP TRIGGER xml_elements_before_insert;
16 20
DROP TABLE xml_documents;
17 21
DROP TABLE xml_attributes;
18 22
DROP TABLE xml_elements;
......
32 36
		FOREIGN KEY (parentnodeid) REFERENCES xml_elements
33 37
);
34 38

  
39
CREATE SEQUENCE xml_elements_id_seq;
40

  
41
CREATE TRIGGER xml_elements_before_insert
42
BEFORE INSERT ON xml_elements FOR EACH ROW
43
BEGIN
44
  SELECT xml_elements_id_seq.nextval
45
    INTO :new.nodeid
46
    FROM dual;
47
END;
48
/
49

  
35 50
/* 
36 51
 * Documents -- table to store XML document catalog
37 52
 */
......
54 69
	attributeid	NUMBER(20),
55 70
	nodeid		NUMBER(20),
56 71
	attributenumber	NUMBER(20),
57
	attributename	VARCHAR2(2000),
72
	attributename	VARCHAR2(256),
58 73
	attributevalue	VARCHAR2(2000),
59 74
	date_created	DATE,
60 75
	date_updated	DATE,
61 76
   CONSTRAINT xml_attributes_pk PRIMARY KEY (attributeid),
62 77
   CONSTRAINT xml_attributes_parent_node FOREIGN KEY (nodeid) 
63
		REFERENCES xml_elements
78
		REFERENCES xml_elements,
79
   CONSTRAINT xml_attributes_uniq_attribute UNIQUE (nodeid,attributename) 
64 80
);
81

  
82
CREATE SEQUENCE xml_attributes_id_seq;
83

  
84
CREATE TRIGGER xml_attributes_before_insert
85
BEFORE INSERT ON xml_attributes FOR EACH ROW
86
BEGIN
87
  SELECT xml_attributes_id_seq.nextval
88
    INTO :new.attributeid
89
    FROM dual;
90
END;
91
/
92

  
DBSAXElement.java
27 27
    public DBSAXElement (Connection conn, String tagname, 
28 28
                         long parent_id) {
29 29
      this.conn = conn;
30
      this.element_id = assignElementID();
31 30

  
32 31
      this.tagname = tagname;
33 32
      this.parent_id = parent_id;
......
36 35
      writeElementToDB();
37 36
    };
38 37
    
39
    private long assignElementID() {
40
        long maxid=0;
38
    /** creates SQL code and inserts new element into DB connection */
39
    private void writeElementToDB() {
40
        try {
41
          conn.setAutoCommit(false);
42
          PreparedStatement pstmt;
43
          if (parent_id != 0) {
44
            pstmt = conn.prepareStatement(
45
                "INSERT INTO xml_elements(nodeid, nodename, parentnodeid) " +
46
                "VALUES (null, ?, ?)");
47
          } else {
48
            pstmt = conn.prepareStatement(
49
                "INSERT INTO xml_elements(nodeid, nodename) " +
50
                "VALUES (null, ?)");
51
          }
52

  
53
          // Bind the values to the query
54
          pstmt.setString(1, getTagName());// The second ? is for NODENAME
55
          if (parent_id != 0) {
56
            pstmt.setLong(2, parent_id);
57
          }
58
          // Do the insertion
59
          pstmt.execute();
60
          pstmt.close();
61
          this.element_id = getAssignedElementID();
62
          conn.commit();
63
          conn.setAutoCommit(true);
64
        } catch (SQLException e) {
65
          System.out.println(e.getMessage());
66
        }
67
    }
68

  
69
    /** look up the assigned element id from DB connection */
70
    private long getAssignedElementID() {
71
        long assigned_id=0;
41 72
        Statement stmt;
42 73
        try {
43 74
          stmt = conn.createStatement();
44
          stmt.execute("SELECT MAX(nodeid) FROM xml_elements");
75
          stmt.execute("SELECT xml_elements_id_seq.currval FROM dual");
45 76
          try {
46 77
            ResultSet rs = stmt.getResultSet();
47 78
            try {
48 79
              boolean tableHasRows = rs.next();
49 80
              if (tableHasRows) {
50 81
                try {
51
                  maxid = rs.getInt(1);
82
                  assigned_id = rs.getLong(1);
52 83
                } catch (SQLException e) {
53 84
                  System.out.println("Error with getInt: " + e.getMessage());
54 85
                }
......
64 95
          System.out.println("Error getting id: " + e.getMessage());
65 96
        }
66 97

  
67
        // assign a new ID number
68
        return (maxid + 1);
98
        // assign the new ID number
99
        return assigned_id;
69 100
    }
70 101

  
71
    private void writeElementToDB() {
72
        try {
73
          PreparedStatement pstmt;
74
          if (parent_id != 0) {
75
            pstmt = conn.prepareStatement(
76
                "INSERT INTO xml_elements(nodeid, nodename, parentnodeid) " +
77
                "VALUES (?, ?, ?)");
78
          } else {
79
            pstmt = conn.prepareStatement(
80
                "INSERT INTO xml_elements(nodeid, nodename) " +
81
                "VALUES (?, ?)");
82
          }
83

  
84
          // Bind the values to the query
85
          pstmt.setLong(1, element_id); // The first ? is for NODEID
86
          pstmt.setString(2, getTagName());// The second ? is for NODENAME
87
          if (parent_id != 0) {
88
            pstmt.setLong(3, parent_id);
89
          }
90
          // Do the insertion
91
          pstmt.execute();
92
          pstmt.close();
93

  
94
        } catch (SQLException e) {
95
          System.out.println(e.getMessage());
96
        }
97
    }
98

  
99 102
    // used by JTree to display this node
100 103
    public String toString ()
101 104
    {
......
131 134
    /** Add a new attribute to this element, or set its value */
132 135
    public void setAttribute(String attName, String attValue) {
133 136
      if (attName != null) {
137
        // Enter the attribute in the hash table
134 138
        attributes.put(attName, attValue);
139
 
140
        // And enter the attribute in the database
141
        try {
142
          PreparedStatement pstmt = conn.prepareStatement(
143
              "INSERT INTO xml_attributes(attributeid, nodeid, " +
144
              "attributename, attributevalue) " +
145
              "VALUES (null, ?, ?, ?)");
146

  
147
          // Bind the values to the query
148
          pstmt.setLong(1, getElementID());
149
          pstmt.setString(2, attName);
150
          pstmt.setString(3, attValue);
151
 
152
          // Do the insertion
153
          pstmt.execute();
154
          pstmt.close();
155
        } catch (SQLException e) {
156
          System.out.println(e.getMessage());
157
        }
158

  
135 159
      } else {
136 160
        System.err.println("Attribute name must not be null!");
137 161
      }

Also available in: Unified diff