|
| 1 | +## PostgreSQL Oracle 兼容性之 dump chars's byte stream - decode |
| 2 | + |
| 3 | +### 作者 |
| 4 | +digoal |
| 5 | + |
| 6 | +### 日期 |
| 7 | +2019-08-13 |
| 8 | + |
| 9 | +### 标签 |
| 10 | +PostgreSQL , 字符串字节流 , dump |
| 11 | + |
| 12 | +---- |
| 13 | + |
| 14 | +## 背景 |
| 15 | +导出字符串的长度,显示逐个字节。 |
| 16 | + |
| 17 | +oracle dump用法如下 |
| 18 | + |
| 19 | +https://docs.oracle.com/cd/B28359_01/server.111/b28286/functions048.htm |
| 20 | + |
| 21 | +``` |
| 22 | +Examples |
| 23 | + |
| 24 | +The following examples show how to extract dump information from a string expression and a column: |
| 25 | + |
| 26 | +SELECT DUMP('abc', 1016) |
| 27 | + FROM DUAL; |
| 28 | + |
| 29 | +DUMP('ABC',1016) |
| 30 | +------------------------------------------ |
| 31 | +Typ=96 Len=3 CharacterSet=WE8DEC: 61,62,63 |
| 32 | +SELECT DUMP(last_name, 8, 3, 2) "OCTAL" |
| 33 | + FROM employees |
| 34 | + WHERE last_name = 'Hunold' |
| 35 | + ORDER BY employee_id; |
| 36 | + |
| 37 | +OCTAL |
| 38 | +------------------------------------------------------------------- |
| 39 | +Typ=1 Len=6: 156,157 |
| 40 | + |
| 41 | +SELECT DUMP(last_name, 10, 3, 2) "ASCII" |
| 42 | + FROM employees |
| 43 | + WHERE last_name = 'Hunold' |
| 44 | + ORDER BY employee_id; |
| 45 | + |
| 46 | +ASCII |
| 47 | +-------------------------------------------------------------------- |
| 48 | +Typ=1 Len=6: 110,111 |
| 49 | +``` |
| 50 | + |
| 51 | + |
| 52 | +## PG dump实现方法 |
| 53 | +借助PG的类型转换,自定义一个sql函数即可。 |
| 54 | + |
| 55 | +``` |
| 56 | +postgres=# show bytea_output ; |
| 57 | + bytea_output |
| 58 | +-------------- |
| 59 | + hex |
| 60 | +(1 row) |
| 61 | + |
| 62 | +postgres=# select 'nihao 德哥'::bytea; |
| 63 | + bytea |
| 64 | +---------------------------- |
| 65 | + \x6e6968616f20e5beb7e593a5 |
| 66 | +(1 row) |
| 67 | +``` |
| 68 | + |
| 69 | +``` |
| 70 | +postgres=# |
| 71 | +select format('Len=%s ', len) || string_agg(res,',') from |
| 72 | + (select to_hex(get_byte(info::bytea, generate_series(0,len-1))) res,len from |
| 73 | + (select info,octet_length(info) len from |
| 74 | + (values('nihao中国digoal德哥')) t(info) |
| 75 | + )t |
| 76 | + )t |
| 77 | +group by len; |
| 78 | + ?column? |
| 79 | +----------------------------------------------------------------------------- |
| 80 | + Len=23 6e,69,68,61,6f,e4,b8,ad,e5,9b,bd,64,69,67,6f,61,6c,e5,be,b7,e5,93,a5 |
| 81 | +(1 row) |
| 82 | +``` |
| 83 | + |
| 84 | +写成函数如下 |
| 85 | + |
| 86 | +``` |
| 87 | +create or replace function dump(text) returns text as $$ |
| 88 | + select format('Len=%s ', len) || string_agg(res,',') from |
| 89 | + (select to_hex(get_byte(info::bytea, generate_series(0,len-1))) res,len from |
| 90 | + (select info,octet_length(info) len from |
| 91 | + (values($1)) t(info) |
| 92 | + )t |
| 93 | + )t |
| 94 | + group by len; |
| 95 | +$$ language sql strict immutable; |
| 96 | +``` |
| 97 | + |
| 98 | +使用dump函数将导出字符串的字节流 |
| 99 | + |
| 100 | +``` |
| 101 | +postgres=# select dump(';nihao中国digoal德哥'); |
| 102 | + dump |
| 103 | +-------------------------------------------------------------------------------- |
| 104 | + Len=24 3b,6e,69,68,61,6f,e4,b8,ad,e5,9b,bd,64,69,67,6f,61,6c,e5,be,b7,e5,93,a5 |
| 105 | +(1 row) |
| 106 | +``` |
| 107 | + |
| 108 | +简单好用。 |
| 109 | + |
| 110 | + |
| 111 | +<a rel="nofollow" href="http://info.flagcounter.com/h9V1" ><img src="http://s03.flagcounter.com/count/h9V1/bg_FFFFFF/txt_000000/border_CCCCCC/columns_2/maxflags_12/viewers_0/labels_0/pageviews_0/flags_0/" alt="Flag Counter" border="0" ></a> |
| 112 | + |
| 113 | + |
| 114 | +## [digoal's 大量PostgreSQL文章入口](https://github.com/digoal/blog/blob/master/README.md "22709685feb7cab07d30f30387f0a9ae") |
| 115 | + |
| 116 | + |
| 117 | +## [免费领取阿里云RDS PostgreSQL实例、ECS虚拟机](https://free.aliyun.com/ "57258f76c37864c6e6d23383d05714ea") |
| 118 | + |
0 commit comments