Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions R/dbQuoteIdentifier_MariaDBConnection_Id.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#' @name mariadb-quoting
#' @usage NULL
dbQuoteIdentifier_MariaDBConnection_Id <- function(conn, x, ...) {
if (length(x@name) >= 3 && any(x@name[[length(x@name) - 2]] != "def")) {
stop('If a "catalog" component is supplied in `Id()`, it must be equal to "def" everywhere.', call. = FALSE)
}
SQL(paste0(dbQuoteIdentifier(conn, x@name), collapse = "."))
}

Expand Down
99 changes: 99 additions & 0 deletions tests/testthat/test-dbQuoteIdentifier.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
test_that("quoting string", {
con <- mariadbDefault()
on.exit(dbDisconnect(con))

quoted <- dbQuoteIdentifier(con, "Robert'); DROP TABLE Students;--")
expect_s4_class(quoted, 'SQL')
expect_equal(as.character(quoted),
"`Robert'); DROP TABLE Students;--`")
})

test_that("quoting SQL", {
con <- mariadbDefault()
on.exit(dbDisconnect(con))

quoted <- dbQuoteIdentifier(con, SQL("Robert'); DROP TABLE Students;--"))
expect_s4_class(quoted, 'SQL')
expect_equal(as.character(quoted),
"Robert'); DROP TABLE Students;--")
})

test_that("quoting Id", {
con <- mariadbDefault()
on.exit(dbDisconnect(con))

quoted <- dbQuoteIdentifier(con, Id(schema = 'Robert', table = 'Students;--'))
expect_s4_class(quoted, 'SQL')
expect_equal(as.character(quoted),
"`Robert`.`Students;--`")
})

test_that("quoting Id with column, #254", {
con <- mariadbDefault()
on.exit(dbDisconnect(con))

quoted <- dbQuoteIdentifier(con, Id(schema = 'Robert', table = 'Students;--', column = "dormitory"))
expect_s4_class(quoted, 'SQL')
expect_equal(as.character(quoted),
"`Robert`.`Students;--`.`dormitory`")
})

test_that("quoting Id with column, unordered", {
con <- mariadbDefault()
on.exit(dbDisconnect(con))

quoted <- dbQuoteIdentifier(con, Id(column = "dormitory", table = 'Students;--'))
expect_s4_class(quoted, 'SQL')
expect_equal(as.character(quoted),
"`Students;--`.`dormitory`")
})

test_that("quoting errors", {
con <- mariadbDefault()
on.exit(dbDisconnect(con))

expect_error(Id(table = 'Robert', table = 'Students;--'))
})

test_that("unquoting identifier - SQL with quotes", {
con <- mariadbDefault()
on.exit(dbDisconnect(con))

expect_equal(dbUnquoteIdentifier(con, SQL('`Students;--`')),
list(Id(table = 'Students;--')))

expect_equal(dbUnquoteIdentifier(con, SQL('`Robert`.`Students;--`')),
list(Id(schema = 'Robert', table = 'Students;--')))

expect_equal(dbUnquoteIdentifier(con, SQL('`Rob``ert`.`Students;--`')),
list(Id(schema = 'Rob`ert', table = 'Students;--')))

expect_equal(dbUnquoteIdentifier(con, SQL('`Rob.ert`.`Students;--`')),
list(Id(schema = 'Rob.ert', table = 'Students;--')))

expect_error(dbUnquoteIdentifier(con, SQL('`Robert.`Students`')),
"^Can't unquote")
})

test_that("unquoting identifier - SQL without quotes", {
con <- mariadbDefault()
on.exit(dbDisconnect(con))

expect_equal(dbUnquoteIdentifier(con, SQL('Students')),
list(Id(table = 'Students')))

expect_equal(dbUnquoteIdentifier(con, SQL('Robert.Students')),
list(Id(schema = 'Robert', table = 'Students')))

expect_error(dbUnquoteIdentifier(con, SQL('Rob``ert.Students')),
"^Can't unquote")
})

test_that("unquoting identifier - Id", {
con <- mariadbDefault()
on.exit(dbDisconnect(con))

expect_equal(dbUnquoteIdentifier(con,
Id(schema = 'Robert', table = 'Students;--')),
list(Id(schema = 'Robert', table = 'Students;--')))
})
4 changes: 2 additions & 2 deletions tests/testthat/test-dbWriteTable.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ test_that("dbWriteTable() throws error if constraint violated", {

x <- data.frame(col1 = 1:10, col2 = letters[1:10])

dbWriteTable(con, "t1", x[1:3, ], overwrite = TRUE)
dbWriteTable(con, "t1", x[1:3, ], overwrite = TRUE, temporary = TRUE)
dbExecute(con, "CREATE UNIQUE INDEX t1_c1_c2_idx ON t1(col1, col2(1))")
expect_error(dbWriteTable(con, "t1", x, append = TRUE), "added 7 rows|Duplicate entry")
})
Expand All @@ -25,7 +25,7 @@ test_that("dbAppendTable() throws error if constraint violated", {

x <- data.frame(col1 = 1:10, col2 = letters[1:10])

dbWriteTable(con, "t1", x[1:3, ], overwrite = TRUE)
dbWriteTable(con, "t1", x[1:3, ], overwrite = TRUE, temporary = TRUE)
dbExecute(con, "CREATE UNIQUE INDEX t1_c1_c2_idx ON t1(col1, col2(1))")
expect_error(dbAppendTable(con, "t1", x), "added 7 rows|Duplicate entry")
})
Expand Down