pg.ddx.io  pgsql-zh-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Quan Zongliang <zongliang.quan@postgresdata.com>
To: pgsql-zh-general@postgresql.org <pgsql-zh-general@postgresql.org>
Subject: 随机字符串函数的C语言实现
Date: Tue, 5 Sep 2017 16:28:18 +0800
Message-ID: <e4b2ab08-6847-e431-9ccb-d42ac805a3d7@postgresdata.com> (raw)
List-Unsubscribe:  <mailto:majordomo@postgresql.org?body=unsub%20pgsql-zh-general>

各位下午好
邮件列表沉寂太久,激活一下大家的情绪。

分享一个C语言实现的随机字符串函数:
PG_FUNCTION_INFO_V1(random_string);
Datum
random_string(PG_FUNCTION_ARGS)
{
	int32 length = PG_GETARG_INT32(0);
	int32 i;
	const char chars[] = 
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	char *result;

	if (length < 0)
		PG_RETURN_NULL();

	if (length == 0)
		PG_RETURN_TEXT_P(cstring_to_text(""));

	result = palloc(length + 1);
	if (result == NULL)
		ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY),
					errmsg("out of memory")));

	for (i=0; i<length; i++)
	{
		result[i] = chars[random()%(sizeof(chars)-1)];
	}
	result[i] = '\0';

	PG_RETURN_TEXT_P(cstring_to_text(result));
}

原文见:
https://my.oschina.net/quanzl/blog/1529438

欢迎关注我司公众号:postgresdata-news

-- 
权宗亮
神州飞象(北京)数据科技有限公司
我们的力量源自最先进的开源数据库PostgreSQL
zongliang.quan@postgresdata.com



-- 
Sent via pgsql-zh-general mailing list (pgsql-zh-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-zh-general



view thread (5+ messages)  latest in thread

Message-ID: <e4b2ab08-6847-e431-9ccb-d42ac805a3d7@postgresdata.com>
Permalink:  ../e4b2ab08-6847-e431-9ccb-d42ac805a3d7@postgresdata.com/
Also on:    postgresql.org/message-id/e4b2ab08-6847-e431-9ccb-d42ac805a3d7@postgresdata.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-zh-general@postgresql.org
  Cc: zongliang.quan@postgresdata.com
  Subject: Re: 随机字符串函数的C语言实现
  In-Reply-To: <e4b2ab08-6847-e431-9ccb-d42ac805a3d7@postgresdata.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox