Package 'nolock'

Title: Append 'WITH (NOLOCK)' to 'SQL' Queries, Get Packages in Active Script
Description: Provides a suite of tools that can assist in enhancing the processing efficiency of 'SQL' and 'R' scripts. - The 'libr_unused()' retrieves a vector of package names that are called within an 'R' script but are never actually used in the script. - The 'libr_used()' retrieves a vector of package names actively utilized within an 'R' script; packages loaded using 'library()' but not actually used in the script will not be included. - The 'libr_called()' retrieves a vector of all package names which are called within an 'R' script. - 'nolock()' appends 'WITH (nolock)' to all tables in 'SQL' queries. This facilitates reading from databases in scenarios where non-blocking reads are preferable, such as in high-transaction environments.
Authors: Arkadiusz W. Pajda [aut, cre, cph]
Maintainer: Arkadiusz W. Pajda <[email protected]>
License: GPL-3
Version: 1.1.0
Built: 2025-03-11 02:59:30 UTC
Source: https://github.com/cran/nolock

Help Index


'Get Packages Called in the Active 'R' Script'

Description

Retrieves a vector of all package names which are called within an 'R' script.

Usage

libr_called(script = NULL)

Arguments

script

Character vector. 'R' script to be processed. If NULL, an active 'R' script is used.

Value

Retrieves a vector of package names.

Examples

script_content <- 'library(rstudioapi)
ActiveDocument <- getActiveDocumentContext()$path
min_c <- min(c(1,2,3))
require(dplyr)
pacman::p_load(tidymodels)'

  temp_script_path <- tempfile(fileext = ".R")
  writeLines(script_content, con = temp_script_path)

  libr_called(script = temp_script_path)

  unlink(temp_script_path)

'Get Unused Packages in the Active 'R' Script'

Description

Retrieves a vector of package names that are called within an 'R' script but are never actually used in the script.

Usage

libr_unused(script = NULL)

Arguments

script

Character vector. 'R' script to be processed. If NULL, an active 'R' script is used.

Value

Retrieves a vector of package names which are never really used in the code.

Examples

script_content <- 'library(rstudioapi)
ActiveDocument <- getActiveDocumentContext()$path
min_c <- min(c(1,2,3))
require(dplyr)
pacman::p_load(tidymodels)'

  temp_script_path <- tempfile(fileext = ".R")
  writeLines(script_content, con = temp_script_path)

  libr_unused(script = temp_script_path)

  unlink(temp_script_path)

'Get Packages Used in the Active Script'

Description

The 'libr_used()' retrieves a vector of package names actively utilized within an 'R' script; packages loaded using 'library()' but not actually used in the script will not be included.

Usage

libr_used(script = NULL)

Arguments

script

Character vector. 'R' script to be processed. If NULL, an active 'R' script is used.

Value

Returns the vector of all package names used in the active 'R' script, based on all the functions used in the script with fully loaded namespaces in the environment.

Examples

script_content <- 'library(rstudioapi)
ActiveDocument <- getActiveDocumentContext()$path
min_c <- min(c(1,2,3))
'

temp_script_path <- tempfile(fileext = ".R")
writeLines(script_content, con = temp_script_path)

libr_used(script = temp_script_path)

unlink(temp_script_path)

'Append WITH (NOLOCK) to SQL Queries'

Description

Automatically appends 'WITH (nolock)' to all tables in 'SQL' queries using a utility function. This facilitates reading from databases in scenarios where non-blocking reads are preferable, such as in high-transaction environments.

Usage

nolock(query = NULL)

Arguments

query

Character vector. 'SQL' query to be processed. If NULL, a temporary 'SQL' text document is opened for user input.

Value

Returns the processed 'SQL' query as a character vector with 'WITH (nolock)' added for each table in the query.

Examples

example_SQL <- "

WITH CTE AS (SELECT C.TABLE_NAME,
                    C.COLUMN_NAME,
                    T.TABLE_TYPE
             FROM INFORMATION_SCHEMA.COLUMNS AS C
                      JOIN INFORMATION_SCHEMA.TABLES T ON C.TABLE_NAME = T.TABLE_NAME)

SELECT *
FROM CTE;"

nolock(query = example_SQL)

## Not run: 
nolock()

## End(Not run)