/src/mpv5/db/common/ReturnValue.java
Java | 252 lines | 134 code | 32 blank | 86 comment | 16 complexity | 721895cb09383d03dc0f12e613e921f3 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
1/* 2 * To change this template, choose Tools | Templates 3 * and open the template in the editor. 4 */ 5package mpv5.db.common; 6 7import java.util.ArrayList; 8import java.util.Arrays; 9import java.util.Iterator; 10import java.util.List; 11 12/** 13 * 14 * 15 */ 16public class ReturnValue { 17 18 private int id = 0; 19 private int updateCount = 0; 20 private Object[][] data; 21 private String[] columnnames; 22 private String message = null; 23 private String[] fullColumnNames = null; 24 25 public ReturnValue(int idOfIt, Object[][] data) { 26 27 this.id = idOfIt; 28 this.data = data; 29 } 30 31 public ReturnValue(int idOfIt, Object[][] data, String[] columnnames) { 32 33 this.id = idOfIt; 34 this.columnnames = columnnames; 35 this.data = data; 36 } 37 38 public ReturnValue(int idOfIt, Object[][] data, String[] columnnames, String[] fullcolnames, String jobmessage) { 39 40 this.id = idOfIt; 41 this.columnnames = columnnames; 42 this.data = data; 43 this.message = jobmessage; 44 this.fullColumnNames = fullcolnames; 45 } 46 47 public ReturnValue(Integer id) { 48 this.id = id; 49 this.columnnames = new String[0]; 50 this.data = new Object[0][0]; 51 } 52 53 /** 54 * 55 */ 56 public ReturnValue() { 57 } 58 59 /** 60 * If an inserting query has generated an ID, it will be available here 61 * 62 * @return the id 63 */ 64 public int getId() { 65 return id; 66 } 67 68 /** 69 * First row of data retrieved by a select query 70 * 71 * @return the data 72 */ 73 public Object[] getFirstRow() { 74 if (hasData()) { 75 return data[0]; 76 } else { 77 return null; 78 } 79 } 80 81 /** 82 * First row of data retrieved by a select query 83 * 84 * @return the data 85 */ 86 public Object[] getFirstColumn() { 87 if (hasData()) { 88 Object[] res = new Object[data.length]; 89 for (int i = 0; i < data.length; i++) { 90 res[i] = data[i][0]; 91 } 92 return res; 93 } else { 94 return null; 95 } 96 } 97 98 /** 99 * First field of data retrieved by a select query 100 * 101 * @return the data 102 */ 103 public Object getFirstEntry() { 104 if (hasData()) { 105 return data[0][0]; 106 } else { 107 return null; 108 } 109 } 110 111 /** 112 * All data retrieved by a select query 113 * 114 * @return the data 115 */ 116 public Object[][] getData() { 117 return data; 118 } 119 120 /** 121 * All data retrieved by a select query 122 * 123 * @return the data 124 */ 125 public List<Object[]> getDataAsList() { 126 return Arrays.asList(getData()); 127 } 128 129 /** 130 * All data retrieved by a select query 131 * 132 * @return the data 133 */ 134 public List<String> getDataAsStringList() { 135 ArrayList<String> l = new ArrayList<String>(data.length); 136 for (int i = 0; i < data.length; i++) { 137 Object[] objects = data[i]; 138 StringBuilder a = new StringBuilder(""); 139 for (int j = 0; j < objects.length; j++) { 140 a.append(objects[j]); 141 if (j != objects.length - 1) { 142 a.append(", "); 143 } 144 } 145 l.add(a.toString()); 146 } 147 return l; 148 } 149 150 /** 151 * All data retrieved by a select query 152 * 153 * @return the data 154 */ 155 public Iterator<Object[]> getDataIterator() { 156 return Arrays.asList(getData()).iterator(); 157 } 158 159 /** 160 * The column names used 161 * 162 * @return the columnnames 163 */ 164 public String[] getColumnnames() { 165 return columnnames; 166 } 167 168 /** 169 * Add all the data of the given ReturnValue 170 * 171 * @param returnValue 172 */ 173 public void set(ReturnValue returnValue) { 174 this.setId(returnValue.id); 175 this.setColumnnames(returnValue.columnnames); 176 this.setData(returnValue.data); 177 this.setMessage(returnValue.message); 178 } 179 180 /** 181 * If the query had a message, this will be non-NULL 182 * 183 * @return the message 184 */ 185 public String getMessage() { 186 return message; 187 } 188 189 /** 190 * @param message the message to set 191 */ 192 public void setMessage(String message) { 193 this.message = message; 194 } 195 196 /** 197 * @param id the id to set 198 */ 199 public void setId(int id) { 200 this.id = id; 201 } 202 203 /** 204 * @param data the data to set 205 */ 206 public void setData(Object[][] data) { 207 this.data = data; 208 } 209 210 /** 211 * @param columnnames the columnnames to set 212 */ 213 public void setColumnnames(String[] columnnames) { 214 this.columnnames = columnnames; 215 } 216 217 /** 218 * Checks if the ReturnValue has any data at all 219 * 220 * @return 221 */ 222 public boolean hasData() { 223 return data != null && data.length > 0 && data[0].length > 0; 224 } 225 226 @Override 227 public String toString() { 228 if (data != null) { 229 return "Rowcount: " + data.length; 230 } 231 return "no rows"; 232 } 233 234 public String[] getFullColumnNames() { 235 return fullColumnNames; 236 } 237 238 public void setFullColumnNames(String[] fullColumnNames) { 239 this.fullColumnNames = fullColumnNames; 240 } 241 242 public void setUpdateCount(int updateCount) { 243 this.updateCount = updateCount; 244 } 245 246 /** 247 * @return the updateCount 248 */ 249 public int getUpdateCount() { 250 return updateCount; 251 } 252}