En el trabajo me encontre con la necesidad de leer todos los campos de una tabla con casi 60 columnas, y crear una clase en Java con todos sus getters y setters, ademas de tener que cargar todos los atributos de esta calse con las columnas de el registro
Por suerte teníamos una hoja de cálculo con las columnas y sus tipos de datos que me ayudó muchísimo.
Lo que hice fué exportar la hoja de cálculo a un archivo separado por comas, y con ayuda de awk creé un par de scripts que pudieron generar el código que necesitaba en Java, aunque para otro lenguaje hubiera sido algo muy similar
tabla2class.awk
#!/usr/bin/awk -f
# tabla2class.awk
# recibe un archivo separado por comas
# NOMBRE_DE_CAMPO, TIPO
# imprime las definiciones de las variables con sus respectivos getters y setters
BEGIN {
FS=","
declaraciones = "\n// Declaraciones de variables"
getters = "\n// Getters"
setters = "\n// Setters"
}
{
if ($2 == "DECIMAL") {
tipo = "Double";
} else if ($2 == "CHARACTER" || $2 == "CHAR" || $2 == "VARCHAR") {
tipo = "String";
} else if ($2 == "TIMESTAMP" || $2 == "DATE" || $2 == "TIME") {
tipo = "Date";
} else if ($2 == "NUMERIC") {
tipo = "Integer";
} else if ($2 == "SMALLINT") {
tipo = "Short";
}
var = nombreVariable($1)
declaraciones = declaraciones "\nprivate " tipo " " var ";\t\t//" $0
getters = getters "\n" getter(tipo, var)
setters = setters "\n" setter(tipo, var)
}
END {
print declaraciones
print getters
print setters
print "\n"
}
function getter(tipo, nombre) {
return "public " tipo " get" nombre "() {\n\treturn " nombre ";\n}"
}
function setter(tipo, nombre) {
return "public void set" nombre"(" tipo " var) {\n\t" nombre " = var;\n}"
}
function nombreVariable(nombre) {
n = split(tolower(nombre), palabras, "_")
var = palabras[1]
for (i = 2; i < = n; i++) {
var = var toupper(substr(palabras[i], 1, 1)) substr(palabras[i], 2)
}
return var
}
fillBean.awk
#!/usr/bin/awk -f
#
# fillBean.awk
# Recibe un archivo separado por comas
# NOMBRE_DE_CAMPO, TIPO
# imprime las lineas necesarias para llenar el objeto con los resultados de un ResultSet
BEGIN {
FS=","
bean = "bean"
rset = "rs"
}
{
if ($2 == "CHARACTER" || $2 == "VARCHAR" || $2 == "CHAR") {
get = "(null==" rset ".getString("" $1 ""))?"":" rset ".getString("" $1 "").trim()"
tipo = "String";
} else if ($2 == "TIMESTAMP" || $2 == "DATE" || $2 == "TIME") {
get = rset ".getDate("" $1 "")"
tipo = "Date";
} else {
if ($2 == "DECIMAL") {
tipo = "Double";
gt = "Double";
} else if ($2 == "NUMERIC") {
gt = "Integer";
tipo = "Int";
} else if ($2 == "SMALLINT") {
gt = "Short";
tipo = "Short";
}
get = "new " gt "(" rset ".get" tipo "("" $1 ""))";
}
var = nombreVariable($1)
print bean ".set" toupper(substr(var, 1, 1)) substr(var, 2) "(" get ");"
}
END {
print "\n"
}
function nombreVariable(nombre) {
n = split(tolower(nombre), palabras, "_")
var = palabras[1]
for (i = 2; i < = n; i++) {
var = var toupper(substr(palabras[i], 1, 1)) substr(palabras[i], 2)
}
return var
}
Eso es ~normal! - Jj’s Blog » Saving the day
March 3rd, 2007
[…] It does feel good when you actually go through this at work every other day . Although I do it with help of Awk, Sed, or Vim […]