PageRenderTime 90ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mpv5/db/common/ReturnValue.java

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