silvioprog / brookframework

Microframework which helps to develop web Pascal applications.
https://github.com/risoflora/brookframework
GNU Lesser General Public License v3.0
170 stars 37 forks source link

dopf & LastInsertID #157

Closed Al-Muhandis closed 5 years ago

Al-Muhandis commented 6 years ago

How to get LastInsertID in dopf lib?

      opfEntity.Add(AnEntity);
      opfEntity.Apply;
      write('AnEntity ID is '+IntToStr(AnEntity.id)); // there are no last IndexID

Connection driver 'MySQL 5.5'

Al-Muhandis commented 6 years ago

So, I've done so far is an interim solution, work directly with the MySQL function

      opfEntity.Add(AnEntity);
      opfEntity.Apply;
      dSQLdbQuery.SQL.Text:='SELECT LAST_INSERT_ID() AS ID';
      dSQLdbQuery.Open;
     AnEntity.id:=dSQLdbQuery.Field('ID').AsInteger;  
      write('AnEntity ID is '+IntToStr(AnEntity.id)); // Now there are valid last IndexID!!

But I don't think that's the best solution. Surely this should be implemented by means of the dopf itself. If anyone knows prompt, I will alter the code to make it more beautiful

silvioprog commented 6 years ago

Hello @Al-Muhandis . A possible improvement would be in the broker connection, something like GetNextValueSQL of the TSQLConnection, but using a better name like NextID or NewID. I'm busy in the B4 development in my free time, but if you provide a patch it could be checked and applied. :-)

Al-Muhandis commented 6 years ago

Hello @silvioprog ! I think it is better to using function with name like the LastInsertID ). This is quite transparent and understandable for those who know [my]SQL.

The fact is that the LastInsertID (as I see) is in the implementation for MySQL 5.5 (at least) in the tconnectionname and TCursorName classes which are used in dopf. See the mysql55conn unit.

That's why I thought it was possible to get this value using dopf functional instead of the direct method I used in the code example above. My code from the fact that this is direct and is not suitable for all due to different nuances. For example, the field id should be AUTOINC (how does dopf know about this?) and some(?) others. Therefore, in this case, my knowledge is not enough to write universal code that would not cause incomprehensible results in other cases.

This is not a big problem I think for those who know SQL, just thought there is a possibility to isolate the code from MySQL in this case. In any case, thanks for the answer!

silvioprog commented 5 years ago

Closing due to already solved in the application. Please feel free to reissue or send it as new feature! :smiley: