Skip to content

Metadata APIs

tashda edited this page Mar 9, 2026 · 1 revision

Metadata APIs

SQLServerNIO provides an immense surface area for querying system metadata natively without requiring you to stitch together fragile sys.* and INFORMATION_SCHEMA queries.

Access these tools on any client or connection.

Introspecting Database Objects

Tables, Views, Columns

Retrieve deep definitions including default values, constraints, and standard collation formatting:

// List all tables
let tables = try await client.listTables(schema: "dbo", includeComments: true)

// List detailed column configurations for a specific table
let columns = try await client.listColumns(schema: "dbo", table: "Employees")

for col in columns {
    print("Column: \(col.name) [\(col.typeName)]")
    print("Nullable: \(col.isNullable), Computed: \(col.isComputed)")
}

Stored Procedures & Functions

Retrieve routines and their required input/output parameters.

let procedures = try await client.listProcedures(schema: "dbo")
let functions = try await client.listFunctions(schema: "dbo")

let parameters = try await client.listParameters(schema: "dbo", object: "usp_GenerateReport")
for param in parameters {
    print("Arg: \(param.name) (\(param.dataType)), Direction: \(param.isReturnValue ? "RETURN" : "INPUT")")
}

Relational Structure (Keys and Indexes)

Determine how tables relate and how they are tuned.

// Primary keys
let pks = try await client.listPrimaryKeys(schema: "dbo", table: "Users")

// Foreign Keys
let fks = try await client.listForeignKeys(schema: "dbo", table: "Orders")
for fk in fks {
    print("\(fk.columnName) references \(fk.referencedTable).\(fk.referencedColumnName)")
}

// Indexes
let indexes = try await client.listIndexes(schema: "dbo", table: "Users")
for idx in indexes {
    print("Index: \(idx.name) (Unique: \(idx.isUnique))")
}

Dependency Graphs

Find out what objects reference a target object (e.g. what views use this table? what triggers watch this view?).

let dependencies = try await client.listDependencies(schema: "dbo", object: "Employees")

Fetching Raw Object Definitions (Source Code)

If you need the actual T-SQL definition script (the source code) used to create a View, Function, Procedure, or Trigger:

// Fetch a single object's source text
let sproc = try await client.fetchObjectDefinition(schema: "dbo", name: "usp_LoginUser", kind: .procedure)
print(sproc?.definition ?? "Object not found")

// Bulk fetch using Identifiers
let identifiers = [
    SQLServerMetadataObjectIdentifier(schema: "dbo", name: "vw_ActiveUsers", kind: .view),
    SQLServerMetadataObjectIdentifier(schema: "ops", name: "trg_Audit", kind: .trigger)
]
let definitions = try await client.fetchObjectDefinitions(identifiers)

Note: You can globally instruct metadata listing commands to automatically eagerly-load these definitions by altering the connection config: configuration.connection.metadataConfiguration.includeRoutineDefinitions = true.


Global Metadata Search

Search across the entire database for object names, source code definitions, column names, or indexes in a single command.

let hits = try await client.searchMetadata(
    query: "PasswordHash",
    scopes: [.objectNames, .columns, .definitions]
)

for hit in hits {
    print("Found match in \(hit.matchKind): \(hit.schema).\(hit.name)")
}

Clone this wiki locally