<commit_before>import numpy as np

import mysql.connector
import astropy.units as u

from . import filter
from . import utils
from . import config as cfg


def write_all(r,update=False):
    """Write sdf results to db."""

    print(" Database")

    # set up connection
    try:
        cnx = mysql.connector.connect(user=cfg.mysql['user'],
                                      password=cfg.mysql['passwd'],
                                      host=cfg.mysql['host'],
                                      database=cfg.mysql['db_results'])
        cursor = cnx.cursor(buffered=True)

    except mysql.connector.InterfaceError:
        print("   Can't connect to {} at {}".format(cfg.mysql['db_results'],
                                                    cfg.mysql['host']) )
        return

    # write to the tables
    if update or update_needed(cursor,cfg.mysql['model_table'],r):
        
        print("   writing")
        
        cursor.execute("DELETE FROM {} "
                       "WHERE id = '{}'".format(cfg.mysql['phot_table'],r.id))
        write_phot(cursor,r)

        cursor.execute("DELETE FROM {} "
                       "WHERE id = '{}'".format(cfg.mysql['model_table'],r.id))
        write_model(cursor,r)

        cursor.execute("DELETE FROM {} "
                       "WHERE id = '{}'".format(cfg.mysql['star_table'],r.id))
        write_star(cursor,r)

        cursor.execute("DELETE FROM {} "
                       "WHERE id = '{}'".format(cfg.mysql['disk_r_table'],r.id))
        write_disk_r(cursor,r)
    
    else:
        print("   no update needed")

    # commit and close
    cnx.commit()
    cursor.close()
    cnx.close()


def update_needed(cursor,table,r):
    """Check mtime against result, deleting old entries."""

    # if entries in table, get file modification time
    cursor.execute("SELECT MIN(model_mtime) "
                   "FROM {} WHERE id = '{}';".format(table,r.id))
    db_mtime = cursor.fetchall()[0][0]

    # False if sql up to date
    if db_mtime is not None:
        if r.pickle_time <= db_mtime:
            return False

    return True


def write_phot(cursor,r):
    """Write photometry from a fit to db.
        
    Photometry is split by model component number, with -1 being total
    fluxes, and observed, "star", and "disk" photometry appearing only<commit_msg>require equal db time for no update<commit_after>import numpy as np

import mysql.connector
import astropy.units as u

from . import filter
from . import utils
from . import config as cfg


def write_all(r,update=False):
    """Write sdf results to db."""

    print(" Database")

    # set up connection
    try:
        cnx = mysql.connector.connect(user=cfg.mysql['user'],
                                      password=cfg.mysql['passwd'],
                                      host=cfg.mysql['host'],
                                      database=cfg.mysql['db_results'])
        cursor = cnx.cursor(buffered=True)

    except mysql.connector.InterfaceError:
        print("   Can't connect to {} at {}".format(cfg.mysql['db_results'],
                                                    cfg.mysql['host']) )
        return

    # write to the tables
    if update or update_needed(cursor,cfg.mysql['model_table'],r):
        
        print("   writing")
        
        cursor.execute("DELETE FROM {} "
                       "WHERE id = '{}'".format(cfg.mysql['phot_table'],r.id))
        write_phot(cursor,r)

        cursor.execute("DELETE FROM {} "
                       "WHERE id = '{}'".format(cfg.mysql['model_table'],r.id))
        write_model(cursor,r)

        cursor.execute("DELETE FROM {} "
                       "WHERE id = '{}'".format(cfg.mysql['star_table'],r.id))
        write_star(cursor,r)

        cursor.execute("DELETE FROM {} "
                       "WHERE id = '{}'".format(cfg.mysql['disk_r_table'],r.id))
        write_disk_r(cursor,r)
    
    else:
        print("   no update needed")

    # commit and close
    cnx.commit()
    cursor.close()
    cnx.close()


def update_needed(cursor,table,r):
    """Check mtime against result, deleting old entries."""

    # if entries in table, get file modification time
    cursor.execute("SELECT MIN(model_mtime) "
                   "FROM {} WHERE id = '{}';".format(table,r.id))
    db_mtime = cursor.fetchall()[0][0]

    # False if sql up to date
    if db_mtime is not None:
        if r.pickle_time == db_mtime:
            return False

    return True


def write_phot(cursor,r):
    """Write photometry from a fit to db.
        
    Photometry is split by model component number, with -1 being total
    fluxes, and observed, "star", and "disk" photometry appearing only
{'fraction_non_alphanumeric': 0.12526539278131635, 'fraction_numerical': 0.0012738853503184713, 'mean_word_length': 1.9462163852407754, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '553034', 'n_tokens_mistral': 1369, 'n_tokens_neox': 1298, 'n_words': 341}