1 package javacli; 2 3 /** 4 * Set of objects returned by select. This class is similar with JDBC ResultSet 5 * class and allows navigation though the selected objects in orward or backward 6 * direction 7 */ 8 public class ObjectSet { 9 /** 10 * Get first fetched object 11 * @return first object in the set or null if no objects were selected 12 */ getFirst()13 public Object getFirst() { 14 return getObject(Connection.cli_cmd_get_first, 0); 15 } 16 17 /** 18 * Get last fetched object 19 * @return last object in the set or null if no objects were selected 20 */ getLast()21 public Object getLast() { 22 return getObject(Connection.cli_cmd_get_last, 0); 23 } 24 25 /** 26 * Get next fetched object 27 * @return next object in the set or null if current object is thelast one in the 28 * set or no objects were selected 29 */ getNext()30 public Object getNext() { 31 return getObject(Connection.cli_cmd_get_next, 0); 32 } 33 34 /** 35 * Get previous fetched object 36 * @return previous object in the set or null if the current object is the first 37 * one in the set or no objects were selected 38 */ getPrev()39 public Object getPrev() { 40 return getObject(Connection.cli_cmd_get_prev, 0); 41 } 42 43 44 /** 45 * Skip specified number of objects. 46 * <UL> 47 * <LI>if <code>n</code> is positive, then this method has the same effect as 48 * executing getNext() mehod <code>n</code> times. 49 * <LI>if <code>n</code> is negative, then this method has the same effect of 50 * executing getPrev() mehod <code>-n</code> times. 51 * <LI>if <code>n</code> is zero, this method has no effect 52 * </UL> 53 * @param n number of objects to be skipped 54 * @return object <code>n</code> positions relative to the current position 55 */ skip(int n)56 Object skip(int n) { 57 return getObject(Connection.cli_cmd_skip, n); 58 } 59 60 /** 61 * Get reference to the current object 62 * @return return reference to the current object or null if no objects were 63 * selected 64 */ getRef()65 public Reference getRef() { 66 return currOid != 0 ? new Reference(currOid) : null; 67 } 68 69 /** 70 * Update the current object in the set. Changes made in the current object 71 * are saved in the database 72 */ update()73 public void update() { 74 if (stmt == null) { 75 throw new CliError("ObjectSet was aleady closed"); 76 } 77 if (stmt.con == null) { 78 throw new CliError("Statement was closed"); 79 } 80 if (currOid == 0) { 81 throw new CliError("No object was selected"); 82 } 83 if (!stmt.forUpdate) { 84 throw new CliError("Updates not allowed"); 85 } 86 if (updated) { 87 throw new CliError("Record was already updated"); 88 } 89 ComBuffer buf = new ComBuffer(Connection.cli_cmd_update, stmt.stmtId); 90 stmt.tableDesc.writeColumnValues(buf, currObj); 91 stmt.con.sendReceive(buf); 92 } 93 94 /** 95 * Remove all selected objects. 96 * All objects in the object set are removed from the database. 97 */ removeAll()98 public void removeAll() { 99 if (stmt == null) { 100 throw new CliError("ObjectSet was aleady closed"); 101 } 102 if (stmt.con == null) { 103 throw new CliError("Statement was closed"); 104 } 105 if (!stmt.forUpdate) { 106 throw new CliError("Updates not allowed"); 107 } 108 stmt.con.sendReceive(Connection.cli_cmd_remove, stmt.stmtId); 109 } 110 111 /** 112 * Get the number of objects in the object set. 113 * @return number of the selected objects 114 */ size()115 public int size() { 116 return nObjects; 117 } 118 119 /** 120 * Close object set. Any followin operation with this object set will raise an 121 * xception. 122 */ close()123 public void close() { 124 if (stmt == null) { 125 throw new CliError("ObjectSet was aleady closed"); 126 } 127 stmt = null; 128 } 129 getObject(int cmd, int n)130 protected Object getObject(int cmd, int n) { 131 if (stmt == null) { 132 throw new CliError("ObjectSet was aleady closed"); 133 } 134 if (stmt.con == null) { 135 throw new CliError("Statement was closed"); 136 } 137 ComBuffer buf = new ComBuffer(cmd, stmt.stmtId); 138 if (cmd == Connection.cli_cmd_skip) { 139 buf.putInt(n); 140 } 141 stmt.con.send(buf); 142 buf.reset(4); 143 stmt.con.receive(buf, 4); 144 int len = buf.getInt(); 145 if (len == Connection.cli_not_found) { 146 return null; 147 } else if (len <= 0) { 148 throw new CliError("Failed to get object"); 149 } 150 buf.reset(len-4); 151 stmt.con.receive(buf, len-4); 152 currOid = buf.getInt(); 153 if (currOid == 0) { 154 return null; 155 } 156 updated = false; 157 return currObj = stmt.tableDesc.readObject(buf); 158 } 159 ObjectSet(Statement stmt, int nObjects)160 protected ObjectSet(Statement stmt, int nObjects) { 161 this.stmt = stmt; 162 this.nObjects = nObjects; 163 } 164 165 protected Statement stmt; 166 protected int nObjects; 167 protected int currOid; 168 protected Object currObj; 169 protected boolean updated; 170 } 171 172 173