Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combine multiple PEcAn.db calls in a single query #3312

Merged
merged 10 commits into from
Jul 10, 2024
22 changes: 16 additions & 6 deletions modules/data.atmosphere/R/read.register.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,24 @@ read.register <- function(register.xml, con) {
} else if ((!is.null(register$format$id) & is.null(register$format$name))
|
(!is.null(register$format$id) & is.null(register$format$mimetype))) {
register$format$name <- PEcAn.DB::db.query(
paste("SELECT name from formats where id = ", register$format$id), con)[[1]]
register$format$mimetype <- PEcAn.DB::db.query(
paste("SELECT mime_type from formats where id = ", register$format$id), con)[[1]]
# Retrieve format name and mimetype from the database
query.format.info <- PEcAn.DB::db.query(
paste(
"SELECT name, type_string AS mimetype",
"FROM formats JOIN mimetypes ON formats.mimetype_id = mimetypes.id",
"WHERE formats.id = ", register$format$id),
con
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two problems here:

  1. Assigning the result to register$format will overwrite the existing format object (which contains at least id and inputtype -- see lines 25 and 35) with one that contains only name and mimetype. Better to query once into a temporary result and then assign from there into register$format$name and `register$format$mimetype.
  2. The existing line 41 is broken because the formats table doesn't have a mime_type column, it has a mimetype_id tht then needs to be looked up in the mimetypes table. I think you want "SELECT name, type_string AS mimetype FROM formats JOIN mimetypes ON formats.mimetype_id = mimetypes.id WHERE formats.id ="

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I seemed to miss it. Fixing it now.
  2. Ahh, I recall us having a conversation about this earlier and that seems to be the case.


register$format$name <- query.format.info$name
register$format$mimetype <- query.format.info$mimetype

} else if (is.null(register$format$id) & !is.null(register$format$name) & !is.null(register$format$mimetype)) {
register$format$id <- PEcAn.DB::db.query(
paste0("SELECT id from formats where name = '", register$format$name,
"' and mime_type = '", register$format$mimetype, "'"), con)[[1]]
paste0(
"SELECT formats.id FROM formats JOIN mimetypes ON formats.mimetype_id = mimetypes.id ",
"WHERE name = '", register$format$name,
"' AND type_string = '", register$format$mimetype, "'"), con)[[1]]
}
}
return(invisible(register))
Expand Down
Loading