Database Introspection

Introspect database metadata to discover tables, columns, indexes, procedures, and more

The bx:dbInfo component allows you to introspect database metadata, providing detailed information about database structures, schemas, tables, columns, indexes, foreign keys, stored procedures, and version information. This is essential for dynamic database operations, schema discovery, and database management tools.

📋 Overview

Database introspection in BoxLang enables you to:

  • Discover Schemas - List all databases and schemas in a connection

  • List Tables - Retrieve all tables, views, and system tables

  • Inspect Columns - Get detailed column metadata including types, constraints, and keys

  • Analyze Indexes - View index information for performance optimization

  • Map Foreign Keys - Discover relationships between tables

  • Find Procedures - List available stored procedures

  • Check Versions - Retrieve database and JDBC driver version information

🔧 Basic Usage

The bx:dbInfo component requires two attributes:

  1. type - The type of metadata to retrieve

  2. name - The variable name to store results

bx:dbInfo
    type="tables"
    name="tableList"
    datasource="myDB";

writeDump( tableList );

📊 Metadata Types

Database Names (DBNAMES)

List all databases and schemas accessible through the connection:

Result Columns:

  • DBNAME - Database or schema name

  • TYPE - Either "CATALOG" or "SCHEMA"

Tables

Retrieve information about tables in the database:

Result Columns:

  • TABLE_CAT - Table catalog (may be null)

  • TABLE_SCHEM - Table schema (may be null)

  • TABLE_NAME - Table name

  • TABLE_TYPE - Table type (TABLE, VIEW, SYSTEM TABLE, etc.)

  • REMARKS - Explanatory comment on the table

  • Plus additional database-specific columns

Common Filter Values:

  • TABLE - Regular tables (default)

  • VIEW - Database views

  • SYSTEM TABLE - System tables

  • GLOBAL TEMPORARY - Global temporary tables

  • LOCAL TEMPORARY - Local temporary tables

  • ALIAS - Table aliases

  • SYNONYM - Table synonyms

Columns

Get detailed information about columns in a table:

Result Columns:

  • TABLE_CAT - Table catalog

  • TABLE_SCHEM - Table schema

  • TABLE_NAME - Table name

  • COLUMN_NAME - Column name

  • DATA_TYPE - SQL data type from java.sql.Types

  • TYPE_NAME - Data source dependent type name

  • COLUMN_SIZE - Column size

  • DECIMAL_DIGITS - Decimal digits (for numeric types)

  • IS_NULLABLE - "YES", "NO", or ""

  • COLUMN_DEF - Default value

  • IS_PRIMARYKEY - Boolean, true if primary key

  • IS_FOREIGNKEY - Boolean, true if foreign key

  • REFERENCED_PRIMARYKEY - Referenced column name (if foreign key)

  • REFERENCED_PRIMARYKEY_TABLE - Referenced table name (if foreign key)

  • Plus additional database-specific columns

Foreign Keys

Discover foreign key relationships for a table:

Result Columns:

  • PKTABLE_CAT - Primary key table catalog

  • PKTABLE_SCHEM - Primary key table schema

  • PKTABLE_NAME - Primary key table name

  • PKCOLUMN_NAME - Primary key column name

  • FKTABLE_CAT - Foreign key table catalog

  • FKTABLE_SCHEM - Foreign key table schema

  • FKTABLE_NAME - Foreign key table name

  • FKCOLUMN_NAME - Foreign key column name

  • KEY_SEQ - Sequence number within foreign key

  • UPDATE_RULE - What happens on UPDATE

  • DELETE_RULE - What happens on DELETE

  • FK_NAME - Foreign key name

  • PK_NAME - Primary key name

  • DEFERRABILITY - Can evaluation be deferred until commit

Indexes

Retrieve index information for a table:

Result Columns:

  • TABLE_CAT - Table catalog

  • TABLE_SCHEM - Table schema

  • TABLE_NAME - Table name

  • NON_UNIQUE - Boolean, false if index values must be unique

  • INDEX_QUALIFIER - Index catalog

  • INDEX_NAME - Index name

  • TYPE - Index type (tableIndexStatistic, tableIndexClustered, etc.)

  • ORDINAL_POSITION - Column sequence within index

  • COLUMN_NAME - Column name

  • ASC_OR_DESC - "A" for ascending, "D" for descending

  • CARDINALITY - Number of unique values in index

  • PAGES - Number of pages used for index

  • FILTER_CONDITION - Filter condition, if any

Stored Procedures

List stored procedures available in the database:

Result Columns:

  • PROCEDURE_CAT - Procedure catalog

  • PROCEDURE_SCHEM - Procedure schema

  • PROCEDURE_NAME - Procedure name

  • REMARKS - Explanatory comment

  • PROCEDURE_TYPE - Procedure type (procedureResultUnknown, procedureNoResult, procedureReturnsResult)

Database Version

Get version information about the database and JDBC driver:

Result Columns:

  • DATABASE_PRODUCTNAME - Database product name (e.g., "MySQL", "PostgreSQL")

  • DATABASE_VERSION - Database version string

  • DRIVER_NAME - JDBC driver name

  • DRIVER_VERSION - JDBC driver version

  • JDBC_MAJOR_VERSION - JDBC major version number

  • JDBC_MINOR_VERSION - JDBC minor version number

🎯 Complete Examples

Dynamic Table Browser

Schema Documentation Generator

Table Relationship Mapper

Database Comparison Tool

🛠️ Component Attributes

Attribute
Type
Required
Description

type

string

Yes

Type of metadata: "dbnames", "tables", "columns", "foreignkeys", "index", "procedures", or "version"

name

string

Yes

Variable name to store the result query

datasource

string

No

Datasource name (uses default if not specified)

table

string

Conditional

Table name (required for "columns", "foreignkeys", "index")

pattern

string

No

Table name pattern using wildcards (%, _) or schema.table syntax

dbname

string

No

Database name (defaults to connection database)

filter

string

No

Table type filter (TABLE, VIEW, etc.) - only for type="tables"

💡 Best Practices

Pattern Matching

  • Use wildcards for flexible table matching: user_% matches all tables starting with "user_"

  • Specify schema when needed: public.% for all tables in public schema

  • Cache metadata for static schemas to avoid repeated queries

Performance

  • Filter by pattern instead of retrieving all tables

  • Use specific queries (columns for one table vs all tables)

  • Cache results for metadata that doesn't change often

  • Limit scope with database and schema names

Error Handling

  • Check table existence before querying columns

  • Handle empty results gracefully

  • Validate user input before using in patterns

  • Test with different databases - metadata structure varies

Security

  • Limit permissions - don't grant metadata access unnecessarily

  • Sanitize patterns - prevent injection via pattern strings

  • Audit usage - log metadata queries in production

  • Filter sensitive tables from user-facing tools

🚫 Common Pitfalls

Not handling NULL values in metadata

Forgetting required attributes

Not checking for empty results

Last updated

Was this helpful?