# Path: nrcif/alf_reader.py
# class ALF(object):
#     '''A simple hander for ALF files.'''
# 
#     layout = collections.OrderedDict()
#     layout["M"] = VarTextChoiceField("Mode", 8, ("BUS", "TUBE", "WALK",
#                                                  "FERRY", "METRO",
#                                                  "TRAM", "TAXI",
#                                                  "TRANSFER"))
#     layout["O"] = TextField("Origin", 3)
#     layout["D"] = TextField("Destination", 3)
#     # changed to avoid clash with keyword
#     layout["T"] = IntegerField("Link Time", 3)
#     layout["S"] = TimeField("Start Time")
#     layout["E"] = TimeField("End Time")
#     layout["P"] = IntegerField("Priority", 1)
#     layout["F"] = DD_MM_YYYYDateField("Start Date")
#     layout["U"] = DD_MM_YYYYDateField("End Date")
#     layout["R"] = DaysField("Days of Week")
# 
#     def __init__(self, cur):
# 
#         self.cur = cur
#         self.sql_insert = "INSERT INTO alf.alf VALUES({})"\
#                           .format(",".join(["%s"]*10))
# 
#     def process(self, record):
#         '''ALF files are in a CSV format with KEY=VALUE in each column. This
#         function takes in a record and produces the appropriate SQL to insert
#         the data into the database.'''
#         fields = record.rstrip().split(",")
# 
#         values = {}
#         for i in fields:
#             key, ignore, value = i.partition("=")
#             values[key] = self.layout[key].read(value)
# 
#         result = []
#         for i in self.layout:
#             if i in values:
#                 result.append(values[i])
#             else:
#                 result.append(None)
# 
#         self.cur.execute(self.sql_insert, result)


from ..alf_reader import ALF
ensure it keeps in sync with the definitions in nrcif.py, alf_reader.py
and nrcif_fields.py'''



def gen_sql(DDL, CONS):

    SCHEMA = "alf"

    DDL.write('-- SQL DDL for data extracted from ATOC .ALF fixed link files\n'
              '-- in CSV format. Auto-generated by schemagen_alf.py\n\n')

    CONS.write('-- SQL constraints & indexes definitions for data extracted \n'
               '-- from ATOC .ALF timetable files in CSV format. \n'
               '-- Auto-generated by schemagen_alf.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('''-- Only one table\n''')
    DDL.write("CREATE TABLE alf (\n")

    first_field = False
    for i in ALF.layout:
        if not first_field:
            first_field = True
        else:
            DDL.write(",\n")
        fieldname = ALF.layout[i].name.lower().replace(" ", "_")

# Path: nrcif/alf_reader.py
# class ALF(object):
#     '''A simple hander for ALF files.'''
# 
#     layout = collections.OrderedDict()
#     layout["M"] = VarTextChoiceField("Mode", 8, ("BUS", "TUBE", "WALK",
#                                                  "FERRY", "METRO",
#                                                  "TRAM", "TAXI",
#                                                  "TRANSFER"))
#     layout["O"] = TextField("Origin", 3)
#     layout["D"] = TextField("Destination", 3)
#     # changed to avoid clash with keyword
#     layout["T"] = IntegerField("Link Time", 3)
#     layout["S"] = TimeField("Start Time")
#     layout["E"] = TimeField("End Time")
#     layout["P"] = IntegerField("Priority", 1)
#     layout["F"] = DD_MM_YYYYDateField("Start Date")
#     layout["U"] = DD_MM_YYYYDateField("End Date")
#     layout["R"] = DaysField("Days of Week")
# 
#     def __init__(self, cur):
# 
#         self.cur = cur
#         self.sql_insert = "INSERT INTO alf.alf VALUES({})"\
#                           .format(",".join(["%s"]*10))
# 
#     def process(self, record):
#         '''ALF files are in a CSV format with KEY=VALUE in each column. This
#         function takes in a record and produces the appropriate SQL to insert
#         the data into the database.'''
#         fields = record.rstrip().split(",")
# 
#         values = {}
#         for i in fields:
#             key, ignore, value = i.partition("=")
#             values[key] = self.layout[key].read(value)
# 
#         result = []
#         for i in self.layout:
#             if i in values:
#                 result.append(values[i])
#             else:
#                 result.append(None)
# 
#         self.cur.execute(self.sql_insert, result)


# Path: nrcif/schema/schemagen_alf.py
from ..alf_reader import ALF

ensure it keeps in sync with the definitions in nrcif.py, alf_reader.py
and nrcif_fields.py'''



def gen_sql(DDL, CONS):

    SCHEMA = "alf"

    DDL.write('-- SQL DDL for data extracted from ATOC .ALF fixed link files\n'
              '-- in CSV format. Auto-generated by schemagen_alf.py\n\n')

    CONS.write('-- SQL constraints & indexes definitions for data extracted \n'
               '-- from ATOC .ALF timetable files in CSV format. \n'
               '-- Auto-generated by schemagen_alf.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('''-- Only one table\n''')
    DDL.write("CREATE TABLE alf (\n")

    first_field = False
    for i in ALF.layout:
        if not first_field:
            first_field = True
        else:
            DDL.write(",\n")
        fieldname = ALF.layout[i].name.lower().replace(" ", "_")

        DDL.write("\t{0}\t\t{1}".format(fieldname, ALF.layout[i].sql_type))