Question
· Jun 18

Databases inside a Namespace

Hi,

I want to write a program to:

- list all defined namespaces

- for each namespace, the databases making up the namespace

Python would be ideal but ObjectScript is OK.

Would someone know how to do this?

Thanks

Discussion (4)3
Log in or sign up to continue

Here is a programmatic solution to list all defined namespaces and their databases using both ObjectScript and Python:

ObjectScript Code

ClassMethod ListNamespacesAndDatabases()
{
    // List all defined namespaces
    Set namespaces = ##class(%SYS.Namespace).ListAll(.result)
    zwrite result

    // For each namespace, get its associated databases
    For i = 1:1:result.Count() {
        Set nsName = result.GetAt(i)
        Write "Namespace: ", nsName, !
        Set dbs = ##class(%SYS.Namespace).Databases(nsName, .dbInfo)
        zwrite dbInfo
    }
}

This code lists namespaces and retrieves their database configurations using the appropriate methods provided by the %SYS.Namespace class [1][2].


Python Code

To do this in Python, you need to work within the context of an InterSystems IRIS Python environment.

import iris

def list_namespaces_and_databases():
    result = []
    # List all namespaces
    namespaces = iris.cls('%SYS.Namespace').ListAll()
    for ns in namespaces:
        databases = iris.cls('%SYS.Namespace').Databases(ns)
        db_info = {key: value for key, value in databases.items()}
        result.append({'namespace': ns, 'databases': db_info})

    return result

# Usage
namespaces_and_databases = list_namespaces_and_databases()
for item in namespaces_and_databases:
    print(f"Namespace: {item['namespace']}")
    for db, info in item['databases'].items():
        print(f" Database: {db}, Info: {info}")

This Python example uses the iris module to interact with the InterSystems IRIS system. It retrieves both the namespaces and their associated databases using the same functionalities as ObjectScript [1][3].


These code snippets allow you to programmatically list namespaces and their databases from InterSystems IRIS. Let me know if you need further clarification or assistance!

Sources: