public static void printAllTableNames(List<Table> tables) throws Exception {
   GLogger.println("\n----All TableNames BEGIN----");
   for (int i = 0; i < tables.size(); i++) {
     String sqlName = ((Table) tables.get(i)).getSqlName();
     //				GLogger.println("g.generateTable(\"" + sqlName + "\");");
     GLogger.println("for maven.please use [mvn rapid:gen -Dtable=" + sqlName + "]");
   }
   GLogger.println("----All TableNames END----");
 }
 private static NodeData getTableConfigXmlNodeData0(String tableSqlName) {
   try {
     File file =
         FileHelper.getFileByClassLoader("generator_config/table/" + tableSqlName + ".xml");
     GLogger.trace("getTableConfigXml() load nodeData by tableSqlName:" + tableSqlName + ".xml");
     return new XMLHelper().parseXML(file);
   } catch (Exception e) { // ignore
     GLogger.trace("not found config xml for table:" + tableSqlName + ", exception:" + e);
     return null;
   }
 }
 public static void reload() {
   try {
     GLogger.println(
         "Start Load GeneratorPropeties from classpath:" + Arrays.toString(PROPERTIES_FILE_NAMES));
     Properties p = new Properties();
     String[] loadedFiles =
         PropertiesHelper.loadAllPropertiesFromClassLoader(p, PROPERTIES_FILE_NAMES);
     GLogger.println("GeneratorPropeties Load Success,files:" + Arrays.toString(loadedFiles));
     setProperties(p);
   } catch (IOException e) {
     throw new RuntimeException("Load " + PROPERTIES_FILE_NAMES + " error", e);
   }
 }
  public static void setProperties(Properties inputProps) {
    props = new PropertiesHelper(resolveProperties(inputProps));
    for (Iterator it = props.entrySet().iterator(); it.hasNext(); ) {
      Map.Entry entry = (Map.Entry) it.next();
      GLogger.println("[Property] " + entry.getKey() + "=" + entry.getValue());
    }
    GLogger.println("");

    GLogger.println(
        "[Auto Replace] [.] => [/] on generator.properties, key=source_key+'_dir', For example: pkg=com.company ==> pkg_dir=com/company  \n");
    Properties dirProperties = autoReplacePropertiesValue2DirValue(props.getProperties());
    props.getProperties().putAll(dirProperties);
  }
Exemple #5
0
 public static void debug(String string, Map templateModel) {
   if (logLevel <= DEBUG) {
     GLogger.println(string);
     if (templateModel == null) return;
     for (Object key : templateModel.keySet()) {
       if (System.getProperties().containsKey(key)
           || GeneratorProperties.getProperties().containsKey(key)) {
         continue;
       }
       if (key.toString().endsWith("_dir")) {
         continue;
       }
       GLogger.println(key + " = " + templateModel.get(key));
     }
   }
 }
 private String getSynonymOwner(String synonymName) {
   PreparedStatement ps = null;
   ResultSet rs = null;
   String ret = null;
   try {
     ps =
         getConnection()
             .prepareStatement(
                 "select table_owner from sys.all_synonyms where table_name=? and owner=?");
     ps.setString(1, synonymName);
     ps.setString(2, getSchema());
     rs = ps.executeQuery();
     if (rs.next()) {
       ret = rs.getString(1);
     } else {
       String databaseStructure = getDatabaseStructureInfo();
       throw new RuntimeException(
           "Wow! Synonym " + synonymName + " not found. How can it happen? " + databaseStructure);
     }
   } catch (SQLException e) {
     String databaseStructure = getDatabaseStructureInfo();
     GLogger.error(e.getMessage(), e);
     throw new RuntimeException("Exception in getting synonym owner " + databaseStructure);
   } finally {
     dbHelper.close(rs, ps);
   }
   return ret;
 }
 public static void setProperty(String key, String value) {
   value = resolveProperty(value, getProperties());
   key = resolveProperty(key, getProperties());
   GLogger.println("[setProperty()] " + key + "=" + value);
   getHelper().setProperty(key, value);
   String dir_value = value.toString().replace('.', '/');
   getHelper().getProperties().put(key + "_dir", dir_value);
 }
 /** 得到模板可以引用的工具类 */
 private static Map getToolsMap() {
   Map toolsMap = new HashMap();
   String[] tools = GeneratorProperties.getStringArray(GENERATOR_TOOLS_CLASS);
   for (String className : tools) {
     try {
       Object instance = ClassHelper.newInstance(className);
       toolsMap.put(Class.forName(className).getSimpleName(), instance);
       GLogger.debug(
           "put tools class:"
               + className
               + " with key:"
               + Class.forName(className).getSimpleName());
     } catch (Exception e) {
       GLogger.error("cannot load tools by className:" + className + " cause:" + e);
     }
   }
   return toolsMap;
 }
  private List getTableColumns(
      Table table, List primaryKeys, List indices, Map uniqueIndices, Map uniqueColumns)
      throws SQLException {
    // get the columns
    List columns = new LinkedList();
    ResultSet columnRs = getColumnsResultSet(table);

    while (columnRs.next()) {
      int sqlType = columnRs.getInt("DATA_TYPE");
      String sqlTypeName = columnRs.getString("TYPE_NAME");
      String columnName = columnRs.getString("COLUMN_NAME");
      String columnDefaultValue = columnRs.getString("COLUMN_DEF");

      String remarks = columnRs.getString("REMARKS");
      if (remarks == null && dbHelper.isOracleDataBase()) {
        remarks = getOracleColumnComments(table.getSqlName(), columnName);
      }

      // if columnNoNulls or columnNullableUnknown assume "not nullable"
      boolean isNullable = (DatabaseMetaData.columnNullable == columnRs.getInt("NULLABLE"));
      int size = columnRs.getInt("COLUMN_SIZE");
      int decimalDigits = columnRs.getInt("DECIMAL_DIGITS");

      boolean isPk = primaryKeys.contains(columnName);
      boolean isIndexed = indices.contains(columnName);
      String uniqueIndex = (String) uniqueIndices.get(columnName);
      List columnsInUniqueIndex = null;
      if (uniqueIndex != null) {
        columnsInUniqueIndex = (List) uniqueColumns.get(uniqueIndex);
      }

      boolean isUnique = columnsInUniqueIndex != null && columnsInUniqueIndex.size() == 1;
      if (isUnique) {
        GLogger.trace("unique column:" + columnName);
      }
      Column column =
          new Column(
              table,
              sqlType,
              sqlTypeName,
              columnName,
              size,
              decimalDigits,
              isPk,
              isNullable,
              isIndexed,
              isUnique,
              columnDefaultValue,
              remarks);
      BeanHelper.copyProperties(
          column, TableOverrideValuesProvider.getColumnOverrideValues(table, column));
      columns.add(column);
    }
    columnRs.close();
    return columns;
  }
Exemple #10
0
 private static Properties loadLog4jProperties() {
   try {
     File file = FileHelper.getFileByClassLoader("log4j.properties");
     Properties props = new Properties();
     FileInputStream in = new FileInputStream(file);
     try {
       props.load(in);
     } finally {
       IOHelper.close(in, null);
     }
     return props;
   } catch (FileNotFoundException e) {
     GLogger.warn("not found log4j.properties, cause:" + e);
     return new Properties();
   } catch (IOException e) {
     GLogger.warn("load log4j.properties occer error, cause:" + e);
     return new Properties();
   }
 }
  private String getDatabaseStructureInfo() {
    ResultSet schemaRs = null;
    ResultSet catalogRs = null;
    String nl = System.getProperty("line.separator");
    StringBuffer sb = new StringBuffer(nl);
    // Let's give the user some feedback. The exception
    // is probably related to incorrect schema configuration.
    sb.append("Configured schema:").append(getSchema()).append(nl);
    sb.append("Configured catalog:").append(getCatalog()).append(nl);

    try {
      schemaRs = getMetaData().getSchemas();
      sb.append("Available schemas:").append(nl);
      while (schemaRs.next()) {
        sb.append("  ").append(schemaRs.getString("TABLE_SCHEM")).append(nl);
      }
    } catch (SQLException e2) {
      GLogger.warn("Couldn't get schemas", e2);
      sb.append("  ?? Couldn't get schemas ??").append(nl);
    } finally {
      dbHelper.close(schemaRs, null);
    }

    try {
      catalogRs = getMetaData().getCatalogs();
      sb.append("Available catalogs:").append(nl);
      while (catalogRs.next()) {
        sb.append("  ").append(catalogRs.getString("TABLE_CAT")).append(nl);
      }
    } catch (SQLException e2) {
      GLogger.warn("Couldn't get catalogs", e2);
      sb.append("  ?? Couldn't get catalogs ??").append(nl);
    } finally {
      dbHelper.close(catalogRs, null);
    }
    return sb.toString();
  }
 private List<String> getTablePrimaryKeys(Table table) throws SQLException {
   // get the primary keys
   List primaryKeys = new LinkedList();
   ResultSet primaryKeyRs = null;
   if (table.getOwnerSynonymName() != null) {
     primaryKeyRs =
         getMetaData()
             .getPrimaryKeys(getCatalog(), table.getOwnerSynonymName(), table.getSqlName());
   } else {
     primaryKeyRs = getMetaData().getPrimaryKeys(getCatalog(), getSchema(), table.getSqlName());
   }
   while (primaryKeyRs.next()) {
     String columnName = primaryKeyRs.getString("COLUMN_NAME");
     GLogger.trace("primary key:" + columnName);
     primaryKeys.add(columnName);
   }
   primaryKeyRs.close();
   return primaryKeys;
 }
 private static void printBeginProcess(String displayText, boolean isDatele) {
   GLogger.println("***************************************************************");
   GLogger.println("* BEGIN " + (isDatele ? " delete by " : " generate by ") + displayText);
   GLogger.println("***************************************************************");
 }
  private void retriveTableColumns(Table table) throws SQLException {
    GLogger.trace("-------setColumns(" + table.getSqlName() + ")");

    List primaryKeys = getTablePrimaryKeys(table);
    table.setPrimaryKeyColumns(primaryKeys);

    // get the indices and unique columns
    List indices = new LinkedList();
    // maps index names to a list of columns in the index
    Map uniqueIndices = new HashMap();
    // maps column names to the index name.
    Map uniqueColumns = new HashMap();
    ResultSet indexRs = null;

    try {

      if (table.getOwnerSynonymName() != null) {
        indexRs =
            getMetaData()
                .getIndexInfo(
                    getCatalog(), table.getOwnerSynonymName(), table.getSqlName(), false, true);
      } else {
        indexRs =
            getMetaData().getIndexInfo(getCatalog(), getSchema(), table.getSqlName(), false, true);
      }
      while (indexRs.next()) {
        String columnName = indexRs.getString("COLUMN_NAME");
        if (columnName != null) {
          GLogger.trace("index:" + columnName);
          indices.add(columnName);
        }

        // now look for unique columns
        String indexName = indexRs.getString("INDEX_NAME");
        boolean nonUnique = indexRs.getBoolean("NON_UNIQUE");

        if (!nonUnique && columnName != null && indexName != null) {
          List l = (List) uniqueColumns.get(indexName);
          if (l == null) {
            l = new ArrayList();
            uniqueColumns.put(indexName, l);
          }
          l.add(columnName);
          uniqueIndices.put(columnName, indexName);
          GLogger.trace("unique:" + columnName + " (" + indexName + ")");
        }
      }
    } catch (Throwable t) {
      // Bug #604761 Oracle getIndexInfo() needs major grants
      // http://sourceforge.net/tracker/index.php?func=detail&aid=604761&group_id=36044&atid=415990
    } finally {
      dbHelper.close(indexRs, null);
    }

    List columns = getTableColumns(table, primaryKeys, indices, uniqueIndices, uniqueColumns);

    for (Iterator i = columns.iterator(); i.hasNext(); ) {
      Column column = (Column) i.next();
      table.addColumn(column);
    }

    // In case none of the columns were primary keys, issue a warning.
    if (primaryKeys.size() == 0) {
      GLogger.warn(
          "WARNING: The JDBC driver didn't report any primary key columns in "
              + table.getSqlName());
    }
  }