[20211009]使用bash計算sql語句的sql_id.txt
[20211009]使用bash計算sql語句的sql_id.txt
--//以前寫的只能在命令列輸入sql語句,如果語句很長很明顯這樣的方式不是很好,嘗試支援從包含sql語句的文字檔案來計算sql_id.
$ cat sql_idx.sh
#! /bin/bash
# calcucate sql_text of full_hash_value(16),hash_value(10),sql_id(32).
# argv1 sql statement or sql of text file
# argv2 flag: 0= sql statement 1=sql of text file
odebug=${ODEBUG:-0}
oflag=${2:-0}
if [ $oflag -eq 0 ]
then
sql_text=${1}'\0'
fi
if [ $oflag -eq 1 ]
then
# sql_text="$( cat $1 | dos2unix | sed '$s/;\s*//')"'\0'
sql_text="$( cat $1 | sed "s/
$//" | sed '$s/;\s*//')"'\0'
# sql_text="$( cat $1 | sed '$s/;\s*//')"'\0'
fi
v1=$(echo -e -n "$sql_text" | md5sum | sed 's/ -//' | xxd -r -p | od -t x4 | sed -n -e 's/^0\+ //' -e 's/ //gp' | tr 'a-z' 'A-Z')
v2=${v1:(-16):16}
v3=${v2:(-8):8}
# v2=$(echo "obase=16;ibase=16; $v1 % 10000000000000000" | bc| tr -d '\\\r\n')
# v3=$(echo "obase=10;ibase=16; $v1 % 100000000" | bc| tr -d '\\\r\n')
if [ $odebug -eq 1 ] ; then
echo v1=$v1 v2=$v2 v3=$v3
fi
echo "sql_text = $sql_text"
echo "full_hash_value(16) = $v1 "
echo "hash_value(10) = $(( 16#$v3 )) "
BASE32=($(echo {0..9} {a..z} | tr -d 'eilo'))
res=''
for i in $(echo "obase=32;ibase=16; $v2" | bc| tr -d '\\\r\n')
do
res=${res}${BASE32[$(( 10#$i ))]}
done
echo "sql_id(32) = $(printf "%13s" $res | tr ' ' '0')"
echo "sql_id(32) = $(printf "%013s" $res)"
res1=$(eval $(echo "obase=32;ibase=16; $v2" | bc| tr -d '\\\r\n' | awk 'BEGIN{RS=" +"; printf "echo " }/./{printf "${BASE32[$(( 10#%02d))]}", $1}' ))
echo "sql_id(32) = $(printf "%013s" $res1)"
--//注如果安裝dos2unix可以使用它,或者使用
代替.實際上文字oracle做了格式化處理變成unix格式來計算,另外文字最後不能有空行.
--//測試如下:
1.測試1,直接輸入sql語句.
$ ./sql_idx.sh 'select * from dept where deptno=10'
sql_text = select * from dept where deptno=10\0
full_hash_value(16) = 1431C45DBDDBB9E74EAA74D53650F131
hash_value(10) = 911274289
sql_id(32) = 4xamnunv51w9j
sql_id(32) = 4xamnunv51w9j
sql_id(32) = 4xamnunv51w9j
./sql_idx.sh 'select * from dept where deptno=10' 0
sql_text = select * from dept where deptno=10\0
full_hash_value(16) = 1431C45DBDDBB9E74EAA74D53650F131
hash_value(10) = 911274289
sql_id(32) = 4xamnunv51w9j
sql_id(32) = 4xamnunv51w9j
sql_id(32) = 4xamnunv51w9j
SCOTT@book> select * from dept where deptno=10;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
SCOTT@book> @ hash
HASH_VALUE SQL_ID CHILD_NUMBER HASH_HEX
---------- ------------- ------------ ---------
911274289 4xamnunv51w9j 0 3650f131
2.測試2,使用檔案aa1.txt:
$ cat -v aa1.txt
select * from dept where deptno=10; ^M
$ ./sql_idx.sh aa 1
sql_text = select * from dept where deptno=10\0
full_hash_value(16) = 1431C45DBDDBB9E74EAA74D53650F131
hash_value(10) = 911274289
sql_id(32) = 4xamnunv51w9j
sql_id(32) = 4xamnunv51w9j
sql_id(32) = 4xamnunv51w9j
SCOTT@book> @ /home/oracle/bin/aa1.txt
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
SCOTT@book> @ hash
HASH_VALUE SQL_ID CHILD_NUMBER HASH_HEX
---------- ------------- ------------ ---------
911274289 4xamnunv51w9j 0 3650f131
--//Ok正確.
$ cat -v aa1.txt
select * from dept where deptno=10 ; ^M
$ ./sql_idx.sh aa1.txt 1
sql_text = select * from dept where deptno=10 \0
full_hash_value(16) = 8FA1DE8E2715321F0E36B1BFE045F536
hash_value(10) = 3762681142
sql_id(32) = 0wdpjrzh4bx9q
sql_id(32) = wdpjrzh4bx9q
sql_id(32) = wdpjrzh4bx9q
--//我安裝的這個bash shell指令碼有點問題,前面的0不會補上.
SCOTT@book> @ /home/oracle/bin/aa1.txt
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
SCOTT@book> @ hash
HASH_VALUE SQL_ID CHILD_NUMBER HASH_HEX
---------- ------------- ------------ ---------
3762681142 0wdpjrzh4bx9q 0 e045f536
3.測試3,文字多行的情況.
$ cat -v aa1.txt
select *^M
from dept where deptno=10; ^M
$ file aa1.txt
aa1.txt: ASCII text, with CRLF line terminators
$ ./sql_idx.sh aa1.txt 1
sql_text = select *
from dept where deptno=10\0
full_hash_value(16) = 1F6FA161B0609741DD5B66C5D8ABB5BB
hash_value(10) = 3635131835
sql_id(32) = duqv6srcarddv
sql_id(32) = duqv6srcarddv
sql_id(32) = duqv6srcarddv
SCOTT@book> @ /home/oracle/bin/aa1.txt
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
SCOTT@book> @ hash
HASH_VALUE SQL_ID CHILD_NUMBER HASH_HEX
---------- ------------- ------------ ---------
3635131835 duqv6srcarddv 0 d8abb5bb
--//Ok正確.
$ dos2unix aa1.txt
dos2unix: converting file aa1.txt to UNIX format ...
$ ./sql_idx.sh aa1.txt 1
sql_text = select *
from dept where deptno=10\0
full_hash_value(16) = 1F6FA161B0609741DD5B66C5D8ABB5BB
hash_value(10) = 3635131835
sql_id(32) = duqv6srcarddv
sql_id(32) = duqv6srcarddv
sql_id(32) = duqv6srcarddv
SCOTT@book> @ /home/oracle/bin/aa1.txt
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
SCOTT@book> @ hash
HASH_VALUE SQL_ID CHILD_NUMBER HASH_HEX
---------- ------------- ------------ ---------
3635131835 duqv6srcarddv 0 d8abb5bb
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-2795029/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- [20191011]通過bash計算sql語句的sql_id.txtSQL
- [20191101]通過zsh計算sql語句的sql_id.txtSQL
- [20220111]該語句的sql_id如何計算的.txtSQL
- 使用sql語句查詢平均值,使用sql語句查詢資料總條數, not in 篩選語句的使用SQL
- 統計介面sql語句SQL
- [20191012]使用bash從sql_id計算hash_value.txtSQL
- Java中如何解析SQL語句、格式化SQL語句、生成SQL語句?JavaSQL
- SQL語句SQL
- SQL語句IN的用法SQL
- 統計報表 -- sql統計語句SQL
- sql語句的程式設計手冊(轉)SQL程式設計
- jsqlparser使用記錄---生成sql語句JSSQL
- Fastapi sqlalchemy DBApi 直接使用sql語句ASTAPISQL
- Sql語句實現不同記錄同一屬性列的差值計算SQL
- 【SQL】Oracle SQL join on語句and和where使用區別SQLOracle
- 【SQL】9 SQL INSERT INTO 語句SQL
- 【SQL】10 SQL UPDATE 語句SQL
- 【SQL】11 SQL DELETE 語句SQLdelete
- SQL SELECT 語句SQL
- sql常用語句SQL
- 用python和計算機對話(計算機的語句)Python計算機
- 計算節點會對收到的SQL 語句做解析、優化、執行並SQL優化
- 4.3.2 關於使用SQL語句建立CDBSQL
- Oracle執行語句跟蹤 使用sql trace實現語句追蹤OracleSQL
- 查詢Oracle正在執行的sql語句及執行該語句的使用者OracleSQL
- sql語句如何執行的SQL
- MySQL中常用的SQL語句MySql
- sqlserver dba常用的sql語句SQLServer
- SQL 語句的注意事項SQL
- sql宣告變數,及if -else語句、while語句的用法SQL變數While
- 1.3. SQL 語句SQL
- Oracle基本SQL語句OracleSQL
- Sql語句小整理SQL
- SQL語句優化SQL優化
- SQL 語句學習SQL
- 資料庫常用的sql語句大全--sql資料庫SQL
- day2-計算符和if語句
- [20190929]bash使用bc計算的相關問題.txt