UnifyWeaver

Book 10: SQL Target

From Prolog to SQL: Declarative Query Generation

Part of the UnifyWeaver Education Series

This book covers UnifyWeaver’s SQL target, enabling you to compile Prolog predicates directly to SQL queries for execution on relational databases.

Prerequisites

Required:

Recommended:

Technical:

What You’ll Learn

SQL Target Overview

The SQL target is unique among UnifyWeaver’s compilation targets:

Target Output Execution
Bash Shell scripts Run directly
C# .NET source Compile & run
Go Go source Compile & run
SQL SQL queries Run on database

Unlike other targets that produce executable programs, the SQL target generates declarative queries meant for external database execution.

Book Structure

Chapter 1: Getting Started with SQL Target

Chapter 2: Basic Queries

Chapter 3: JOINs

Chapter 4: Aggregations

Chapter 5: Subqueries

Chapter 6: Window Functions

Chapter 7: Common Table Expressions

Chapter 8: Recursive CTEs

Chapter 9: Set Operations

Chapter 10: SQL Functions

Chapter 11: CASE WHEN Expressions

Chapter 12: Practical Applications

Quick Start Example

% Load the SQL target
:- use_module('src/unifyweaver/targets/sql_target').

% Declare table schema
:- sql_table(employees, [id-integer, name-text, dept-text, salary-integer]).

% Define a predicate
high_earners(Name, Salary) :-
    employees(_, Name, _, Salary),
    Salary > 100000.

% Compile to SQL
?- compile_predicate_to_sql(high_earners/2, [], SQL).
% SQL = 'SELECT name, salary FROM employees WHERE salary > 100000;'

Run the generated SQL on SQLite:

sqlite3 mydb.db "SELECT name, salary FROM employees WHERE salary > 100000;"

Feature Summary

Feature Prolog Syntax SQL Output
Basic SELECT table(A, B, C) SELECT a, b, c FROM table
WHERE Salary > 50000 WHERE salary > 50000
INNER JOIN Shared variables INNER JOIN ... ON ...
LEFT JOIN sql_left_join(...) LEFT JOIN ... ON ...
GROUP BY sql_group_by([Dept]) GROUP BY dept
Aggregate sql_count(*) COUNT(*)
Window sql_window(rank, ...) RANK() OVER (...)
CTE compile_with_cte(...) WITH ... AS (...)
Recursive compile_recursive_cte(...) WITH RECURSIVE ...
UNION compile_set_operation(union, ...) ... UNION ...
CASE sql_case([when(...), ...], Default) CASE WHEN ... END

Database Compatibility

Generated SQL is primarily SQLite-compatible but works with:

Example Projects

Throughout this book, you’ll build:

When to Use SQL Target

Use SQL Target when:

Use other targets when:

Testing Generated SQL

# Create test database
sqlite3 test.db < schema.sql

# Run generated query
sqlite3 test.db "$(cat generated.sql)"

# Or use the write_sql_file/2 predicate
?- compile_predicate_to_sql(my_query/2, [], SQL),
   write_sql_file('output/my_query.sql', SQL).

Learning Path

  1. Start with Chapters 1-2 - Basic query generation
  2. Work through Chapters 3-4 - JOINs and aggregations
  3. Advance to Chapters 5-6 - Subqueries and window functions
  4. Master Chapters 7-8 - CTEs and recursive queries
  5. Complete Chapters 9-11 - Set operations, functions, CASE WHEN
  6. Apply in Chapter 12 - Real-world applications

Additional Resources

Going Further

After completing this book, you’ll be able to:

License

This educational content is licensed under CC BY 4.0. Code examples are dual-licensed under MIT OR Apache-2.0.

Feedback

Found an issue or have suggestions?