SqlNotebook is the Windows Forms GUI application that provides a Jupyter-style notebook interface for interactive SQL data exploration. It is the main user-facing project in the solution.
+---------------------------------------------------------------+
| MainForm |
| +----------+ +--------------------------------------+ |
| | Explorer | | Document Area | |
| | Control | | +------------+ +------------------+ | |
| | (tree of | | | PageControl| | QueryDocControl | | |
| | pages, | | | (blocks) | | (SQL editor + | | |
| | scripts,| | | | | results grid) | | |
| | tables, | | +------------+ +------------------+ | |
| | views) | | +------------------+ | |
| | | | | TableDocControl | | |
| | | | | (table viewer) | | |
| +----------+ +--------------------------------------+ |
| +----------------------------------------------------+ |
| | ConsoleControl (interactive query console) | |
| +----------------------------------------------------+ |
+---------------------------------------------------------------+
| |
NotebookManager UserOptions
|
SqlNotebookScript.Notebook
- Root - Main form, entry point, core controls, and utilities
- Pages/ - Page/block UI system (notebook pages with text and query blocks)
- Import/ - Data import wizards and infrastructure
- Csv/ - CSV/TSV import wizard
- Xls/ - Excel import wizard
- Database/ - External database import (SQL Server, PostgreSQL, MySQL, DuckDB, SQLite)
Application startup sequence:
- Set High DPI mode (
SystemAware) - Enable visual styles and register code page encoding provider
- Call
Notebook.InitSqlite()to initialize the native SQLite engine - Open a notebook from command-line argument, or create a new one via
Notebook.New() - Launch
MainFormwith the notebook instance - On exit: clean up temporary files and shut down SQLite
Inherits from ZForm (a custom Form base class). Uses WeifenLuo.WinFormsUI.Docking (DockPanel) for a Visual Studio-style docking layout.
Layout:
- Menu bar: File, Edit, View, Help menus plus a search textbox (Ctrl+H) and update notification
- Left dock:
ExplorerControl- tree view of notebook items (Pages, Scripts, Tables, Views) with schema browser - Center: Document area with tabbed docking for
PageControl,QueryDocumentControl,TableDocumentControl - Bottom (auto-hide):
ConsoleControl- interactive SQL console with output history
Key responsibilities:
- Owns the
NotebookManagerinstance - Handles menu commands (New, Open, Save, Import, Export, etc.)
- Coordinates document opening/closing and dirty state tracking
- Manages the docking panel layout and theme (
VS2012Light)
Central coordinator between the UI and the SqlNotebookScript.Notebook engine. Provides:
- Item management: Create, rename, delete notebook items (pages, scripts, tables, views)
- Script execution:
ExecuteScript()wrapsScriptParser+ScriptRunnerto parse and run SQL, returningScriptOutput - Transaction tracking: Monitors
_isTransactionOpenvia aSlot<bool>to disable Save/Import during open transactions - Schema snapshots:
DatabaseSchemacaptures current table/column structure for UI display - Events: Publishes
NotebookChange,NotebookItemOpenRequest,NotebookItemRename,NotebookItemDeleteevents
All dockable document types implement IDocumentControl:
A notebook page containing an ordered list of blocks in a FlowLayoutPanel:
- TextBlockControl: Rich text content block
- QueryBlockControl: SQL code block with inline results display. Uses GDI+ custom painting for result rendering. Configurable: show/hide SQL, show/hide results, max display rows.
- DividerBlockControl: Clickable divider between blocks for inserting new blocks
Pages support edit mode (rearrange, add, delete blocks) and view mode (run all queries, display results).
Full-screen SQL editor for scripts. Contains a QueryEmbeddedControl which combines:
SqlTextControl(Scintilla-based SQL editor) at the top- Tabbed results area (DataGridView) at the bottom
- Execute with F5
Read-only table viewer. Displays table contents in a DataGridView when opened. Implements IDocumentControlOpenNotification to load data lazily on first display.
Wraps ScintillaNET (Scintilla text editor component) with SQL Notebook-specific configuration:
- Syntax highlighting using SQLite tokenizer for accurate keyword/string/number coloring
- F5 key binding for query execution
- Line numbers, word wrap, and configurable fonts
Left-panel tree view organized into sections:
- Pages - Notebook pages (double-click opens
PageControl) - Scripts - Stored SQL scripts (double-click opens
QueryDocumentControl) - Tables - Database tables with expandable column lists
- Views - Database views with expandable column lists
Supports context menus for rename, delete, and import operations. Refreshes on NotebookChange events.
Bottom-panel interactive SQL execution console:
- Text input area for ad-hoc queries
- Scrolling output history showing results as DataGridViews
- Limits display to 10,000 rows per query to prevent memory exhaustion
Dispatches file imports based on extension:
.csv,.tsv,.txt->ImportCsvForm.xls,.xlsx->ImportXlsForm.sqlite3,.sqlite,.db->SQLiteImportSession->DatabaseImportTablesForm.duckdb->DuckDBImportSession->DatabaseImportTablesForm
Menu-driven database imports dispatch to DatabaseConnectionForm -> DatabaseImportTablesForm.
Multi-step wizard:
- Options: Delimiter, encoding, header row handling
- Columns: Column name mapping and type selection (with automatic type detection via
TypeDetection.cs) - Preview: Shows the generated
IMPORT CSVSQL statement
Two-step wizard:
- Select worksheets to import
- Column mapping and type selection
Generic framework for importing from external databases:
IImportSessioninterface with implementations for each database vendorDatabaseConnectionForm: Build connection stringsDatabaseImportTablesForm: Select tables to import (with rename support)DatabaseImportScriptForm: Choose between linking (virtual table) and copyingDatabaseImportCustomQueryForm: Import via custom SQL query
Automatic column type inference from sample data. Checks in priority order: Integer -> Real -> DateTime/Date -> Text. Uses a flags-based approach to handle ambiguous columns.
Modal dialog for exporting notebook items:
- Select a Script, Table, or View
- Export as SQL text or CSV data
- Uses standard
SaveFileDialogfor file selection
Full-text search over embedded HTML documentation:
- On first search, loads all HTML doc files from the
doc/application subdirectory - Creates an in-memory SQLite database with an FTS5 virtual table
- Indexes document titles and content
HelpSearchResultsFormdisplays ranked results
The help doc content is generated at build time by ps1/Update-Docs.ps1 from the doc/ directory and SQLite documentation.
- Ui.cs: DPI-aware layout helper. Defines "x-width" and "x-height" units scaled relative to font metrics for consistent sizing across display densities.
- Bind.cs: Two-way data binding between WinForms controls and
Slot<T>reactive properties. - DataGridViewUtil.cs: Factory for double-buffered
DataGridViewinstances (prevents flicker). - MenuRenderer.cs: Custom
ToolStripRendererfor consistent menu appearance. - UserOptions.cs: Persisted user preferences (fonts, colors) stored in application settings.
- Slot: Reactive property wrapper (defined in SqlNotebookScript.Utils) used for
_isDirty,_isTransactionOpen, and data binding. - WaitForm.cs: Modal progress dialog for async operations with optional cancellation support.
| Dependency | Purpose |
|---|---|
| WeifenLuo.WinFormsUI.Docking | Dockable panel framework (VS2012Light theme) |
| ScintillaNET (fernandreu) | Syntax-highlighted code editor control |
| Windows-API-Code-Pack | Native Windows file/folder dialogs |
| DuckDB.NET, Microsoft.Data.SqlClient, Npgsql, MySql.Data | Database import drivers |
| HtmlAgilityPack | HTML parsing for help documentation |
| Microsoft.Data.Sqlite | SQLite ADO.NET access |
- DockPanel over MDI: Provides flexible, resizable, auto-hideable panels that users can customize, similar to Visual Studio.
- Page blocks as UI model: Pages are
FlowLayoutPanelcontainers of editable blocks, giving a Jupyter-like notebook experience within WinForms. - Custom GDI+ rendering: Query blocks render results using
OnPaintwith GDI+ for efficient display without embedding heavyweight controls. - Scintilla for code editing: Wraps the mature Scintilla C++ editor for syntax highlighting, using the native SQLite tokenizer for accurate coloring.
- Event-driven coordination:
NotebookManagerpublishes events thatMainForm,ExplorerControl, and document controls subscribe to, keeping components decoupled. - Async with WaitForm: Long operations (import, save, open) run on background threads with
WaitFormproviding progress feedback and cancellation.