Last active
June 5, 2018 07:48
-
-
Save aleDsz/e5c7d79e62bf928a54e0ef60d99c72cd to your computer and use it in GitHub Desktop.
Generics em R - Exemplo 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library ("methods") | |
| # Levando em conta que carregamos a classe `contacts` na memória, seria simples utilizar a classe a seguir: | |
| setRefClass("contactsController", | |
| methods = list ( | |
| findAll = function (params = list ()) { | |
| Contacts = new("contacts") | |
| fieldNames <- names((Contacts$getClass())@fieldClasses) | |
| className <- as.character((Contacts$getClass())@className) | |
| whereSql <- character(0) | |
| if (length(params) > 0) { | |
| paramNames <- names (params) | |
| whereSql <- character(0) | |
| maxLength <- 1 | |
| for (param in paramNames) { | |
| whereSql[maxLength] <- paste0(param, " = '" , params[[param]] , "'") | |
| maxLength <- maxLength + 1 | |
| } | |
| if (length(whereSql) > 0) { | |
| whereSql <- paste0(" WHERE ", paste(whereSql, collapse = " AND ")) | |
| } | |
| } | |
| stringSql <- paste0("SELECT ", paste(fieldNames, collapse = ", ", sep = ""), " FROM ", className, whereSql) | |
| return (stringSql) | |
| } | |
| ) | |
| ) | |
| ContactsController <- new ("contactsController") | |
| params <- list ( | |
| email = "[email protected]" | |
| ) | |
| print (ContactsController$findAll(params)) | |
| # Output: SELECT id, first_name, last_name, age, email FROM contacts WHERE email = '[email protected]' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment