def add_group(dn, entry, source):
    group = {
             'cn' : entry['cn'][0],
             'source' : source,
             }
    for dn in entry['member']:
        insert_group = dict(group, **{'member' : dn})
        query = insert(GROUP_TABLE,  insert_group)
        cursor.execute(query)
def add_person(dn, entry, source):
    person = {'dn' : dn}
    for attribute in entry:
        if attribute in person2table.keys():
            # TODO: Fix; Unreadable as f**k.
            person[person2table[attribute]] = entry[attribute][0]
    person['source'] = source
    print person
    query = insert(PEOPLE_TABLE, person)
    cursor.execute(query)
def putkeys():
    cursor.execute("SELECT id, dn FROM thunderpeople")
    people = cursor.fetchall()

    for person in people:
        person_id = person[0]
        person_dn = person[1]

        # Handle email addresses with pesky quotes.
        person_dn = person_dn.replace('"', r'\"') 

        query = 'UPDATE thundergroup SET thunderpeople_id' \
        ' = "%s" WHERE member = "%s"' % (person_id, person_dn)
        print query
        cursor.execute(query)


    query = 'SELECT * FROM thundergroup WHERE thunderpeople_id' \
    ' IS NULL;'
    cursor.execute(query)
    result = cursor.fetchall()
    if result:
        try:
            raise Exception('F*ck thunder bird')
        except Exception:
            print "thundergroup records with no foreign key:"
            for record in result:
                print record
            raise
@author: etherealite
'''
import os, sys
proj_dir = os.path.abspath(os.path.dirname(__file__))
packages_dir = os.path.join(proj_dir, 'packages')
sys.path.append(packages_dir)


import settings
from mysql_env import cursor, db_connect

# Create a new cursor for mysql introspection DB.
info_c = db_connect('information_schema')

cursor.execute("DROP TABLE IF EXISTS column_dups")
create_info = """
    CREATE TABLE column_dups
    (
        id INT(11) AUTO_INCREMENT,
        attribute varchar(60),
        value varchar(60),
        occurrances INT(11),
        table_name varchar(60),
        PRIMARY KEY(id)
    )
"""
cursor.execute(create_info)

get_columns = """
SELECT column_name FROM COLUMNS
    mdb_tablename_sanitized = re.sub('[^\w]+', '_', mdb_tablename)
    final_tablename = database_key

#    # MySQL being a fuckup
#    # Drop all possible conflicting tables
#    drop_query = """DROP IF EXISTS "%s", "%s","%s";""" % (
#            mdb_tablename, mdb_tablename_sanitized, final_tablename
#            )
#    print drop_query
#    cursor.execute(drop_query)

    print "working on database: %s" % final_tablename
    print "inserting table: %s, from file: %s" % (mdb_tablename, mdb_filename)

    create_query = tablefrom_mdb(mdb_tablename, mdb_filename)
    cursor.execute(create_query)

    insert_queries = datafrom_mdb(mdb_filename, mdb_tablename)
    for query in insert_queries:
        cursor.execute(query)

    # Name the table according to the key in the settings dict.
    rename_query = "RENAME TABLE `%s` TO `%s`;" % (
            mdb_tablename_sanitized, final_tablename
            )
    cursor.execute(rename_query)

    addsource_col = """
    ALTER TABLE %s 
    ADD COLUMN source varchar(60),
    ADD COLUMN id int(10) PRIMARY KEY AUTO_INCREMENT FIRST;