PageRenderTime 313ms CodeModel.GetById 19ms RepoModel.GetById 5ms app.codeStats 0ms

/src/sql/java/SqlMetaPeer.java

https://bitbucket.org/bedlaczech/fan-1.0
Java | 195 lines | 139 code | 30 blank | 26 comment | 7 complexity | cdc99915376082b9643fe71783d83004 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. //
  2. // Copyright (c) 2011, Brian Frank and Andy Frank
  3. // Licensed under the Academic Free License version 3.0
  4. //
  5. // History:
  6. // 04 Jan 11 Brian Frank Creation
  7. //
  8. package fan.sql;
  9. import java.sql.*;
  10. import fan.sys.*;
  11. public class SqlMetaPeer
  12. {
  13. //////////////////////////////////////////////////////////////////////////
  14. // Peer Factory
  15. //////////////////////////////////////////////////////////////////////////
  16. public static SqlMetaPeer make(SqlMeta fan)
  17. {
  18. return new SqlMetaPeer();
  19. }
  20. //////////////////////////////////////////////////////////////////////////
  21. // Versions
  22. //////////////////////////////////////////////////////////////////////////
  23. public String productName(SqlMeta self)
  24. {
  25. try { return jmeta.getDatabaseProductName(); }
  26. catch(SQLException e) { throw err(e); }
  27. }
  28. public Version productVersion(SqlMeta self)
  29. {
  30. try { return ver(jmeta.getDatabaseMajorVersion(), jmeta.getDatabaseMinorVersion()); }
  31. catch(SQLException e) { throw err(e); }
  32. }
  33. public String productVersionStr(SqlMeta self)
  34. {
  35. try { return jmeta.getDatabaseProductVersion(); }
  36. catch(SQLException e) { throw err(e); }
  37. }
  38. public String driverName(SqlMeta self)
  39. {
  40. try { return jmeta.getDriverName(); }
  41. catch(SQLException e) { throw err(e); }
  42. }
  43. public Version driverVersion(SqlMeta self)
  44. {
  45. return ver(jmeta.getDriverMajorVersion(), jmeta.getDriverMinorVersion());
  46. }
  47. public String driverVersionStr(SqlMeta self)
  48. {
  49. try { return jmeta.getDriverVersion(); }
  50. catch(SQLException e) { throw err(e); }
  51. }
  52. //////////////////////////////////////////////////////////////////////////
  53. // Limits
  54. //////////////////////////////////////////////////////////////////////////
  55. public Long maxColName(SqlMeta self)
  56. {
  57. try { return max(jmeta.getMaxColumnNameLength()); }
  58. catch(SQLException e) { throw err(e); }
  59. }
  60. public Long maxTableName(SqlMeta self)
  61. {
  62. try { return max(jmeta.getMaxTableNameLength()); }
  63. catch(SQLException e) { throw err(e); }
  64. }
  65. //////////////////////////////////////////////////////////////////////////
  66. // Tables
  67. //////////////////////////////////////////////////////////////////////////
  68. public boolean tableExists(SqlMeta self, String tableName)
  69. {
  70. try
  71. {
  72. ResultSet tables =
  73. jmeta.getTables(null, // catalog
  74. null, // schema pattern
  75. tableName, // table name pattern
  76. null); // types
  77. boolean exists = tables.next();
  78. tables.close();
  79. return exists;
  80. }
  81. catch (SQLException ex)
  82. {
  83. throw err(ex);
  84. }
  85. }
  86. public List tables(SqlMeta self)
  87. {
  88. try
  89. {
  90. ResultSet tables =
  91. jmeta.getTables(null, // catalog
  92. null, // schema pattern
  93. null, // table name pattern
  94. null); // types
  95. int nameIndex = tables.findColumn("TABLE_NAME");
  96. List tableList = new List(Sys.StrType, 32);
  97. while (tables.next())
  98. {
  99. String tableName = tables.getString(nameIndex);
  100. tableList.add(tableName);
  101. }
  102. tables.close();
  103. return tableList.ro();
  104. }
  105. catch (SQLException ex)
  106. {
  107. throw err(ex);
  108. }
  109. }
  110. public Row tableRow(SqlMeta self, String tableName)
  111. {
  112. try
  113. {
  114. ResultSet columns = jmeta.getColumns(null, null, tableName, null);
  115. // map the meta-data to a dynamic type
  116. List cols = new List(SqlUtil.colType);
  117. int nameIndex = columns.findColumn("COLUMN_NAME");
  118. int typeIndex = columns.findColumn("DATA_TYPE");
  119. int typeNameIndex = columns.findColumn("TYPE_NAME");
  120. int colIndex = 0;
  121. while (columns.next())
  122. {
  123. String name = columns.getString(nameIndex);
  124. String typeName = columns.getString(typeNameIndex);
  125. Type fanType = SqlUtil.sqlToFanType(columns.getInt(typeIndex));
  126. if (fanType == null)
  127. {
  128. System.out.println("WARNING: Cannot map " + typeName + " to Fan type");
  129. fanType = Sys.StrType;
  130. }
  131. cols.add(Col.make(Long.valueOf(colIndex++), name, fanType, typeName));
  132. }
  133. if (colIndex == 0)
  134. throw SqlErr.make("Table not found: " + tableName);
  135. Row row = Row.make();
  136. row.peer.cols = new Cols(cols);
  137. row.peer.cells = new Object[cols.sz()];
  138. return row;
  139. }
  140. catch (SQLException ex)
  141. {
  142. throw err(ex);
  143. }
  144. }
  145. //////////////////////////////////////////////////////////////////////////
  146. // Utils
  147. //////////////////////////////////////////////////////////////////////////
  148. static Long max(int limit)
  149. {
  150. if (limit <= 0) return null;
  151. return Long.valueOf(limit);
  152. }
  153. static Version ver(int major, int minor)
  154. {
  155. return Version.fromStr("" + major + "." + minor);
  156. }
  157. static RuntimeException err(SQLException e)
  158. {
  159. return SqlErr.make(e.getMessage(), Err.make(e));
  160. }
  161. //////////////////////////////////////////////////////////////////////////
  162. // Fields
  163. //////////////////////////////////////////////////////////////////////////
  164. DatabaseMetaData jmeta;
  165. }