Skip to content

Instantly share code, notes, and snippets.

@cosmez
Created September 28, 2015 07:34
Show Gist options
  • Save cosmez/351eb4fcdfccd45809f9 to your computer and use it in GitHub Desktop.
Save cosmez/351eb4fcdfccd45809f9 to your computer and use it in GitHub Desktop.
Auto Generate Java Models and Routes Code
#lang racket
(require db)
(define package-text "conviasc.db")
(define connection (mysql-connect #:user "root" #:database "colegio" #:password "admin"))
(define str string-append)
;class template, requires class name and fields
(define class-template (str "package " package-text ";\n\npublic class ~a {\n ~a}"))
;1.- Route, 2.- Table Name ,3.- Class Name,
(define route-all-template "get(\"/db/~a\", RouteHelper.dbRouteAll(\"~a\", ~a.class), new JsonTransformer());\n")
;1.- Route, 2.- Table Name ,3.- Class Name,
(define route-single-template "get(\"/db/~a/:id\", RouteHelper.dbRouteSingle(\"~a\", ~a.class) , new JsonTransformer());\n")
;1.- Route, 2.- Table Name
(define route-delete-template "delete(\"/db/~a/:id\", RouteHelper.deleteRouteSingle(\"~a\"));\n")
(define catalogs
'(("cat_estado" "Estado" "estado")
("cat_estado_contacto" "EstadoContacto" "estadocontacto")
("cat_estado_evento" "EstadoEvento" "estadoevento")
("cat_estado_usuario" "EstadoUsuario" "estadousuario")
("cat_idioma" "Idioma" "idioma")
("cat_nivel_estudio" "NivelEstudio" "nivelestudio")
("cat_pregunta_secreta" "PreguntaSecreta" "preguntasecreta")
("cat_tipo_curso" "TipoCurso" "tipocurso")
("cat_tipo_duda" "TipoDuda" "tipoduda")
("cat_titulo_profesional" "TituloProfesional" "tituloprofesional")))
(define (convert-type type)
(match type
[(regexp #rx"tinyint") "boolean"]
[(regexp #rx"int") "int"]
[(regexp #rx"varchar") "String"]
[(regexp #rx"date") "Date"]))
(define (field-code field)
(match field
((vector field type _ _ _ _)
(str "\t" (convert-type type) " " field ";\n"))))
(define (get-class-code catalog)
(define fields
(query-rows connection (format "SHOW COLUMNS FROM ~a;" (first catalog))))
(define class-name (second catalog))
(define fields-code (string-join (map field-code fields)))
(format class-template class-name fields-code))
;;create each catalog java class
(define (create-code catalog)
(define filename (str "output\\" (second catalog) ".java"))
(define class-code (get-class-code catalog))
(when (file-exists? filename)
(delete-file filename))
(display-to-file class-code filename))
;;create the code for the routes
(define (generate-routes catalog)
(match-define
(list table-name class-name route)
catalog)
(str
(format route-all-template route table-name class-name)
(format route-single-template route table-name class-name)
(format route-delete-template route table-name)))
(define (create-routes-code catalogs)
(define routes-filename "output\\routes.txt")
(when (file-exists? routes-filename)
(delete-file routes-filename))
(display-to-file (string-join (map generate-routes catalogs) "") routes-filename))
(unless (directory-exists? "output")
(make-directory "output"))
(create-routes-code catalogs) ;create routes code
(map create-code catalogs) ;;create each catalog java class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment