use of org.mybatis.generator.api.IntrospectedColumn in project generator by mybatis.
the class DeleteByPrimaryKeyMethodGenerator method addImplementationElements.
@Override
public void addImplementationElements(TopLevelClass topLevelClass) {
Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
Method method = getMethodShell(importedTypes);
StringBuilder sb = new StringBuilder();
if (!introspectedTable.getRules().generatePrimaryKeyClass()) {
// no primary key class, but primary key is enabled. Primary
// key columns must be in the base class.
FullyQualifiedJavaType keyType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
topLevelClass.addImportedType(keyType);
sb.setLength(0);
sb.append(keyType.getShortName());
//$NON-NLS-1$
sb.append(" _key = new ");
sb.append(keyType.getShortName());
//$NON-NLS-1$
sb.append("();");
method.addBodyLine(sb.toString());
for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {
sb.setLength(0);
//$NON-NLS-1$
sb.append("_key.");
sb.append(getSetterMethodName(introspectedColumn.getJavaProperty()));
sb.append('(');
sb.append(introspectedColumn.getJavaProperty());
//$NON-NLS-1$
sb.append(");");
method.addBodyLine(sb.toString());
}
}
sb.setLength(0);
//$NON-NLS-1$
sb.append("int rows = ");
sb.append(daoTemplate.getDeleteMethod(introspectedTable.getIbatis2SqlMapNamespace(), introspectedTable.getDeleteByPrimaryKeyStatementId(), //$NON-NLS-1$
"_key"));
method.addBodyLine(sb.toString());
//$NON-NLS-1$
method.addBodyLine("return rows;");
if (context.getPlugins().clientDeleteByPrimaryKeyMethodGenerated(method, topLevelClass, introspectedTable)) {
topLevelClass.addImportedTypes(importedTypes);
topLevelClass.addMethod(method);
}
}
use of org.mybatis.generator.api.IntrospectedColumn in project generator by mybatis.
the class DatabaseIntrospector method calculateIdentityColumns.
/**
* Calculate identity columns.
*
* @param tc
* the tc
* @param columns
* the columns
*/
private void calculateIdentityColumns(TableConfiguration tc, Map<ActualTableName, List<IntrospectedColumn>> columns) {
GeneratedKey gk = tc.getGeneratedKey();
if (gk == null) {
// no generated key, then no identity or sequence columns
return;
}
for (Map.Entry<ActualTableName, List<IntrospectedColumn>> entry : columns.entrySet()) {
for (IntrospectedColumn introspectedColumn : entry.getValue()) {
if (isMatchedColumn(introspectedColumn, gk)) {
if (gk.isIdentity() || gk.isJdbcStandard()) {
introspectedColumn.setIdentity(true);
introspectedColumn.setSequenceColumn(false);
} else {
introspectedColumn.setIdentity(false);
introspectedColumn.setSequenceColumn(true);
}
}
}
}
}
use of org.mybatis.generator.api.IntrospectedColumn in project generator by mybatis.
the class DatabaseIntrospector method applyColumnOverrides.
/**
* Apply column overrides.
*
* @param tc
* the tc
* @param columns
* the columns
*/
private void applyColumnOverrides(TableConfiguration tc, Map<ActualTableName, List<IntrospectedColumn>> columns) {
for (Map.Entry<ActualTableName, List<IntrospectedColumn>> entry : columns.entrySet()) {
for (IntrospectedColumn introspectedColumn : entry.getValue()) {
ColumnOverride columnOverride = tc.getColumnOverride(introspectedColumn.getActualColumnName());
if (columnOverride != null) {
if (logger.isDebugEnabled()) {
logger.debug(getString(//$NON-NLS-1$
"Tracing.4", introspectedColumn.getActualColumnName(), entry.getKey().toString()));
}
if (stringHasValue(columnOverride.getJavaProperty())) {
introspectedColumn.setJavaProperty(columnOverride.getJavaProperty());
}
if (stringHasValue(columnOverride.getJavaType())) {
introspectedColumn.setFullyQualifiedJavaType(new FullyQualifiedJavaType(columnOverride.getJavaType()));
}
if (stringHasValue(columnOverride.getJdbcType())) {
introspectedColumn.setJdbcTypeName(columnOverride.getJdbcType());
}
if (stringHasValue(columnOverride.getTypeHandler())) {
introspectedColumn.setTypeHandler(columnOverride.getTypeHandler());
}
if (columnOverride.isColumnNameDelimited()) {
introspectedColumn.setColumnNameDelimited(true);
}
introspectedColumn.setGeneratedAlways(columnOverride.isGeneratedAlways());
introspectedColumn.setProperties(columnOverride.getProperties());
}
}
}
}
use of org.mybatis.generator.api.IntrospectedColumn in project generator by mybatis.
the class DatabaseIntrospector method getColumns.
/**
* This method returns a Map<ActualTableName, List<ColumnDefinitions>> of columns returned from the database
* introspection.
*
* @param tc
* the tc
* @return introspected columns
* @throws SQLException
* the SQL exception
*/
private Map<ActualTableName, List<IntrospectedColumn>> getColumns(TableConfiguration tc) throws SQLException {
String localCatalog;
String localSchema;
String localTableName;
boolean delimitIdentifiers = tc.isDelimitIdentifiers() || stringContainsSpace(tc.getCatalog()) || stringContainsSpace(tc.getSchema()) || stringContainsSpace(tc.getTableName());
if (delimitIdentifiers) {
localCatalog = tc.getCatalog();
localSchema = tc.getSchema();
localTableName = tc.getTableName();
} else if (databaseMetaData.storesLowerCaseIdentifiers()) {
localCatalog = tc.getCatalog() == null ? null : tc.getCatalog().toLowerCase();
localSchema = tc.getSchema() == null ? null : tc.getSchema().toLowerCase();
localTableName = tc.getTableName() == null ? null : tc.getTableName().toLowerCase();
} else if (databaseMetaData.storesUpperCaseIdentifiers()) {
localCatalog = tc.getCatalog() == null ? null : tc.getCatalog().toUpperCase();
localSchema = tc.getSchema() == null ? null : tc.getSchema().toUpperCase();
localTableName = tc.getTableName() == null ? null : tc.getTableName().toUpperCase();
} else {
localCatalog = tc.getCatalog();
localSchema = tc.getSchema();
localTableName = tc.getTableName();
}
if (tc.isWildcardEscapingEnabled()) {
String escapeString = databaseMetaData.getSearchStringEscape();
StringBuilder sb = new StringBuilder();
StringTokenizer st;
if (localSchema != null) {
//$NON-NLS-1$
st = new StringTokenizer(localSchema, "_%", true);
while (st.hasMoreTokens()) {
String token = st.nextToken();
if (//$NON-NLS-1$
token.equals("_") || token.equals("%")) {
//$NON-NLS-1$
sb.append(escapeString);
}
sb.append(token);
}
localSchema = sb.toString();
}
sb.setLength(0);
//$NON-NLS-1$
st = new StringTokenizer(localTableName, "_%", true);
while (st.hasMoreTokens()) {
String token = st.nextToken();
if (//$NON-NLS-1$
token.equals("_") || token.equals("%")) {
//$NON-NLS-1$
sb.append(escapeString);
}
sb.append(token);
}
localTableName = sb.toString();
}
Map<ActualTableName, List<IntrospectedColumn>> answer = new HashMap<ActualTableName, List<IntrospectedColumn>>();
if (logger.isDebugEnabled()) {
String fullTableName = composeFullyQualifiedTableName(localCatalog, localSchema, localTableName, '.');
//$NON-NLS-1$
logger.debug(getString("Tracing.1", fullTableName));
}
ResultSet rs = databaseMetaData.getColumns(localCatalog, localSchema, localTableName, //$NON-NLS-1$
"%");
boolean supportsIsAutoIncrement = false;
boolean supportsIsGeneratedColumn = false;
ResultSetMetaData rsmd = rs.getMetaData();
int colCount = rsmd.getColumnCount();
for (int i = 1; i <= colCount; i++) {
if ("IS_AUTOINCREMENT".equals(rsmd.getColumnName(i))) {
//$NON-NLS-1$
supportsIsAutoIncrement = true;
}
if ("IS_GENERATEDCOLUMN".equals(rsmd.getColumnName(i))) {
//$NON-NLS-1$
supportsIsGeneratedColumn = true;
}
}
while (rs.next()) {
IntrospectedColumn introspectedColumn = ObjectFactory.createIntrospectedColumn(context);
introspectedColumn.setTableAlias(tc.getAlias());
//$NON-NLS-1$
introspectedColumn.setJdbcType(rs.getInt("DATA_TYPE"));
//$NON-NLS-1$
introspectedColumn.setLength(rs.getInt("COLUMN_SIZE"));
//$NON-NLS-1$
introspectedColumn.setActualColumnName(rs.getString("COLUMN_NAME"));
introspectedColumn.setNullable(//$NON-NLS-1$
rs.getInt("NULLABLE") == DatabaseMetaData.columnNullable);
//$NON-NLS-1$
introspectedColumn.setScale(rs.getInt("DECIMAL_DIGITS"));
//$NON-NLS-1$
introspectedColumn.setRemarks(rs.getString("REMARKS"));
//$NON-NLS-1$
introspectedColumn.setDefaultValue(rs.getString("COLUMN_DEF"));
if (supportsIsAutoIncrement) {
//$NON-NLS-1$ //$NON-NLS-2$
introspectedColumn.setAutoIncrement("YES".equals(rs.getString("IS_AUTOINCREMENT")));
}
if (supportsIsGeneratedColumn) {
//$NON-NLS-1$ //$NON-NLS-2$
introspectedColumn.setGeneratedColumn("YES".equals(rs.getString("IS_GENERATEDCOLUMN")));
}
ActualTableName atn = new ActualTableName(//$NON-NLS-1$
rs.getString("TABLE_CAT"), //$NON-NLS-1$
rs.getString("TABLE_SCHEM"), //$NON-NLS-1$
rs.getString("TABLE_NAME"));
List<IntrospectedColumn> columns = answer.get(atn);
if (columns == null) {
columns = new ArrayList<IntrospectedColumn>();
answer.put(atn, columns);
}
columns.add(introspectedColumn);
if (logger.isDebugEnabled()) {
logger.debug(getString(//$NON-NLS-1$
"Tracing.2", introspectedColumn.getActualColumnName(), Integer.toString(introspectedColumn.getJdbcType()), atn.toString()));
}
}
closeResultSet(rs);
if (answer.size() > 1 && !stringContainsSQLWildcard(localSchema) && !stringContainsSQLWildcard(localTableName)) {
// issue a warning if there is more than one table and
// no wildcards were used
ActualTableName inputAtn = new ActualTableName(tc.getCatalog(), tc.getSchema(), tc.getTableName());
StringBuilder sb = new StringBuilder();
boolean comma = false;
for (ActualTableName atn : answer.keySet()) {
if (comma) {
sb.append(',');
} else {
comma = true;
}
sb.append(atn.toString());
}
warnings.add(getString(//$NON-NLS-1$
"Warning.25", inputAtn.toString(), sb.toString()));
}
return answer;
}
use of org.mybatis.generator.api.IntrospectedColumn in project generator by mybatis.
the class InsertMethodGenerator method getMethodShell.
private Method getMethodShell(Set<FullyQualifiedJavaType> importedTypes) {
Method method = new Method();
FullyQualifiedJavaType returnType;
if (introspectedTable.getGeneratedKey() != null) {
IntrospectedColumn introspectedColumn = introspectedTable.getColumn(introspectedTable.getGeneratedKey().getColumn());
if (introspectedColumn == null) {
// the specified column doesn't exist, so don't do the generated
// key
// (the warning has already been reported)
returnType = null;
} else {
returnType = introspectedColumn.getFullyQualifiedJavaType();
importedTypes.add(returnType);
}
} else {
returnType = null;
}
method.setReturnType(returnType);
method.setVisibility(JavaVisibility.PUBLIC);
DAOMethodNameCalculator methodNameCalculator = getDAOMethodNameCalculator();
method.setName(methodNameCalculator.getInsertMethodName(introspectedTable));
FullyQualifiedJavaType parameterType = introspectedTable.getRules().calculateAllFieldsClass();
importedTypes.add(parameterType);
//$NON-NLS-1$
method.addParameter(new Parameter(parameterType, "record"));
for (FullyQualifiedJavaType fqjt : daoTemplate.getCheckedExceptions()) {
method.addException(fqjt);
importedTypes.add(fqjt);
}
context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
return method;
}
Aggregations