Chapter 9. Database metadata access layer module

Table of Contents

Common interface for all meta objects
Retrieving an object's name
Schemas
Fetching the schema list
Checking whether a schema exists
Fetching a specific schema
Retrieving the name of an object's schema
Schemas and qualified names
Tables
Fetching the table list
Checking whether a table exists
Fetching a specific table
Retrieving the name of an object's table
Columns
Fetching a table's columns list
Checking whether a column exists in a table
Fetching a given column from a table
Checking whether a column has a default value
Retrieving the expression of the default value
Checking whether a column is nullable
Retrieving the position of a column in the table
Generating a validator for a column
Primary keys
Checking whether a table has a primary key
Fetching the primary key of a table
Fetching the names of the columns of a primary key
Foreign keys
Fetching a table's foreign keys list
Checking whether a foreign key exists in a table
Fetching a given foreign key from a table
Fetching the names of the columns of a foreign key
Retrieving the name of the referenced table
Retrieving the name of the referenced schema
Retrieving the names of the referenced columns
Commentable objects
Checking whether a database object is commentable
Retrieving the comment of a database object

Sometimes, you don't want to fetch data from a database table. Instead, you want to fetch data about a database table. DbMeta is our answer to this problem. Using this API, you can easily retrieve the columns or the primary key of a table and much more. Useful examples include writing automatically various queries or writing a differential tool to compare two different versions of a table in a database.

All the database drivers and their PDO counterparts have DbMeta support. They all provide access to the tables, columns and primary and foreign keys of the database. The PostgreSQL and Oracle ones also provide methods to retrieve data about database schemas.

This documentation comes in the form of a function reference of everything you can do using DbMeta. The usage is rather straightforward and the examples should explain a lot.

To query the database you must use the DbMeta object. To retrieve an instance of this object for your database, simply call the method meta of your database driver.

<?php

// If you are using the application module
$oMeta = weeApp()->db->meta();

// Otherwise
$oMeta = $oDb->meta();

Common interface for all meta objects

Every database object represented in DbMeta is an instance of weeDbMetaObject.

A database object can be a schema, a table, a column, a primary key, a foreign key, a sequence... and much more depending on the database you use.

Retrieving an object's name

Every database object have a name. You can retrieve this name by using the name method.

<?php

$sName = $o->name();

Alternatively, you can also directly obtain the quoted name of the database object by using the quotedName method. The name returned will be quoted using the escapeIdent method of the associated driver and will thus be safe to use in a query.

<?php

$sQuotedName = $o->quotedName();