KDb/Drivers/How to implement a KDb driver: Difference between revisions

From KDE Community Wiki
< KDb‎ | Drivers
No edit summary
No edit summary
Line 28: Line 28:
* 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.
* 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.
* Support for file-based (not server-connection based) has been added. Currently it is used for SQLite driver, the default driver for KEXI. Adding another file-based driver may be practical but at the moment not for KEXI, due to possible maintenance overhead.


* 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.
* During the implementation you can learn about details of implementation of, for example, PostgreSQL driver's implementation, if the new database driver is server-based, or into SQLite driver's implementation, if the new database driver is file-based. These two drivers are usually best implemented and are updated for current API.


: 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.
* Plugin description files. Each KDb driver is a software service implementing "KDb/Driver" service type. Thus, any driver has it's own .json file containing, the following fields (apart of other standard ones such as name, version):


* 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.
* X-KDb-FileBased: true for file-based driver
* X-KDb-ImportingEnabled: true if database importing for this driver is supported


: Driver .desktop file has following custom fields defined:
For file-based drivers the Mimetypes field is important as well.
:* '''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:
Naming scheme for the driver binary is: kdb_*driver.so (on *nix), e.g.  kdb_sqlitedriver.so.
 
Naming scheme for the driver's json file is kdb_*driver.json.
  kexidb_{name}driver.desktop
 
: where {name} is a unique driver name, eg. sqlite3 or mysql.

Revision as of 23:53, 24 June 2023

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. Adding another file-based driver may be practical but at the moment not for KEXI, due to possible maintenance overhead.
  • During the implementation you can learn about details of implementation of, for example, PostgreSQL driver's implementation, if the new database driver is server-based, or into SQLite driver's implementation, if the new database driver is file-based. These two drivers are usually best implemented and are updated for current API.
  • Plugin description files. Each KDb driver is a software service implementing "KDb/Driver" service type. Thus, any driver has it's own .json file containing, the following fields (apart of other standard ones such as name, version):
  • X-KDb-FileBased: true for file-based driver
  • X-KDb-ImportingEnabled: true if database importing for this driver is supported

For file-based drivers the Mimetypes field is important as well.

Naming scheme for the driver binary is: kdb_*driver.so (on *nix), e.g. kdb_sqlitedriver.so. Naming scheme for the driver's json file is kdb_*driver.json.