PageRenderTime 42ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/derby-10.9.1.0/db-derby-10.9.1.0-src/java/engine/org/apache/derby/impl/sql/GenericResultDescription.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 339 lines | 165 code | 39 blank | 135 comment | 21 complexity | 028986f0f3d7154ca7ccfe60bc95da0a MD5 | raw file
  1. /*
  2. Derby - Class org.apache.derby.impl.sql.GenericResultDescription
  3. Licensed to the Apache Software Foundation (ASF) under one or more
  4. contributor license agreements. See the NOTICE file distributed with
  5. this work for additional information regarding copyright ownership.
  6. The ASF licenses this file to you under the Apache License, Version 2.0
  7. (the "License"); you may not use this file except in compliance with
  8. the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. */
  16. package org.apache.derby.impl.sql;
  17. import org.apache.derby.iapi.reference.SQLState;
  18. import org.apache.derby.iapi.sql.ResultColumnDescriptor;
  19. import org.apache.derby.iapi.sql.ResultDescription;
  20. import org.apache.derby.iapi.services.sanity.SanityManager;
  21. import org.apache.derby.iapi.services.io.StoredFormatIds;
  22. import org.apache.derby.iapi.services.io.FormatIdUtil;
  23. import org.apache.derby.iapi.services.io.Formatable;
  24. import org.apache.derby.iapi.util.ReuseFactory;
  25. import org.apache.derby.iapi.util.StringUtil;
  26. import java.io.ObjectOutput;
  27. import java.io.ObjectInput;
  28. import java.io.IOException;
  29. import java.sql.ResultSetMetaData;
  30. import java.util.Collections;
  31. import java.util.HashMap;
  32. import java.util.Map;
  33. /**
  34. * GenericResultDescription: basic implementation of result
  35. * description, used in conjunction with the other
  36. * implementations in this package. This implementation
  37. * of ResultDescription may be used by anyone.
  38. *
  39. */
  40. public final class GenericResultDescription
  41. implements ResultDescription, Formatable
  42. {
  43. /********************************************************
  44. **
  45. ** This class implements Formatable. That means that it
  46. ** can write itself to and from a formatted stream. If
  47. ** you add more fields to this class, make sure that you
  48. ** also write/read them with the writeExternal()/readExternal()
  49. ** methods.
  50. **
  51. ** If, inbetween releases, you add more fields to this class,
  52. ** then you should bump the version number emitted by the getTypeFormatId()
  53. ** method.
  54. **
  55. ********************************************************/
  56. private ResultColumnDescriptor[] columns;
  57. private String statementType;
  58. /**
  59. * Saved JDBC ResultSetMetaData object.
  60. * @see ResultDescription#setMetaData(java.sql.ResultSetMetaData)
  61. */
  62. private transient ResultSetMetaData metaData;
  63. /**
  64. * A map which maps a column name to a column number.
  65. * Entries only added when accessing columns with the name.
  66. */
  67. private Map columnNameMap;
  68. /**
  69. * Niladic constructor for Formatable
  70. */
  71. public GenericResultDescription()
  72. {
  73. }
  74. /**
  75. * Build a GenericResultDescription from columns and type
  76. *
  77. * @param columns an array of col descriptors
  78. * @param statementType the type
  79. */
  80. public GenericResultDescription(ResultColumnDescriptor[] columns,
  81. String statementType)
  82. {
  83. this.columns = columns;
  84. this.statementType = statementType;
  85. }
  86. /**
  87. * Build a GenericResultDescription
  88. *
  89. * @param rd the result description
  90. * @param theCols the columns to take from the input rd
  91. */
  92. public GenericResultDescription
  93. (
  94. ResultDescription rd,
  95. int[] theCols
  96. )
  97. {
  98. if (SanityManager.DEBUG)
  99. {
  100. SanityManager.ASSERT(theCols != null, "theCols argument to GenericResultDescription is null");
  101. }
  102. this.columns = new ResultColumnDescriptor[theCols.length];
  103. for (int i = 0; i < theCols.length; i++)
  104. {
  105. columns[i] = rd.getColumnDescriptor(theCols[i]);
  106. }
  107. this.statementType = rd.getStatementType();
  108. }
  109. //
  110. // ResultDescription interface
  111. //
  112. /**
  113. * @see ResultDescription#getStatementType
  114. */
  115. public String getStatementType() {
  116. return statementType;
  117. }
  118. /**
  119. * @see ResultDescription#getColumnCount
  120. */
  121. public int getColumnCount()
  122. {
  123. return (columns == null) ? 0 : columns.length;
  124. }
  125. public ResultColumnDescriptor[] getColumnInfo() {
  126. return columns;
  127. }
  128. /**
  129. * position is 1-based.
  130. * @see ResultDescription#getColumnDescriptor
  131. */
  132. public ResultColumnDescriptor getColumnDescriptor(int position) {
  133. return columns[position-1];
  134. }
  135. /**
  136. * Get a new result description that has been truncated
  137. * from input column number. If the input column is
  138. * 5, then columns 5 to getColumnCount() are removed.
  139. * The new ResultDescription points to the same
  140. * ColumnDescriptors (this method performs a shallow
  141. * copy.
  142. *
  143. * @param truncateFrom the starting column to remove
  144. *
  145. * @return a new ResultDescription
  146. */
  147. public ResultDescription truncateColumns(int truncateFrom)
  148. {
  149. if (SanityManager.DEBUG)
  150. {
  151. if (!(truncateFrom > 0 && columns != null))
  152. {
  153. SanityManager.THROWASSERT("bad truncate value: "+truncateFrom+" is too low");
  154. }
  155. if (truncateFrom > columns.length)
  156. {
  157. SanityManager.THROWASSERT("bad truncate value: "+truncateFrom+" is too high");
  158. }
  159. }
  160. ResultColumnDescriptor[] newColumns = new ResultColumnDescriptor[truncateFrom-1];
  161. System.arraycopy(columns, 0, newColumns, 0, newColumns.length);
  162. return new GenericResultDescription(newColumns, statementType);
  163. }
  164. //////////////////////////////////////////////
  165. //
  166. // FORMATABLE
  167. //
  168. //////////////////////////////////////////////
  169. /**
  170. * Write this object out
  171. *
  172. * @param out write bytes here
  173. *
  174. * @exception IOException thrown on error
  175. */
  176. public void writeExternal(ObjectOutput out) throws IOException
  177. {
  178. int len = (columns == null) ? 0 : columns.length;
  179. out.writeObject(statementType);
  180. out.writeInt(len);
  181. while(len-- > 0)
  182. {
  183. /*
  184. ** If we don't have a GenericColumnsDescriptor,
  185. ** create one now and use that to write out.
  186. ** Do this to avoid writing out query tree
  187. ** implementations of ResultColumnDescriptor
  188. */
  189. if (!(columns[len] instanceof
  190. GenericColumnDescriptor))
  191. {
  192. columns[len] = new GenericColumnDescriptor(columns[len]);
  193. }
  194. out.writeObject(columns[len]);
  195. }
  196. }
  197. /**
  198. * Read this object from a stream of stored objects.
  199. *
  200. * @param in read this.
  201. *
  202. * @exception IOException thrown on error
  203. * @exception ClassNotFoundException thrown on error
  204. */
  205. public void readExternal(ObjectInput in)
  206. throws IOException, ClassNotFoundException
  207. {
  208. int len;
  209. columns = null;
  210. statementType = (String)in.readObject();
  211. len = in.readInt();
  212. if (len > 0)
  213. {
  214. columns = new GenericColumnDescriptor[len];
  215. while(len-- > 0)
  216. {
  217. columns[len] = (ResultColumnDescriptor)in.readObject();
  218. }
  219. }
  220. }
  221. /**
  222. * Get the formatID which corresponds to this class.
  223. *
  224. * @return the formatID of this class
  225. */
  226. public int getTypeFormatId() { return StoredFormatIds.GENERIC_RESULT_DESCRIPTION_V01_ID; }
  227. public String toString()
  228. {
  229. if (SanityManager.DEBUG)
  230. {
  231. StringBuffer colStr = new StringBuffer();
  232. for (int i = 0; i < columns.length; i++)
  233. {
  234. colStr.append("column["+i+"]\n");
  235. colStr.append(columns[i].toString());
  236. }
  237. return "GenericResultDescription\n" +
  238. "\tStatementType = "+statementType+"\n" +
  239. "\tCOLUMNS\n" + colStr.toString();
  240. }
  241. else
  242. {
  243. return "";
  244. }
  245. }
  246. /**
  247. * Set the meta data if it has not already been set.
  248. */
  249. public synchronized void setMetaData(ResultSetMetaData rsmd) {
  250. if (metaData == null)
  251. metaData = rsmd;
  252. }
  253. /**
  254. * Get the saved meta data.
  255. */
  256. public synchronized ResultSetMetaData getMetaData() {
  257. return metaData;
  258. }
  259. /**
  260. * Find a column name based upon the JDBC rules for
  261. * getXXX and setXXX. Name matching is case-insensitive,
  262. * matching the first name (1-based) if there are multiple
  263. * columns that map to the same name.
  264. */
  265. public int findColumnInsenstive(String columnName) {
  266. final Map workMap;
  267. synchronized (this) {
  268. if (columnNameMap==null) {
  269. // updateXXX and getXXX methods are case insensitive and the
  270. // first column should be returned. The loop goes backward to
  271. // create a map which preserves this property.
  272. Map map = new HashMap();
  273. for (int i = getColumnCount(); i>=1; i--) {
  274. final String key = StringUtil.
  275. SQLToUpperCase(
  276. getColumnDescriptor(i).getName());
  277. final Integer value = ReuseFactory.getInteger(i);
  278. map.put(key, value);
  279. }
  280. // Ensure this map can never change.
  281. columnNameMap = Collections.unmodifiableMap(map);
  282. }
  283. workMap = columnNameMap;
  284. }
  285. Integer val = (Integer) workMap.get(columnName);
  286. if (val==null) {
  287. val = (Integer) workMap.get(StringUtil.SQLToUpperCase(columnName));
  288. }
  289. if (val==null) {
  290. return -1;
  291. } else {
  292. return val.intValue();
  293. }
  294. }
  295. }