Recent

Author Topic: Looking for an embedded SQL Database  (Read 8247 times)

Чебурашка

  • Hero Member
  • *****
  • Posts: 588
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: Looking for an embedded SQL Database
« Reply #15 on: March 13, 2024, 04:19:08 pm »
I don't see any static loading code of SQLite3 for Windows in the sources.

This is why I said that I was looking forward to see a working example of static linking with great interest.
I had already in the past similar "whishes" but I was not able to obtain it just with a couple of configurations/directives.
« Last Edit: March 13, 2024, 04:20:44 pm by Чебурашка »
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

Zvoni

  • Hero Member
  • *****
  • Posts: 3135
Re: Looking for an embedded SQL Database
« Reply #16 on: March 13, 2024, 04:29:02 pm »
I don't see any static loading code of SQLite3 for Windows in the sources.

This is why I said that I was looking forward to see a working example of static linking with great interest.
I had already in the past similar "whishes" but I was not able to obtain it just with a couple of configurations/directives.

There is also no true static linking for Linux.

Only for Darwin as far as i could see
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Чебурашка

  • Hero Member
  • *****
  • Posts: 588
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: Looking for an embedded SQL Database
« Reply #17 on: March 13, 2024, 04:36:06 pm »
There is also no true static linking for Linux.

This is even more why I said that I was looking forward to see a working example of static linking with great interest.
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

Zvoni

  • Hero Member
  • *****
  • Posts: 3135
Re: Looking for an embedded SQL Database
« Reply #18 on: March 13, 2024, 04:55:57 pm »
There is also no true static linking for Linux.

This is even more why I said that I was looking forward to see a working example of static linking with great interest.
Which is actually pretty simple.
See my answer #10 and #12

What the work is, is creating the „interface“ and the a/o file, since not everyone is comfortable compiling c-code
In case of sqlite, it‘s the sqlite3.inc file and the corresponding a/o file

I might take up the challenge
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Thaddy

  • Hero Member
  • *****
  • Posts: 18305
  • Here stood a man who saw the Elbe and jumped it.
Re: Looking for an embedded SQL Database
« Reply #19 on: March 13, 2024, 04:58:31 pm »
Static linking can be done on Windows and Linux. I am not at home. so be patient.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

DonAlfredo

  • Hero Member
  • *****
  • Posts: 1840
Re: Looking for an embedded SQL Database
« Reply #20 on: March 13, 2024, 05:12:38 pm »
For me, the main selling point of static mORMot is the fact that the database can be encrypted. In use(static)  on Windows, Linux and Android.

rvk

  • Hero Member
  • *****
  • Posts: 6885
Re: Looking for an embedded SQL Database
« Reply #21 on: March 13, 2024, 06:10:15 pm »
What the work is, is creating the „interface“ and the a/o file, since not everyone is comfortable compiling c-code
In case of sqlite, it‘s the sqlite3.inc file and the corresponding a/o file
The sqlite3.inc already largely consist of ifdef for symbol S which is set when not dynamically.
So that could be 'easy'.

But everywhere in FPC itself (for the connector), sqlite3dyn is used in the uses. So that also needs to be changed.

Thaddy

  • Hero Member
  • *****
  • Posts: 18305
  • Here stood a man who saw the Elbe and jumped it.
Re: Looking for an embedded SQL Database
« Reply #22 on: March 13, 2024, 06:43:45 pm »
I know that. But this from a Phone.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Zvoni

  • Hero Member
  • *****
  • Posts: 3135
Re: Looking for an embedded SQL Database
« Reply #23 on: March 14, 2024, 08:02:54 am »
What the work is, is creating the „interface“ and the a/o file, since not everyone is comfortable compiling c-code
In case of sqlite, it‘s the sqlite3.inc file and the corresponding a/o file
The sqlite3.inc already largely consist of ifdef for symbol S which is set when not dynamically.
So that could be 'easy'.

But everywhere in FPC itself (for the connector), sqlite3dyn is used in the uses. So that also needs to be changed.
Which should be pretty simple.
As far as i could find out, sqlite3dyn is only "used" in sqlite3conn (which makes sense) (maybe on those sqliteXXds files. Haven't looked)
Since you would need a compile-time define, it should go along the lines of in the uses-clause
Uses ......, {$IFDEF LOAD_DYNAMICALLY}sqlite3dyn,{$ELSE}sqlite3static,{$ENDIF} ....
with sqlite3static being the "container" defining "S" and then pulling in the sqlite3.inc
or along those lines.....
haven't spent much time thinking about it
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Zvoni

  • Hero Member
  • *****
  • Posts: 3135
Re: Looking for an embedded SQL Database
« Reply #24 on: March 14, 2024, 09:33:24 am »
In a nutshell:
{$LinkLib LIBNAME} --> you need the LIBNAME.a file
{$Link LIBNAME} --> you need the LIBNAME.o file

Thanks for that, useful and concise summary.

MarkMLl

Proof of concept:
Lazarus 2.2.2 32-Bit/FPC3.2.2 32-Bit

File: libzvoni.c
Code: C  [Select][+][-]
  1. #include <stdio.h>
  2.  
  3. int ZvoniAdd(int a, int b, int* c){
  4.         *c=42;
  5.         return a+b;
  6. }
  7.  
I used Geany, and compiled with
gcc -Wall -m32 -c libzvoni.c -o libzvoni.o
(-m32 because i only have Laz/FPC 32-Bit. For 64 leave out the -m32 or specify your own Architecture)

libzvoni.c/libzvoni.o are placed directly besides my lpr/pas files (same folder)

File project1.lpr
Code: Pascal  [Select][+][-]
  1. program project1;
  2. {$mode objfpc}{$H+}
  3. {$Link libzvoni}
  4.  
  5. Function ZvoniAdd(Const a:Integer;Const b:Integer;out Surprise:Integer):Integer;cdecl;external;
  6. Var
  7.   Check:Integer;
  8. begin
  9.   Writeln('Result: ',ZvoniAdd(2,5,Check),'- Surprise: ',Check);
  10. end.
Once your exe is compile, you can remove/rename the o-file for proof, and run it again from console

Note: No Idea about name-mangling.
I did a nm -a on my project1.exe, and found
_ZvoniAdd (Note the underscore)
I admit: definitely not knowledgable about stuff like that
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

PierceNg

  • Sr. Member
  • ****
  • Posts: 412
    • SamadhiWeb
Re: Looking for an embedded SQL Database
« Reply #25 on: March 14, 2024, 01:25:10 pm »
This is even more why I said that I was looking forward to see a working example of static linking with great interest.

mORMot does static linking of SQLite on Linux and Windows.

Windows 10 running 64-bit executable cross-compiled from FPC trunk on WSL2 Ubuntu 22.04 running on said Windows:
Code: Text  [Select][+][-]
  1. Software version tested: 2.2.6584 (2024-03-14 19:25:30)
  2.  
  3. Windows 10 64bit (10.0.19045) [utf8 13.9GB 4A651701]
  4. Using mORMot 2.2.6584 x64MMs
  5.     TSqlite3LibraryStatic 3.44.2 with internal MM
  6. Generated with: Free Pascal 3.3.1 64 bit Win compiler
  7.  
  8. Total assertions failed for all test suits:  24 / 79,223,382

Doh, some failed tests!

Same Windows 10 running 32-bit executable cross-compiled from FPC trunk on same WSL2 Ubuntu:
Code: Text  [Select][+][-]
  1. Software version tested: 2.2.6584 (2024-03-14 19:32:44)
  2.  
  3. Windows 10 64bit (10.0.19045) [utf8 13.9GB 4A651701]
  4. Using mORMot 2.2.6584
  5.     TSqlite3LibraryStatic 3.44.2 with internal MM
  6. Generated with: Free Pascal 3.3.1 32 bit Win compiler
  7.  
  8. Total assertions failed for all test suits:  0 / 79,213,645

Another computer running Ubuntu 22.04:
Code: Text  [Select][+][-]
  1. Software version tested: 2.2.6584 (2024-03-13 16:33:54)
  2.  
  3. Ubuntu 22.04.4 LTS - Linux 5.15.0-100-generic [utf8 31.2GB 50F0010]
  4. Using mORMot 2.2.6584 and OpenSSL 30000020
  5.     TSqlite3LibraryStatic 3.44.2 with internal MM
  6. Generated with: Free Pascal 3.3.1 64 bit Linux compiler
  7.  
  8. Total assertions failed for all test suits:  0 / 79,307,848

Relevant source file is db/mormot.db.raw.sqlite3.static.pas:

Code: Pascal  [Select][+][-]
  1. {$ifdef FPC}  // FPC expects .o linking
  2.  
  3.   {$ifdef OSWINDOWS}
  4.     {$ifdef CPU64}
  5.       {$L ..\..\static\x86_64-win64\sqlite3.o}
  6.     {$else}
  7.       {$L ..\..\static\i386-win32\sqlite3.o}
  8.     {$endif CPU64}
  9.   {$endif OSWINDOWS}
  10.  
  11. // blah blah blah

Чебурашка

  • Hero Member
  • *****
  • Posts: 588
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: Looking for an embedded SQL Database
« Reply #26 on: March 14, 2024, 01:57:43 pm »
Examples posted are nice, but would it be possible to make the same with sqlite, which is what OP seems to need?
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

rvk

  • Hero Member
  • *****
  • Posts: 6885
Re: Looking for an embedded SQL Database
« Reply #27 on: March 14, 2024, 02:07:12 pm »
Examples posted are nice, but would it be possible to make the same with sqlite, which is what OP seems to need?
Thaddy said he was working on an example/solution  :D

But seeing the code of sqldb in FPC, I wonder if it's that easy. You probably don't want to include the static sqlite in lazarus.exe itself, but only for compilation. And because sqldb uses it in TSQLite3Connection it would mean a complete rewrite, I think. If using it in lazarus.exe static too, it might just be a matter of a few lines (just adding the link/linklib).

Probably the easiest solution is to look at mORMot2.

rvk

  • Hero Member
  • *****
  • Posts: 6885
Re: Looking for an embedded SQL Database
« Reply #28 on: March 14, 2024, 02:33:43 pm »
BTW. There are also two other options.
Storing the sqlite3.dll in a resource and either:
1) unpacking the sqlite3.dll to a temp directory and loading it
2) loading it directly from the resource (via BTMemoryModule) (Edit: Woops, no 64 bit for that???)

Where 1 would be the easiest.
No need to change anything in FPC.

But this might still be called "fake" embedding ;)

So... there isn't really a "real" embedded option for any database at all in FPC.


Zvoni

  • Hero Member
  • *****
  • Posts: 3135
Re: Looking for an embedded SQL Database
« Reply #29 on: March 14, 2024, 02:50:28 pm »
*snip* And because sqldb uses it in TSQLite3Connection it would mean a complete rewrite, I think. *snip*
I took a look at sqlite3conn, and i think pretty much everything can be done with conditional compilation (That "InitializeSQLite"/"ReleaseSQLite"-Stuff or whatever it's called).

I found two places in sqlite3conn, where they check
"... If Assigned(sqlite3_somefunction) Then ..."
which would obviously complain if using static ("wrong number of arguments"), since the standard is dynamic it uses variables to store the function-pointer which you can check with Assigned
No idea how to resolve that
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

 

TinyPortal © 2005-2018