KDb/Drivers/How to implement a KDb driver

From KDE Community Wiki
< KDb‎ | Drivers
Revision as of 23:40, 24 June 2023 by Jstaniek (talk | contribs)

Note

Development questions: contact the maintainer: [1]


Introduction

The internal KDb API of database drivers is designed for development new and maintaining existing drivers. Along the way a number of rules and recommendations have been formulated.

  • For best results, contact the maintainer prior to starting any work
  • New drivers should be developed for the master or lastest stable branch
  • The KDb git repository is sufficient source code dependency for the task
  • For automatic testing, tests from the KDb repository should 100% succeed, it is recommended to add new specific tests when it makes sense
  • For manual tests it is adviced to also build KEXI (kexi.git), and run with the new driver

Directory layout

  • Drivers are located in src/drivers/ subdirectory
  • Autotests are located in autotests/ subdirectory
  • Manual tests are located in tests/ subdirectory

Details

  • The API is split to cover a number of smaller internal aspects of a driver. They should be easier to understand by the developer without leaving space for interpretation.
  • It is advised to use lower-level C API of the given backend/database, instead of using its C++ (or any other high level) API. C++ API usually implements data handling aspects in its own specific manner. This can make hard or impossible to "map" the APIs to KDb. Aother possible benefit of using the C API is performance due to higher level of control. Then, lower-level APIs may be more stable and more wide accessible.
  • To implement a driver derive from a few KDb classes:
    • KDbConnection
    • KDbCursor
    • KDbDriver
  • Class naming convention: add a prefix of your drivers name to the class name, for example in case of MySQL the name of the cursor class was mapped from KDbCursor to KDbMySqlCursor. Names should be started from uppercase letter.
  • Support for file-based (not server-connection based) has been added. Currently it is used for SQLite driver, the default driver for KEXI.
  • During implementation you can learn how things were implemented by looking into PostgreSQL driver's implementation (if your database is server-based) and into SQLite driver's implementation (if your database is file-based). These two drivers are usually best implemented and updated for current API state.
Note: please don't be confused that PostgreSQL driver uses libpqxx C++ API, while above we've discussed that using C API would be a better choice. PostgreSQL driver case is just exception from the rule and shows that using C++ API there could be redundant.
  • Description files. Each KexiDB driver is a KDE Service implementing "Kexi/DBDriver" service type (see kexidb/kexidb_driver.desktop file). Thus, any driver has it's own .desktop file installed to KDE services directory on install time.
Driver .desktop file has following custom fields defined:
  • X-Kexi-DriverName, e.g. SQLite3 or MySQL, case insensitive
  • X-Kexi-DriverType, possible types:
    • File for file-based db drivers (currently SQLite 2 and 3)
    • Server for server-based db drivers
  • X-Kexi-FileDBDriverMime, only meaningful for file-based db drivers (e.g. application/x-kexiproject-sqlite3 for SQLite 3). It's used to detect files by it's header and/or name extension
  • X-Kexi-KexiDBVersion, driver version, it's important since it must be compatible with kexidb library. If not, driver will not be loaded. Format of version string is currently X.Y, where X == major version, Y == minor version (example: 1.4).
Note: Versions are usually backward compatible (1.4 driver should work when 1.3 is expected) but not in the opposite way. Always keep version value of X-Kexi-KexiDBVersion field of your driver updated for current KexiDB version.
  • Naming guidelines. .desktop file should be named using following form:
 kexidb_{name}driver.desktop
where {name} is a unique driver name, eg. sqlite3 or mysql.