build-a-sql-database / lesson-28.md
Lesson 28 · Executing queries

The REPL

Your engine can run SQL - now let a person type it. Today you build the read-eval-print loop that tokenizes, parses, and executes a line of SQL and prints the result.

The goal

Build a loop that reads a SQL statement, runs it end to end, and prints the result set or an error.

Start here - the target
TO DO
Scenario: Running statements through the REPL
Giventhe input lines "CREATE TABLE t (id INTEGER);", "INSERT INTO t VALUES (1);", "SELECT * FROM t;"
Whenthey are fed to the REPL in order
Thenthe SELECT prints a grid whose header is "id" and whose next line is "1"
Anda statement with a syntax error prints an error message and the loop continues
Background

This is the lesson the project becomes a database you use. A REPL - read-eval-print loop - reads a statement, runs it through the full stack you have built (tokenize, parse, execute), and prints the outcome: a formatted grid for a SELECT, a short acknowledgement for CREATE/INSERT, and a readable error for anything malformed. Crucially, an error prints and the loop continues - one bad query should never kill the session.

Wire it to read lines from standard input so you can pipe a .sql file in or type interactively. Everything from here on - ordering, limits, aggregates, joins - becomes usable the instant you implement it, because this loop already turns text into answers. Make each later lesson’s feature runnable at this prompt and you keep a true walking skeleton to the end.

Make it work
// read statements (split the input on ';'), then for each:
// Tokenize -> Parse -> db.Exec
// on error, print it and keep going (don't crash the loop)
// on a result set, print Format(rs); DDL/INSERT can print "ok"
CheckpointDONE
You have an interactive SQL prompt - type a statement, see the result. Commit and stop here.