Phase 1: Foundation Types (100% complete)
- query_helpers: SQL LIKE sanitization with pipe operators
- numeric: Integer parsing with pattern matching guards
- result: Pure Elixir Result monad (map, and_then, unwrap_or)
Phase 2: Ecto Domain Types (100% complete)
- ip_address: IPv4/IPv6 validation using :inet directly
- mac_address: Multi-format MAC parsing (colon/hyphen/dot/compact)
- snmp_oid: OID parsing/manipulation with recursive pattern matching
All 198 tests passing across converted modules.
API changed from Gleam-style {:some/:none to idiomatic {:ok/:error.
Refactored parse_numeric_oid to use with statement, reducing nesting depth.
Reviewed-on: graham/towerops-web#196
31 lines
826 B
Elixir
31 lines
826 B
Elixir
defmodule Towerops.QueryHelpers do
|
|
@moduledoc """
|
|
Helpers for building safe SQL queries.
|
|
|
|
Provides utilities for sanitizing user input before using it in SQL LIKE/ILIKE queries.
|
|
"""
|
|
|
|
@doc """
|
|
Sanitizes a string for use in SQL LIKE/ILIKE queries by escaping
|
|
the wildcard characters `%` and `_`, and the escape character `\\`.
|
|
|
|
## Examples
|
|
|
|
iex> Towerops.QueryHelpers.sanitize_like("100%")
|
|
"100\\\\%"
|
|
|
|
iex> Towerops.QueryHelpers.sanitize_like("some_value")
|
|
"some\\\\_value"
|
|
|
|
iex> Towerops.QueryHelpers.sanitize_like("hello world")
|
|
"hello world"
|
|
|
|
"""
|
|
@spec sanitize_like(String.t()) :: String.t()
|
|
def sanitize_like(query) when is_binary(query) do
|
|
query
|
|
|> String.replace("\\", "\\\\")
|
|
|> String.replace("%", "\\%")
|
|
|> String.replace("_", "\\_")
|
|
end
|
|
end
|