Package wsatools :: Package DbConnect :: Module Schema
[hide private]

Module Schema

source code

Provides access to the database schema. As described in the collection of *.sql files in the sql directory. This allows CUs to become schema driven.

Usage

Case 1 - Retrieving table schema details

Call the parseTables() function to retrieve a dictionary of table schemas from a supplied .sql schema file, e.g. :

 import wsatools.DbConnect.Schema as schema
 tableSchema = schema.parseTables("WSA_MultiframeSchema.sql")
 print(tableSchema[0].name)
 for column in tableSchema[0].columns:
     print(column.name, column.dataType)

Read documentation for Table and Attribute classes to see what data is available and how to access it.

Case 2 - Retrieving table index details

Call the parseIndices() function to retrieve a dictionary of table indices, e.g. :

 import wsatools.DbConnect.Schema    as schema
 from   wsatools.SystemConstants import WsaConstants
 idxInfo = schema.parseIndices(WsaConstants.indexScript)
 db.addIndex(idxInfo["lasSource"][0])
 # to apply just the first or to all indices to lasSource:
 db.createObjects(idxInfo["lasSource"])

Read documentation for Index class to see what data is available and how to access it.

Case 3 - Retrieving view schema details

Call the parseViews() function to retrieve an ordered list of view schemas, e.g. :

 import wsatools.DbConnect.Schema as schema
 db.createObjects(schema.parseViews())

Schema Objects

Class Hierarchy for _Schema
Class Hierarchy for _Schema


Author: R.S. Collins

Organization: WFAU, IfA, University of Edinburgh

To Do:
Classes [hide private]
  Attribute
A table column attribute schema.
  _Schema
Abstract base class representing a schema for any generic database object.
  _Constraint
A generic table constraint schema.
  PrimaryKeyConstraint
A primary key constraint schema.
  ForeignKeyConstraint
A foreign key constraint schema.
  UniquenessConstraint
A uniqueness constraint schema.
  Index
A database non-clustered index schema.
  ClusteredIndex
A database clustered index schema.
  Procedure
A database stored procedure schema.
  Function
A database stored function schema.
  Table
A database table schema.
  View
A database view schema.
    Errors & Exceptions
  MismatchError
Exception thrown if schema mis-matches database.
  ParsingError
Exception thrown if schema parsing fails.
Functions [hide private]
list(Procedure)
parseFuncProcs(schemaFileName)
Parses the supplied .sql schema, returning an ordered list of Procedure and Function objects.
source code
defaultdict(str: list(Index))
parseIndices(schemaFileName='WSA_Indices.sql')
Parses the supplied .sql schema, returning a dictionary of Index objects referenced by table name representing the indices defined that schema script for that table.
source code
list(Table)
parseTables(schemaFileName, tableList=[], reqAllTables=True)
Parses the supplied .sql schema file, returning Table objects for every table described in the file.
source code
list(View)
parseViews(schemaFileName)
Parses the supplied .sql schema, returning an ordered list of View objects.
source code
Attribute
_parseAttribute(detailsList)
Parse the list of properties for this attribute supplied by parseTables() from the .sql schema file.
source code
Variables [hide private]
  __package__ = 'wsatools.DbConnect'
Function Details [hide private]

parseFuncProcs(schemaFileName)

source code 

Parses the supplied .sql schema, returning an ordered list of Procedure and Function objects.

Parameters:
  • schemaFileName (str) - Name of .sql schema file to parse.
Returns: list(Procedure)
A list of Procedure and Function objects ordered as supplied in the script file.

parseIndices(schemaFileName='WSA_Indices.sql')

source code 

Parses the supplied .sql schema, returning a dictionary of Index objects referenced by table name representing the indices defined that schema script for that table.

Parameters:
  • schemaFileName (str) - Name of .sql schema file to parse.
Returns: defaultdict(str: list(Index))
Dictionary of Index object lists for every table, referenced by table name.

parseTables(schemaFileName, tableList=[], reqAllTables=True)

source code 

Parses the supplied .sql schema file, returning Table objects for every table described in the file. (Unless a subset is requested).

Parameters:
  • schemaFileName (str) - Name of .sql schema file to parse.
  • tableList (list(str)) - Optional list of tables to be extracted, by default all tables are extracted.
  • reqAllTables (bool) - If True, an exception is thrown if any tables in the supplied list are missing from the schema.
Returns: list(Table)
A list of Table objects in the order given by the schema file.

Note: The .sql file is assumed to be in the SystemConstants.sqlScriptPath. Include any sub-directory names in schemaFileName, e.g. "NonSurvey/WSA_Schema.sql". Use DataFactory.ProgrammeTable.getSchemaScript() for schemaFileName to parse programme schemas (e.g. lasSchema.sql) as this automatically handles non-surveys correctly.

parseViews(schemaFileName)

source code 

Parses the supplied .sql schema, returning an ordered list of View objects.

Parameters:
  • schemaFileName (str) - Name of .sql schema file to parse.
Returns: list(View)
A list of View objects ordered as supplied in the script file.

_parseAttribute(detailsList)

source code 

Parse the list of properties for this attribute supplied by parseTables() from the .sql schema file. This consists of both SQL attribute and user-defined tag properties.

Parameters:
  • detailsList (list(str)) - The unprocessed attribute line from the .sql file split on whitespace.
Returns: Attribute
The attribute object containing the full schema for this attribute.