OK, with the one-time <span style="font-family:courier new,monospace"><span style="background-color:rgb(192,192,192)">CREATE UNIQUE INDEX on 'table' (</span></span><span style="background-color:rgb(153,153,153)"><span style="background-color:rgb(192,192,192)"><span style="font-family:courier new,monospace"><span style="background-color:rgb(192,192,192)">hostname,realm,data_key);</span><span style="background-color:rgb(255,255,255)"> </span></span></span></span><span style="background-color:rgb(255,255,255)">s</span>tmt in place at the schema-level, a portable work-a-like of INSERT OR REPLACE stmt is the UPDATE-INSERT (i.e. "UPSERT" lol) stmt pair that follows,<br>
<br><span style="background-color:rgb(153,153,153)"><span style="background-color:rgb(192,192,192)"><span style="font-family:courier new,monospace">BEGIN TRANSACTION; <br>
UPDATE 'table' SET data_value='value_data' <span style="background-color:rgb(255,255,102)">WHERE hostname='value_hostname' AND realm='value_realm' AND data_key='value_data_key'</span> ;<br>
INSERT INTO 'table' (hostname,realm,data_key,data_value) SELECT 'value_hostname', 'value_realm', 'value_data_key', 'value_data_value'<br> WHERE NOT EXISTS (SELECT 1 FROM 'table' <span style="background-color:rgb(255,255,0)">WHERE hostname='value_hostname' AND realm='value_realm' AND data_key='value_data_key'</span> );<br>
COMMIT; </span></span><br></span><br>The two WHERE clauses must be identical and the variables should be in the same order as the unique compound index is ordered.<br>
The UPDATE fails quietly and benignly if the record does NOT already exist and succeeds if it does.<br>The INSERT ... WHERE NOT EXISTS stmt succeeds if the record does NOT already exist fails quietly and benignly if it does. <br>
The BEGIN and COMMIT aren't typically necessary but they're over-safe just in case the same record is attempted by another thread.<br><br>I think this get's it done.<br><br>:-)<br><br><br><div class="gmail_quote">
On 18 November 2012 06:25, S. Scott <span dir="ltr"><<a href="mailto:8f27e956@gmail.com" target="_blank">8f27e956@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF"><div><div>The IF NOT EXITS term is easily omitted. It's a helper term for batch scripted SQL to prevent an error being thrown in the case of the index already exists as in the case where the make schema script is run more than once. Easily omitted and, perhaps more properly, replaced with a DROP INDEX stmt and CREATE UNIQUE INDEX stmt pair.</div>
<div><br></div><div>Yeah, i checked further, the OR REPLACE modifier is a little more Hit/miss. The capability is there typically present, but the syntax varies -- e.g sqlite3 ON REPLACE is mySQL's ON DUPLICATE KEY UPDATE.</div>
<div><br></div><div>Darn shame sqlite3 doesn't support stored procedures 'cause then it's more easily abstracted for portability up the SQL server engine curve.</div><div><div class="h5"><div><br>On 2012-11-18, at 3:53, Gerald Weber <<a href="mailto:gerald.weber@besharp.at" target="_blank">gerald.weber@besharp.at</a>> wrote:<br>
<br></div><div></div><blockquote type="cite"><div><div><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d" lang="EN-US">AFAIK, „if not exists“ and „or replace“ is not ansi sql syntax, so you will get in trouble if </span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d" lang="EN-US">someone uses odbc to a db that doesn’t support non standard syntax.</span></p><p class="MsoNormal">
<span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d" lang="EN-US"> </span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d" lang="EN-US">The insert or replace could be replaced with the merge statement.</span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d" lang="EN-US"> </span></p><p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"" lang="DE">Von:</span></b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"" lang="DE"> <a href="mailto:freeswitch-users-bounces@lists.freeswitch.org" target="_blank">freeswitch-users-bounces@lists.freeswitch.org</a> [mailto:<a href="mailto:freeswitch-users-bounces@lists.freeswitch.org" target="_blank">freeswitch-users-bounces@lists.freeswitch.org</a>] <b>Im Auftrag von </b>Scott<br>
<b>Gesendet:</b> Sonntag, 18. November 2012 02:42<br><b>An:</b> FreeSWITCH Users Help<br><b>Betreff:</b> [Freeswitch-users] Suggestion to harmonize 'hash' & 'db' insert method</span></p><p class="MsoNormal">
</p><p class="MsoNormal" style="margin-bottom:12.0pt">Are there reasons why the function 'hash' (non-persistent storage) and function 'db' (persistent storage) share a look-a-like user interface (e.g. select/insert/delete) but do not work-a-like. In particular, in 'hash', insert overwrites an identical realm/data_key pair whereas 'db' , in so far as I can tell, just blindly adds, and adds, adds, the insert(s). However, the 'db' select method returns one record only even if the db has accumulated many realm/data_key records, including data_value duplicates. <br>
<br>NOTWITHSTANDING reasons-unknow-to-me, a remedy to harmonize the non-persistent 'hash' with the persistent 'db' I *think* is straight forward ...<br><br>(1) At the call_limit.db schema-level, a one-time create UNIQUE COMPOUND index, as follows,<br>
<br>CREATE UNIQUE INDEX IF NOT EXISTS 'idx_db_data_HostRealmDK' ON 'db_data' ('hostname','realm','data_key');<br><br>(1) At the c-language embedded SQL string-level,<br><br>change the existing INSERT to be the following,<br>
<br>INSERT OR REPLACE INTO db_data (hostname,realm,data_key,data) VALUES (%s,%s,%s,%s);<br><br>The index will help speed UP the reads. Neither change is "exotic" and should be portable across standard sql implementations.<br>
<br>;-) ... thoughts ?<br><br>Thanks,<br><br><br></p></div></div></blockquote></div></div><blockquote type="cite"><div><span>_________________________________________________________________________</span><br><span>Professional FreeSWITCH Consulting Services:</span><br>
<span><a href="mailto:consulting@freeswitch.org" target="_blank">consulting@freeswitch.org</a></span><br><span><a href="http://www.freeswitchsolutions.com" target="_blank">http://www.freeswitchsolutions.com</a></span><br>
<span></span><br><span>FreeSWITCH-powered IP PBX: The CudaTel Communication Server</span><br>
<span><a href="http://www.cudatel.com" target="_blank">http://www.cudatel.com</a></span><br><span></span><br><span>Official FreeSWITCH Sites</span><br><span><a href="http://www.freeswitch.org" target="_blank">http://www.freeswitch.org</a></span><br>
<span><a href="http://wiki.freeswitch.org" target="_blank">http://wiki.freeswitch.org</a></span><br>
<span><a href="http://www.cluecon.com" target="_blank">http://www.cluecon.com</a></span><br><span></span><br><span>FreeSWITCH-users mailing list</span><br><span><a href="mailto:FreeSWITCH-users@lists.freeswitch.org" target="_blank">FreeSWITCH-users@lists.freeswitch.org</a></span><br>
<span><a href="http://lists.freeswitch.org/mailman/listinfo/freeswitch-users" target="_blank">http://lists.freeswitch.org/mailman/listinfo/freeswitch-users</a></span><br><span>UNSUBSCRIBE:<a href="http://lists.freeswitch.org/mailman/options/freeswitch-users" target="_blank">http://lists.freeswitch.org/mailman/options/freeswitch-users</a></span><br>
<span><a href="http://www.freeswitch.org" target="_blank">http://www.freeswitch.org</a></span><br></div></blockquote></div><div><span></span></div></div>
</blockquote></div><br>