#!/usr/bin/env python

import sys
import sqlite3

def max_column_lengths(cursor):
    """Return a dictionary containing the count of the longest piece of data for column"""

    column_len = {}

    # Get the max lengths of text columns from device
    query = """
        select
            max(device_name_lengths)        device_name,
            max(pv_position_lengths)	    pv_position
        from 
            (   select
                    length(device.ZNAME)                device_name_lengths,
                    length(device.ZPVPOSITION)          pv_position_lengths
                from
                    ZDEVICE         device
            );"""
    
    try:
        cursor.execute(query)
        result = cursor.fetchone()
    except sqlite3.Error, e:
        print "get_max_column_lengths:", e.args[0]
        sys.exit(1)
    
    column_len['device_name'] = result['device_name']
    column_len['pv_position'] = result['pv_position']
    
    # Get the max PV Device Type length
    query = """
        select
            max(pv_device_type_lengths)  pv_device_type
        from 
            (select length(pvdevt.ZNAME) pv_device_type_lengths from ZPVDEVICETYPE pvdevt);
        """
    
    try:
        cursor.execute(query)
        result = cursor.fetchone()
    except sqlite3.Error, e:
        print "get_max_column_lengths:", e.args[0]
        sys.exit(1)
    
    column_len['pv_device_type'] = result['pv_device_type']
    
    
    # Get the max PV Device Type length
    query = """
        select
            max(pv_area_lengths) pv_area
        from 
            (select length(pvarea.ZNAME) pv_area_lengths from ZPVAREA pvarea);
        """
    
    try:
        cursor.execute(query)
        result = cursor.fetchone()
    except sqlite3.Error, e:
        print "get_max_column_lengths:", e.args[0]
        sys.exit(1)
    
    column_len['pv_area'] = result['pv_area']
    
    # Get the fault name, pv attribute, ok and fault statenames, and description   
    query = """
        select
            max(fault_name_lengths)         fault_name,
            max(pv_attribute_lengths)	    pv_attribute,
            max(ok_state_name_lengths)	    ok_state_name,
            max(faulted_state_name_lengths)	faulted_state_name,
            max(description_lengths)        description
        from 
            (   select
                    length( fault.ZNAME)                pv_attribute_lengths,
                    length( fault.ZNAME)                fault_name_lengths,
                    length( fault.ZOKSTATENAME)         ok_state_name_lengths,
                    length( fault.ZFAULTEDSTATENAME)    faulted_state_name_lengths,
                    length( fault.ZDESCRIPT)            description_lengths
                from
                    ZFAULT fault
            );
    """
    
    try:
        cursor.execute(query)
        result = cursor.fetchone()
    except sqlite3.Error, e:
        print "get_max_column_lengths:", e.args[0]
        sys.exit(1)
    
    
    column_len['fault_name']         = result['fault_name']
    column_len['pv_attribute']       = result['pv_attribute']
    column_len['ok_state_name']      = result['ok_state_name']
    column_len['faulted_state_name'] = result['faulted_state_name']
    column_len['description']        = result['description']
    
        
    column_len['state_name']         = max(column_len['ok_state_name'], column_len['faulted_state_name'])
    column_len['fault_number']       = 6
    column_len['link_node_id']       = 3
    column_len['card']               = 2
    column_len['channel']            = 2
    column_len['autorecoverable']    = 1
    column_len['debounce_time_us']   = 8
    
    return column_len
    

