最近開始研究PostgreSQL的相關知識,寫了一個簡單的存儲結構,慢慢的發現PostgreSQL還是很值得去研究的

-- -- 建立小時記錄與日記錄的關聯規則 DROP FUNCTION IF EXISTS group_tbl_moburlvisit (name, timestamp); CREATE FUNCTION group_tbl_moburlvisit ( _tgrp name, _tick timestamp ) RETURNS name AS $$ DECLARE _strtick name; _grpname name; _tbregex name; _subtbl name; _grps name; _gfmt name; _tfmt name; _tmp integer; _cursor refcursor; BEGIN -- 確保只能從比匯聚時間小一個級別的數據中進行匯聚 IF _tgrp = 'H1' THEN _grps = '1Hour'; _gfmt = 'YYYYMMDDHH24'; _tfmt = 'YYYYMMDDHH24..'; ELSIF _tgrp = 'D1' THEN _grps = '1Day'; _gfmt = 'YYYYMMDD'; _tfmt = 'YYYYMMDD..'; ELSIF _tgrp = 'M1' THEN _grps = '1Month'; _gfmt = 'YYYYMM'; _tfmt = 'YYYYMM..'; ELSIF _tgrp = 'Y1' THEN _grps = '1Year'; _gfmt = 'YYYY'; _tfmt = 'YYYY..'; END IF; _grpname := 'tbl_moburlvisit_' || to_char(_tick, _gfmt); _tbregex := 'tbl_moburlvisit_' || to_char(_tick, _tfmt); RAISE DEBUG 'Group for %, To: %, From: %', _grps, _grpname, _tbregex; -- 獲取所有符合命名規則的表,如無則退出 EXECUTE 'SELECT count(*) FROM pg_class WHERE relname ~ ' || quote_literal(_tbregex) INTO _tmp; IF _tmp <> 0 THEN RAISE DEBUG 'Group % sub-tables into %', _tmp, _grpname; ELSE RAISE DEBUG 'Sub-tables not found: %', quote_literal(_tbregex); RETURN NULL; END IF; -- 檢查現有復合表的子表關系,並將該子表和復合表的繼承關系去除 EXECUTE 'select count(*) from pg_class where relname = ' || quote_literal(_grpname) INTO _tmp; IF _tmp <> 0 THEN OPEN _cursor FOR EXECUTE 'select a.relname from pg_catalog.pg_class a join pg_catalog.pg_inherits b on a.oid = b.inhrelid join pg_catalog.pg_class c on b.inhparent = c.oid where c.relname = ' || quote_literal(_grpname); LOOP FETCH _cursor INTO _subtbl; IF NOT FOUND THEN EXIT; END IF; RAISE DEBUG 'Cut table % from %', _subtbl, _grpname; EXECUTE 'ALTER TABLE ' || _subtbl || ' NO INHERIT ' || _grpname; END LOOP; CLOSE _cursor; -- 檢查現有復合表,如有則刪除 EXECUTE 'DROP TABLE IF EXISTS ' || _grpname; END IF; -- 建立復合表 EXECUTE 'CREATE TABLE ' || _grpname || ' ( id_moburlvisit BIGINT NOT NULL, mob BIGINT NOT NULL, lac INTEGER NOT NULL, ci INTEGER NOT NULL, id_urlrule INTEGER NOT NULL, id_uarule INTEGER NOT NULL, tick TIMESTAMP NOT NULL, rate INTEGER NOT NULL, pv INTEGER NOT NULL ) WITH (OIDS=FALSE)'; -- 檢查各子表,並將該子表添加進復合表 OPEN _cursor FOR EXECUTE 'SELECT relname FROM pg_class WHERE relname ~ ' || quote_literal(_tbregex); LOOP FETCH _cursor INTO _subtbl; IF NOT FOUND THEN EXIT; END IF; RAISE DEBUG 'Group table % into %', _subtbl, _grpname; EXECUTE 'ALTER TABLE ' || _subtbl || ' INHERIT ' || _grpname; END LOOP; CLOSE _cursor; RETURN _grpname; END; $$ LANGUAGE PLpgSQL;
其中查看了很多的相關博客,我也來總結一下PostgreSQL里面的字符串相關的函數吧!
函數:string || string
說明:String concatenation 字符串連接操作
例子:select 'Post' || 'greSQL'; = PostgreSQL
函數:string || non-string or non-string || string
說明:String concatenation with one non-string input 字符串與非字符串類型進行連接操作
例子:select 'Value: ' || 42; = Value: 42
函數:bit_length(string)
說明:Number of bits in string 計算字符串的位數
例子:select bit_length('pmars') = 40
函數:char_length(string) or character_length(string)
說明:Number of characters in string 計算字符串中字符個數
例子:select char_length('pmars'); = 5
函數:lower(string)
說明:Convert string to lower case 轉換字符串為小寫
例子:select lower('PmArS'); = "pmars"
函數:octet_length(string)
說明:Number of bytes in string 計算字符串的字節數
例子:select octet_length('我是pmars'); = 11 select octet_length('我'); = 3
函數:overlay(string placing string from int [for int])
說明:Replace substring 替換字符串中任意長度的子字串為新字符串
例子:select overlay('I am pmars' placing 'ming' from 6 for 5); = "I am ming"
函數:position(substring in string)
說明:Location of specified substring 子串在一字符串中的位置
例子:select position('ma' in 'pmars'); = 2
函數:substring(string [from int] [for int])
說明:Extract substring 截取任意長度的子字符串
例子:select substring('topmars' from 3 for 3); = "pma"
函數:substring(string from pattern)
說明:Extract substring matching POSIX regular expression. See Section 9.7 for more information on pattern matching. 利用正則表達式對一字符串進行任意長度的字串的截取
例子:select substring('topmars' from 'p.*$'); = "pmars"
函數:substring(string from pattern for escape)
說明:Extract substring matching SQL regular expression. See Section 9.7 for more information on pattern matching. 利於正則表達式對某類字符進行刪除,以得到子字符串
例子:select substring('Thomas' from '%#"o_a#"_' for '#'); = "oma"
函數:trim([leading | trailing | both] [characters] from string)
說明:Remove the longest string containing only the characters (a space by default) from the start/end/both ends of the string 去除盡可能長開始,結束或者兩邊的某類字符,默認為去除空白字符,當然可以自己指定,可同時指定多個要刪除的字符串
例子:select trim(leading 'p' from 'pmars'); = "mars"
函數:upper(string)
說明:Convert string to uppercase 將字符串轉換為大寫
例子:select upper('pmars'); = "PMARS"
函數:ascii(string)
說明:ASCII code of the first character of the argument. For UTF8 returns the Unicode code point of the character. For other multibyte encodings. the argument must be a strictly ASCII character. 得到某一個字符的Assii值
例子:select ascii('pmars'); = select ascii('p'); = 112
函數:btrim(string text [, characters text])
說明:Remove the longest string consisting only of characters in characters (a space by default) from the start and end of string 去除字符串兩邊的所有指定的字符,可同時指定多個字符
例子:select btrim('pmars','prs'); = "ma"
函數:chr(int)
說明:Character with the given code. For UTF8 the argument is treated as a Unicode code point. For other multibyte encodings the argument must designate a strictly ASCII character. The NULL (0) character is not allowed because text data types cannot store such bytes. 得到某ACSII值對應的字符
例子:select chr(65); = A
函數:convert(string bytea, src_encoding name, dest_encoding name)
說明:Convert string to dest_encoding. The original encoding is specified by src_encoding. The string must be valid in this encoding. Conversions can be defined by CREATE CONVERSION. Also there are some predefined conversions. See Table 9-7 for available conversions. 轉換字符串編碼,指定源編碼與目標編碼
例子:select convert('我是pmars_in_utf8', 'UTF8', 'GBK'); = "\316\322\312\307pmars_in_utf8"
函數:convert_from(string bytea, src_encoding name)
說明:Convert string to the database encoding. The original encoding is specified by src_encoding. The string must be valid in this encoding. 轉換字符串編碼,自己要指定源編碼,目標編碼默認為數據庫指定編碼,
例子:select convert_from('\316\322\312\307pmars','GBK'); = "我是pmars"
函數:convert_to(string text, dest_encoding name)
說明:Convert string to dest_encoding.轉換字符串編碼,源編碼默認為數據庫指定編碼,自己要指定目標編碼,
例子:select convert_to('我是pmars_in_utf8','GBK'); = "\316\322\312\307pmars_in_utf8"
函數:decode(string text, type text)
說明:Decode binary data from string previously encoded with encode. Parameter type is same as in encode. 對字符串按指定的類型進行解碼
例子:select decode('MTIzAAE=', 'base64'); = "123\000\001"
函數:encode(data bytea, type text)
說明:Encode binary data to different representation. Supported types are: base64, hex, escape. Escape merely outputs null bytes as \000 and doubles backslashes. 與decode相反,對字符串按指定類型進行編碼
例子:select encode('123\000\001','base64'); = "MTIzAAE="
函數:initcap(string)
說明:Convert the first letter of each word to uppercase and the rest to lowercase. Words are sequences of alphanumeric characters separated by non-alphanumeric characters. 將字符串所有的單詞進行格式化,首字母大寫,其它為小寫
例子:select initcap('I AM PMARs'); = "I Am Pmars"
函數:length(string)
說明:Number of characters in string 講算字符串長度
例子:select length('我是pmars'); = 7
函數:length(stringbytea, encoding name )
說明:Number of characters in string in the given encoding. The string must be valid in this encoding. 計算字符串長度,指定字符串使用的編碼
例子:select length('我是pmars','GBK'); = 8
函數:lpad(string text, length int [, fill text])
說明:Fill up the string to length length by prepending the characters fill (a space by default). If the string is already longer than length then it is truncated (on the right). 對字符串左邊進行某類字符自動填充,即不足某一長度,則在左邊自動補上指定的字符串,直至達到指定長度,可同時指定多個自動填充的字符
例子:select lpad('pmars', 10, 'to'); = "tototpmars"
函數:ltrim(string text [, characters text])
說明:Remove the longest string containing only characters from characters (a space by default) from the start of string 刪除字符串左邊某一些的字符,可以時指定多個要刪除的字符
例子:select ltrim('pmars','amp'); = "rs"
函數:md5(string)
說明:Calculates the MD5 hash of string, returning the result in hexadecimal 將字符串進行md5編碼
例子:select md5('pmars'); = "1018ceb949f1472f7252f7da1f5eff42"
函數:pg_client_encoding()
說明:Current client encoding name 得到pg客戶端編碼
例子:select pg_client_encoding(); = "UTF8"
函數:quote_ident(string text)
說明:Return the given string suitably quoted to be used as an identifier in an SQL statement string. Quotes are added only if necessary (i.e., if the string contains non-identifier characters or would be case-folded). Embedded quotes are properly doubled. 對某一字符串加上兩引號
例子:quote_ident('Foo bar') = "Foo bar"
函數:quote_literal(string text)
說明:Return the given string suitably quoted to be used as a string literal in an SQL statement string. Embedded single-quotes and backslashes are properly doubled. 對字符串里兩邊加上單引號,如果字符串里面出現sql編碼的單個單引號,則會被表達成兩個單引號
例子:quote_literal('O\'Reilly') = 'O''Reilly'
函數:quote_literal(value anyelement)
說明:Coerce the given value to text and then quote it as a literal. Embedded single-quotes and backslashes are properly doubled. 將一數值轉換為字符串,並為其兩邊加上單引號,如果數值中間出現了單引號,也會被表示成兩個單引號
例子:quote_literal(42.5) = '42.5'
函數:regexp_matches(string text, pattern text [, flags text])
說明:Return all captured substrings resulting from matching a POSIX regular expression against the string. See Section 9.7.3 for more information. 對字符串按正則表達式進行匹配,如果存在則會在結果數組中表示出來
例子:regexp_matches('foobarbequebaz', '(bar)(beque)') = {bar,beque}
函數:regexp_replace(string text, pattern text, replacement text [, flags text])
說明:Replace substring(s) matching a POSIX regular expression. See Section 9.7.3 for more information. 利用正則表達式對字符串進行替換
例子:regexp_replace('Thomas', '.[mN]a.', 'M') = ThM
函數:regexp_split_to_array(string text, pattern text [, flags text ])
說明:Split string using a POSIX regular expression as the delimiter. See Section 9.7.3 for more information. 利用正則表達式將字符串分割成數組
例子:regexp_split_to_array('hello world', E'\\s+') = {hello,world}
函數:regexp_split_to_table(string text, pattern text [, flags text])
說明:Split string using a POSIX regular expression as the delimiter. See Section 9.7.3 for more information. 利用正則表達式將字符串分割成表格
例子:regexp_split_to_table('hello world', E'\\s+') =
hello
world
(2 rows)
函數:repeat(string text, number int)
說明:Repeat string the specified number of times 重復字符串一指定次數
例子:repeat('Pg', 4) = PgPgPgPg
函數:replace(string text, from text, to text)
說明:Replace all occurrences in string of substring from with substring to 將字符的某一子串替換成另一子串
例子:('abcdefabcdef', 'cd', 'XX') = abXXefabXXef
函數:rpad(string text, length int [, fill text])
說明:Fill up the string to length length by appending the characters fill (a space by default). If the string is already longer than length then it is truncated. 對字符串進行填充,填充內容為指定的字符串
例子:rpad('hi', 5, 'xy') = hixyx
函數:rtrim(string text [, characters text])
說明:Remove the longest string containing only characters from characters (a space by default) from the end of string
去除字符串右邊指定的字符
例子:rtrim('trimxxxx', 'x') = trim
函數:split_part(string text, delimiter text, field int)
說明:Split string on delimiter and return the given field (counting from one) 對字符串按指定子串進行分割,並返回指定的數值位置的值
例子:split_part('abc~@~def~@~ghi', '~@~', 2) = def
函數:strpos(string, substring)
說明:Location of specified substring (same as position(substring in string), but note the reversed argument order) 指定字符串在目標字符串的位置
例子:strpos('high', 'ig') = 2
函數:substr(string, from [, count])
說明:Extract substring (same as substring(string from from for count)) 截取子串
例子:substr('alphabet', 3, 2) = ph
函數:to_ascii(string text [, encoding text])
說明:Convert string to ASCII from another encoding (only supports conversion from LATIN1, LATIN2, LATIN9, and WIN1250 encodings) 將字符串轉換成ascii編碼字符串
例子:to_ascii('Karel') = Karel
函數:to_hex(number int or bigint)
說明:Convert number to its equivalent hexadecimal representation 對數值進行十六進制編碼
例子:to_hex(2147483647) = 7fffffff
函數:translate(string text, from text, to text)
說明:Any character in string that matches a character in the from set is replaced by the corresponding character in the to set 將字符串中某些匹配的字符替換成指定字符串,目標字符與源字符都可以同時指定多個
例子:translate('12345', '14', 'ax') = a23x5