# Path: nrcif/msn_records.py



from ..msn_records import layouts
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#

'''Generate SQL that will create a suitable schema for storing data
from ATOC .MSN timetable files. This is done dynamically to ensure it
keeps in sync with the definitions in nrcif.py and nrcif_fields.py'''



def gen_sql(DDL, CONS):

    SCHEMA = "msn"

    DDL.write('-- SQL DDL for data extracted from ATOC .MSN timetable files\n'
              '-- in NR CIF format. Auto-generated by schemagen_msn.py\n\n')

    CONS.write('-- SQL constraints & indexes definitions for data extracted\n'
               '-- from ATOC .MSN timetable files in NR CIF format. \n'
               '-- Auto-generated by schemagen_msn.py\n\n')

    DDL.write('CREATE SCHEMA {0};\n'
              'SET search_path TO {0},public;\n\n'.format(SCHEMA))
    CONS.write("SET search_path TO {0},public;\n\n".format(SCHEMA))

    normal_template = "CREATE TABLE {} (\n"

    for i in ('A', 'L', 'V'):
        tablename = layouts[i].name.lower().replace(" ", "_")
        DDL.write(normal_template.format(tablename))

# Path: nrcif/msn_records.py



# Path: nrcif/schema/schemagen_msn.py
from ..msn_records import layouts

#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#

'''Generate SQL that will create a suitable schema for storing data
from ATOC .MSN timetable files. This is done dynamically to ensure it
keeps in sync with the definitions in nrcif.py and nrcif_fields.py'''



def gen_sql(DDL, CONS):

    SCHEMA = "msn"

    DDL.write('-- SQL DDL for data extracted from ATOC .MSN timetable files\n'
              '-- in NR CIF format. Auto-generated by schemagen_msn.py\n\n')

    CONS.write('-- SQL constraints & indexes definitions for data extracted\n'
               '-- from ATOC .MSN timetable files in NR CIF format. \n'
               '-- Auto-generated by schemagen_msn.py\n\n')

    DDL.write('CREATE SCHEMA {0};\n'
              'SET search_path TO {0},public;\n\n'.format(SCHEMA))
    CONS.write("SET search_path TO {0},public;\n\n".format(SCHEMA))

    normal_template = "CREATE TABLE {} (\n"

    for i in ('A', 'L', 'V'):
        tablename = layouts[i].name.lower().replace(" ", "_")
        DDL.write(normal_template.format(tablename))

        DDL.write(layouts[i].generate_sql_ddl())