Discussing the article: "Simplifying Databases in MQL5 (Part 2): Using metaprogramming to create entities"

 

Check out the new article: Simplifying Databases in MQL5 (Part 2): Using metaprogramming to create entities.

We explored the advanced use of #define for metaprogramming in MQL5, creating entities that represent tables and column metadata (type, primary key, auto-increment, nullability, etc.). We centralized these definitions in TickORM.mqh, automating the generation of metadata classes and paving the way for efficient data manipulation by the ORM, without having to write SQL manually.

In the previous article, we took the first steps toward understanding how MQL5 handles databases: we created tables, inserted, updated, and deleted records, and even explored transaction features, data import and export. All of this was done directly, writing raw SQL and calling the native functions offered by the language. This step was crucial because it laid the foundation upon which any abstraction layer will be built. But now an inevitable question arises: do we want to write SQL every time we need to handle data within our robots and indicators?

If we consider a more robust system, the answer is no. Working solely with SQL makes the code verbose, repetitive, and error-prone, especially as the application grows and begins to handle multiple tables, relationships, and validations. This is where an ORM (Object-Relational Mapping) comes in: a way to bridge the gap between the object-oriented world in which we program and the relational world in which the data lives. The first step in this direction is to create a way to represent database tables directly as classes in MQL5.

In this second article, we'll learn how to use an often-underestimated but extremely powerful language feature: #define. It will allow us to automate and standardize the creation of structures, avoiding duplication and facilitating future expansions. With it, we'll build our first entities (classes that represent tables) and also a mechanism for describing column metadata, such as data type, primary key, auto-increment, required fields, and default values.

This approach will be the foundation for everything that follows: repositories, query builders, and automatic table creation. In other words, we're beginning to shape the MQL5 ORM we envisioned from the beginning.


Author: joaopedrodev