Sign in Join
Wiki copytemporary

Database metadata: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11Database metadata is data about a database. In this article, we also cover information about the database connection/server/client.
22
33## SQLDB
44
55### Server & client information
66
77In FPC development versions as of November 2012, you can use *GetConnectionInfo*. See [GetConnectionInfo documentation](http://www.freepascal.org/docs-html/fcl/sqldb/tsqlconnection.getconnectioninfo.html) Clarification:
88
99- *citServerVersion*: the server version is returned in the form of a string formatted as a number (e.g. 0205 representing 2.5)
1010
1111if the T*Connection in question does not support (part of) this, the functions will return empty values.
1212
1313#### Firebird/Interbase specifics
1414
1515- *citServerVersion* returns major and minor numbers only for Firebird (e.g. 0205 for version 2.5.1); it returns 3 numbers for Interbase
1616- *citClientVersion* is not supported and probably will never be supported for Firebird on Linux/*Nix due to missing functionality
1717
1818In addition to the functions above, as of December 2012, the Firebird connector provides the *GetODS* function that shows the ODS major version of the database you are connected to. This ODS version determines what features are available for use - as you can e.g. use a database created with Firebird 2.1 with a Firebird 2.5 server, but the features will be limited to 2.1 functionality.
1919
2020### Schema information
2121
2222Also known as a database's DDL (Data Definition Language), a schema is the representation of the tables, views, procedures and other objects that make up a database.
2323
2424See [Retrieving Schema Information documentation](http://www.freepascal.org/docs-html/fcl/sqldb/retrievingschemainformation.html)
2525
2626*GetSchemaInfoSQL* is not implemented equally for every connector; the Firebird connector currently has most of the functionality implemented; the others (e.g. MS SQL, Sybase, Oracle) have less functionality. Patches are welcome.
2727
2828#### Proposal for extension/uniformization
2929
3030**Note: there were discussions/a proposal on how to harmonize and extend metadata retrieval for SQLDB on the FPC mailing list. Please update the below with all results of that discussion:**
3131
3232Suggest converging to SQL standard INFORMATION_SCHEMA column naming in existing GetSchemaInfoSQL. So for:
3333
3434- stTables use INFORMATION_SCHEMA.TABLES with column naming:
3535
3636`   TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE ('BASE TABLE', 'VIEW', 'GLOBAL TEMPORARY', 'LOCAL TEMPORARY'), ...`
3737
3838(for databases which do not implement INFORMATION_SCHEMA views leave queries to system tables but use above mentioned column naming)
3939
4040- stColumns use INFORMATION_SCHEMA.COLUMNS with column naming:
4141
4242`   TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, ORDINAL_POSITION, COLUMN_DEFAULT, IS_NULLABLE, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, CHARACTER_OCTET_LENGTH, NUMERIC_PRECISION, NUMERIC_SCALE, ..., CHARACTER_SET_NAME, COLLATION_NAME, DOMAIN_NAME, ..., IS_IDENTITY, IS_GENERATED`
4343
4444- stProcedures use INFORMATION_SCHEMA.ROUTINES with column naming:
4545
4646`   SPECIFIC_CATALOG, SPECIFIC_SCHEMA, SPECIFIC_NAME, ROUTINE_CATALOG, ROUTINE_SCHEMA, ROUTINE_NAME, ROUTINE_TYPE ('PROCEDURE', 'FUNCTION',...), ...`
4747
4848Note: On other side Delphi uses slightly different column naming, described in [The_Structure_of_Metadata_Datasets](http://docwiki.embarcadero.com/RADStudio/XE3/en/The_Structure_of_Metadata_Datasets) But tests with Delphi XE MySQL and Interbase DBX drivers shows results which do not match this specification (f.e. for stTables these columns are returned: CatalogName, SchemaName, TableName, TableType (without "_") and without RECNO column) and for **ADO OpenSchema** method these columns are returned:
4949
5050`siTables (adSchemaTables): TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE(widestring)`\
5151`siColumns (adSchemaColumns):   TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, ORDINAL_POSITION, COLUMN_DEFAULT, IS_NULLABLE, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, CHARACTER_OCTET_LENGTH, NUMERIC_PRECISION, NUMERIC_SCALE, ..., CHARACTER_SET_NAME, COLLATION_NAME, DOMAIN_NAME, ..., IS_COMPUTED`\
5252`siIndexes (adSchemaIndexes):   TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, INDEX_CATALOG, INDEX_SCHEMA, INDEX_NAME, PRIMARY_KEY(boolean), UNIQUE(boolean), CLUSTERED(boolean), TYPE(word), FILL_FACTOR, ..., ORDINAL_POSITION, COLUMN_NAME, ... (one row for every column in index)`\
5353`siProcedures (adSchemaProcedures): PROCEDURE_CATALOG, PROCEDURE_SCHEMA, PROCEDURE_NAME, PROCEDURE_TYPE(Smallint), ...`\
5454`siSchemata (adSchemaSchemata): CATALOG_NAME, SCHEMA_NAME, SCHEMA_OWNER, ...`
5555
5656#### Lazarus TSQLQuery metadata tool
5757
5858*GetSchemaInfoSQL* is used in Lazarus: in design mode, if you use the Object Inspector to edit the SQL property of a *TSQLQuery* where the database connection is connected, you can not only edit the text of the SQL query in the SQL Code tab, but also retrieve information in the Metadata tab by filling in some details and pressing on the Play/Go key:
5959
60-<figure>
61-<img src="MetaDataEditor.png" title="MetaDataEditor.png" />
62-<figcaption>MetaDataEditor.png</figcaption>
63-</figure>
60+![](/wiki-images/5/5a/MetaDataEditor.png)
6461
6562### Data dictionary schema information
6663
6764There is a separate FPC data dictionary package that has support for schema information. This package is used in the [[lazdatadesktop]] project supplied with Lazarus.
6865
6966## Database specific information
7067
7168Many databases have either
7269
7370- system tables/views (which you can select from) or
7471- stored procedures
7572
7673that show information about database objects as well as server version etc.
7774
7875Examples are the @@VERSION variables in MS SQL Server and Sybase servers.
7976
8077If your database connection library does not support other means of getting this information, you can use this (and submit a patch/improvement request on the relevant bug tracker for incorporation into SQLDB/Zeos etc)
8178
8279## See also
8380
8481- [[Working With TSQLQuery]] More information about queries
8582
8683---
8784*From the Free Pascal wiki, page "Database metadata", as it stood on 2022-10-03 after a change by Slawek, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
8885Category: Databases
8986Category: FPC
9087Category: Lazarus

Back to history