Log In
|
Sign Up
Search
RestAPI를 활용한 테이블 간 JOIN 예제
Writer
조민욱
Description
RestAPI를 활용한 테이블 간 JOIN 예제입니다. JOIN 특성 상, 대상이 되는 테이블 간 곱집합의 카디널리티 만큼의 연산이 필요합니다. 이를 효율적으로 하기 위하여 현재 구현된 내용을 SQL을 기준으로 설명하면 1. 대상이 되는 테이블들의 데이터를 가져오는 시점에 where 절을 수행하도록 하였습니다. 2. 데이터들을 가져온 이후에 on 절을 수행합니다. 위와 같이 구현한 이유는 곱집합의 대상이 되는 각각의 집합들(각 테이블의 검색결과)을 최소하하기 위함이므로 이러한 기능을 활용하기 위해서는 각 테이블의 검색 조건이 타이트하여야 합니다. 따라서, 다음과 같이 활용할 수 있습니다. 1. 어떠한 key를 기준으로 테이블 간 join을 수행하여 하나의 상세한 row를 생성할 수 있습니다. 2. 조회 대상이 되는 key들의 list가 있을 때에도(예) 페이징을 지원하는 조회 화면) 각 테이블별로 where 절에 in 연산자를 활용한 조건을 주어 효율적인 검색을 수행할 수 있습니다. 사용예제(sample.jpg) ![Image Description][1] 구현코드 string restfulJoin(dict argv) as rest 'GET /ksm/restfulJoin/search' { /* tables = {key, {volume, table, where, on}} */ hash
tables; /* Set From Statement */ list
from = string(argv['from']).split(','); int i; for(i=0; i
tableList = from[i].split(['.', '|']); string volume = tableList[0]; string table = tableList[1]; string key = tableList.length() == 2 ? table : tableList[2]; list
where; list
on; tables.insert(key, {'volume' : volume, 'table' : table, 'where' : where, 'on' : on}); } /* Set Where Statement */ list
onDic; list
whereStmt = string(argv['where']).split(['and', 'AND']); for(i=0; i
=, !=, =, <, in) */ string op; if(strstr(whereStmt[i], '<=') != -1) op = '<='; else if(strstr(whereStmt[i], '>=') != -1) op = '>='; else if(strstr(whereStmt[i], '!=') != -1) op = '!='; else if(strstr(whereStmt[i], '=') != -1) op = '='; else if(strstr(whereStmt[i], '<') != -1) op = '>'; else if(strstr(whereStmt[i], 'in') != -1) op = 'in'; else if(strstr(whereStmt[i], 'IN') != -1) op = 'IN'; /* 2. split by op */ list
condition = whereStmt[i].split(['<=', '>=', '!=', '=', '<', '>', 'in', 'IN']); /* 3. set lhs */ list
lhs = condition[0].trim().split('.'); lhs[0] = lhs[0].trim(); lhs[1] = lhs[1].trim(); /* 4. set rhs */ list
rhs = condition[1].trim().split('.'); rhs[0] = rhs[0].trim(); /* 5. judge statement (on(join) or where) */ bool isJoin = rhs.length() == 2 ? true : false; /* 6. if isJoin == false then set Where Statement */ if(!isJoin) { dict table = tables.at(tables.find(lhs[0])); list
where = table['where']; where.append({'lhs' : lhs[1], 'rhs' : rhs, 'op' : op}); table['where'] = where; tables.insert(lhs[0], table); } else { rhs[1] = rhs[1].trim(); onDic.append({'lhs' : lhs, 'rhs' : rhs, 'op' : op}); } } /* Set On Statement (ordering by 'hash
tables' order) */ for(i=0; i
lhs = dic['lhs']; list
rhs = dic['rhs']; if(lhsIdx < rhsIdx) { list
tmpList = dic['lhs']; lhs = dic['rhs']; rhs = tmpList; int tmpIdx = lhsIdx; lhsIdx = rhsIdx; rhsIdx = tmpIdx; } dict table = tables.at(lhsIdx); list
on = table['on']; on.append({'lhsTable' : lhs[0], 'lhsCol' : lhs[1], 'rhsTable' : rhs[0], 'rhsCol' : rhs[1], 'op' : dic['op']}); table['on'] = on; tables.insert(lhs[0], table); } /* Select Table */ list
ret; list
keys = tables.keys(); for(i=0; i
where = table['where']; string whereStmt = ''; int j; for(j=0; j
tmp = d['result']; for(j=0; j
keyList = d.keys(); dict row; int k; for(k=0; k
newRet; list
onDic = table['on']; int k; int l; for(j=0; j
=') { if(!(ret[j][on['rhsTable'] + '.' + on['rhsCol']] >= tmp[k][on['lhsTable'] + '.' + on['lhsCol']])) { add = false; break; } } else if(op == '<') { if(!(ret[j][on['rhsTable'] + '.' + on['rhsCol']] < tmp[k][on['lhsTable'] + '.' + on['lhsCol']])) { add = false; break; } } else if(op == '>') { if(!(ret[j][on['rhsTable'] + '.' + on['rhsCol']] > tmp[k][on['lhsTable'] + '.' + on['lhsCol']])) { add = false; break; } } } if(add) { dict row; list
keys1 = ret[j].keys(); list
keys2 = tmp[k].keys(); int m; for(m=0; m
Tag
RESTful REST
Module Name
restfulJoin
Attachments
restfulJoin.k (7.4kb)
sample.jpg (422.1kb)
sample.jpg (422.1kb)
Comments
Save
Save
Cancel
Save
Cancel
Warning
Login