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

From KDE Community Wiki
< KDb‎ | Drivers
(Created page with "{{Note|Development questions: contact the maintainer: [https://community.kde.org/Kexi/Contact#Contact_active_contributors_directly]}} 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...")
 
No edit summary
Line 1: Line 1:
{{Note|Development questions: contact the maintainer: [https://community.kde.org/Kexi/Contact#Contact_active_contributors_directly]}}
{{Note|Development questions: contact the maintainer: [https://community.kde.org/Kexi/Contact#Contact_active_contributors_directly]}}
== 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.
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.
Line 9: Line 11:
* For manual tests  it is adviced to also build KEXI (kexi.git), and run with the new driver
* 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


  Drivers are located in kexidb/drivers/ 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.  
* The API is splitted for a number of '''really''' small internal pieces (methods) that should be well understood by driver developer. Pieces are smaller to avoid misunderstunding what '''exactly''' the given method should deliver and how it shopuld behave. Small pieces can also avoid much of development work when KexiDB is about to change and there is something to add or change in the drivers code.
* 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:
* It is hightly adviced to use lower-level '''C API''' of the given database, instead of using C++ API. In fact, C++ API usually implements things like database cursors in own (often magic) fashion, while KexiDB tries to deliver generic cursor's framework. Other issue when you're using '''C API''' instead of '''C++''' is efficiency of the resulted solution.
 
* Newer KexiDB API versions have more-and-more options that allow given driver's implementation talk to KexiDB about what functionality driver can deliver and/or in what fashion (for example transactions support: no trasactions/single transaction/multiple transaction/multiple nested transactions).
 
* KexiDB drivers are work as the KDE Service Libraries
 
* To add your driver implementation you will need to derive from few classes from KexiDB (not just the Driver class):


  KexiDB::Connection
**  KDbConnection
  KexiDB::Cursor
**  KDbCursor
  KexiDB::Driver
**  KDbDriver


* Naming convention: add a prefix of your drivers name to the class name, for example: KexiDB::Cursor --> KexiDB::MySqlCursor. 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.


* 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 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.

Revision as of 23:40, 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.
  • 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.