Skip to main content

apply_index_offset

Function apply_index_offset 

Source
pub fn apply_index_offset(expression: &str, offset: i64) -> Option<String>
Expand description

Applies an offset to a given integer literal expression for array indexing.

This is used for dialects that have different array indexing conventions (0-based vs 1-based indexing).

§Arguments

  • expression - The index expression (should be an integer literal)
  • offset - The offset to apply (e.g., 1 for 0-based to 1-based conversion)

§Returns

The expression with the offset applied if it’s an integer literal, otherwise returns the original expression unchanged.

§Example

use polyglot_sql::helper::apply_index_offset;

// Convert 0-based index to 1-based
assert_eq!(apply_index_offset("0", 1), Some("1".to_string()));
assert_eq!(apply_index_offset("5", 1), Some("6".to_string()));

// Not an integer, return None
assert_eq!(apply_index_offset("col", 1), None);