def updateTrunkInformation():
    #Get existing information:
    am = AsteriskMySQLManager()
    am.connect('nextor_tarificador')
    sql = "SELECT * from tarifica_provider"
    am.cursor.execute(sql)
    existing_providers = am.cursor.fetchall()

    #Get current providers on asterisk's db
    todays_providers = am.getTrunkInformation()
    new_providers = []
    for today_p in todays_providers:
        existing = False
        for ex_p in existing_providers:
            if today_p['trunkid'] == ex_p['asterisk_id']:
                #Same provider, we just update its name:
                if today_p['name'] != ex_p['asterisk_name']:
                    print "Provider", ex_p['name'], '(saved as', ex_p[
                        'asterisk_name'], ') has changed. New name is', today_p[
                            'name']
                    ex_p['asterisk_name'] = today_p['name']
                existing = True
        if not existing:
            #Didn't exist before, so we create one:
            new_provider = (
                today_p['trunkid'],
                today_p['name'],
                today_p['name'],
                today_p['tech'],
                today_p['channelid'],
            )
            new_providers.append(new_provider)
            print "New provider found:", today_p['name']
    #Now, we update all previously existing providers and save the new ones
    am.connect('nextor_tarificador')
    for ex_p in existing_providers:
        sql = "UPDATE tarifica_provider \
            SET asterisk_name = %s \
            WHERE id = %s"

        am.cursor.execute(sql, (ex_p['asterisk_name'], ex_p['id']))
        print "Provider", ex_p['name'], "updated."

    sql = "INSERT INTO tarifica_provider \
    (asterisk_id, asterisk_name, name, provider_tech, asterisk_channel_id) \
    VALUES(%s, %s, %s, %s, %s)"

    totalRowsSaved = am.cursor.executemany(sql, new_providers)
    am.db.commit()
    print "----------------------------------------"
    print totalRowsSaved, "new providers saved."
def updateTrunkInformation():
    #Get existing information:
    am = AsteriskMySQLManager()
    am.connect('nextor_tarificador');
    sql = "SELECT * from tarifica_provider"
    am.cursor.execute(sql)
    existing_providers = am.cursor.fetchall()

    #Get current providers on asterisk's db
    todays_providers = am.getTrunkInformation()
    new_providers = []
    for today_p in todays_providers:
        existing = False
        for ex_p in existing_providers:
            if today_p['trunkid'] == ex_p['asterisk_id']:
                #Same provider, we just update its name:
                if today_p['name'] != ex_p['asterisk_name']:
                    print "Provider", ex_p['name'],'(saved as',ex_p['asterisk_name'],') has changed. New name is',today_p['name']
                    ex_p['asterisk_name'] = today_p['name']
                existing = True
        if not existing:
            #Didn't exist before, so we create one:
            new_provider = (
                today_p['trunkid'],
                today_p['name'],
                today_p['name'],
                today_p['tech'],
                today_p['channelid'],
            )
            new_providers.append(new_provider)
            print "New provider found:", today_p['name']
    #Now, we update all previously existing providers and save the new ones
    am.connect('nextor_tarificador');
    for ex_p in existing_providers:
        sql = "UPDATE tarifica_provider \
            SET asterisk_name = %s \
            WHERE id = %s"
        am.cursor.execute(sql, (ex_p['asterisk_name'], ex_p['id']))
        print "Provider", ex_p['name'], "updated."

    sql = "INSERT INTO tarifica_provider \
    (asterisk_id, asterisk_name, name, provider_tech, asterisk_channel_id) \
    VALUES(%s, %s, %s, %s, %s)"
    totalRowsSaved = am.cursor.executemany(sql, new_providers)
    am.db.commit()
    print "----------------------------------------"
    print totalRowsSaved, "new providers saved."