<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div><br class=""><blockquote type="cite" class=""><div class=""><div class="">After this patch client will get last_insert_id<br class="">as additional metadata when he executes some<br class="">statements.<br class=""></div></div></blockquote><div><br class=""></div><div>Some statements? Lets write ‘when SQL statements are executed’ or sort of.</div></div><div><br class=""><blockquote type="cite" class=""><div class=""><div class=""><br class="">Part of #2618<br class=""><br class="">@TarantoolBot document<br class="">Title: SQL function last_insert_id() and IPROTO key last_insert_id.<br class="">Function last_insert_id() returns first primary key value<br class="">autogenerated in last INSERT/REPLACE statement in current<br class="">session. </div></div></blockquote><div><br class=""></div><div>Mention that it is set only for AUTOINCREMENT primary key.</div><div>In other cases it will be 0.</div><div>Moreover, if you copy this feature from MySQL, you can take</div><div>part of its description: it is quite good documented there:</div><div><a href="https://dev.mysql.com/doc/refman/8.0/en/mysql-insert-id.html" class="">https://dev.mysql.com/doc/refman/8.0/en/mysql-insert-id.html</a></div><div><br class=""></div><div>The only doubt I have concerning this patch - last_insert_id()</div><div>returns FIRST autogenerated id for last stmt, which in turn</div><div>seems quite misleading (I expected that it returns LAST id of LAST stmt).</div><div>I known that MySQL uses exactly this variant, but should we follow this way?</div><br class=""><blockquote type="cite" class=""><div class=""><div class="">User can have more than one session and this<br class="">function will work properly for each one of them. Return<br class="">value of function is undetermined when more than one<br class="">INSERT/REPLACE statements executed asynchronously.<br class="">IPROTO key last_insert_id is a metadata returned through<br class="">IPROTO after such statements as INSERT, REPLACE, UPDATE<br class="">etc. Value of this key is equal to value returned by<br class="">function last_insert_id() executed after the statement.<br class="">Example:<br class="">CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, a INTEGER);<br class="">INSERT INTO test VALUES (NULL, 1);<br class="">SELECT last_insert_id();<br class=""><br class="">diff --git a/src/box/<a href="http://session.cc" class="">session.cc</a> b/src/box/<a href="http://session.cc" class="">session.cc</a><br class="">index 64714cd..2b8dab9 100644<br class="">--- a/src/box/<a href="http://session.cc" class="">session.cc</a><br class="">+++ b/src/box/<a href="http://session.cc" class="">session.cc</a><br class="">@@ -108,6 +108,7 @@ session_create(enum session_type type)<br class=""> <span class="Apple-tab-span" style="white-space:pre">    </span>session->type = type;<br class=""> <span class="Apple-tab-span" style="white-space:pre">      </span>session->sql_flags = default_flags;<br class=""> <span class="Apple-tab-span" style="white-space:pre">        </span>session->sql_default_engine = SQL_STORAGE_ENGINE_MEMTX;<br class="">+<span class="Apple-tab-span" style="white-space:pre">    </span>session->sql_last_insert_id = 0;<br class=""><br class=""> <span class="Apple-tab-span" style="white-space:pre">      </span>/* For on_connect triggers. */<br class=""> <span class="Apple-tab-span" style="white-space:pre">        </span>credentials_init(&session->credentials, guest_user->auth_token,<br class="">diff --git a/src/box/session.h b/src/box/session.h<br class="">index df1dcbc..6ee22bc 100644<br class="">--- a/src/box/session.h<br class="">+++ b/src/box/session.h<br class="">@@ -92,6 +92,11 @@ union session_meta {<br class=""> struct session {<br class=""> <span class="Apple-tab-span" style="white-space:pre">  </span>/** Session id. */<br class=""> <span class="Apple-tab-span" style="white-space:pre">    </span>uint64_t id;<br class="">+<span class="Apple-tab-span" style="white-space:pre">  </span>/**<br class="">+<span class="Apple-tab-span" style="white-space:pre">   </span> * First primary key autogenerated in last INSERT/REPLACE<br class="">+<span class="Apple-tab-span" style="white-space:pre">     </span> * statement in which primary key was generated.<br class="">+<span class="Apple-tab-span" style="white-space:pre">      </span> */<br class=""></div></div></blockquote><div><br class=""></div><div>If AUTOINCEMENT is specified, otherwise it is 0.</div><br class=""><blockquote type="cite" class=""><div class=""><div class="">+<span class="Apple-tab-span" style="white-space:pre"> </span>int64_t sql_last_insert_id;<br class=""> <span class="Apple-tab-span" style="white-space:pre">   </span>/** SQL Tarantool Default storage engine. */<br class=""> <span class="Apple-tab-span" style="white-space:pre">  </span>uint8_t sql_default_engine;<br class=""> <span class="Apple-tab-span" style="white-space:pre">   </span>/** SQL Connection flag for current user session */<br class="">diff --git a/src/box/sql/func.c b/src/box/sql/func.c<br class="">index 45056a7..2607cc3 100644<br class="">--- a/src/box/sql/func.c<br class="">+++ b/src/box/sql/func.c<br class="">@@ -38,6 +38,7 @@<br class=""> #include "vdbeInt.h"<br class=""> #include "version.h"<br class=""> #include "coll.h"<br class="">+#include "box/session.h"<br class=""> #include <unicode/ustring.h><br class=""> #include <unicode/ucasemap.h><br class=""> #include <unicode/ucnv.h><br class="">@@ -601,6 +602,23 @@ changes(sqlite3_context * context, int NotUsed, sqlite3_value ** NotUsed2)<br class=""> }<br class=""><br class=""> /*<br class="">+ * Return first primary key autogenerated in last INSERT/REPLACE<br class="">+ * statement in which primary key was generated in current<br class="">+ * session.<br class="">+ *<br class="">+ * @param context Context being used.<br class="">+ * @param not_used Unused.<br class="">+ * @param not_used2 Unused.<br class="">+ */<br class="">+static void<br class="">+last_insert_id(sqlite3_context *context, int not_used,<br class="">+<span class="Apple-tab-span" style="white-space:pre">        </span>       sqlite3_value **not_used2)<br class=""></div></div></blockquote><div><br class=""></div><div>Nitpick: use ’struct’ prefixes.</div></div><br class=""></body></html>