/*
|
* BaseLink - Generic object relational mapping
|
* Copyright (C) 2011-2020 Ulrich Hilger, http://uhilger.de
|
*
|
* This program is free software: you can redistribute it and/or modify
|
* it under the terms of the GNU General Public License as published by
|
* the Free Software Foundation, either version 3 of the License, or
|
* (at your option) any later version.
|
*
|
* This program is distributed in the hope that it will be useful,
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* GNU General Public License for more details.
|
*
|
* You should have received a copy of the GNU General Public License
|
* along with this program. If not, see http://www.gnu.org/licenses/
|
*/
|
|
package de.uhilger.baselink;
|
|
import java.util.*;
|
import java.util.logging.*;
|
import java.sql.*;
|
|
/**
|
* Utility methods for the BaseLink package
|
*
|
* @author Copyright (c) Ulrich Hilger, <a href="http://uhilger.de">http://uhilger.de</a>
|
* @author Published under the terms and conditions of
|
* the <a href="http://www.gnu.org/licenses/" target="_blank">GNU General Public License</a>
|
* @version 1, October 31, 2014
|
*/
|
public class Util {
|
|
private static final Logger logger = Logger.getLogger(Util.class.getName());
|
|
public static final boolean DO_INCLUDE_BLOBS = true;
|
public static final boolean DONT_INCLUDE_BLOBS = false;
|
|
/**
|
* Generate a database transfer object (DTO) for a table
|
*
|
* @param pm an object to be used for database access
|
* @param schemaName name of database schema that contains the table
|
* @param tableName name of table to create DAO for
|
* @return java source code of DTO
|
*/
|
public String generateDTO(PersistenceManager pm, String schemaName, String tableName) {
|
String indentation = " ";
|
//String indent = "";
|
StringBuilder code = new StringBuilder();
|
Connection c = null;
|
try {
|
c = pm.getConnection();
|
code.append("@DBTable(name=\"");
|
code.append(schemaName.toLowerCase());
|
code.append(".");
|
code.append(tableName.toLowerCase());
|
code.append("\")");
|
code.append(System.lineSeparator());
|
code.append("@DBPrimaryKey({\"");
|
List<List<String>> keys = getPrimaryKeys(pm, c, schemaName, tableName);
|
String colName = null;
|
for(int row = 1; row < keys.size(); row++) {
|
if(row > 1) {
|
code.append(",\"");
|
}
|
colName = keys.get(row).get(3).toLowerCase();
|
code.append(colName);
|
code.append("\"");
|
}
|
code.append("})");
|
code.append(System.lineSeparator());
|
code.append("public class ");
|
code.append(tableName.toLowerCase());
|
code.append(" {");
|
code.append(System.lineSeparator());
|
|
/*
|
columns.size(): Anzahl Zeilen
|
columns.get(0).size(): Anzahl Felder
|
Column Name: columns.get(1).get(3)
|
Datentyp: columns.get(1).get(4)
|
*/
|
List<List<String>> columns = getColumns(pm, c, schemaName, tableName);
|
String fieldname = null;
|
String typename = null;
|
int type = -1;
|
|
// class members
|
for(int row = 1; row < columns.size(); row++) {
|
type = Integer.parseInt(columns.get(row).get(4));
|
fieldname = columns.get(row).get(3).toLowerCase();
|
typename = getTypeName(type);
|
code.append(indentation);
|
code.append("private ");
|
code.append(typename);
|
code.append(" ");
|
code.append(fieldname);
|
code.append(";");
|
code.append(System.lineSeparator());
|
}
|
|
// setters and getters
|
for(int row = 1; row < columns.size(); row++) {
|
type = Integer.parseInt(columns.get(row).get(4));
|
fieldname = columns.get(row).get(3).toLowerCase();
|
typename = getTypeName(type);
|
code.append(System.lineSeparator());
|
code.append(System.lineSeparator());
|
code.append(indentation);
|
code.append("public void set");
|
code.append(fieldname);
|
code.append("(");
|
code.append(typename);
|
code.append(" wert) {");
|
code.append(System.lineSeparator());
|
code.append(indentation);
|
code.append(indentation);
|
code.append(fieldname);
|
code.append(" = wert;");
|
code.append(System.lineSeparator());
|
code.append(indentation);
|
code.append("}");
|
code.append(System.lineSeparator());
|
code.append(System.lineSeparator());
|
code.append(indentation);
|
// @DBColumn(name="dc_content", type=DBColumn.Type.BLOB)
|
code.append("@DBColumn(name = \"");
|
code.append(fieldname);
|
if(type == java.sql.Types.BLOB) {
|
code.append("\", type=DBColumn.Type.BLOB)");
|
} else {
|
code.append("\")");
|
}
|
code.append(System.lineSeparator());
|
code.append(indentation);
|
code.append("public ");
|
code.append(typename);
|
code.append(" get");
|
code.append(fieldname);
|
code.append("() {");
|
code.append(System.lineSeparator());
|
code.append(indentation);
|
code.append(indentation);
|
code.append("return ");
|
code.append(fieldname);
|
code.append(";");
|
code.append(System.lineSeparator());
|
code.append(indentation);
|
code.append("}");
|
}
|
|
code.append(System.lineSeparator());
|
code.append("}");
|
c.close();
|
c = null;
|
} catch (Exception ex) {
|
logger.log(Level.SEVERE, ex.getMessage(), ex);
|
} finally {
|
pm.closeConnectionFinally(c);
|
}
|
return code.toString();
|
}
|
|
/**
|
* Generate a database transfer object (DTO) for a table
|
*
|
* This method is kept for compatibility reasons, it maps to
|
* <code>generateDTO</code>.
|
*
|
* @param pm an object to be used for database access
|
* @param schemaName name of database schema that contains the table
|
* @param tableName name of table to create DAO for
|
* @return java source code of DTO
|
*/
|
public String generateDAO(PersistenceManager pm, String schemaName, String tableName) {
|
return generateDTO(pm, schemaName, tableName);
|
}
|
|
/**
|
* Get a description of the columns of a database table
|
*
|
* @param pm an object to be used for database access
|
* @param c the database connection that has been opened for this action
|
* @param schemaName name of database schema that contains the table
|
* @param tableName name of table to get column descriptions for
|
* @return column description as returned by java.sql.DatabaseMetaData,
|
* transformed to a List of rows, each row being a List of Strings, each
|
* String being a field of the column description for respective table
|
* column
|
*/
|
public List<List<String>> getColumns(PersistenceManager pm, Connection c, String schemaName, String tableName) {
|
List<List<String>> list = null;
|
ResultSet rs = null;
|
try {
|
DatabaseMetaData meta = c.getMetaData();
|
rs = meta.getColumns(null, schemaName, tableName, null);
|
list = pm.toList(rs, DONT_INCLUDE_BLOBS);
|
rs.close();
|
rs = null;
|
} catch (Exception ex) {
|
logger.log(Level.SEVERE, ex.getMessage(), ex);
|
} finally {
|
pm.closeResultSetFinally(rs);
|
}
|
return list;
|
}
|
|
/**
|
* Get a description of the primary keys of a database table
|
*
|
* @param pm an object to be used for database access
|
* @param c the database connection that has been opened for this action
|
* @param schemaName name of database schema that contains the table
|
* @param tableName name of table to get primary key descriptions for
|
* @return primary key description as returned by java.sql.DatabaseMetaData,
|
* transformed to a List of rows, each row being a List of Strings, each
|
* String being a field of the primary key description for respective table
|
*/
|
public List<List<String>> getPrimaryKeys(PersistenceManager pm, Connection c, String schemaName, String tableName) {
|
List<List<String>> list = null;
|
ResultSet rs = null;
|
try {
|
DatabaseMetaData meta = c.getMetaData();
|
rs = meta.getPrimaryKeys(null, schemaName, tableName);
|
list = pm.toList(rs, DONT_INCLUDE_BLOBS);
|
rs.close();
|
rs = null;
|
} catch (Exception ex) {
|
logger.log(Level.SEVERE, ex.getMessage(), ex);
|
} finally {
|
pm.closeResultSetFinally(rs);
|
}
|
return list;
|
}
|
|
|
/**
|
* Convert a java.sql.Type to a Java type name
|
* @param type SQL type to be converted
|
* @return Java type name, e.g. String oder int;
|
* if the type can not be converted without an
|
* individually developed conversion 'tbd' is
|
* returned with the SQL type name included, e.g.
|
* tbd_CLOB, tbd_DISTINCT, etc.
|
*/
|
private String getTypeName(int type) {
|
String typename = null;
|
switch(type) {
|
case java.sql.Types.ARRAY:
|
typename = "tbd_ARRAY";
|
break;
|
case java.sql.Types.BIGINT:
|
typename = "long";
|
break;
|
case java.sql.Types.BINARY:
|
typename = "byte[]";
|
break;
|
case java.sql.Types.BIT:
|
typename = "tbd_BIT";
|
break;
|
case java.sql.Types.BLOB:
|
typename = "String";
|
break;
|
case java.sql.Types.BOOLEAN:
|
typename = "boolean";
|
break;
|
case java.sql.Types.CHAR:
|
typename = "String";
|
break;
|
case java.sql.Types.CLOB:
|
typename = "tbd_CLOB";
|
break;
|
case java.sql.Types.DATALINK:
|
typename = "tbd_DATALINK";
|
break;
|
case java.sql.Types.DATE:
|
typename = "java.sql.Date";
|
break;
|
case java.sql.Types.DECIMAL:
|
typename = "float";
|
break;
|
case java.sql.Types.DISTINCT:
|
typename = "tbd_DISTINCT";
|
break;
|
case java.sql.Types.DOUBLE:
|
typename = "double";
|
break;
|
case java.sql.Types.FLOAT:
|
typename = "float";
|
break;
|
case java.sql.Types.INTEGER:
|
typename = "int";
|
break;
|
case java.sql.Types.JAVA_OBJECT:
|
typename = "Object";
|
break;
|
case java.sql.Types.LONGVARCHAR:
|
typename = "String";
|
break;
|
case java.sql.Types.NCHAR:
|
typename = "String";
|
break;
|
case java.sql.Types.NCLOB:
|
typename = "tbd_NCLOB";
|
break;
|
case java.sql.Types.NULL:
|
typename = "tbd_NULL";
|
break;
|
case java.sql.Types.NUMERIC:
|
typename = "float";
|
break;
|
case java.sql.Types.NVARCHAR:
|
typename = "String";
|
break;
|
case java.sql.Types.OTHER:
|
typename = "tbd_OTHER";
|
break;
|
case java.sql.Types.REAL:
|
typename = "float";
|
break;
|
case java.sql.Types.REF:
|
typename ="tbd_REF";
|
break;
|
case java.sql.Types.ROWID:
|
typename = "tbd_ROWID";
|
break;
|
case java.sql.Types.SMALLINT:
|
typename = "integer";
|
break;
|
case java.sql.Types.SQLXML:
|
typename = "tbd_SQLXML";
|
break;
|
case java.sql.Types.STRUCT:
|
typename = "tbd_STRUCT";
|
break;
|
case java.sql.Types.TIME:
|
typename = "tbd_TIME";
|
break;
|
case java.sql.Types.TIMESTAMP:
|
typename = "java.sql.Timestamp";
|
break;
|
case java.sql.Types.TINYINT:
|
typename = "integer";
|
break;
|
case java.sql.Types.VARBINARY:
|
typename = "tbd_VARBINARY";
|
break;
|
case java.sql.Types.VARCHAR:
|
typename = "String";
|
break;
|
default:
|
typename = "tbd_UNDEFINED";
|
break;
|
}
|
return typename;
|
}
|
}
|