Example #1
0
 /**
  * Get array of ColumnInfos that contain Column Name and its associated PDataType
  *
  * @param columns
  * @return
  * @throws SQLException
  */
 private ColumnInfo[] generateColumnInfo(List<String> columns) throws SQLException {
   Map<String, Integer> columnNameToTypeMap = Maps.newLinkedHashMap();
   DatabaseMetaData dbmd = conn.getMetaData();
   // TODO: escape wildcard characters here because we don't want that behavior here
   String escapedTableName = StringUtil.escapeLike(tableName);
   String[] schemaAndTable = escapedTableName.split("\\.");
   ResultSet rs = null;
   try {
     rs =
         dbmd.getColumns(
             null,
             (schemaAndTable.length == 1 ? "" : schemaAndTable[0]),
             (schemaAndTable.length == 1 ? escapedTableName : schemaAndTable[1]),
             null);
     while (rs.next()) {
       columnNameToTypeMap.put(
           rs.getString(QueryUtil.COLUMN_NAME_POSITION), rs.getInt(QueryUtil.DATA_TYPE_POSITION));
     }
   } finally {
     if (rs != null) {
       rs.close();
     }
   }
   ColumnInfo[] columnType;
   if (columns == null) {
     int i = 0;
     columnType = new ColumnInfo[columnNameToTypeMap.size()];
     for (Map.Entry<String, Integer> entry : columnNameToTypeMap.entrySet()) {
       columnType[i++] = new ColumnInfo(entry.getKey(), entry.getValue());
     }
   } else {
     // Leave "null" as indication to skip b/c it doesn't exist
     columnType = new ColumnInfo[columns.size()];
     for (int i = 0; i < columns.size(); i++) {
       String columnName = SchemaUtil.normalizeIdentifier(columns.get(i).trim());
       Integer sqlType = columnNameToTypeMap.get(columnName);
       if (sqlType == null) {
         if (isStrict) {
           throw new SQLExceptionInfo.Builder(SQLExceptionCode.COLUMN_NOT_FOUND)
               .setColumnName(columnName)
               .setTableName(tableName)
               .build()
               .buildException();
         }
         unfoundColumnCount++;
       } else {
         columnType[i] = new ColumnInfo(columnName, sqlType);
       }
     }
     if (unfoundColumnCount == columns.size()) {
       throw new SQLExceptionInfo.Builder(SQLExceptionCode.COLUMN_NOT_FOUND)
           .setColumnName(Arrays.toString(columns.toArray(new String[0])))
           .setTableName(tableName)
           .build()
           .buildException();
     }
   }
   return columnType;
 }