<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>ndbapi_event.cpp</title> <link href="doxygen.css" rel="stylesheet" type="text/css"> <link href="tabs.css" rel="stylesheet" type="text/css"> </head><body> <!-- Generated by Doxygen 1.5.5 --> <div class="contents"> <h1><a class="anchor" name="ndbapi_event.cpp">ndbapi_event.cpp </a></h1><div class="fragment"><pre class="fragment"><span class="comment">/* Copyright (C) 2003 MySQL AB</span> <span class="comment"></span> <span class="comment"> This program is free software; you can redistribute it and/or modify</span> <span class="comment"> it under the terms of the GNU General Public License as published by</span> <span class="comment"> the Free Software Foundation; version 2 of the License.</span> <span class="comment"></span> <span class="comment"> This program is distributed in the hope that it will be useful,</span> <span class="comment"> but WITHOUT ANY WARRANTY; without even the implied warranty of</span> <span class="comment"> MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</span> <span class="comment"> GNU General Public License for more details.</span> <span class="comment"></span> <span class="comment"> You should have received a copy of the GNU General Public License</span> <span class="comment"> along with this program; if not, write to the Free Software</span> <span class="comment"> Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */</span> <span class="preprocessor">#include <NdbApi.hpp></span> <span class="comment">// Used for cout</span> <span class="preprocessor">#include <stdio.h></span> <span class="preprocessor">#include <iostream></span> <span class="preprocessor">#include <unistd.h></span> <span class="preprocessor">#define APIERROR(error) \</span> <span class="preprocessor"> { std::cout << "Error in " << __FILE__ << ", line:" << __LINE__ << ", code:" \</span> <span class="preprocessor"> << error.code << ", msg: " << error.message << "." << std::endl; \</span> <span class="preprocessor"> exit(-1); }</span> <span class="preprocessor"></span> <span class="keywordtype">int</span> myCreateEvent(<a class="code" href="classNdb.html" title="Represents the NDB kernel and is the main class of the NDB API.">Ndb</a>* myNdb, <span class="keyword">const</span> <span class="keywordtype">char</span> *eventName, <span class="keyword">const</span> <span class="keywordtype">char</span> *eventTableName, <span class="keyword">const</span> <span class="keywordtype">char</span> **eventColumnName, <span class="keyword">const</span> <span class="keywordtype">int</span> noEventColumnName); <span class="keywordtype">int</span> main() { ndb_init(); <a class="code" href="classNdb__cluster__connection.html" title="Represents a connection to a cluster of storage nodes.">Ndb_cluster_connection</a> *cluster_connection= <span class="keyword">new</span> <a class="code" href="classNdb__cluster__connection.html" title="Represents a connection to a cluster of storage nodes.">Ndb_cluster_connection</a>(); <span class="comment">// Object representing the cluster</span> <span class="keywordtype">int</span> r= cluster_connection-><a class="code" href="classNdb__cluster__connection.html#1c1523c47bc56f17beb5c7e9124f7f47">connect</a>(5 <span class="comment">/* retries */</span>, 3 <span class="comment">/* delay between retries */</span>, 1 <span class="comment">/* verbose */</span>); <span class="keywordflow">if</span> (r > 0) { std::cout << <span class="stringliteral">"Cluster connect failed, possibly resolved with more retries.\n"</span>; exit(-1); } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (r < 0) { std::cout << <span class="stringliteral">"Cluster connect failed.\n"</span>; exit(-1); } <span class="keywordflow">if</span> (cluster_connection-><a class="code" href="classNdb__cluster__connection.html#02f01d4e1dba63679a4796838e3c3c7a">wait_until_ready</a>(30,30)) { std::cout << <span class="stringliteral">"Cluster was not ready within 30 secs."</span> << std::endl; exit(-1); } <a class="code" href="classNdb.html" title="Represents the NDB kernel and is the main class of the NDB API.">Ndb</a>* myNdb= <span class="keyword">new</span> <a class="code" href="classNdb.html" title="Represents the NDB kernel and is the main class of the NDB API.">Ndb</a>(cluster_connection, <span class="stringliteral">"TEST_DB"</span>); <span class="comment">// Object representing the database</span> <span class="keywordflow">if</span> (myNdb-><a class="code" href="classNdb.html#b938638f0ced10e31ab67b3a80901270">init</a>() == -1) APIERROR(myNdb-><a class="code" href="classNdb.html#80b53a037bb2e93b3cd6830eda58643a">getNdbError</a>()); <span class="keyword">const</span> <span class="keywordtype">char</span> *eventName= <span class="stringliteral">"CHNG_IN_TAB0"</span>; <span class="keyword">const</span> <span class="keywordtype">char</span> *eventTableName= <span class="stringliteral">"TAB0"</span>; <span class="keyword">const</span> <span class="keywordtype">int</span> noEventColumnName= 3; <span class="keyword">const</span> <span class="keywordtype">char</span> *eventColumnName[noEventColumnName]= {<span class="stringliteral">"COL0"</span>, <span class="stringliteral">"COL1"</span>, <span class="stringliteral">"COL11"</span>}; <span class="comment">// Create events</span> myCreateEvent(myNdb, eventName, eventTableName, eventColumnName, noEventColumnName); <span class="keywordtype">int</span> j= 0; <span class="keywordflow">while</span> (j < 5) { <span class="comment">// Start "transaction" for handling events</span> NdbEventOperation* op; printf(<span class="stringliteral">"create EventOperation\n"</span>); <span class="keywordflow">if</span> ((op = myNdb->createEventOperation(eventName,100)) == NULL) APIERROR(myNdb-><a class="code" href="classNdb.html#80b53a037bb2e93b3cd6830eda58643a">getNdbError</a>()); printf(<span class="stringliteral">"get values\n"</span>); <a class="code" href="classNdbRecAttr.html" title="Contains value of an attribute.">NdbRecAttr</a>* recAttr[noEventColumnName]; <a class="code" href="classNdbRecAttr.html" title="Contains value of an attribute.">NdbRecAttr</a>* recAttrPre[noEventColumnName]; <span class="comment">// primary keys should always be a part of the result</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < noEventColumnName; i++) { recAttr[i] = op->getValue(eventColumnName[i]); recAttrPre[i] = op->getPreValue(eventColumnName[i]); } <span class="comment">// set up the callbacks</span> printf(<span class="stringliteral">"execute\n"</span>); <span class="comment">// This starts changes to "start flowing"</span> <span class="keywordflow">if</span> (op->execute()) APIERROR(op->getNdbError()); <span class="keywordtype">int</span> i= 0; <span class="keywordflow">while</span>(i < 40) { <span class="comment">// printf("now waiting for event...\n");</span> <span class="keywordtype">int</span> r= myNdb->pollEvents(1000); <span class="comment">// wait for event or 1000 ms</span> <span class="keywordflow">if</span> (r > 0) { <span class="comment">// printf("got data! %d\n", r);</span> <span class="keywordtype">int</span> overrun; <span class="keywordflow">while</span> (op->next(&overrun) > 0) { i++; <span class="keywordflow">if</span> (!op->isConsistent()) printf(<span class="stringliteral">"A node failure has occured and events might be missing\n"</span>); <span class="keywordflow">switch</span> (op->getEventType()) { <span class="keywordflow">case</span> NdbDictionary::Event::TE_INSERT: printf(<span class="stringliteral">"%u INSERT: "</span>, i); <span class="keywordflow">break</span>; <span class="keywordflow">case</span> NdbDictionary::Event::TE_DELETE: printf(<span class="stringliteral">"%u DELETE: "</span>, i); <span class="keywordflow">break</span>; <span class="keywordflow">case</span> NdbDictionary::Event::TE_UPDATE: printf(<span class="stringliteral">"%u UPDATE: "</span>, i); <span class="keywordflow">break</span>; <span class="keywordflow">default</span>: abort(); <span class="comment">// should not happen</span> } printf(<span class="stringliteral">"overrun %u pk %u: "</span>, overrun, recAttr[0]->u_32_value()); <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 1; i < noEventColumnName; i++) { <span class="keywordflow">if</span> (recAttr[i]->isNULL() >= 0) { <span class="comment">// we have a value</span> printf(<span class="stringliteral">" post[%u]="</span>, i); <span class="keywordflow">if</span> (recAttr[i]->isNULL() == 0) <span class="comment">// we have a non-null value</span> printf(<span class="stringliteral">"%u"</span>, recAttr[i]->u_32_value()); <span class="keywordflow">else</span> <span class="comment">// we have a null value</span> printf(<span class="stringliteral">"NULL"</span>); } <span class="keywordflow">if</span> (recAttrPre[i]->isNULL() >= 0) { <span class="comment">// we have a value</span> printf(<span class="stringliteral">" pre[%u]="</span>, i); <span class="keywordflow">if</span> (recAttrPre[i]->isNULL() == 0) <span class="comment">// we have a non-null value</span> printf(<span class="stringliteral">"%u"</span>, recAttrPre[i]->u_32_value()); <span class="keywordflow">else</span> <span class="comment">// we have a null value</span> printf(<span class="stringliteral">"NULL"</span>); } } printf(<span class="stringliteral">"\n"</span>); } } else ;<span class="comment">//printf("timed out\n");</span> } <span class="comment">// don't want to listen to events anymore</span> <span class="keywordflow">if</span> (myNdb->dropEventOperation(op)) APIERROR(myNdb-><a class="code" href="classNdb.html#80b53a037bb2e93b3cd6830eda58643a">getNdbError</a>()); j++; } { <a class="code" href="classNdbDictionary_1_1Dictionary.html" title="Dictionary for defining and retreiving meta data.">NdbDictionary::Dictionary</a> *myDict = myNdb-><a class="code" href="classNdb.html#f27ca0f4e5c074211252a7c83060a3a4">getDictionary</a>(); <span class="keywordflow">if</span> (!myDict) APIERROR(myNdb-><a class="code" href="classNdb.html#80b53a037bb2e93b3cd6830eda58643a">getNdbError</a>()); <span class="comment">// remove event from database</span> <span class="keywordflow">if</span> (myDict->dropEvent(eventName)) APIERROR(myDict-><a class="code" href="classNdbDictionary_1_1Dictionary.html#0af68ea9b273172b46528ee1bd806f71">getNdbError</a>()); } <span class="keyword">delete</span> myNdb; <span class="keyword">delete</span> cluster_connection; ndb_end(0); <span class="keywordflow">return</span> 0; } <span class="keywordtype">int</span> myCreateEvent(<a class="code" href="classNdb.html" title="Represents the NDB kernel and is the main class of the NDB API.">Ndb</a>* myNdb, <span class="keyword">const</span> <span class="keywordtype">char</span> *eventName, <span class="keyword">const</span> <span class="keywordtype">char</span> *eventTableName, <span class="keyword">const</span> <span class="keywordtype">char</span> **eventColumnNames, <span class="keyword">const</span> <span class="keywordtype">int</span> noEventColumnNames) { <a class="code" href="classNdbDictionary_1_1Dictionary.html" title="Dictionary for defining and retreiving meta data.">NdbDictionary::Dictionary</a> *myDict= myNdb-><a class="code" href="classNdb.html#f27ca0f4e5c074211252a7c83060a3a4">getDictionary</a>(); <span class="keywordflow">if</span> (!myDict) APIERROR(myNdb-><a class="code" href="classNdb.html#80b53a037bb2e93b3cd6830eda58643a">getNdbError</a>()); <span class="keyword">const</span> <a class="code" href="classNdbDictionary_1_1Table.html" title="Represents a table in NDB Cluster.">NdbDictionary::Table</a> *table= myDict-><a class="code" href="classNdbDictionary_1_1Dictionary.html#1a30b1bb5fa164113cb839eed64eb3b6">getTable</a>(eventTableName); <span class="keywordflow">if</span> (!table) APIERROR(myDict-><a class="code" href="classNdbDictionary_1_1Dictionary.html#0af68ea9b273172b46528ee1bd806f71">getNdbError</a>()); NdbDictionary::Event myEvent(eventName, *table); myEvent.addTableEvent(NdbDictionary::Event::TE_ALL); <span class="comment">// myEvent.addTableEvent(NdbDictionary::Event::TE_INSERT); </span> <span class="comment">// myEvent.addTableEvent(NdbDictionary::Event::TE_UPDATE); </span> <span class="comment">// myEvent.addTableEvent(NdbDictionary::Event::TE_DELETE);</span> myEvent.addEventColumns(noEventColumnNames, eventColumnNames); <span class="comment">// Add event to database</span> <span class="keywordflow">if</span> (myDict->createEvent(myEvent) == 0) myEvent.print(); <span class="keywordflow">else</span> <span class="keywordflow">if</span> (myDict-><a class="code" href="classNdbDictionary_1_1Dictionary.html#0af68ea9b273172b46528ee1bd806f71">getNdbError</a>().<a class="code" href="structNdbError.html#bc9a4841feb45bb8ca5fdaa8e14d20d6">classification</a> == <a class="code" href="structNdbError.html#a1ca0a1dc5881fce46374c62cee0bdf10e8cdd8b9f060e7fdb00456837f690ea">NdbError::SchemaObjectExists</a>) { printf(<span class="stringliteral">"Event creation failed, event exists\n"</span>); printf(<span class="stringliteral">"dropping Event...\n"</span>); <span class="keywordflow">if</span> (myDict->dropEvent(eventName)) APIERROR(myDict-><a class="code" href="classNdbDictionary_1_1Dictionary.html#0af68ea9b273172b46528ee1bd806f71">getNdbError</a>()); <span class="comment">// try again</span> <span class="comment">// Add event to database</span> <span class="keywordflow">if</span> ( myDict->createEvent(myEvent)) APIERROR(myDict-><a class="code" href="classNdbDictionary_1_1Dictionary.html#0af68ea9b273172b46528ee1bd806f71">getNdbError</a>()); } <span class="keywordflow">else</span> APIERROR(myDict-><a class="code" href="classNdbDictionary_1_1Dictionary.html#0af68ea9b273172b46528ee1bd806f71">getNdbError</a>()); <span class="keywordflow">return</span> 0; } </pre></div> </div> <hr> <address> <small> <center> Documentation generated Sun Apr 20 06:25:31 2008 from mysql source files.<br> © 2003-2004 <a href="http://www.mysql.com">MySQL AB</a> <br> </center> </small></address> </body> </html>