<?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>Andrew Morgan's MySQL Cluster Database Blog &#187; time</title>
	<atom:link href="http://www.clusterdb.com/tag/time/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.clusterdb.com</link>
	<description>MySQL Cluster database &#38; MySQL Replication</description>
	<lastBuildDate>Wed, 01 Feb 2012 18:35:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Creating a MySQL plugin to produce an integer timestamp</title>
		<link>http://www.clusterdb.com/mysql/creating-a-mysql-plugin-to-produce-an-interger-timestamp/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=creating-a-mysql-plugin-to-produce-an-interger-timestamp</link>
		<comments>http://www.clusterdb.com/mysql/creating-a-mysql-plugin-to-produce-an-interger-timestamp/#comments</comments>
		<pubDate>Wed, 12 Aug 2009 12:54:30 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[MySQL Cluster]]></category>
		<category><![CDATA[shared library]]></category>
		<category><![CDATA[Stored Procedures]]></category>
		<category><![CDATA[time]]></category>
		<category><![CDATA[timestamp]]></category>

		<guid isPermaLink="false">http://www.clusterdb.com/?p=451</guid>
		<description><![CDATA[This article shows how to create a MySQL-plugin that can be used to create a function which can in turn be used in stored procedures. The function will produce an integer value representing the time (to the nearest usec). I&#8217;m working on an article for conflict detection/resolution when using MySQL Cluster asynchronous replication which requires [...]]]></description>
			<content:encoded><![CDATA[<p>This article shows how to create a MySQL-plugin that can be used to create a function which can in turn be used in stored procedures. The function will produce an integer value representing the time (to the nearest usec).</p>
<p>I&#8217;m working on an article for conflict detection/resolution when using MySQL Cluster asynchronous replication which requires an integer column to store a timestamp for comparison purposes. In fact, it doesn&#8217;t actually need the timestamp to represent an absolute or even a relative point in time &#8211; all it cares about is that the if the function is called twice on 2 different hosts that the 2nd call will always result in a larger number than the 1st. Obviously, in a production environment the times on the 2 hosts would need to be kept in sync.</p>
<h3>The c code (inttime.c)</h3>
<pre style="padding-left: 30px;color: #993300;font-size: 11px">#include &lt;mysql.h&gt;
#include &lt;sys/time.h&gt;

my_bool inttime_init(UDF_INIT *initid,UDF_ARGS *args, char *message) {
  return 0;
}

void inttime_deinit(UDF_INIT *initid) {};

unsigned long int inttime(UDF_INIT *initid, UDF_ARGS *args, char *result,
                          unsigned long  *length, char *is_null, char *error)
{
  struct timeval tv;
  gettimeofday(&amp;tv,(void *)0);
  return ((double)tv.tv_usec)+tv.tv_sec*1000000;
}</pre>
<h3>Compiling and deploying the shared library</h3>
<pre style="padding-left: 30px;color: #993300;font-size: 11px">[billy@ws1 timestamp]$ gcc -I/usr/local/mysql/include/ -fPIC -shared -o inttime.so inttime.c
[billy@ws1 timestamp]$ cp inttime.so /usr/local/mysql/lib/plugin/</pre>
<h3>Creating the function</h3>
<pre style="padding-left: 30px;color: #993300;font-size: 11px">mysql&gt; create function inttime RETURNS REAL SONAME 'inttime.so';</pre>
<h3>Test the function</h3>
<pre style="padding-left: 30px;color: #993300;font-size: 11px">mysql&gt; select inttime();select inttime();
+------------------+
| inttime()        |
+------------------+
| 1250080524270706 |
+------------------+
1 row in set (0.00 sec)

+------------------+
| inttime()        |
+------------------+
| 1250080524270833 |
+------------------+
1 row in set (0.00 sec)</pre>
<p>Note that the results are different and the second result is always larger than the first &#8211; function is fit for purpose <img src='http://www.clusterdb.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h3>Using the function from a stored procedure</h3>
<pre style="padding-left: 30px;color: #993300;font-size: 11px">mysql&gt; CREATE TRIGGER updateTAB1 BEFORE UPDATE ON TAB1 FOR EACH ROW SET NEW.ts = inttime();</pre>
<h3>Acknowledgements</h3>
<p>I used the c code found at http://lists.mysql.com/internals/33702 as the starting point.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.clusterdb.com/mysql/creating-a-mysql-plugin-to-produce-an-interger-timestamp/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

