Skip to content

Commit

Permalink
add meta_sql_interpolate in package
Browse files Browse the repository at this point in the history
  • Loading branch information
vikram-rawat committed Mar 20, 2024
1 parent 1377203 commit 1218d1f
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 9 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: read.sql
Title: Reads .SQL Files And Execute Them
Version: 0.2.1
Version: 0.3.0
Authors@R:
person(
given = "Vikram",
Expand All @@ -15,7 +15,7 @@ License: GPL (>= 3)
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.2
RoxygenNote: 7.3.1
Depends: R (>= 3.6.0)
Imports:
DBI,
Expand Down
53 changes: 46 additions & 7 deletions R/read_sql.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,35 @@ print.sql_query <- function(
)
}

# meta_sql_interpolate -------------------------------------
#' interpolate meta data in sql query
#'
#' @description This function just returns a new sql_query object after an interpolation of meta data
#'
#' @param sql_query a sql_query object that will be used for sqlinterpolation
#' @param meta_query_params A list of values for interpolation in the SQL file
#'
#' @return query object
#'
#' @import DBI
#'
meta_sql_interpolate <- function(sql_query, meta_query_params) {
# Loop over each item in the query_params list
for (param in names(meta_query_params)) {
# Replace the placeholder in the query with the parameter value
sql_query <- stringi::stri_replace_all_fixed(
sql_query,
stringi::stri_sprintf(
format = "{%s}",
param
),
meta_query_params[[param]],
vectorize_all = FALSE
)
}
# Return the interpolated query
return(sql_query)
}

# get_sql_query_from_files_interpolated -------------------------------------
#' get SQL query object
Expand All @@ -77,17 +106,27 @@ print.sql_query <- function(
rs_interpolate <- function(
sql_query,
sql_conn,
query_params = list()) {
query_params = list(),
meta_query_params = NULL) {
# if meta_sql_interopolate is available: ----------------------------------
if (length(meta_query_params) >= 1) {
sql_query$sql_query <- meta_sql_interpolate(
sql_query = sql_query$sql_query,
meta_query_params = meta_query_params
)
}

# set Variables ------------------------------------------------------------

sql_query$sql_query <- SQL(
DBI::sqlInterpolate(
conn = sql_conn,
sql = sql_query$sql_query,
.dots = query_params
)
sql_query$sql_query <- DBI::sqlInterpolate(
conn = sql_conn,
sql = sql_query$sql_query,
.dots = query_params
)

# convert to SQL class: ----------------------------------
sql_query$sql_query <- SQL(sql_query$sql_query)

return(sql_query)
}

Expand Down
29 changes: 29 additions & 0 deletions renv.lock
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,17 @@
],
"Hash": "c0d8599494bc7fb408cd206bbdd9cab0"
},
"cachem": {
"Package": "cachem",
"Version": "1.0.8",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
"fastmap",
"rlang"
],
"Hash": "c35768291560ce302c0a6589f92e837d"
},
"callr": {
"Package": "callr",
"Version": "3.7.5",
Expand Down Expand Up @@ -246,6 +257,13 @@
],
"Hash": "962174cf2aeb5b9eea581522286a911f"
},
"fastmap": {
"Package": "fastmap",
"Version": "1.1.1",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "f7736a18de97dea803bde0a2daaafb27"
},
"fontawesome": {
"Package": "fontawesome",
"Version": "0.5.2",
Expand Down Expand Up @@ -368,6 +386,17 @@
],
"Hash": "7ce2733a9826b3aeb1775d56fd305472"
},
"memoise": {
"Package": "memoise",
"Version": "2.0.1",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
"cachem",
"rlang"
],
"Hash": "e2817ccf4a065c5d9d7f2cfbe7c1d78c"
},
"pillar": {
"Package": "pillar",
"Version": "1.9.0",
Expand Down
8 changes: 8 additions & 0 deletions tests/sql/simplq_sql_interpolation.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
select
*
from
{main_table}
where
{column1} >= ?mincol1
and
{column2} >= ?mincol2
51 changes: 51 additions & 0 deletions tests/testthat/test-db_func.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,54 @@
# load library ------------------------------------------------------------

library(DBI)
library(read.sql)

# establish a connection --------------------------------------------------

conn <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")

# insert data -------------------------------------------------------------

DBI::dbWriteTable(conn, "iris", iris, overwrite = TRUE)

range(iris$Sepal.Length)
range(iris$Petal.Length)

# check functions ---------------------------------------------------------

sql_query_object <- rs_read_query(
"tests/sql/simplq_sql_interpolation.sql"
)

query_obj <- read.sql::rs_interpolate(
sql_query = sql_query_object, # object created from rs_read_query function
sql_conn = conn,
meta_query_params = list(
main_table = "iris",
column1 = "`Sepal.Length`",
column2 = "`Petal.Length`"
),
query_params = list(
mincol1 = 5,
mincol2 = 5
)
)

query_obj |>
read.sql::rs_execute(
sql_conn = conn
)

dbGetQuery(
conn,
"
select
*
from
'iris'
where
'sepal_length' >= 5
and
'petal_length' >= 5
"
)

0 comments on commit 1218d1f

Please sign in to comment.