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

test: Test for quoting columns with dbQuoteIdentifier() #255

Merged
merged 4 commits into from
Apr 1, 2024

Conversation

dpprdan
Copy link
Contributor

@dpprdan dpprdan commented Dec 29, 2021

This allows quoting columns with Id() and dbQuoteIdentifier() and fixes the quoting of a schema, closes #254.

I've added tests for dbQuoteIdentifier() and dbUnquoteIdentifier() from RPostgres, too.

library(RMariaDB)
con <- mariadbDefault()

# quote column
column_id <- Id(schema = "myschema", table = "mytable", column = "mycolumn")
dbQuoteIdentifier(con, column_id)
#> <SQL> `myschema`.`mytable`.`mycolumn`

# quote schema without dot
(schema_id <- dbQuoteIdentifier(con, Id(schema = "myschema")))
#> <SQL> `myschema`

# glue_sql() example: https://glue.tidyverse.org/reference/glue_sql.html
library(glue)
iris2 <- iris
colnames(iris2) <- gsub("[.]", "_", tolower(colnames(iris)))
DBI::dbWriteTable(con, "iris", iris2)

iris_db <- "iris"
nicknames_db <- "nicknames"

nicknames <- data.frame(
  species = c("setosa", "versicolor", "virginica"),
  nickname = c("Beachhead Iris", "Harlequin Blueflag", "Virginia Iris"),
  stringsAsFactors = FALSE
)

DBI::dbWriteTable(con, nicknames_db, nicknames)

cols <- list(
  DBI::Id(table = iris_db, column = "sepal_length"),
  DBI::Id(table = iris_db, column = "sepal_width"),
  DBI::Id(table = nicknames_db, column = "nickname")
)

iris_species <- DBI::Id(table = iris_db, column = "species")
nicknames_species <- DBI::Id(table = nicknames_db, column = "species")

query <- glue_sql("
  SELECT {`cols`*}
  FROM {`iris_db`}
  JOIN {`nicknames_db`}
  ON {`iris_species`}={`nicknames_species`}",
  .con = con
)
query
#> <SQL> SELECT `iris`.`sepal_length`, `iris`.`sepal_width`, `nicknames`.`nickname`
#> FROM `iris`
#> JOIN `nicknames`
#> ON `iris`.`species`=`nicknames`.`species`

DBI::dbGetQuery(con, query, n = 5)
#>   sepal_length sepal_width       nickname
#> 1          5.1         3.5 Beachhead Iris
#> 2          4.9         3.0 Beachhead Iris
#> 3          4.7         3.2 Beachhead Iris
#> 4          4.6         3.1 Beachhead Iris
#> 5          5.0         3.6 Beachhead Iris

dbExecute(con, glue_sql("DROP TABLE {`nicknames_db`}, iris", .con = con))
#> [1] 0

dbDisconnect(con)

@dpprdan
Copy link
Contributor Author

dpprdan commented Dec 29, 2021

I noticed that a table t1 was left behind in the database, so I added temporary = TRUE to two dbWriteTable() tests.

@dpprdan dpprdan marked this pull request as draft January 3, 2022 10:37
@dpprdan
Copy link
Contributor Author

dpprdan commented Jan 3, 2022

Let's make r-dbi/RPostgres#372 work first.

Note that the list_objects_features test (in DBItest) is skipped in RMariaDB, which is why tests are passing here. I.e. I think we should not skip it here, but we might have to modify it, so that it tests better what we want it to test.

# Fails on Ubuntu 18.04:
"list_objects_features",

Copy link
Contributor

aviator-app bot commented Apr 1, 2024

Current Aviator status

Aviator will automatically update this comment as the status of the PR changes.
Comment /aviator refresh to force Aviator to re-examine your PR (or learn about other /aviator commands).

This PR was merged using Aviator.


See the real-time status of this PR on the Aviator webapp.
Use the Aviator Chrome Extension to see the status of your PR within GitHub.

@krlmlr krlmlr changed the title allow columns in dbQuoteIdentifier() feat: Test for quoting columns with dbQuoteIdentifier() Apr 1, 2024
@krlmlr krlmlr marked this pull request as ready for review April 1, 2024 14:27
@krlmlr
Copy link
Member

krlmlr commented Apr 1, 2024

Again, implemented independently, but the tests are fine. Thanks!

@aviator-app aviator-app bot added the blocked label Apr 1, 2024
Copy link
Contributor

aviator-app bot commented Apr 1, 2024

This pull request failed to merge: PR cannot be automatically rebased, please rebase manually to continue. Once the issues are resolved, remove the blocked label and re-queue the pull request. Note that the pull request will be automatically re-queued if it has the mergequeue label.

Additional debug info: Failed to rebase this PR onto the latest changes from the base branch. You will probably need to rebase this PR manually and resolve conflicts).

@krlmlr krlmlr removed the blocked label Apr 1, 2024
@aviator-app aviator-app bot added the blocked label Apr 1, 2024
Copy link
Contributor

aviator-app bot commented Apr 1, 2024

This pull request failed to merge: PR cannot be automatically rebased, please rebase manually to continue. Once the issues are resolved, remove the blocked label and re-queue the pull request. Note that the pull request will be automatically re-queued if it has the mergequeue label.

Additional debug info: Failed to rebase this PR onto the latest changes from the base branch. You will probably need to rebase this PR manually and resolve conflicts).

@krlmlr krlmlr removed the blocked label Apr 1, 2024
@krlmlr krlmlr force-pushed the col-in-dbQuoteIdentifier branch 2 times, most recently from 11fe730 to 6dff90e Compare April 1, 2024 16:47
@krlmlr krlmlr changed the title feat: Test for quoting columns with dbQuoteIdentifier() test: Test for quoting columns with dbQuoteIdentifier() Apr 1, 2024
dpprdan and others added 3 commits April 1, 2024 16:54
Original: e587868

Disconnect on exit in `dbQuoteIdentifier()` tests
use temporary tables in `dbWriteTable()` tests, so they don't get left behind
@aviator-app aviator-app bot added the blocked label Apr 1, 2024
Copy link
Contributor

aviator-app bot commented Apr 1, 2024

This pull request failed to merge: some CI status(es) failed. Once the issues are resolved, remove the blocked label and re-queue the pull request. Note that the pull request will be automatically re-queued if it has the mergequeue label.

Failed CI(s): Smoke test: stock R

@krlmlr krlmlr removed the blocked label Apr 1, 2024
@aviator-app aviator-app bot merged commit a2c22b1 into r-dbi:main Apr 1, 2024
22 checks passed
@dpprdan dpprdan deleted the col-in-dbQuoteIdentifier branch April 3, 2024 16:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

dbQuoteIdentifier() does not allow columns in Id() object.
2 participants