New Feature: Named SQL Parameters - Supports both positional (?) and named (:name) parameters - Named parameters are order-independent and more readable - Syntax: sqlite_param :param_name $variable Implementation: - Updated sqlite_param directive to accept 1 or 2 arguments - ModuleConfig.query_params now stores (name, variable) pairs - execute_query() detects named vs positional parameters - Extracted row_to_map closure to avoid type conflicts - Named params use rusqlite named parameter binding Examples (Port 8082): - Book detail: WHERE id = :book_id - Genre filter: WHERE genre = :genre_name - Year range: WHERE year >= :min_year AND year <= :max_year - Title search: WHERE title LIKE '%' || :search_term || '%' - Rating filter: WHERE rating >= :min_rating Benefits of Named Parameters: - Order-independent: params can be in any order in config - Self-documenting: :book_id is clearer than first ? - Maintainable: can add/remove params without reordering - Recommended for all but simplest queries Configuration: - conf/book_named_params.conf: Complete named params example - start_named_params.sh: Quick start script for port 8082 Documentation: - Added named vs positional comparison in README_PARAMETERS.md - Updated README.md with named parameter examples - Documented both syntaxes in directive reference All examples tested and working with both parameter styles.
51 lines
1.5 KiB
Bash
Executable File
51 lines
1.5 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Start script for named parameters example
|
|
|
|
set -e
|
|
|
|
echo "📚 Starting Named Parameters Example..."
|
|
echo ""
|
|
|
|
# Check if database exists
|
|
if [ ! -f "book_catalog.db" ]; then
|
|
echo "Database not found. Running setup..."
|
|
./setup_book_catalog.sh
|
|
echo ""
|
|
fi
|
|
|
|
# Check if module is built
|
|
if [ ! -f "target/debug/libsqlite_serve.dylib" ]; then
|
|
echo "Module not built. Building..."
|
|
direnv exec "$PWD" cargo build
|
|
echo ""
|
|
fi
|
|
|
|
# Start nginx
|
|
echo "Starting nginx on http://localhost:8082"
|
|
./ngx_src/nginx-1.28.0/objs/nginx -c conf/book_named_params.conf -p .
|
|
|
|
echo ""
|
|
echo "✅ Named Parameters Example is running!"
|
|
echo ""
|
|
echo "Named Parameter Examples:"
|
|
echo " • http://localhost:8082/book?id=1"
|
|
echo " Query: SELECT * FROM books WHERE id = :book_id"
|
|
echo " Param: :book_id = \$arg_id"
|
|
echo ""
|
|
echo " • http://localhost:8082/genre?genre=Programming"
|
|
echo " Query: ... WHERE genre = :genre_name"
|
|
echo " Param: :genre_name = \$arg_genre"
|
|
echo ""
|
|
echo " • http://localhost:8082/years?min=2015&max=2024"
|
|
echo " Query: ... WHERE year >= :min_year AND year <= :max_year"
|
|
echo " Params: :min_year = \$arg_min, :max_year = \$arg_max"
|
|
echo ""
|
|
echo " • http://localhost:8082/search?q=Rust"
|
|
echo " Search by title with named parameter"
|
|
echo ""
|
|
echo " • http://localhost:8082/top-rated?rating=4.7"
|
|
echo " Filter by minimum rating"
|
|
echo ""
|
|
echo "To stop: ./ngx_src/nginx-1.28.0/objs/nginx -s stop -c conf/book_named_params.conf -p ."
|
|
|