Setting up database as complete beginner

Hi there!

So first of all im really new to Ada and im a beginner at coding overall but so far i really like the language.

For the past 31 days ive been doing almost every lab from learn.adacore, and now I wanted to try to make a bigger project to apply everything ive learned and for the past 3 days ive been trying to setup a database by trying both ADO and Gnatcoll_db(im not sure if any of these is the modern choice, if there is more modern stuff, please tell me), but so far without any success. When reading the docs for lets say Gnatcoll it more or less believes the reader has all needed packages installed without saying what packages you need so when i install gnatcoll_db it only complains about alot of packages not existing in my pc and i dont know how get them. So does someone here know what the best solution is for a database using ADA as main language? Or can put me in the right direction. I do use Alire on WSL and code inside of it using VS code if that helps, i want to use Sqlite as database but if someone has anything easier or better in mind dont hesitate to tell me.

Thank you in advance.

// complete beginner

2 Likes

I cannot tell about gnatcoll. For serious stuff I am using ODBC. A database setup depends on the database. You need unrelated to Ada:

  1. Set up the RDBMS server on some host, if you do not have it. See the DB vendor documentation.
  2. Set up the DB client. See the DB vendor documentation.
  3. Install the ODBC on the client side. Under Windows is there by default. Under Linux it is the unixodbc package you have to install using apt, dnf or whatever packaging system you have.
  4. Install the DB ODBC driver on the client. See the DB vendor documentation.
  5. Create the database on the server. See the DB vendor documentation.
  6. Configure the database source on the client, the so-called DSN. Under Windows there is a settings dialog, under Linux there is a config file. The data source parameters are vendor specific, except for user and password you would need to connect to the database.

That is all. From the Ada side you do not need any additional packages if you use ODBC. This is the main advantage of ODBC, it abstracts away any vendor specific.

Now, the toy things. If you want SQLite, which is not really a RDBMS. You still can use ODBC because SQLite has an ODBC driver. However, it would be silly to do so for it. SQLite is not meant for serious database application. It is great if you want to store some configuration settings or a few data in a single file. So ODBC is an overkill here. You can simply use direct SQLite bindings and SQLite attached to your Ada project. In that case you do not need any packages at all, because the bindings use SQLite amalgamation that is a single C file. No libraries needed.

1 Like

Thank you for answer, i appritiate it. Is there any ORM like features in ada i can use with that odbc? Or do I need to write the sql statements myself in ada? (It feels safer with ORM for some reason).

And how do I connect my ada program to odbc?

declare
   Environment : aliased ODBC_Environment;
   Connection  : aliased ODBC_Connection (Environment'Access);
begin
   Connection.Connect
   (   Server_Name  => "Greenwood",  -- The datasource name
       User_Name    => "Winnie_The_Pooh",
       Password     => "Honey",
       Auto_Commit  => True
   );

You cannot do it in a typed language, because the types of parameters and results are statically unknown unless you generate code from some database description. Just write wrappers for frequently used queries, use tagged types for more advanced OO API.

1 Like

Thank you for Helping me, when installing odbc, do I do that via alire or something completly different or is it already inside of ada?

1 Like

No, the ODBC bindings are not under Alire, AFAIK Alire cannot handle 32/64 choices ODBC requires. ODBC 32-bit sufficiently differs from ODBC 64-bit.

You can just download the sources here or install a precompiled package if you have a Linux system.

In your project file include:

with "components.gpr";
with "components-odbc.gpr";

Choose the project scenario variables for your system. They are described here. I have no idea how Visual Studio works with Ada projects. (I use the GNAT Studio which is a far better IDE, IMO)

Anyway, you can build from the bash command line using the gprbuild like this:

gprbuild -XDevelopment=Release\
          -XAtomic_Access=Pragma-atomic\
          -Xarch=x86_64\
          -Xodbc=unixODBC\
          -XTarget_OS=Linux\
          -p -P <your-project>.gpr

Linux, 64-bit Intel.

If you target Windows then:

gprbuild -XDevelopment=Release^
          -XAtomic_Access=Pragma-atomic^
          -Xarch=x86_64^
          -p -P max_home_automation.gpr

P.S. I use virtual machines instead of WSL because I want to be able to back it up and run many distributions of Linux (Debian, Fedora, Ubuntu, CentOS).

1 Like

Thank u i will check it out. Does gnat studio cost something or is it a free IDE?

GNAT Studio is free.

Ive fixed both gnatstudio and odbc at the moment, now to another problem.

I wanted to make a Ada AWS project, is there any sort of templates for AWS Like in other languages or do I need to code everything from start?

When starting a new AWS project with :

Alr init

Cd project_folder

Alr with AWS

Gnatstudio

It cant find any packages for AWS even thou the “alr with aws” says everything was successfull and added the packages/dependencies.

Is there a fix for this or am i doing something wrong?

Thanks in advance.

There is the ADA_PROJECT_PATH environment variable that controls where to look for project files. Look where Alire stored AWS.

Since you have got ODBC you can also use HTTP server and client from there too. You need to choose between GNUTLS and OpenSSL for HTTPS and install the corresponding packages.

1 Like

Okey and how do I look for that path and what should i do after that so my project find the aws.gpr file?

I think the problem is that you launched GNATstudio directly. Ideally, you should always run “alr edit”, which will set all the environment variables correctly and launch GNATstudio in such a way, that it will understand the project structure and just work.

Best regards,
Fer

1 Like

I did not know about that command, thank you, i will try it!

Search for gpr files. The issue with Alire is that it disregards the target system policies. Linux distributions specify the standard directories where gpr files must be placed. If you use the native toolchain its compiler looks there. Alire goes its own way of which the compiler knows nothing.

Alr edit still dont find the aws.gpr file, how can it be so hard to setup this for development :’)

Is there a step by step guide somone can give a noob like me?

Thanks.

I simply use GNAT Studio and gprbuild.

Question, did you run alr build at least once after you alr with aws? If not, some files may not have been regenerated… Try doing that and see if alr build succeeds.

1 Like

this worked , atleast it have not given me any errors so far.
now when ive started the project using alr with aws and build, then alr edit, chosen gnatstudio, how come there is no “public” files for html and css and so on? or is that normal?

And why there should be any?