Example #1
0
def create_people():
    result = ds.create(
        'lahman2017.people', {
            'playerID': 'yz3400',
            'nameLast': 'Zhao',
            'nameFirst': 'Yuxin',
            'birthCountry': 'CHN'
        })
    print('create_people returned: ', result)
Example #2
0
def handle_collection(dbname, resource_name):

    resp = Response("Internal server error", status=500, mimetype="text/plain")

    try:

        # Form the compound resource names dbschema.table_name
        if request.args.get("children"):
            return handle_children(dbname, resource_name, request.args)

        resource = dbname + "." + resource_name

        if request.method == "GET":
            # Get the field list if it exists.
            field_list = request.args.get('fields', None)
            if field_list is not None:
                field_list = field_list.split(",")

            limit = request.args.get("limit", "10")
            offset = request.args.get("offset", "0")
            order_by = request.args.get("order_by", None)

            # The query string is of the form ?f1=v1&f2=v2& ...
            # This maps to a query template of the form { "f1" : "v1", ... }
            # We need to ignore the fields parameters.
            tmp = None
            for k, v in request.args.items():
                if (not k == "fields") and (not k == "limit") and (not k == "offset") and \
                        (not k == "order_by"):
                    if tmp is None:
                        tmp = {}
                    tmp[k] = v

            # Find by template.
            result = ds.get_by_template(resource, tmp, field_list=field_list, limit=limit, offset=offset, \
                                        order_by=order_by)

            if result:
                result = {"data": result}
                result = compute_links(result, limit, offset)
                result_data = json.dumps(result, default=str)
                resp = Response(result_data,
                                status=200,
                                mimetype='application/json')
            else:
                resp = Response("Not found", status=404, mimetype="text/plain")

        elif request.method == "POST":
            print(request)
            new_r = request.get_json()
            result = ds.create(resource, new_r)
            if result and result == 1:
                resp = Response("CREATED", status=201, mimetype="text/plain")
    except Exception as e:
        utils.debug_message("Something awlful happened, e = ", e)

    return resp
Example #3
0
def create_fantasy_manager():
    result = ds.create(
        "HW1.fantasy_manager", {
            "id": "9",
            "last_name": "Ferguson",
            "first_name": "Douglas",
            "email": "*****@*****.**"
        })

    print("create_fantasy_manager: ", result)
Example #4
0
def handle_tables(dbname, resource_name, resource_name2, primary_key):

    resp = Response("Internal server error", status=500, mimetype="text/plain")

    try:
        key_columns = primary_key.split(key_delimiter)
        # Form the compound resource names dbschema.table_name
        # resource = dbname + "." + resource_name
        # resource2 = dbname + "." + resource_name2

        if request.method == "GET":
            # Get the field list if it exists.
            field_list = request.args.get('fields', None)
            if field_list is not None:
                field_list = field_list.split(",")

            limit = request.args.get("limit", "10")
            offset = request.args.get("offset", "0")
            order_by = request.args.get("order_by", None)

            # The query string is of the form ?f1=v1&f2=v2& ...
            # This maps to a query template of the form { "f1" : "v1", ... }
            # We need to ignore the fields parameters.
            tmp = None
            for k, v in request.args.items():
                if (not k == "fields") and (not k == "limit") and (not k == "offset") and \
                        (not k == "order_by"):
                    if tmp is None:
                        tmp = {}
                    tmp[k] = v

            # Find by template.
            join_paths = ds.get_join_column_mapping(dbname, resource_name,
                                                    dbname, resource_name2)

            if join_paths:
                columns = join_paths[list(join_paths.keys())[0]]["MAP"][0]
                referenced_table_name = columns["REFERENCED_TABLE_NAME"]
                referenced_column_name = columns["REFERENCED_COLUMN_NAME"]
                table_name = columns["TABLE_NAME"]
                column_name = columns["COLUMN_NAME"]

                referenced_row = ds.get_by_primary_key(dbname +"."+ referenced_table_name, key_columns, \
                                                       field_list=None, commit=True)
                tmp[column_name] = referenced_row[column_name]
                result = ds.get_by_template(dbname +"."+ table_name, tmp, \
                            field_list=field_list, limit=limit, offset=offset, order_by=order_by, commit=True)

                result = {"data": result}
                result = compute_links(result, limit, offset)
                result_data = json.dumps(result, default=str)
                resp = Response(result_data,
                                status=200,
                                mimetype='application/json')
            else:
                resp = Response("Not found", status=404, mimetype="text/plain")

        elif request.method == "POST":
            join_paths = ds.get_join_column_mapping(dbname, resource_name,
                                                    dbname, resource_name2)

            if join_paths:
                columns = join_paths[list(join_paths.keys())[0]]["MAP"][0]
                referenced_table_name = columns["REFERENCED_TABLE_NAME"]
                referenced_column_name = columns["REFERENCED_COLUMN_NAME"]
                table_name = columns["TABLE_NAME"]
                column_name = columns["COLUMN_NAME"]
                new_r = request.get_json()
                referenced_row = ds.get_by_primary_key(dbname + "." +
                                                       referenced_table_name,
                                                       key_columns,
                                                       field_list=None,
                                                       commit=True)
                new_r[column_name] = referenced_row[column_name]

                result = ds.create(dbname + "." + table_name, new_r)
                if result and result == 1:
                    resp = Response("CREATED",
                                    status=201,
                                    mimetype="text/plain")

            else:
                resp = Response("Not found", status=404, mimetype="text/plain")

    except Exception as e:
        utils.debug_message("Something awlful happened, e = ", e)

    return resp
Example #5
0
def handle_collection(dbname, resource_name):

    resp = Response("Internal server error", status=500, mimetype="text/plain")
    result = None
    e = None
    #context = get_context()

    try:

        # Form the compound resource names dbschema.table_name
        #children = context.get("children", None)
        #if children is not None:
            #resp = handle_path(dbname, resource_name, context)
        #else:
        resource = dbname + "." + resource_name
            #tmp = context.get("query_params", None)
            #field_list = context.get("field", None)


        if request.method == 'GET':

            children = request.args.get('children', None)
            if children is None:
                # Get the field list if it exists.
                field_list = request.args.get('fields', None)
                if field_list is not None:
                    field_list = field_list.split(",")

                order_by = request.args.get('order_by', None)
                limit = request.args.get('limit', None)
                offset = request.args.get('offset', None)

                # The query string is of the form ?f1=v1&f2=v2& ...
                # This maps to a query template of the form { "f1" : "v1", ... }
                # We need to ignore the fields parameters.
                tmp = None
                for k,v in request.args.items():
                    if (not k == 'fields') and (not k == 'limit') and (not k == 'offset') and (not k == 'order_by'):
                        if tmp is None:
                            tmp = {}
                        tmp[k] = v

                # Find by template
                result = ds.get_by_template(resource, tmp, field_list=field_list, limit = limit, offset = offset,
                                            order_by = order_by)

                if result:
                    #result = process_result(result[0],dbname,resource_name)
                    result = {'data': result}
                    result = compute_links(result, limit, offset)
                    result_data = json.dumps(result, default=str)
                    resp = Response(result_data, status=200, mimetype='application/json')
                else:
                    resp = Response("Not found", status=404, mimetype="text/plain")
            else:
                result = _get_with_join(dbname, resource_name)
                if result:
                    result_data=json.dumps(result, default=str)
                    resp = Response(result_data, status=200, mimetype='application/json')
                else:
                    resp = Response("Not found", status=404, mimetype="text/plain")

        elif request.method == 'POST':
            new_r = request.get_json()
            result = ds.create(resource, new_r)
            if result and result == 1:
                resp = Response("CREATED", status=201, mimetype="text/plain")

    except Exception as e:
        utils.debug_message("Something awful happened, e = ", e)

    return resp
Example #6
0
def handle_collection(dbname, resource_name):

    resp = Response("Internal server error", status=500, mimetype="text/plain")

    try:

        # Form the compound resource names dbschema.table_name
        resource = dbname + "." + resource_name
        _default_offset = 10
        _default_limit = 10
        if request.method == 'GET':

            # Get the field list if it exists.
            field_list = request.args.get('fields', None)
            children_list = request.args.get('children', None)

            if field_list is not None:
                field_list = field_list.split(",")
            if children_list is not None:
                children_list = children_list.split(",")

            limit = request.args.get('limit', _default_limit)
            offset = request.args.get('offset', _default_offset)
            order_by = request.args.get('order_by', None)

            # The query string is of the form ?f1=v1&f2=v2& ...
            # This maps to a query template of the form { "f1" : "v1", ... }
            # We need to ignore the fields parameters.
            tmp = None
            for k, v in request.args.items():
                if (not k == 'fields') and (not k == 'limit') and (
                        not k == 'offset') and (not k == 'order_by') and (
                            not k == 'children'):
                    if tmp is None:
                        tmp = {}
                    tmp[k] = v

            # Find by template.
            if children_list is None:
                result = ds.get_by_template(resource,
                                            tmp,
                                            field_list=field_list)

            else:
                result = ds.get_by_q_from_h(resource, children_list, tmp,
                                            field_list)

            if result:
                result = {'data': result}
                result = compute_links(result, limit, offset)
                result_data = json.dumps(result, default=str)
                resp = Response(result_data,
                                status=200,
                                mimetype='application/json')
            else:
                resp = Response("Not found", status=404, mimetype="text/plain")
        elif request.method == 'POST':
            new_r = request.get_json()
            result = ds.create(resource, new_r)
            if result and result == 1:
                resp = Response("created", status=201, mimetype="text/plain")

    except Exception as e:
        utils.debug_message("Something awlful happened, e = ", e)

    return resp
Example #7
0
def handle_collection(dbname, resource_name):

    resp = Response("Internal server error", status=500, mimetype="text/plain")

    try:
        resource = dbname + "." + resource_name

        if request.method == 'GET':
            # Form the compound resource names dbschema.table_name

            # Get the field list if it exists.
            field_list = request.args.get('fields', None)
            if field_list is not None:
                field_list = field_list.split(",")

            # for queries of type 127.0.0.1:5000/api/lahman2017/batting?teamID=BOS&limit=10
            limit = min(int(request.args.get('limit', 10)), 10)
            offset = request.args.get('offset', 0)
            order_by = request.args.get('order_by', None)
            children = request.args.get('children', None)

            # The query string is of the form ?f1=v1&f2=v2& ...
            # This maps to a query template of the form { "f1" : "v1", ... }
            # We need to ignore the fields parameters.
            tmp = None
            for k, v in request.args.items():
                if (not k == 'fields') and (not k == 'limit') and (not k=='offset') and \
                    (not k == 'order_by') and (not k == 'children'):
                    if tmp is None:
                        tmp = {}
                    tmp[k] = v

            # Find by template
            result = ds.get_by_template(resource,
                                        tmp,
                                        field_list=field_list,
                                        limit=limit,
                                        offset=offset,
                                        order_by=order_by,
                                        children=children)

            if result:
                result = {"data": result}
                result = compute_links(result, limit, offset)
                result_data = json.dumps(result, default=str)
                resp = Response(result_data,
                                status=200,
                                mimetype='application/json')
            else:
                resp = Response("Not found", status=404, mimetype="text/plain")

        elif request.method == 'POST':
            new_r = request.get_json()
            k = result = ds.create(resource, new_r)

            if k:
                location = get_location(dbname, resource_name, k)
                resp = Response("CREATED", status=201, mimetype="text/plain")
                resp.headers['Location'] = location

    except Exception as e:
        resp = Response("STATUS 422 " + str(e),
                        status=422,
                        mimetype="text/plain")
        utils.debug_message("Something awlful happened, e = ", e)

    return resp
Example #8
0
def handle_path_resource(dbname, resource_name, primary_key, subresource_name):

    resp = Response("Internal server error", status=500, mimetype="text/plain")

    try:
        resource = dbname + "." + resource_name
        subresource = dbname + "." + subresource_name
        ##make args mutable
        request.args = request.args.copy()
        actual_key = ds.get_key(resource)
        keys_input = primary_key.split('_')
        if len(keys_input) != len(actual_key):
            raise ValueError("Wrong key length please try " + actual_key)

        benchmark_row = ds.get_by_primary_key(resource, keys_input)

        if request.method == 'GET':
            # Form the compound resource names dbschema.table_name

            foreign_key = ds.get_foreign_key(resource, subresource)
            for k, v in foreign_key.items():
                map = v['MAP'][0]
                request.args[map['REFERENCED_COLUMN_NAME']] = benchmark_row[0][
                    map['COLUMN_NAME']]

            # Get the field list if it exists.
            field_list = request.args.get('fields', None)
            if field_list is not None:
                field_list = field_list.split(",")

            # for queries of type 127.0.0.1:5000/api/lahman2017/batting?teamID=BOS&limit=10
            limit = min(int(request.args.get('limit', 10)), 10)
            offset = request.args.get('offset', 0)
            order_by = request.args.get('order_by', None)
            children = request.args.get('children', None)

            # The query string is of the form ?f1=v1&f2=v2& ...
            # This maps to a query template of the form { "f1" : "v1", ... }
            # We need to ignore the fields parameters.
            tmp = None
            for k, v in request.args.items():
                if (not k == 'fields') and (not k == 'limit') and (not k=='offset') and \
                    (not k == 'order_by') and (not k == 'children'):
                    if tmp is None:
                        tmp = {}
                    tmp[k] = v

            # Find by template
            result = ds.get_by_template(subresource,
                                        tmp,
                                        field_list=field_list,
                                        limit=limit,
                                        offset=offset,
                                        order_by=order_by,
                                        children=children)

            if result:
                result = {"data": result}
                result = compute_links(result, limit, offset)
                result_data = json.dumps(result, default=str)
                resp = Response(result_data,
                                status=200,
                                mimetype='application/json')
            else:
                resp = Response("Not found", status=404, mimetype="text/plain")

        elif request.method == 'POST':

            actual_key = ds.get_key(resource)
            keys_input = primary_key.split('_')
            if len(keys_input) != len(actual_key):
                raise ValueError("Wrong key length please try " + actual_key)

            new_r = request.get_json()

            foreign_key = ds.get_foreign_key(resource, subresource)
            for k, v in foreign_key.items():
                map = v['MAP'][0]
                new_r[map['COLUMN_NAME']] = benchmark_row[0][
                    map['REFERENCED_COLUMN_NAME']]
            ####Might need to handle multiple keys here
            k = result = ds.create(subresource, new_r)
            if k:
                location = get_location(dbname, subresource_name, k)
                resp = Response("CREATED", status=201, mimetype="text/plain")
                resp.headers['Location'] = location

    except Exception as e:
        resp = Response("STATUS 422 " + str(e),
                        status=422,
                        mimetype="text/plain")
        utils.debug_message("Something awlful happened, e = ", e)

    return resp