25. The miraj-cli console
miraj-cli is the Miraj interactive SQL console: it opens a data folder, executes SQL statements and displays the results. It is used both for interactive exploration (REPL) and for running .sql scripts in batch mode (scheduled tasks, migrations, deployments).
25.1 General syntax#
miraj-cli [--root <folder>] [--config <file.xml>] [-d <database>] [-e "<SQL>"]
[--lang en|fr|zh|hi|es|ar|pt|ru|de|ja] [--json] [--timing] [--no-history]
[<file.sql>]Option names are case-insensitive (--ROOT is equivalent to --root).
Exit codes:
| Code | Meaning |
|---|---|
0 | Success (all statements succeeded) |
1 | At least one statement failed, or a .sql file given as an argument was not found |
2 | Error while opening the embedded server (root folder, configuration file, folder already opened by another Miraj process) |
25.2 Command-line options#
| Option | Description | Example |
|---|---|---|
--root <folder> | Root folder of the data to open (default: data, created if missing). All the databases it contains are loaded at startup. | miraj-cli --root C:\data\miraj |
--config <file.xml> | XML engine configuration file to use, instead of <root folder>\miraj_config.xml. If the file does not exist, a commented template is created at that location; miraj_default.xml, the reference list of all variables with their default value, is regenerated at each startup next to it (it is never read). | miraj-cli --config C:\conf\miraj-prod.xml |
-d <database> | Database to select after opening (equivalent to typing USE <database>; as the first statement). | miraj-cli -d shop |
-e "<SQL>" | Executes the given SQL statement (or script) then exits, without entering the REPL. Disables the history. | miraj-cli -d shop -e "SELECT COUNT(*) FROM articles" |
--lang <code> | Language of error and warning messages: en (default), fr, zh, hi, es, ar, pt, ru, de, ja. | miraj-cli --lang fr |
--json | Displays results as JSON instead of a text table. Can also be enabled during a session with \json. | miraj-cli --json -e "SELECT * FROM articles" |
--timing | Displays the execution time of each statement. Can also be enabled during a session with \timing. | miraj-cli --timing |
--no-history | Disables reading and writing of the persistent statement history. | miraj-cli --no-history |
<file.sql> | Executes this file in batch mode then exits (recognized by its .sql extension, without being the value of -e). The last .sql argument on the command line is the one retained. | miraj-cli migration.sql |
--help, -h, /? | Displays the syntax summary and exits. | miraj-cli --help |
Notes:
-eand<file.sql>take precedence over the REPL, in that order: if-eis present, it is executed and the console exits without reading any file or opening a REPL; otherwise, if there is a.sqlargument, that file is executed; otherwise, the REPL starts.-d <database>can be combined with-eor with a.sqlfile: the database is selected before execution.- The executed
.sqlfile may begin with a UTF-8 BOM, which is ignored automatically. - The console embeds the engine: it opens the data folder itself and takes its exclusive lock (
miraj.lck). As long as amiraj-serverholds that folder, it cannot open it (exit code2): connect to the server with a network client (mysql, see chapter 26).
25.3 Internal REPL commands#
In the REPL, an SQL statement can span several lines; it is sent to the engine as soon as a line ends with ;. The following commands, most of them prefixed with \, are not SQL and run immediately, without a semicolon:
| Command | Purpose | Example |
|---|---|---|
\help, \h, help | Displays the list of commands. | \help |
\timing | Toggles the display of execution time (on/off). | \timing |
\json | Toggles the result display format between table and JSON. | \json |
\d | Lists the tables of the current database (SHOW TABLES). | \d |
\d <table> | Describes the columns of the given table (DESCRIBE <table>). | \d articles |
\l | Lists the databases of the server (SHOW DATABASES). | \l |
\history [n] | Displays the last n statements of the history (20 by default). A negative number displays nothing. | \history 50 |
\!n | Re-executes statement number n of the history (number shown by \history). | \!12 |
source <file.sql>, \. <file.sql> | Executes a .sql file without leaving the REPL. | source migration.sql |
\q, exit, quit | Exits the console. | \q |
| up / down arrows | Recall of previously typed lines (Windows console). | — |
25.4 Typical interactive use#
> miraj-cli --root C:\data\miraj --timing
Miraj 1.0 Enterprise - tapez \help pour l'aide, \q pour quitter.
miraj> \l
+----------+
| Database |
+----------+
| shop |
+----------+
1 ligne(s) (0.42 ms)
miraj> USE shop;
OK, 0 ligne(s) affectée(s) (0.10 ms)
shop> \d
+-----------+
| Table |
+-----------+
| articles |
+-----------+
1 ligne(s) (0.08 ms)
shop> \d articles
+--------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------+------+-----+---------+----------------+
| id | int | NO | PRI | NULL | auto_increment |
| nom | varchar(50) | NO | | NULL | |
| prix | decimal(10,2) | YES | | NULL | |
+--------+---------------+------+-----+---------+----------------+
3 ligne(s) (0.15 ms)
shop> SELECT nom, prix
-> FROM articles
-> WHERE prix > 10;
+---------+-------+
| nom | prix |
+---------+-------+
| Cahier | 12.90 |
+---------+-------+
1 ligne(s) (0.31 ms)
shop> \json
Résultats au format JSON
shop> SELECT nom, prix FROM articles WHERE prix > 10;
[
{"nom": "Cahier", "prix": 12.90}
]
1 ligne(s) (0.12 ms)
shop> \qThe prompt shows the current database (shop>) or miraj> if no database is selected; it becomes -> while continuing an unfinished multi-line statement.
25.5 Running SQL scripts in batch mode#
Three ways to run a script without going through the interactive REPL:
# File given as an argument: executed, then the console exits
miraj-cli --root C:\data\miraj -d shop migration.sql
# Inline statement or script with -e (several statements separated by ;)
miraj-cli --root C:\data\miraj -e "USE shop; DELETE FROM articles WHERE stock = 0;"
# From the REPL, without leaving the current session
miraj> source migration.sqlA non-zero exit code signals a failed script; combined with --json, the output of a script executed with -e can be consumed directly by another program:
miraj-cli --root C:\data\miraj -d shop --json -e "SELECT id, nom FROM articles" > articles.json
if ($LASTEXITCODE -ne 0) { Write-Error "Échec du script" }25.6 Practical tips#
- Persistent history: each statement submitted in the REPL is appended to
%APPDATA%\MIRAJ\cli-history.sql(or~/.config/MIRAJ/cli-history.sqloutside Windows), with up to 1000 entries kept. This history is reloaded at the next startup and feeds\historyand\!n.--no-history(or-e, which disables it automatically) ignores it, which is useful for scripts called in a loop from a scheduled task. - Timing:
--timing(or\timingduring a session) is useful for quickly comparing two formulations of the same query without instrumenting a client. - JSON format:
--json(or\json) makes it easy to pass results to another command-line tool (ConvertFrom-Jsonin PowerShell,jq, etc.) without writing a dedicated client. - Configuration file: at the first startup,
miraj-cliwrites a commented templatemiraj_config.xmlin the root folder (or at the path given by--config) and regeneratesmiraj_default.xml, the reference of all engine variables. Editingmiraj_config.xmlthen restarting the console applies the settings; an invalid or unreadable file makes the console exit with code2. Unlikemiraj-server, the console does not look for amiraj_config.xmlnext to its executable. - Passwords: a statement that contains a password or an encryption key (
CREATE USER … IDENTIFIED BY,SET PASSWORD, etc.) can still be replayed with\!nin the session but is never written to the history file. - Errors: a failing statement displays
ERREUR <code> (<SQLSTATE>): <message>, followed byposition: line L, column Cwhen the parser knows it. - Warnings: session warnings (truncations, default values applied, etc.) are displayed after the result of each statement that produced any.