# Path: nrcif/tsi_reader.py
# class TSI(object):
#     '''A simple hander for TSI files.'''
# 
#     layout = [TextField("Station code", 3),
#               TextField("Arriving train TOC", 2),
#               TextField("Departing train TOC", 2),
#               IntegerField("Minimum Interchange Time", 2),
#               VarTextField("Comments", 100)]
# 
#     def __init__(self, cur):
# 
#         self.cur = cur
# 
#     def process(self, record):
#         '''TSI files are in a simple CSV format with five columns. This
#         function takes in a record and produces the appropriate SQL to insert
#         the data into the database.'''
# 
#         fields = record.rstrip().split(",")
#         if len(fields) != 5:
#             raise ValueError("Line {} in the TSI file does not have "
#                              "five fields".format(record))
# 
#         result = [None] * 5
#         for i in range(0, 5):
#             result[i] = self.layout[i].read(fields[i])
# 
#         self.cur.execute("INSERT INTO tsi.tsi VALUES(%s,%s,%s,%s,%s);", result)


from ..tsi_reader import TSI
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  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 .TSI timetable files. This is done dynamically to ensure it
keeps in sync with the definitions in nrcif.py, tsi_reader.py and
nrcif_fields.py'''



def gen_sql(DDL, CONS):

    SCHEMA = "tsi"

    DDL.write('-- SQL DDL for data extracted from ATOC .TSI interchange \n'
              '-- files in CSV format. Auto-generated by schemagen_tsi.py\n\n')

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

    DDL.write('''-- Only one table\n''')
    DDL.write("CREATE TABLE tsi (\n")

    first_field = False
    for i in TSI.layout:
        if not first_field:

# Path: nrcif/tsi_reader.py
# class TSI(object):
#     '''A simple hander for TSI files.'''
# 
#     layout = [TextField("Station code", 3),
#               TextField("Arriving train TOC", 2),
#               TextField("Departing train TOC", 2),
#               IntegerField("Minimum Interchange Time", 2),
#               VarTextField("Comments", 100)]
# 
#     def __init__(self, cur):
# 
#         self.cur = cur
# 
#     def process(self, record):
#         '''TSI files are in a simple CSV format with five columns. This
#         function takes in a record and produces the appropriate SQL to insert
#         the data into the database.'''
# 
#         fields = record.rstrip().split(",")
#         if len(fields) != 5:
#             raise ValueError("Line {} in the TSI file does not have "
#                              "five fields".format(record))
# 
#         result = [None] * 5
#         for i in range(0, 5):
#             result[i] = self.layout[i].read(fields[i])
# 
#         self.cur.execute("INSERT INTO tsi.tsi VALUES(%s,%s,%s,%s,%s);", result)


# Path: nrcif/schema/schemagen_tsi.py
from ..tsi_reader import TSI

#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  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 .TSI timetable files. This is done dynamically to ensure it
keeps in sync with the definitions in nrcif.py, tsi_reader.py and
nrcif_fields.py'''



def gen_sql(DDL, CONS):

    SCHEMA = "tsi"

    DDL.write('-- SQL DDL for data extracted from ATOC .TSI interchange \n'
              '-- files in CSV format. Auto-generated by schemagen_tsi.py\n\n')

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

    DDL.write('''-- Only one table\n''')
    DDL.write("CREATE TABLE tsi (\n")

    first_field = False
    for i in TSI.layout:
        if not first_field:

            first_field = True