⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sourcegen.java.predonlyonchange

📁 把java对象映射成数据库表中的一条记录
💻 PREDONLYONCHANGE
📖 第 1 页 / 共 2 页
字号:
            return new JClass("java.sql.Timestamp");

        case Types.OTHER:
            return JClass.Object;
        }

    return null;
    }



  private String getDefaultValue(short type, boolean nullable)
    {

    switch (type)
        {
        case -9:
        case -10:
        case Types.LONGVARCHAR:
        case Types.CHAR:
        case Types.VARCHAR:
            if (nullable)
                {
                return "DEFAULT_TO_NULL";
                }
            else
                {
                return "DEFAULT_TO_EMPTY_STRING";
                }

        case Types.INTEGER:
            if (nullable)
                {

                return "DEFAULT_TO_NULL";
                }
            else
                {
                return "DEFAULT_TO_ZERO";
                }

        case Types.TIMESTAMP:

            if (nullable)
                {
                return "DEFAULT_TO_NULL";
                }
            else
                {
                return "DEFAULT_TO_NOW";
                }

        }

    return null;
    }



  private String getColumnSpecName(short type)
    {

    switch (type)
        {

        case -9:
        case -10:
        case Types.LONGVARCHAR:
        case Types.CHAR:
        case Types.VARCHAR:
            return "StringColumnSpec";

        case Types.BINARY:
        case Types.VARBINARY:
        case Types.LONGVARBINARY:
            return "ByteArrayColumnSpec";

        case Types.BIT:
            return "BooleanColumnSpec";

        case Types.TINYINT:
        case Types.SMALLINT:
            return "ShortColumnSpec";

        case Types.INTEGER:
            return "IntegerColumnSpec";

        case Types.BIGINT:
            return "LongColumnSpec";

        case Types.REAL:
            return "FloatColumnSpec";

        // JDBC Guide: Getting Started: 8 - Mapping SQL and Java Types
        // recommends FLOAT be represented as Java Double.
        case Types.FLOAT: //
        case Types.DOUBLE:
            return "DoubleColumnSpec";

        case Types.NUMERIC:
        case Types.DECIMAL:
            return "BigDecimalColumnSpec";

        case Types.DATE:
            return "SQLDateColumnSpec";

        case Types.TIME:
            return "SQLTimeColumnSpec";

        case Types.TIMESTAMP:
            return "TimestampColumnSpec";

        case Types.OTHER:
            return "OtherColumnSpec";

        case Types.NULL:
            return "NullColumnSpec";
        }
    return null;
    }

  /**
   * Convert a table name to a class name.
   *
   * @param tableName a value of type 'String'
   * @return a value of type 'String'
   */
  private String className(String tableName)
    {
    return this.javaName(tableName, CAPITALIZE);
    }


  /**
   * Convert a column name to a getter name.
   *
   * @param columnName a value of type 'String'
   * @return a value of type 'String'
   */
  private String getterName(String columnName)
    {
    return "get" + this.javaName(columnName, CAPITALIZE);
    }


  /**
   * Convert a column name to a setter name.
   *
   * @param columnName a value of type 'String'
   * @return a value of type 'String'
   */
  private String setterName(String columnName)
    {
    return "set" + this.javaName(columnName, CAPITALIZE);
    }


  /**
   * convert a column name to a field name.
   *
   * @param columnName a value of type 'String'
   * @return a value of type 'String'
   */
  private String fieldName(String columnName)
    {
    return "i_" + this.javaName(columnName, UNCAPITALIZE);
    }


  /**
   * Remove underscores and properly case things.
   *
   * @param sqlName a value of type 'String'
   * @param capitalize a value of type 'boolean' - if true, capitalize the
   *                   first letter.
   * @return a value of type 'String'
   */
  private String javaName(String sqlName, boolean capitalize)
    {
    String name = null;
    if (capitalize)
        {
        if (sqlName.length() == 1)
            {
            return sqlName.toUpperCase();
            }
        else
            {
            name = (sqlName.substring(0, 1).toUpperCase()
                    + sqlName.substring(1).toLowerCase());
            }
        }
    else
        {
        name = sqlName.toLowerCase();
        }
    int ix = name.indexOf('_');
    if (ix < 0)
        {
        return name;
        }
    else
        {
        // If it's the last character, just omit it
        if (ix == (name.length() - 1))
            {
            return name.substring(0,ix);
            }
        else
            {
            return name.substring(0,ix)
                + this.javaName(name.substring(ix+1),
                                CAPITALIZE);
            }
        }
    }




  public DatabaseMetaData getMetaData()
    {
    return i_metaData;
    }


  public void run()
          throws SQLException
    {
    // Create a list of table names
    String[] tableTypes =
        {"TABLE"};
    ResultSet tables =
            i_metaData.getTables(null, null, "%", tableTypes);

    List tableList = new ArrayList();
    while (tables.next())
        {
        String tableName = tables.getString("TABLE_NAME");
        tableList.add(tableName);
        }
    tables.close();

    // Loop through the tables and generate the files
    Iterator iterator = tableList.iterator();
    while (iterator.hasNext())
        {
        String tableName = (String) iterator.next();

        // Create a set of primary key column names.
        Set primaryKeys = new HashSet();
        ResultSet rs =
                i_metaData.getPrimaryKeys(null, null, tableName);
        while (rs.next())
            {
            primaryKeys.add(rs.getString("COLUMN_NAME"));
            }

        // Create a list of ColumnData objects
        rs = i_metaData.getColumns(null, null, tableName, "%");
        List columnList = new ArrayList();
        while (rs.next())
            {
            columnList.add(this.new ColumnData(rs, primaryKeys));
            }

        // Create the java files
        this.generatePersistentObjectClass(tableName, columnList);
        this.generateDomainClass(tableName, columnList);

        } // while (iterator.hasNext())
    } // run()


  public static void main(String[] args)
          throws SQLException
    {

    try
        {
        SourceGen jrfsg =
                new SourceGen(
                    JRFProperties.getProperty("SourceGen.outputdir"),
                    JRFProperties.getProperty("SourceGen.package"),
                    JRFProperties.getProperty("databasePolicy"));

        jrfsg.run();
        }
    catch (SQLException e)
        {
        LOG.error("SQLException occurred in SourceGen", e);
        throw e;
        }

    System.exit(0);
    }


  /**
   *  This is a private inner class that is used to hold column data.
   */
  private class ColumnData
    {

    String  colName = null;
    short   colType = 0;
    int     colSize = 0;
    boolean isNullable = true;
    int     decimalDigits = 0;
    String  colSpecName = null;
    boolean isPrimaryKey = false;

    public ColumnData(ResultSet rs, Set primaryKeys)
            throws SQLException
      {
      this.colName = rs.getString("COLUMN_NAME");
      this.colType = rs.getShort("DATA_TYPE");
      this.colSize = rs.getInt("COLUMN_SIZE");
      this.colSpecName = getColumnSpecName(colType);
      String nullable = rs.getString("IS_NULLABLE");
      if (nullable == null ||
          nullable.trim().equalsIgnoreCase("NO"))
          {
          this.isNullable = false;
          }
      this.decimalDigits = rs.getInt("DECIMAL_DIGITS");
      this.isPrimaryKey = primaryKeys.contains(colName);
      }

    } // ColumnData


  } // SourceGen


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -