现有的DWH系统的是按天创建数据表的,使得定期维护变得麻烦,例如每个月底需要将按当月产生的临时表archive。
方式1.批量生成SQL,按固定的日期值生成一堆SQL,SQL生成方法多样。但是需要确认全部sql是否正确。
方式2.编写PL/SQL function,使用游标方式批量执行。
DROP FUNCTION fun_datecursor(from_date text,to_date text,cond text);
CREATE OR REPLACE FUNCTION fun_datecursor(from_date text,to_date text,cond text) RETURNS text
AS $
DECLARE
cur_date refcursor;
buffer text := '';
var_day DATE;
BEGIN
OPEN cur_date FOR EXECUTE 'SELECT generate_series('''|| from_date ||'''::date,'''|| to_date ||'''::date,'''|| cond ||''')';
loop --开始循环
fetch cur_date INTO var_day; --将游标指定的值赋值给变量
IF found THEN
---------------------------------------
--任意的逻辑,或调用其他function
---------------------------------------
buffer:= buffer || to_char(var_day,'yyyyMMdd');
ELSE
exit;
END IF;
END loop; --结束循环
close cur_date; --关闭游标
RETURN buffer;
END
$ LANGUAGE plpgsql; |
drop function fun_datecursor(from_date text,to_date text,cond text);
create or replace function fun_datecursor(from_date text,to_date text,cond text) returns text
as $
declare
cur_date refcursor;
buffer text := '';
var_day date;
begin
open cur_date for execute 'SELECT generate_series('''|| from_date ||'''::date,'''|| to_date ||'''::date,'''|| cond ||''')';
loop --开始循环
fetch cur_date into var_day; --将游标指定的值赋值给变量
if found then
---------------------------------------
--任意的逻辑,或调用其他function
---------------------------------------
buffer:= buffer || to_char(var_day,'yyyyMMdd');
else
exit;
end if;
end loop; --结束循环
close cur_date; --关闭游标
return buffer;
end
$ language plpgsql;
执行function
SELECT fun_datecursor('2008-03-01','2008-03-31','1 day’); |
select fun_datecursor('2008-03-01','2008-03-31','1 day’);
PostgreSQL可以通过schema和table级别对数据表进行只读控制
一般会使用PostgreSQL创建只读用户,然后给予相应的只读权限方式实现
通过使用
-- 创建readonly_user用户,密码为readonly_password
CREATE USER readonly_user WITH encrypted password 'readonly_password';
-- 设置readonly_user用户为只读事务
ALTER USER readonly_user SET default_transaction_read_only=ON;
-- 授予usage权限给到readonly_user用户
GRANT usage ON schema "public" TO readonly_user;
-- 将默认"public"schema下新建表的读取权限授予给readonly_user
ALTER DEFAULT privileges IN schema "public" GRANT SELECT ON TABLES TO readonly_user;
-- 授予select权限给到readonly_user用户
GRANT SELECT ON ALL TABLES IN schema "public" TO readonly_user;
GRANT SELECT ON ALL sequences IN schema "public" TO readonly_user;
-- 允许readonly_user用户连接到指定数据库
GRANT CONNECT ON DATABASE splrp_dev TO readonly_user; |
-- 创建readonly_user用户,密码为readonly_password
create user readonly_user with encrypted password 'readonly_password';
-- 设置readonly_user用户为只读事务
alter user readonly_user set default_transaction_read_only=on;
-- 授予usage权限给到readonly_user用户
grant usage on schema "public" to readonly_user;
-- 将默认"public"schema下新建表的读取权限授予给readonly_user
alter default privileges in schema "public" grant select on tables to readonly_user;
-- 授予select权限给到readonly_user用户
grant select on all tables in schema "public" to readonly_user;
grant select on all sequences in schema "public" to readonly_user;
-- 允许readonly_user用户连接到指定数据库
grant connect on database splrp_dev to readonly_user;
注意:
已有的数据表进行readonly设置,可以通过
-- 授予usage权限给到readonly_user用户
GRANT usage ON schema "public" TO readonly_user;
-- 授予select权限给到readonly_user用户
GRANT SELECT ON ALL TABLES IN schema "public" TO readonly_user;
GRANT SELECT ON ALL sequences IN schema "public" TO readonly_user; |
-- 授予usage权限给到readonly_user用户
grant usage on schema "public" to readonly_user;
-- 授予select权限给到readonly_user用户
grant select on all tables in schema "public" to readonly_user;
grant select on all sequences in schema "public" to readonly_user;
对于将来创建的新表,则需要通过
-- 将默认"public"schema下新建表的读取权限授予给readonly_user
ALTER DEFAULT privileges IN schema "public" GRANT SELECT ON TABLES TO readonly_user; |
-- 将默认"public"schema下新建表的读取权限授予给readonly_user
alter default privileges in schema "public" grant select on tables to readonly_user;
可以讲以后创建的table也赋予readonly_user只读权限。
VBA实现Base64编码和Base64解码,用于处理加密的URL非常方便。
VBA Base64 编码/加密函数:
'VBA Base64 编码/加密函数:
Function Base64Encode(StrA As String) As String 'Base64 编码
On Error GoTo over '排错
Dim buf() As Byte, length As Long, mods As Long
Dim Str() As Byte
Dim i, kk As Integer
kk = Len(StrA) - 1
ReDim Str(kk)
For i = 0 To kk
Str(i) = Asc(Mid(StrA, i + 1, 1))
Next i
Const B64_CHAR_DICT = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
mods = (UBound(Str) + 1) Mod 3 '除以3的余数
length = UBound(Str) + 1 - mods
ReDim buf(length / 3 * 4 + IIf(mods <> 0, 4, 0) - 1)
For i = 0 To length - 1 Step 3
buf(i / 3 * 4) = (Str(i) And &HFC) / &H4
buf(i / 3 * 4 + 1) = (Str(i) And &H3) * &H10 + (Str(i + 1) And &HF0) / &H10
buf(i / 3 * 4 + 2) = (Str(i + 1) And &HF) * &H4 + (Str(i + 2) And &HC0) / &H40
buf(i / 3 * 4 + 3) = Str(i + 2) And &H3F
Next
If mods = 1 Then
buf(length / 3 * 4) = (Str(length) And &HFC) / &H4
buf(length / 3 * 4 + 1) = (Str(length) And &H3) * &H10
buf(length / 3 * 4 + 2) = 64
buf(length / 3 * 4 + 3) = 64
ElseIf mods = 2 Then
buf(length / 3 * 4) = (Str(length) And &HFC) / &H4
buf(length / 3 * 4 + 1) = (Str(length) And &H3) * &H10 + (Str(length + 1) And &HF0) / &H10
buf(length / 3 * 4 + 2) = (Str(length + 1) And &HF) * &H4
buf(length / 3 * 4 + 3) = 64
End If
For i = 0 To UBound(buf)
Base64Encode = Base64Encode + Mid(B64_CHAR_DICT, buf(i) + 1, 1)
Next
over:
End Function |
'VBA Base64 编码/加密函数:
Function Base64Encode(StrA As String) As String 'Base64 编码
On Error GoTo over '排错
Dim buf() As Byte, length As Long, mods As Long
Dim Str() As Byte
Dim i, kk As Integer
kk = Len(StrA) - 1
ReDim Str(kk)
For i = 0 To kk
Str(i) = Asc(Mid(StrA, i + 1, 1))
Next i
Const B64_CHAR_DICT = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
mods = (UBound(Str) + 1) Mod 3 '除以3的余数
length = UBound(Str) + 1 - mods
ReDim buf(length / 3 * 4 + IIf(mods <> 0, 4, 0) - 1)
For i = 0 To length - 1 Step 3
buf(i / 3 * 4) = (Str(i) And &HFC) / &H4
buf(i / 3 * 4 + 1) = (Str(i) And &H3) * &H10 + (Str(i + 1) And &HF0) / &H10
buf(i / 3 * 4 + 2) = (Str(i + 1) And &HF) * &H4 + (Str(i + 2) And &HC0) / &H40
buf(i / 3 * 4 + 3) = Str(i + 2) And &H3F
Next
If mods = 1 Then
buf(length / 3 * 4) = (Str(length) And &HFC) / &H4
buf(length / 3 * 4 + 1) = (Str(length) And &H3) * &H10
buf(length / 3 * 4 + 2) = 64
buf(length / 3 * 4 + 3) = 64
ElseIf mods = 2 Then
buf(length / 3 * 4) = (Str(length) And &HFC) / &H4
buf(length / 3 * 4 + 1) = (Str(length) And &H3) * &H10 + (Str(length + 1) And &HF0) / &H10
buf(length / 3 * 4 + 2) = (Str(length + 1) And &HF) * &H4
buf(length / 3 * 4 + 3) = 64
End If
For i = 0 To UBound(buf)
Base64Encode = Base64Encode + Mid(B64_CHAR_DICT, buf(i) + 1, 1)
Next
over:
End Function
Read more…
Recent Comments