Package: odbc2orm
odbc2ormThis package convert an ODBC schema into a sqlalchemy ORM schema.
Its primary written for MS Access schemas, but it should work for all ODBC
relational databases. for this only the driver should be configured. And for
the target project extra packages may be needed (seeRequired packages for target project).LicenceThis package is under GNU GENERAL PUBLIC LICENSE, Version 2 only.
Therefore article 9 of "GNU GENERAL PUBLIC LICENSE, Version 2, June 1991" does not apply.See LICENCE file for the GNU GENERAL PUBLIC LICENSE, Version 2, June 1991Required packages for target projectAssummingsqlalchemyis already in your project. The following two
packages are required when accessing MS Access databases;sqlalchemy-accesspyodbcFor other ODBC database connections other packages may be required.
Currently this package is tested with MS Access databases.Usage>python-modbc2ormdatabase.mdbOptionsThe following options are available at command line.-v                      Verbose output.

-V/--version            version output 

-h/--help               This help information.

-o/--output <filename>  write Python code file, instead of stdout.
                        default STDOUT   

-c/--config <filename>  Configure driver, output and template files.

-d/--driver <driver>    The ODBC driver to be used,
                        default "Microsoft Access Driver (*.mdb, *.accdb)"

-D/--dump               Include the dump_tables() function in the output,
                        default: False

-t/--template <folder>  Create template files and configuration YAML file
                        for personal customizing.When using a configuration file--outputand--drivermay override the configuration
by setting those after the configuration option.ConfigurationA default configuration file can be created through the--templateoption.Typical configuration file looks like this;driver:Microsoft Access Driver (*.mdb, *.accdb)template:leadin:custom\template\leadin,templleadout:custom\template\leadout.templtable_column:custom\template\table_column.templtable_leadin:custom\template\table_leadin.templtable_leadout:custom\template\table_leadout.templverbose:falseinclude_dump:false,Thetemplatesection are the Mako template files to generate the ORM schema
Python file.TemplatesBy using the--templatecommand line option the standard templates and configuration
is exported to a specified folder. This gives the opportunity to customize the generation
of the output of the application.leadin and leadoutThe leadin and leadout templates receive the following variables;config; dictionary with configuration as being used during generation.datetime; Python datetime module.version; string with the version of odbf2orm module.copyright; string with the copyright of odbf2orm module.author; string with the author of odbf2orm module.username; string with the current username from the Operating System.table_leadin and table_leadoutThe table_leadin and table_leadout templates receive the following variables;config; dictionary with configuration as being used during generation.table; OdbcTable objecttable_columnThe table_column template receive the following variables;config; dictionary with configuration as being used during generation.table; OdbcTable objectcolumn; OdbcColumn objectODBC objectsOdbcTableThe OdbcTable object is actual a list object with extra properties, the list contains the OdbcColumn objects.Extra properiesname; string with the table name.catalog; string with catalog name where the table belong to.schema; string with schema name where the table belong to.OdbcColumnThe OdbcColumn object contains the column properies;table_cat; string with catalog name where the column belong to.schema; string with schema name where the column belong to.table_name; string with the table name.column_name; string with column name.data_type; integer with the column data type, see for more informationhttps://github.com/mkleehammer/pyodbc/wiki/Data-Typestype_name; string with the column type name, see for more informationhttps://github.com/mkleehammer/pyodbc/wiki/Data-Typescolumn_size; the number of octets that are used to store the column databuffer_length; the number of octets that are used for the internal buffer in pyodbcdecimal_digit; the number of decimal fraction digitis.num_spec_radix; ?nullable; integer (1/0) (True/False) if the column is nullable.remarks; string a description of the column.column_default: the column dafault valuesql_data_type; ?sql_datetime_sub; ?char_octet_length; integerordinal_position; integer with the column position in the row.is_nullable; string ("YES"/"NO") if the column is nullable.ordinal; integer same asordinal_position.For some databases there are more properies available, those are not supported.Typical outputThe following is a simple example of the output the odbc2orm tool shall generate with the
default builtin templates.#
# This file was created by odbc2orm.py 1.0.5 Copyright (C) 2023 Marc Bertens-Nguyen
# odbc2orm.py comes with ABSOLUTELY NO WARRANTY. This is free software,
# and you are welcome to redistribute under GNU General Public License, version 2 only
#
# Source file created: username at 2023-04-16 15:06:57
# from test-database.mdb with driver Microsoft Access Driver (*.mdb, *.accdb)
#
from typing import Optional
from sqlalchemy import engine, create_engine
from sqlalchemy import Column, String, Integer, Text, Boolean, DECIMAL, FLOAT
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker


Base = declarative_base()


class SomeTable( Base ):
    __tablename__            = 'SomeTable'
    Id                       = Column( Integer, primary_key = True )
    Name                     = Column( String( 50 ), nullable = True )
    Description              = Column( String( 255 ), nullable = True )
    Filename                 = Column( String( 200 ), nullable = True )
    DataFormat               = Column( Integer, nullable = True )

    def __repr__(self):
        return f"<SomeTable {self.Id}, Name = '{self.Name}', Description = '{self.Description}', Filename = '{self.Filename}'>"


__engine = None
__session = None
__connection = None


def getEngine( database: Optional[str] = None ):
    driver = 'Microsoft Access Driver (*.mdb, *.accdb)'
    if not database:
        # This the file with this file was created.
        database = 'G:\emagic\equens.mdb'

    connection_string = f"DRIVER={{{driver}}};DBQ={database};;ExtendedAnsiSQL=1;"
    connection_url = engine.URL.create( "access+pyodbc",
                                        query = { "odbc_connect": connection_string } )
    global __engine, __connection
    __engine = create_engine( connection_url )
    __connection = __engine.connect()
    return __connection


def getConnection():
    global __connection
    return __connection


def getSession():
    global __engine, __session
    if not __engine:
        raise Exception( "Connection not opened yet." )

    if __session:
        return __session

    SessionObject = sessionmaker( bind = __engine )
    __session = SessionObject()
    return __session


def dump_tables():
    getEngine()
    session = getSession()
    # Dump contents of table 'FileTp1'
    for rec in session.query( FileTp1 ).all():
        print( rec )

    return


if __name__ == '__main__':
    dump_tables()