이것저것 참고용
참고용
도토리즈
2023. 6. 19. 00:45
@api_view(['POST'])
def test(request):
mapping_datas = request.data['data']
query_parts = []
dynamic_tables = []
for data in mapping_datas:
id = data['id']
where_filters = data['where_condition']
query_model = TotalQuery.objects.get(id=id)
select_query = query_model.query
if where_filters:
where_query = " WHERE 1=1 "
for filter in where_filters:
filters = get_where_condition(filter)
where_query += filters
else:
where_query = ""
query_parts.append(f"({select_query}) AS table{len(query_parts)+1}")
dynamic_tables.append(f"table{len(query_parts)}")
if where_query:
query_parts[-1] += where_query
first_columns = get_first_column_select(query_parts[0]) # Assuming the first query has the desired columns
join_conditions = " AND ".join([f"{dynamic_tables[i]}.id = {dynamic_tables[0]}.id" for i in range(1, len(query_parts))])
total_query = f"SELECT * FROM {', '.join(query_parts)} WHERE 1=1 {join_conditions}"
return Response({"query": total_query})
def get_where_condition(where):
where_query = " AND "
for key, value in where.items():
condition = f"{key} = '{value}'"
where_query += condition
return where_query
def get_first_column_select(query):
pattern = r"SELECT\s+(\w+)\s+FROM"
match = re.search(pattern, query, re.IGNORECASE)
if match:
return match.group(1)
return ""
from django.shortcuts import render
from rest_framework.decorators import api_view
from rest_framework.response import Response
from .models import *
from django.db import connections
@api_view(['POST'])
def test(request):
mapping_datas = request.data
if mapping_datas.get('data'):
data_list = mapping_datas['data']
query_parts = []
for data in data_list:
id = data['id']
where_filters = data['where_condition']
query_model = TotalQuery.objects.get(id=id)
select_query = query_model.query
where_query = get_where_query(where_filters)
if where_query:
select_query += where_query
query_parts.append(select_query)
total_query = generate_join_query(query_parts)
print(total_query)
return Response({"query": total_query})
def get_where_query(where_filters):
if not where_filters:
return ''
where_conditions = []
for filter in where_filters:
for key, value in filter.items():
condition = f"{key} = '{value}'"
where_conditions.append(condition)
where_query = " AND ".join(where_conditions)
return f" WHERE {where_query}"
def generate_join_query(query_parts):
join_query = ''
first_columns = []
for index, query_part in enumerate(query_parts):
first_column = get_first_column_select(query_part)
first_columns.append(first_column)
join_query += f"( {query_part} ) as table{index+1}, "
join_query = join_query[:-2]
join_conditions = " AND ".join([f"table{i}.pk_name = table1.name" for i in range(2, len(query_parts)+1)])
total_query = f"SELECT {', '.join(first_columns)} FROM {join_query} WHERE 1=1 AND {join_conditions};"
return total_query
def get_first_column_select(query):
import re
match = re.search(r"SELECT ([^,]+)", query)
if match:
first_column = match.group(1)
return first_column.strip()
return "*"
from django.shortcuts import render
from rest_framework.decorators import api_view
from rest_framework.response import Response
from .models import *
from django.db import connections
import re
@api_view(['POST'])
def test(request):
mapping_datas = request.data
if mapping_datas.get('data'):
data_list = mapping_datas['data']
query_parts = []
first_data = data_list[0]
first_id = first_data['id']
first_where_filters = first_data['where_condition']
first_query_model = TotalQuery.objects.get(id=first_id)
first_select_query = first_query_model.query
first_where_query = get_where_query(first_where_filters)
first_query = f"{first_select_query} {first_where_query}"
query_parts.append(first_query)
for data in data_list[1:]:
id = data['id']
where_filters = data['where_condition']
query_model = TotalQuery.objects.get(id=id)
select_query = query_model.query
if where_filters:
where_query = get_where_query(where_filters)
select_query += where_query
query_parts.append(select_query)
total_query = generate_join_query(query_parts)
print(total_query)
return Response({"query": total_query})
def get_where_query(where_filters):
where_query = " WHERE 1=1 "
for filter in where_filters:
where_condition = get_where_condition(filter)
where_query += where_condition
return where_query
def get_where_condition(where):
where_query = " AND "
for key, value in where.items():
condition = f"{key} = '{value}'"
where_query += condition
return where_query
def generate_join_query(query_parts):
join_query = ''
first_columns = []
for index, query_part in enumerate(query_parts):
first_column = get_first_column_select(query_part)
first_columns.append(first_column)
join_query += f"( {query_part} ) as table{index+1}, "
join_query = join_query[:-2]
join_conditions = " AND ".join([f"table{i}.pk_name = table1.name" for i in range(2, len(query_parts)+1)])
total_query = f"SELECT {', '.join(first_columns)} FROM {join_query} WHERE 1=1 AND {join_conditions};"
return total_query
def get_first_column_select(query):
match = re.search(r"SELECT ([^,]+)", query)
if match:
first_column = match.group(1)
return first_column.strip()
return "*"
셋중 뭐든 도움이 되긴 하것지 ..ssang