import dbCommands as db

###################################################################################################
# VARIABLES
####################################################################################################

# This list must represent what the database contains as tables.
connection  = db.mariadb.connections.Connection
cursor = db.mariadb.cursors.Cursor

# Chnages in the databse structure must be reflected here
tables = ["market", "project", "project_element", "segment"]

#Tables with the main element of database table. 
tablesColumnNames = [] #name of each column of the table
tablesPrimaryKeys = [] #Primary key of each table
tablesForeignKeys = [] #Foreign keys of the table (this is a relational database)

####################################################################################################
# INTIALISATION
####################################################################################################

####################################################################################################
# COMMON FUNCTIONS
####################################################################################################
#Connect
def connect(): 
    #Connect to the database
    connection = db.connect("minibase_projects")
    cursor = connection.cursor()
    #Initialise tables wiht information on the server! This will drastically hep reduce server 
    #Requests jus for table indformations
    for table in tables:
        buffer = db.getColumnNames(cursor, table)
        tablesColumnNames.append(buffer)
    
        buffer = db.getPrimaryKey(cursor, table)
        tablesPrimaryKeys.append(buffer)

        buffer = db.getForeignKeys(cursor, table)
        tablesForeignKeys.append(buffer)

#Get the tables contained in this database
def getTables():
    return tables

#Get the foreign keys list of the given table
def getForeignKeys(table):
    return(tablesForeignKeys[tables.index(table)])
    
#Get the amount of foreign key of the given table
def getForeignKeyCount(table):
    return(len(tablesForeignKeys[tables.index(table)]))

#Get the index of a foreign key's name contained in the given table
def getForeignKeyIndex(table, keyName):
    return(tablesForeignKeys[tables.index(table)].index(keyName))

#Get the name of a foreign key's index contained in the given table
def getForeignKeyName(table, keyIndex):
    return(tablesForeignKeys[tables.index(table)][keyIndex])

#Get the primary key's name of the given table
def getPrimaryKey(table): 
    return(tablesPrimaryKeys[tables.index(table)])

#Get the column Names list of the given table
def getColumnNames(table):
    return(tablesColumnNames[tables.index(table)])

#Get the amount of column's of the given table
def getColumnCount(table):
    return(len(tablesColumnNames[tables.index(table)]))

#Get the index of a column's name contained in the given table
def getColumnNameIndex(table, columnName):
    return(tablesColumnNames[tables.index(table)].index(columnName))
    
#Get the name of a columns's index contained in the given table
def getColumnName(table, columnIndex):
    return(tablesColumnNames[tables.index(table)][columnIndex])

def commit():
    connection.commit()

def close():
     connection.close()
     return 0

def getDataTable(table):
    tableData = db.select(cursor,table,"*")
    return tableData

def getDataTableRowsByName(table, rowName):
    tableData = db.selectWhere(cursor,table,"*","name",rowName)

def getDataTableRowsById(table, rowId):
    tableData = db.selectWhere(cursor,table,"*","id",str(rowId))

####################################################################################################
# DEBUG FUNCTIONS
####################################################################################################
def printTablesInfo():
    for table in getTables():
        print( "Table: " + table)
        print( " - Primary Key : " + getPrimaryKey(table))
        for column in getColumnNames(table):
            print( " - Colum: " + column)
        for key in getForeignKeys(table):
            print( " - Foreign Key : " + key)

####################################################################################################
# TABLE SPECIFIC FUNCTIONS
####################################################################################################
def insertMarket(name): 
    command="'"+name+"', '"+str(db.currentTime)+"'"
    db.insert(cursor,"market", "name, upload_date",command) 
    commit()

#getDataTable("market")
#getDataTableRowsByName(tables[0],"Industrial")
#getDataTableRowsById(tables[0],19)

#commit()
#close()