<commit_before>#!/usr/bin/env python
"""Update callback."""

import itertools


import gizer.psql_requests
from gizer.opinsert import *
from gizer.oppartial_record import get_tables_data_from_oplog_set_command

from opdelete import op_delete_stmts as delete, get_conditions_list
from util import *

import bson
import datetime


# UPSERT_TMLPT = """\
# LOOP
#     {update}
#     IF found THEN
#         RETURN;
#     END IF;
#     BEGIN
#         {insert}
#         RETURN;
#     EXCEPTION WHEN unique_violation THEN
#     END;
# END LOOP;
# """


def localte_in_schema(schema_in, path):
    if type(schema_in) is list:
        schema = schema_in[0]
    else:
        schema = schema_in
    new_path_clear = []
    for el in path:
        if not el.isdigit():
            new_path_clear.append(el)

    current_element = new_path_clear[0]
    if current_element in schema.keys():
        if len(new_path_clear) > 1:
            new_path = new_path_clear[1:]
            if type(schema[current_element]) is list:
                next_element = schema[current_element][0]
            elif type(schema[current_element]) is dict:
                next_element = schema[current_element]

            if len(new_path) >= 1:
                return localte_in_schema(next_element, new_path)
            else:
                if new_path in next_element.keys():
                    return  True
                else:
                    return False
        else:
            return True
    else:
        return False


def update(dbreq, schema_e, oplog_data, database_name, schema_name):
    if type(schema_e) != dict:
        schema = schema_e.schema
    else:
        schema = schema_e
    oplog_data_object_id = oplog_data['o2']
    oplog_data_ns = oplog_data['ns']
    oplog_data_set = oplog_data['o']['$set']
    root_table_name = oplog_data_ns.split('.')[-1]
    tables_mappings = get_tables_structure(schema, root_table_name, {}, {}, '')
    ret_val = []
    is_root_object_updated = False
    for element in oplog_data_set:
        updating_obj = element.split('.')
        upd_path = [root_table_name] + updating_obj
        if not localte_in_schema(schema[0], updating_obj):
            continue

        if len(updating_obj) > 1:
            if updating_obj[-1].isdigit():
                ret_val.extend(update_cmd(dbreq,schema_e,oplog_data_set,oplog_data_object_id,oplog_data_ns,tables_mappings,database_name,schema_name))
            else:
                new_oplog_data_set = { '.'.join(updating_obj[:-1]):{updating_obj[-1]:oplog_data_set[element]}}
                ret_val.extend(update_cmd(dbreq,schema_e,new_oplog_data_set,oplog_data_object_id,oplog_data_ns,tables_mappings,database_name,schema_name))
        else:
            if not is_root_object_updated:
                ret_val.extend (update_cmd(dbreq,schema_e,oplog_data_set,oplog_data_object_id,oplog_data_ns,tables_mappings,database_name,schema_name))
            is_root_object_updated = True
    return ret_val

def update_list (dbreq, schema_e, upd_path_str, oplog_data_set, oplog_data_object_id, database_name, schema_name):
    if type(schema_e) != dict:
        schema = schema_e.schema
    else:
        schema = schema_e
    ret_val = []
    doc_id = get_obj_id_recursive(oplog_data_object_id, [], [])
    del_stmnt = delete(dbreq, schema, upd_path_str, doc_id.itervalues().next(), database_name, schema_name)
    for op in del_stmnt:
        if type(del_stmnt[op]) is dict:
            for k in del_stmnt[op]:
                ret_val.append({k:[tuple(del_stmnt[op][k])]})

    tables, initial_indexes \
        = get_tables_data_from_oplog_set_command(schema_e,
                                                 oplog_data_set,
                                                 oplog_data_object_id)
    for name, table in tables.iteritems():
        rr = generate_insert_queries(table, schema_name, "", initial_indexes)

        ret_val.append({rr[0]:rr[1]})
    return  ret_val
<commit_msg>fix issue with empty SET clause<commit_after>#!/usr/bin/env python
"""Update callback."""

import itertools


import gizer.psql_requests
from gizer.opinsert import *
from gizer.oppartial_record import get_tables_data_from_oplog_set_command

from opdelete import op_delete_stmts as delete, get_conditions_list
from util import *

import bson
import datetime


# UPSERT_TMLPT = """\
# LOOP
#     {update}
#     IF found THEN
#         RETURN;
#     END IF;
#     BEGIN
#         {insert}
#         RETURN;
#     EXCEPTION WHEN unique_violation THEN
#     END;
# END LOOP;
# """


def localte_in_schema(schema_in, path):
    if type(schema_in) is list:
        schema = schema_in[0]
    else:
        schema = schema_in
    new_path_clear = []
    for el in path:
        if not el.isdigit():
            new_path_clear.append(el)

    current_element = new_path_clear[0]
    if current_element in schema.keys():
        if len(new_path_clear) > 1:
            new_path = new_path_clear[1:]
            if type(schema[current_element]) is list:
                next_element = schema[current_element][0]
            elif type(schema[current_element]) is dict:
                next_element = schema[current_element]

            if len(new_path) >= 1:
                return localte_in_schema(next_element, new_path)
            else:
                if new_path in next_element.keys():
                    return  True
                else:
                    return False
        else:
            return True
    else:
        return False


def update(dbreq, schema_e, oplog_data, database_name, schema_name):
    if type(schema_e) != dict:
        schema = schema_e.schema
    else:
        schema = schema_e
    oplog_data_object_id = oplog_data['o2']
    oplog_data_ns = oplog_data['ns']
    oplog_data_set = oplog_data['o']['$set']
    root_table_name = oplog_data_ns.split('.')[-1]
    tables_mappings = get_tables_structure(schema, root_table_name, {}, {}, '')
    ret_val = []
    is_root_object_updated = False
    for element in oplog_data_set:
        updating_obj = element.split('.')
        upd_path = [root_table_name] + updating_obj
        if not localte_in_schema(schema[0], updating_obj):
            continue

        if len(updating_obj) > 1:
            last_digit = -1
            for i, path_el in enumerate(updating_obj):
                if path_el.isdigit():
                    last_digit = i
            if last_digit >= 0:
                if last_digit == len(updating_obj) - 1:
                    ret_val.extend(update_cmd(dbreq,schema_e,oplog_data_set,oplog_data_object_id,oplog_data_ns,tables_mappings,database_name,schema_name))
                else:
                    new_oplog_data_set = oplog_data_set[element]
                    for i in reversed(range(last_digit + 1, len(updating_obj))):
                        new_oplog_data_set = {updating_obj[i]:new_oplog_data_set}
                    new_oplog_data_set = { '.'.join(updating_obj[:last_digit+1]):new_oplog_data_set}
                    ret_val.extend(update_cmd(dbreq,schema_e,new_oplog_data_set,oplog_data_object_id,oplog_data_ns,tables_mappings,database_name,schema_name))
        else:
            if not is_root_object_updated:
                ret_val.extend (update_cmd(dbreq,schema_e,oplog_data_set,oplog_data_object_id,oplog_data_ns,tables_mappings,database_name,schema_name))
            is_root_object_updated = True
    return ret_val

def update_list (dbreq, schema_e, upd_path_str, oplog_data_set, oplog_data_object_id, database_name, schema_name):
    if type(schema_e) != dict:
        schema = schema_e.schema
    else:
        schema = schema_e
    ret_val = []
    doc_id = get_obj_id_recursive(oplog_data_object_id, [], [])
    del_stmnt = delete(dbreq, schema, upd_path_str, doc_id.itervalues().next(), database_name, schema_name)
    for op in del_stmnt:
        if type(del_stmnt[op]) is dict:
            for k in del_stmnt[op]:
                ret_val.append({k:[tuple(del_stmnt[op][k])]})

    tables, initial_indexes \
        = get_tables_data_from_oplog_set_command(schema_e,
                                                 oplog_data_set,
                                                 oplog_data_object_id)
    for name, table in tables.iteritems():
        rr = generate_insert_queries(table, schema_name, "", initial_indexes)

        ret_val.append({rr[0]:rr[1]})
    return  ret_val

{'fraction_non_alphanumeric': 0.09197184778718835, 'fraction_numerical': 0.003817249194798998, 'mean_word_length': 2.4716356107660458, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1409304', 'n_tokens_mistral': 2833, 'n_tokens_neox': 2738, 'n_words': 497}