Beispiel #1
0
def create_constraint_trigger(name,
                              database,
                              collection,
                              field,
                              parent_collection,
                              parent_field,
                              data=None):
    if not data:
        try:
            data = read_db_file(database)
        except DatabaseNotExist as e:
            return Answer(e.__str__(), error=True)
    if data["collections"] and collection in data["collections"]:
        data = create_trigger_dict(collection, "constraint", data)
        data["collections"][collection]["triggers"]["constraint"][name] = {
            "field": field,
            "parent": {
                "location": parent_collection,
                "field": parent_field
            }
        }
    else:
        return Answer('Collection {} of database {} does not exist'.format(
            collection, database),
                      error=True)
    try:
        write_db_file(database, data)
    except DatabaseNotExist as e:
        return Answer(e.__str__(), error=True)
    return Answer("Operation finished")
Beispiel #2
0
def delete_index(database, collection, field, data=None):
    if not data:
        try:
            data = read_db_file(database)
        except DatabaseNotExist as e:
            return Answer(e.__str__(), error=True)
    if data["collections"] and collection in data["collections"]:
        if not (data["collections"][collection]
                and "indexes" in data["collections"][collection]
                and data["collections"][collection]["indexes"]
                and field in data["collections"][collection]["indexes"]):
            return Answer(
                "Field {} in collection {} of {} is not indexing".format(
                    field, collection, database),
                error=True)
        if data["collections"][collection] and "triggers" in data["collections"] and data["collections"][collection] \
                ["triggers"]:
            if "unique" in data["collections"][collection]["triggers"] and data["collections"][collection]["triggers"] \
                    ["unique"] and field in data["collections"][collection]["triggers"]["unique"]:
                return Answer(
                    "There exists a trigger 'unique' on the field {} in collection {} of {}"
                    .format(field, collection, database),
                    error=True)
            if "auto increment" in data["collections"][collection]["triggers"] and \
                    data["collections"][collection]["triggers"]["auto increment"] and \
                    field in data["collections"][collection]["triggers"]["auto increment"]:
                return Answer(
                    "There exists a trigger 'autoincrement' on the field {} in collection {} of {}"
                    .format(field, collection, database),
                    error=True)
        return delete_index_from_file(database, collection, field, data)
    else:
        return Answer('Collection {} of database {} does not exist'.format(
            collection, database),
                      error=True)
Beispiel #3
0
def create_index(database, collection, field, data=None):
    if not data:
        try:
            data = read_db_file(database)
        except DatabaseNotExist as e:
            return Answer(e.__str__(), error=True)
    if data["collections"] and collection in data["collections"]:
        if data["collections"][collection]["triggers"]:
            if data["collections"][collection]["triggers"]["auto increment"] and \
                    data["collections"][collection]["triggers"]["auto increment"][field]:
                result = create_unique_index(database, collection, field, data,
                                             ("int", ))
                if result.error:
                    data["collections"][collection]["triggers"][
                        "auto increment"].pop(field)
                    write_db_file(database, data)
                    return result
                return result

            if data["collections"][collection]["triggers"]["unique"] and \
                    data["collections"][collection]["triggers"]["unique"][field]:
                if data["collections"][collection]["triggers"]["check type"] and \
                        data["collections"][collection]["triggers"]["check type"][field]:
                    result = create_unique_index(
                        database, collection, field, data, data["collections"]
                        [collection]["triggers"]["check type"][field])
                else:
                    result = create_unique_index(database, collection, field,
                                                 data)
                if result.error:
                    data["collections"][collection]["triggers"]["unique"].pop(
                        field)
                    write_db_file(database, data)
                    return result
                return result

            if data["collections"][collection]["triggers"]["check type"] and \
                    data["collections"][collection]["triggers"]["check type"][field]:
                if {"float", "int", "bool"}.isdisjoint(
                        set(data["collections"][collection]["triggers"]
                            ["check type"][field])):
                    return Answer(
                        'Indexing of field {} of collection {} in database {} is no use',
                        error=True)
                else:
                    return create_btree_index(
                        database, collection, field, data,
                        list({"string", "float", "int", "bool"}
                             & set(data["collections"][collection]["triggers"]
                                   ["check type"][field])))
        return create_btree_index(database, collection, field, data)
    else:
        return Answer('Collection {} of database {} does not exist'.format(
            collection, database),
                      error=True)
Beispiel #4
0
def create_unique_index(database,
                        collection,
                        field,
                        data=None,
                        object_types=("object", "string", "float", "int",
                                      "array", "bool")):
    if not data:
        try:
            data = read_db_file(database)
        except DatabaseNotExist as e:
            return Answer(e.__str__(), error=True)
    files = data["collections"][collection]["files"]
    files = files.values()
    index_dict = {}
    old_directory = os.getcwd()
    enter_in_system_directory()
    os.chdir(database)
    for file in files:
        with open(file, "rb") as file_data:
            for line in file_data:
                item = bson.loads(line)
                if field in item:
                    if isinstance(item[field], object_types):
                        if item[field] in index_dict:
                            os.chdir(old_directory)
                            return Answer(
                                "Fields {} in collection {} of {} is not unique"
                                .format(field, collection, database),
                                error=True)
                        index_dict[item[field]] = item["_id"]
    if "indexes" not in data["collections"][collection]:
        data["collections"][collection]["indexes"] = {}
    if field in data["collections"][collection]["indexes"]:
        try:
            os.remove(
                data["collections"][collection]["indexes"][field]["filename"])
        except FileNotFoundError:
            pass
        del data["collections"][collection]["indexes"][field]
    data["collections"][collection]["indexes"][field] = {
        "type indexing":
        "unique",
        "filename":
        "{}.bs".format(get_hash({
            "collection": collection,
            "index": field
        }))
    }
    filename = data["collections"][collection]["indexes"][field]["filename"]
    with open(filename, 'wb') as file:
        pickle.dump(data, file, protocol=0)
    os.chdir(old_directory)
    return Answer("Operation finished")
Beispiel #5
0
def create_unique_trigger(database, collection, field, data=None):
    if not data:
        try:
            data = read_db_file(database)
        except DatabaseNotExist as e:
            return Answer(e.__str__(), error=True)
    if data["collections"] and collection in data["collections"]:
        data = create_trigger_dict(collection, "unique", data)
        data["collections"][collection]["triggers"]["unique"][field] = True
        return create_index(database, collection, field, data)
    else:
        return Answer('Collection {} of database {} does not exist'.format(
            collection, database),
                      error=True)
Beispiel #6
0
def delete_trigger(database,
                   collection,
                   type_trigger,
                   field,
                   name=None,
                   situation=None,
                   data=None):
    if not data:
        try:
            data = read_db_file(database)
        except DatabaseNotExist as e:
            return Answer(e.__str__(), error=True)

    if data["collections"] and collection in data["collections"]:
        if data["collections"][collection]["triggers"] and type_trigger in data[
                "collections"][collection]["triggers"]:
            if type_trigger == "custom":
                try:
                    data["collections"][collection]["triggers"]["custom"][
                        situation].pop(name)
                except KeyError:
                    pass

            elif type_trigger == "constraint":
                try:
                    data["collections"][collection]["triggers"][
                        "constraint"].pop(name)
                except KeyError:
                    pass

            else:
                try:
                    data["collections"][collection]["triggers"][
                        type_trigger].pop(field)
                except KeyError:
                    pass
    else:
        return Answer('Collection {} of database {} does not exist'.format(
            collection, database),
                      error=True)

    try:
        write_db_file(database, data)
    except DatabaseNotExist as e:
        return Answer(e.__str__(), error=True)
    return Answer("Operation finished")
Beispiel #7
0
def create_not_null_trigger(database, collection, field, data=None):
    if not data:
        try:
            data = read_db_file(database)
        except DatabaseNotExist as e:
            return Answer(e.__str__(), error=True)
    if data["collections"] and collection in data["collections"]:
        data = create_trigger_dict(collection, "not null", data)
        data["collections"][collection]["triggers"]["not null"][field] = True
    else:
        return Answer('Collection {} of database {} does not exist'.format(
            collection, database),
                      error=True)
    try:
        write_db_file(database, data)
    except DatabaseNotExist as e:
        return Answer(e.__str__(), error=True)
    return Answer("Operation finished")
Beispiel #8
0
def delete_index(database, collection, field, data=None):
    if not data:
        try:
            data = read_db_file(database)
        except DatabaseNotExist as e:
            return Answer(e.__str__(), error=True)
    old_directory = os.getcwd()
    enter_in_system_directory()
    os.chdir(database)
    if "indexes" in data["collections"][collection]:
        if data["collections"][collection]["indexes"] and field in data[
                "collections"][collection]["indexes"]:
            try:
                os.remove(data["collections"][collection]["indexes"][field]
                          ["filename"])
            except FileNotFoundError:
                pass
            del data["collections"][collection]["indexes"][field]
    os.chdir(old_directory)
    return Answer("Operation finished")
Beispiel #9
0
def create_custom_trigger(name,
                          database,
                          collection,
                          situation,
                          parameters,
                          code,
                          actions_on_true=None,
                          actions_on_false=None,
                          data=None):
    if not data:
        try:
            data = read_db_file(database)
        except DatabaseNotExist as e:
            return Answer(e.__str__(), error=True)
    if data["collections"] and collection in data["collections"]:
        data = create_trigger_dict(collection, "custom", data)
        if situation not in data["collections"][collection]["triggers"][
                "custom"]:
            data["collections"][collection]["triggers"]["custom"][
                situation] = {}
        data["collections"][collection]["triggers"]["custom"][situation][
            name] = {
                "code": code,
                "parameters": parameters,
                "actions": {
                    "true": actions_on_true,
                    "false": actions_on_false
                }
            }
    else:
        return Answer('Collection {} of database {} does not exist'.format(
            collection, database),
                      error=True)
    try:
        write_db_file(database, data)
    except DatabaseNotExist as e:
        return Answer(e.__str__(), error=True)
    return Answer("Operation finished")