dao-generator/server/rowMapper.js

61 lines
2.1 KiB
JavaScript

const additionalTypeMapping = {
'LocalDate': 'Date',
'LocalTime': 'Time',
'LocalDateTime': 'Timestamp',
'BigDecimal': 'BigDecimal',
'BigInteger': 'BigInteger'
};
function capitalizeFirstLetter( string ) {
return string.charAt( 0 ).toUpperCase() + string.slice( 1 );
}
function lowerFirstLetter( string ) {
return string.charAt( 0 ).toLowerCase() + string.slice( 1 );
}
function isDateTimeAttribute( attributeType ) {
return ['LocalDate', 'LocalTime', 'LocalDateTime'].includes( attributeType );
}
function preparePojoSetters( mapping ) {
let str = '';
let i = 0;
let attrCount = Object.keys( mapping.attributes ).length;
for ( const attr of mapping.attributes ) {
i++
if ( Object.keys( additionalTypeMapping ).includes( attr.pojoAttributeType ) ) {
// null condition for date/time/datetime
if ( isDateTimeAttribute( attr.pojoAttributeType ) ) {
str += `if ( rs.get${additionalTypeMapping[attr.pojoAttributeType]}( "${attr.tableAttribute}" ) != null ) {\n\t\t`;
}
str += `${lowerFirstLetter( mapping.pojoName )}.set${capitalizeFirstLetter(attr.pojoAttribute)}( rs.get${additionalTypeMapping[attr.pojoAttributeType]}( "${attr.tableAttribute}" ).to${attr.pojoAttributeType}() );`;
// null condition for date/time/datetime
if ( isDateTimeAttribute( attr.pojoAttributeType ) ) {
str += `\n\t}`;
}
} else {
str += `${lowerFirstLetter( mapping.pojoName )}.set${capitalizeFirstLetter(attr.pojoAttribute)}( rs.get${capitalizeFirstLetter(attr.pojoAttributeType)}( "${attr.tableAttribute}" ) );`;
}
if ( i < attrCount ) {
str += `\n\t`;
}
}
return str;
}
exports.createFileContent = function( mapping, payload ) {
return `import ${payload.modelPackage}.${mapping.pojoName};
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
public class ${mapping.pojoName}RowMapper implements RowMapper {
public ${mapping.pojoName} mapRow( ResultSet rs, int rowNum ) throws SQLException {
${mapping.pojoName} ${lowerFirstLetter( mapping.pojoName )} = new ${mapping.pojoName}();
${preparePojoSetters( mapping )}
return ${lowerFirstLetter( mapping.pojoName )};
}
}`;
};