mac到sn转换sql 脚本

--16进制转10进制

CREATE OR REPLACE FUNCTION hex_to_int(hexval varchar) RETURNS bigint AS $$
DECLARE
    result  bigint;
BEGIN
    EXECUTE 'SELECT x' || quote_literal(hexval) || '::bigint' INTO result;
    RETURN result;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT;

--allMac转allEsn

CREATE OR REPLACE FUNCTION getallesn(sn varchar(100))
  RETURNS varchar(100)
  LANGUAGE plpgsql
AS
$$
declare
  resn varchar(100);
  arrsn varchar(100) [];
  s varchar(10);
  x varchar(1);
  y varchar(1);
  i int;
  j int;
begin
  resn='';
  arrsn =regexp_split_to_array(sn,':');
  <<label1>> foreach s  in array arrsn
  loop
    x=substring(s,1,1);
y=substring(s,2,1);
--i
if x<>' ' then
i=hex_to_int(x); 
if i%2=0 then 
i=i+1;
else 
i=i-1;
end if;
end if;
--j
if y<>' ' then
j=hex_to_int(y); 
if j%2=0 then 
j=j+1;
else 
j=j-1;
end if;
end if;
    resn=resn||to_hex(i)||to_hex(j)||':';
  end loop label1;
  resn=substring(resn,1,length(resn)-1);
  return resn;
end;
 
$$;

--mac转esn

CREATE OR REPLACE FUNCTION public.getesn(
sn character varying)
    RETURNS character varying
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
AS $BODY$
declare
  resn varchar(100);
  arrsn varchar(100) [];
  s varchar(10);
  x varchar(1);
  y varchar(1);
  i int;
  j int;
begin
  resn='';
  arrsn =regexp_split_to_array(sn,':');
  <<label1>> foreach s  in array arrsn
  loop
    x=substring(s,1,1);
y=substring(s,2,1);
--i
if x<>' ' then
i=hex_to_int(x); 
if i%2=0 then 
i=i+1;
else 
i=i-1;
end if;
else
x='0';
i=hex_to_int(x); 
if i%2=0 then 
i=i+1;
else 
i=i-1;
end if;
end if;
--j
if y<>' ' then
j=hex_to_int(y); 
if j%2=0 then 
j=j+1;
else 
j=j-1;
end if;
else 
y='0';
j=hex_to_int(y); 
if j%2=0 then 
j=j+1;
else 
j=j-1;
end if;
end if;
    resn=resn||to_hex(i)||to_hex(j)||':';
  end loop label1;
  resn=substring(resn,1,length(resn)-1);
  return resn;
end;
$BODY$;

-------------- 阿里环境mac处理转换

--esn 赋值到mac
update ne set extattrs=jsonb_set(extattrs, '{mac}', concat('"', extattrs->>'esn', '"')::jsonb,true) where extattrs->>'esn' is not null;
--allEsn 赋值到allMac
update ne set extattrs=jsonb_set(extattrs, '{allMac}', concat('"', extattrs->>'allEsn', '"')::jsonb,true) where extattrs->>'allMac' is not null;
--从allMac转换到allEsn  偶数+1  奇数-1
update ne set extattrs=jsonb_set(extattrs, '{allEsn}', concat('"', getallesn(extattrs->>'allMac'), '"')::jsonb,true) where extattrs->>'allEsn' is not null; 
--从mac转换到esn  偶数+1  奇数-1
update ne set extattrs=jsonb_set(extattrs, '{esn}', concat('"', getesn(extattrs->>'mac'), '"')::jsonb,true) where extattrs->>'allEsn' is not null;

--给cpe设备 snOrMac 赋值默认值 为mac
update ne set extattrs=jsonb_set(extattrs, '{snOrMac}', concat('"', 'mac', '"')::jsonb,true)  where 
 connectiontypes->0=('"CALLHOME"')::jsonb and extattrs ->>'snOrMac' is null;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值