How mariadb-corporation/mariadb-connector-python works
Project Type and Purpose
• This is a database-connector library (client-side driver) that implements the Python DB API 2.0 (PEP-249).
• Users import it in their Python applications to communicate with MariaDB/MySQL servers.
• It combines pure-Python modules, C extensions, and links to the MariaDB Connector/C client library.
File Structure
• Top-level modules and packages:
• mariadb/ ← Pure-Python API modules (connection, cursors, constants, pooling)
• mariadb_*.c files ← C-extension sources (binding layer, codecs, parser, exceptions)
• / ← C headers for building the extension
• helper/ ← Code-gen scripts for error constants
• Build and configuration:
• setup.py, pyproject.toml, site.cfg
• .github/, .travis.yml CI workflows
• Documentation and examples:
• docs/source/ ← Sphinx reST files (API reference, usage, connection, pooling)
• docs/examples/ ← Python scripts demonstrating basic usage
• Testing:
• testing/bench.py and testing/benchmarks/ for performance tests
• testing/test/ ← Unit and integration tests under pytest or unittest
Extract Insights from README
• Confirms it’s a DB API 2.0-compliant client implemented in C.
• Depends on the MariaDB Connector/C library for network I/O.
• Targets Python 3.7+ and LGPL-2.1 licensing.
Main Components
a. Public-API Layer (Python)
• mariadb.dbapi20, connections.py, cursors.py, connectionpool.py
• Constants packages under mariadb/constants
• Exception handling modules
b. C-Extension Layer
• mariadb.c (module init and Python–C dispatch)
• mariadb_connection.c, mariadb_cursor.c, mariadb_exception.c, mariadb_parser.c
• mariadb_codecs.c (character-set conversions)
c. Native Client Library
• MariaDB Connector/C (external C library)
• libmariadb.so / msvcr dependencies
d. Network (External Service)
• MariaDB/MySQL server over TCP/IP or socket
e. Support Infrastructure (secondary, shown periphery)
• Build system (setup.py, C compiler)
• CI pipelines (.github/workflows, .travis.yml)
• Documentation site generator (Sphinx)
• Test harness and benchmarking suite
Architectural Patterns & Principles
• Layered architecture: clear separation between API, native binding, and network client.
• Adapter pattern: C extension adapts the C client library to Python’s DB-API interface.
• Factory pattern: connectionpool creates and manages Connection objects.
• Single Responsibility: separate modules for constants, errors, parsing, and codecs.