示例#1
0
    def __insert_row(self, request, schema, table, row, row_id=None):
        if row_id and row.get("id", int(row_id)) != int(row_id):
            return actions._response_error(
                "The id given in the query does not " "match the id given in the url"
            )
        if row_id:
            row["id"] = row_id

        context = {
            "connection_id": request.data["connection_id"],
            "cursor_id": request.data["cursor_id"],
            "user": request.user,
        }

        query = {
            "schema": schema,
            "table": table,
            "values": [row] if isinstance(row, dict) else row,
        }

        if not row_id:
            query["returning"] = [{"type": "column", "column": "id"}]
        result = actions.data_insert(query, context)

        return result
示例#2
0
    def put(self, request, schema, table, row_id=None):
        schema, table = actions.get_table_name(schema, table)
        if not row_id:
            return JsonResponse(actions._response_error('This methods requires an id'),
                                status=status.HTTP_400_BAD_REQUEST)

        column_data = request.data['query']

        if row_id and column_data.get('id', int(row_id)) != int(row_id):
            raise actions.APIError(
                'Id in URL and query do not match. Ids may not change.',
                status=status.HTTP_409_CONFLICT)

        engine = actions._get_engine()
        conn = engine.connect()

        # check whether id is already in use
        exists = conn.execute('select count(*) '
                             'from {schema}.{table} '
                             'where id = {id};'.format(schema=schema,
                                                     table=table,
                                                     id=row_id)).first()[0] > 0 if row_id else False
        conn.close()
        if exists:
            response = self.__update_rows(request, schema, table, column_data, row_id)
            actions.apply_changes(schema, table)
            return JsonResponse(response)
        else:
            result = self.__insert_row(request, schema, table, column_data, row_id)
            actions.apply_changes(schema, table)
            return JsonResponse(result, status=status.HTTP_201_CREATED)
示例#3
0
    def __insert_row(self, request, schema, table, row, row_id=None):
        if row_id and row.get('id', int(row_id)) != int(row_id):
            return actions._response_error(
                'The id given in the query does not '
                'match the id given in the url')
        if row_id:
            row['id'] = row_id

        context = {
            'connection_id': request.data['connection_id'],
            'cursor_id': request.data['cursor_id'],
            'user': request.user
        }

        query = {
            'schema': schema,
            'table': table,
            'values': [row] if isinstance(row, dict) else row
        }

        if not row_id:
            query['returning'] = [{'type': 'column', 'column': 'id'}]
        result = actions.data_insert(query, context)

        return result
示例#4
0
    def put(self, request, schema, table, row_id=None, action=None):
        if action:
            raise APIError(
                "This request type (PUT) is not supported. The "
                "'new' statement is only possible in POST requests."
            )
        schema, table = actions.get_table_name(schema, table)
        if not row_id:
            return JsonResponse(
                actions._response_error("This methods requires an id"),
                status=status.HTTP_400_BAD_REQUEST,
            )

        column_data = request.data["query"]

        if row_id and column_data.get("id", int(row_id)) != int(row_id):
            raise actions.APIError(
                "Id in URL and query do not match. Ids may not change.",
                status=status.HTTP_409_CONFLICT,
            )

        engine = actions._get_engine()
        # check whether id is already in use
        exists = (
            engine.execute(
                "select count(*) "
                "from {schema}.{table} "
                "where id = {id};".format(schema=schema, table=table, id=row_id)
            ).first()[0]
            > 0
            if row_id
            else False
        )
        if exists:
            response = self.__update_rows(request, schema, table, column_data, row_id)
            actions.apply_changes(schema, table)
            return JsonResponse(response)
        else:
            result = self.__insert_row(request, schema, table, column_data, row_id)
            actions.apply_changes(schema, table)
            return JsonResponse(result, status=status.HTTP_201_CREATED)