Query

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.0 kB
1
Indexable
Never
# app.py

# params.txt example
"""
SQL::Access: aligned:
SQL::Access:    [0]: EN
SQL::Access:    [1]: US
SQL::Access:    [2]: US
"""

PARAMS_FILE = './params.txt'
QUERY_FILE = './query.txt'

params = []
with open(PARAMS_FILE) as f:
    lines = f.readlines()
    for line in lines:
        idx_last_colon = line.rfind(':')
        value = line[idx_last_colon+1:].strip()
        
        if value == "": continue
        if value != "NULL": value = "'" + value + "'"
        params.append(value)

# Build PG query
with open(QUERY_FILE) as f:
    content = f.read()
    placeholder_content = content.replace('?', '{}')
    result = placeholder_content.format(*params)
    print(result)

==========================================
# collect.sh
#!/bin/bash

main() {
  declare -a default_items=("NAME" "ID" "POS" "POSCHG" "PCTOS" "PCTOSCHG" "MV" "MVCHG" "PCPRT" "PCPRTCHG" "SOURCE")
  items=(${@:-${default_items[@]}})

  for item in ${items[@]}
  do
  {
    export ITEM="$item"
    printf "=======$item========\n"

    folderName="collections"
    mkdir -p $folderName;
    filePath="$folderName/$item.txt"

    for dbName in "m" "s"
    do
      collect_all_ERR $dbName $filePath

      if [ "$dbName" == "s" ];then
        # Generate PG query by using app.py with params.txt and query.txt
        echo $(./s.sh | grep "^SQL::Access\:" | sed -n 'H; /^SQL::Access: aligned:/h; ${g;p;}' > 'params.txt') # Must execute again
        echo "$log" | awk '/loaded/,/aligned/' | tail -n 2 > 'query.txt'
        python3 app.py | tee -a $filePath
      else
        # Generate My query
        echo "$log" | awk  '/Command/,/RESULT/' | tee -a $filePath
      fi
    done
  }
  done
}

collect_all_ERR(){
  # Collect all ERR messages by using PRINT("ERR ") in formula editor for each dbName
  dbName=$1
  filePath=$2

  printf "===$dbName===\n" >> $filePath

  log=$(eval "./$dbName.sh 2>&1")
  echo "$log" | grep "^ERR" >> $filePath
}

main "$@"

========================