쿼리 asis->tobe 컬럼 변환 위해 만든 함수

ino5·2023년 9월 23일
0

23년 회사

목록 보기
3/6

// 카멜케이스 변환 함수
function toCamelCase(paramStr) {
  return paramStr.toLowerCase().replace(/_[a-z]/g, str => str[1].toUpperCase());
}

// 테이블 포함하고 있는지
function hasTable($txaBefore, asisTableName) {
  return $txaBefore.value.includes(asisTableName);
}

// 변경하기
function replaceWord(paramStr, asis, tobe, isTable, tobeColumnComment, afterColumnList, tobeTableNmae) {
  // 테이블인 경우 앞에 스키마명 붙여주기
  if (isTable) {
    tobe = TABLE_SCHEMA + '.' + tobe;
  }
  
  // TO-BE가 존재하지 않는 경우 AS-IS 컬럼명 그대로
  if (tobe.includes(NOT_EXIST_TEXT) {
    tobe = `${asis} /* ${asis}(AS-IS) ${NOT_EXIST_TEXT} in ${tobeTableName}(TO-BE) */`;
    tobeColumnComment = '';
  }

  // 코멘트까지 붙이는 경우
  if (!isTable) {
    const regExpStrForComment = `(?<=([^_A-Za-z]))${asis}[ ]*?(?=([\\n]))`;
    const regExpForComment = new RegExp(regExpStrForComment, 'g');
    const regExpStrForCommentTobe = `(?<=([^_A-Za-z]))${tobe}[ ]*?(?=([\\n]))`;
    const regExpForCommentTobe = new RegExp(regExpStrForCommentTobe, 'g');

    // 코멘트 더해서 변환하기
    if (tobeColumnComment != null && tobeColumnComment.length > 0) {
      paramStr = paramStr.replace(regExpForComment, tobe + ` /* ${tobeColumnComment} */ `);
      paramStr = paramStr.replace(regExpForCommentTobe, tobe + ` /* ${tobeColumnComment} */`); // TO-BE 문자열을 AS-IS 쿼리에 넣었을 경우 코멘트만 추가로 넣어주기
    }

    // 변환하기
    const regExpStr = `(?<=([^_A-Za-z]))${asis}?(?=([\\s}\\)\\,]))`;
    const regExp = new RegExp(regExpStr, 'g');

    paramStr = paramStr.replace(regExp, tobe);

    // 컬럼 목록에 넣기
    const regExpTobe = `(?<=([^_A-Za-z]))${tobe}?(?=([\\s}\\)\\,]))`;
    if (afterColumnList != null && paramStr.search(regExpTobe) >= 0 && !tobe.includes(NOT_EXIST_TEXT)) {
      const contains = afterColumnList.some(item => item.substring(0, toCamelCase(tobe).length) == toCamelCase(tobe)); // 목록에 이미 넣었는지 확인
      if (!contains) {
        afterColumnList.push(`${toCamelCase(tobe)}(${tobeColumnComment}`);
      }
    }
  }

  return paramStr
}
profile
지금은 네이버 블로그만 해요... https://blog.naver.com/chero77

0개의 댓글