# Path: nrcif/records.py

# 
# Path: nrcif/ztr_reader.py
# class ZTR(nrcif.mca_reader.MCA):
#     def __init__(self, cur):


from ..records import layouts as mca_layouts
from ..ztr_reader import reduced_hd, corrected_bx
'''Generate SQL that will create a suitable schema for storing data
from ATOC .ZTR timetable files. This is done dynamically to ensure it
keeps in sync with the definitions in nrcif.py, ztr_reader.py and
nrcif_fields.py'''




def gen_sql(DDL, CONS):

    SCHEMA = "ztr"

    layouts = mca_layouts.copy()
    layouts["HD"] = reduced_hd
    layouts["BX"] = corrected_bx

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

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

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

    DDL.write("-- The BS, BX and TN records are stored in the same table\n")
    DDL.write("CREATE TABLE basic_schedule (\n")


# Path: nrcif/records.py

# 
# Path: nrcif/ztr_reader.py
# class ZTR(nrcif.mca_reader.MCA):
#     def __init__(self, cur):


# Path: nrcif/schema/schemagen_ztr.py
from ..records import layouts as mca_layouts
from ..ztr_reader import reduced_hd, corrected_bx

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




def gen_sql(DDL, CONS):

    SCHEMA = "ztr"

    layouts = mca_layouts.copy()
    layouts["HD"] = reduced_hd
    layouts["BX"] = corrected_bx

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

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

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

    DDL.write("-- The BS, BX and TN records are stored in the same table\n")
    DDL.write("CREATE TABLE basic_schedule (\n")


    DDL.write(layouts['BS'].generate_sql_ddl()+",\n")