<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sinkro.net &#187; c</title>
	<atom:link href="http://www.sinkro.net/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sinkro.net</link>
	<description>Software e dintorni. ... soprattutto dintorni ;-)</description>
	<lastBuildDate>Mon, 26 Sep 2011 01:42:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Come creare una funzione in C per PostgreSql</title>
		<link>http://www.sinkro.net/2006/08/01/come-creare-una-funzione-in-c-per-postgresql/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=come-creare-una-funzione-in-c-per-postgresql</link>
		<comments>http://www.sinkro.net/2006/08/01/come-creare-una-funzione-in-c-per-postgresql/#comments</comments>
		<pubDate>Tue, 01 Aug 2006 11:23:03 +0000</pubDate>
		<dc:creator>Massimiliano</dc:creator>
				<category><![CDATA[Codice]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[db]]></category>
		<category><![CDATA[postgres]]></category>
		<category><![CDATA[postgresql]]></category>

		<guid isPermaLink="false">http://www.sinkro.net/2006/08/01/come-creare-una-funzione-in-c-per-postgresql/</guid>
		<description><![CDATA[Qualche tempo fa, ho avuto necessità di eseguire uno script esterno legato ad un trigger di PostgreSql. All'epoca, l'unica soluzione che ho trovato è stata di creare una funzione in C che richiamasse lo script con i dovuti parametri. Questo il codice: #include #include #include &#34;postgres.h&#34; #include #include &#34;fmgr.h&#34; #include &#34;utils/builtins.h&#34; #define _textout(str) DatumGetPointer(DirectFunctionCall1(textout, PointerGetDatum(str))) [...]


Altri articoli di interesse:<ol><li><a href='http://www.sinkro.net/2005/01/20/rilasciato-postgresql-80/' rel='bookmark' title='Rilasciato PostgreSQL 8.0'>Rilasciato PostgreSQL 8.0</a> <small>NY, NY: 19 Gennaio 2005 - Il Gruppo di Sviluppo...</small></li>
<li><a href='http://www.sinkro.net/2005/11/09/rilasciato-postgresql-81/' rel='bookmark' title='Rilasciato PostgreSQL 8.1'>Rilasciato PostgreSQL 8.1</a> <small>PostgreSQL 8.1 contiene piu' di 120 tra nuove caratteristiche e...</small></li>
<li><a href='http://www.sinkro.net/2006/10/22/lightning-admin/' rel='bookmark' title='Lightning Admin'>Lightning Admin</a> <small>Postgres Lightning Admin o PGLA per gli amici, &egrave; un...</small></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Qualche tempo fa, ho avuto necessità di eseguire uno script esterno legato ad un trigger di PostgreSql.<br />
All'epoca, l'unica soluzione che ho trovato è stata di creare una funzione in C che richiamasse lo script con i dovuti parametri.</p>
<p>Questo il codice:</p>
<div class="syntax_hilite">
<div id="c-2">
<div class="c"><span style="color: #339933;">#include</span><br />
<span style="color: #339933;">#include</span><br />
<span style="color: #339933;">#include &quot;postgres.h&quot;</span><br />
<span style="color: #339933;">#include</span><br />
<span style="color: #339933;">#include &quot;fmgr.h&quot;</span><br />
<span style="color: #339933;">#include &quot;utils/builtins.h&quot;</span></p>
<p><span style="color: #339933;">#define _textout(str) DatumGetPointer(DirectFunctionCall1(textout, PointerGetDatum(str)))</span></p>
<p>PG_FUNCTION_INFO_V1<span style="color: #66cc66;">&#40;</span>esegui<span style="color: #66cc66;">&#41;</span>;<br />
Datum&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;esegui<span style="color: #66cc66;">&#40;</span>PG_FUNCTION_ARGS<span style="color: #66cc66;">&#41;</span>;</p>
<p>Datum&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;esegui<span style="color: #66cc66;">&#40;</span>PG_FUNCTION_ARGS<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><br />
<span style="color: #993333;">int</span> pid;</p>
<p><span style="color: #993333;">char</span> *path, *cmd, *host, *user, *pwd, *param; <span style="color: #808080; font-style: italic;">//Parametri della funzione</span></p>
<p>path&nbsp; &nbsp;= _textout<span style="color: #66cc66;">&#40;</span>PG_GETARG_TEXT_P<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
cmd&nbsp; &nbsp; = _textout<span style="color: #66cc66;">&#40;</span>PG_GETARG_TEXT_P<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
host&nbsp; &nbsp;= _textout<span style="color: #66cc66;">&#40;</span>PG_GETARG_TEXT_P<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
user&nbsp; &nbsp;= _textout<span style="color: #66cc66;">&#40;</span>PG_GETARG_TEXT_P<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
pwd&nbsp; &nbsp; = _textout<span style="color: #66cc66;">&#40;</span>PG_GETARG_TEXT_P<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
param&nbsp; = _textout<span style="color: #66cc66;">&#40;</span>PG_GETARG_TEXT_P<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</p>
<p><span style="color: #808080; font-style: italic;">// Eseguo un fork per evitare di rallentare le operazioni sul database</span><br />
<span style="color: #b1b100;">switch</span><span style="color: #66cc66;">&#40;</span>pid=fork<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><br />
<span style="color: #b1b100;">case</span> -<span style="color: #cc66cc;">1</span>: <span style="color: #808080; font-style: italic;">//Errore. Non posso eseguire il fork.</span><br />
exit<span style="color: #66cc66;">&#40;</span>-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">0</span> : <span style="color: #808080; font-style: italic;">//Fork riuscita. Eseguo il codice &quot;figlio&quot;</span><br />
execl<span style="color: #66cc66;">&#40;</span>path, cmd, host, user, pwd, param,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// Questo e' il codice &quot;figlio&quot;</span><br />
exit<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #b1b100;">default</span>:<br />
<span style="color: #b1b100;">return</span> pid;<br />
exit<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>Per utilizzare questa funzione con postgres, sono necessari pochi semplici passi:</p>
<p>Per prima cosa compiliamo la nostra funzione c (nel mio caso il file era: <em>execcmdrouter.c</em>).<br />
<code><br />
gcc -I/usr/include/postgresql/server/ -shared execcmdrouter.c -o execcmdrouter.so<br />
</code><br />
Copiamo l'oggetto ottenuto in "casa" di postgres e attribuiamogliene la proprietà <img src='http://www.sinkro.net/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /><br />
<code><br />
cp execcmdrouter.so /var/lib/postgres/<br />
chown postgres.postgres /var/lib/postgres/execcmdrouter.so<br />
</code></p>
<p>Quindi creiamo la funzione in postgres:</p>
<p><code><br />
CREATE FUNCTION esegui(text,text,text,text,text,text) RETURNS INTEGER<br />
AS '/var/lib/postgres/execcmdrouter.so' LANGUAGE C;<br />
</code></p>
<p>Fatto.</p>
<p>Spero possa a essere utile a qualcuno.</p>


<p>Altri articoli di interesse:<ol><li><a href='http://www.sinkro.net/2005/01/20/rilasciato-postgresql-80/' rel='bookmark' title='Rilasciato PostgreSQL 8.0'>Rilasciato PostgreSQL 8.0</a> <small>NY, NY: 19 Gennaio 2005 - Il Gruppo di Sviluppo...</small></li>
<li><a href='http://www.sinkro.net/2005/11/09/rilasciato-postgresql-81/' rel='bookmark' title='Rilasciato PostgreSQL 8.1'>Rilasciato PostgreSQL 8.1</a> <small>PostgreSQL 8.1 contiene piu' di 120 tra nuove caratteristiche e...</small></li>
<li><a href='http://www.sinkro.net/2006/10/22/lightning-admin/' rel='bookmark' title='Lightning Admin'>Lightning Admin</a> <small>Postgres Lightning Admin o PGLA per gli amici, &egrave; un...</small></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.sinkro.net/2006/08/01/come-creare-una-funzione-in-c-per-postgresql/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

