Skip to main content

Django Tibero Backend

Project description

Tibero backend for Django

Features

  • Supports Django 5.1
  • Tested on Tibero 6 FS07_CS_2005, Tibero 6.7, Tibero 7.2
  • Passes most of the tests of the Django test suite

Dependencies

  • pyodbc 5.0 or newer

Installation

  1. Install pyodbc 5.0 (or newer) and Django

  2. Install django-tibero:

    pip install django-tibero
    
  3. Set the ENGINE setting in the settings.py file used by your Django application or project to 'mssql':

    'ENGINE': 'django_tibero'
    

Configuration

Example Configurations

Example 1: Basic Connection Using Driver

This example demonstrates how to configure the connection using the Tibero ODBC driver in Django. The key aspect is specifying the driver in the OPTIONS section, as UnixODBC uses this value to determine which driver to load. The driver must be defined in the odbcinst.ini file.

settings.py Configuration

# driver option is necessary because UnixODBC uses the value to choose driver.
# the value must exist in odbcinst.ini.
DATABASES = {
    "ENGINE": "django_tibero",
    "HOST": "127.0.0.1",
    "PORT": 8629,
    "NAME": "tibero",  # SID name
    "USER": "tibero",
    "PASSWORD": "tmax",
    "OPTIONS": {
        "driver": "Tibero7Driver"
    }
}

odbcinst.ini Configuration

[Tibero7Driver]
Description=ODBC Driver for Tibero 7
Driver=/home/tibero7/client/lib/libtbodbc.so

Example 2: Connection Using DSN

This example demonstrates how to configure a connection using a DSN (Data Source Name) defined in the odbc.ini file. The DSN contains the connection details.

settings.py Configuration

# dsn name from odbc.ini. In this case, odbc.ini may contain
# user, password. This example overrides DSN user and DSN password if
# user, password exist in odbc.ini file. 
DATABASES = {
    "ENGINE": "django_tibero",
    "USER": "django-user",
    "PASSWORD": "django-password",
    "OPTIONS": {
        "dsn": "Tibero7DSN"  # DSN name from odbc.ini
    }
}

/etc/odbc.ini Configuration

[Tibero7DSN]
Description = Tibero7 ODBC Datasource
Driver      = Tibero7Driver
SID         = tibero7

Standard Django settings

The following keys in the DATABASES setting control the behavior of the backend:

  • ENGINE

    • String. Must be set to "django_tibero". Required.
  • NAME

    • String. The SID name of the Tibero database. Optional.
  • HOST

    • String. The database server address. Example: "127.0.0.1", "localhost". Optional.
  • PORT

    • String or Integer. The server instance port. Optional.
  • USER

    • String. The database username. Optional.
  • PASSWORD

    • String. The database user password. Optional.

OPTIONS (Advanced Settings)

In addition to the basic settings, you can configure advanced options using the OPTIONS dictionary in the DATABASES setting. These options allow you to control various aspects of the database connection and query behavior:

  • driver

    • String. Specifies the ODBC driver name as defined in odbcinst.ini.
    • driver or dsn option is required because unixodbc uses this option to choose driver.
    • Example: "Tibero7Driver"
  • dsn

    • String. Specifies the Data Source Name (DSN) as defined in odbc.ini.
    • driver or dsn option is required because unixodbc uses this option to choose driver.
    • Example: "TiberoDSN"
  • connection_timeout

    • Type: Integer
    • Description: Specifies the timeout for the connection attempt, in seconds.
    • Default: 0 (no timeout)
    • Behavior: Passed to the timeout parameter of pyodbc.connect().
  • connection_retries

    • Type: Integer
    • Description: Defines the number of retry attempts if a connection fails.
    • Default: 5
    • Behavior: If the initial connection attempt fails, the backend retries up to this number of times.
  • connection_retry_backoff_time

    • Type: Integer
    • Description: Specifies the wait time (in seconds) between connection retry attempts.
    • Default: 5
    • Behavior: The system waits this amount of time before retrying a failed connection attempt.
  • query_timeout

    • Type: Integer
    • Description: Sets the timeout for SQL queries, in seconds.
    • Default: 0 (no timeout)
    • Behavior:
      • This timeout applies to all queries executed via the connection.
      • If a query exceeds this timeout, the database raises an OperationalError with SQLSTATE HYT00 or HYT01.
      • Passed to the timeout property of the pyodbc connection object.
  • setencoding

    • Type: String
    • Description: Defines the text encoding for SQL statements and text parameters.
    • Behavior:
      • Passed to the setencoding() method of the pyodbc connection.
      • Must be a valid Python encoding (e.g., "utf-8").
  • setdecoding

    • Type: String
    • Description: Defines the text decoding method for reading SQL_CHAR or SQL_WCHAR values from the database.
    • Behavior:
      • Passed to the setdecoding() method of the pyodbc connection.
      • Must be a valid Python encoding (e.g., "utf-16le").

Connection Handling Logic inside Tibero ODBC driver

The following logic explains how options are used when UnixODBC calls the driver’s SQLDriverConnect() function.

  1. Before connection string is passed to pyodbc.connect() method. Following logic inside django-tibero backend transforms options in settings.py. And then, transform to ODBC Connection String. This logic is not handled in Tibero driver.
cstr_parts = {
    'DRIVER': options.get('driver', None),
    'DSN': options.get('dsn', None),
    'Server': conn_params.get('HOST', None),
    'Database': conn_params.get('NAME', None),
    'Port': str(conn_params.get('PORT', None)),
    'User': conn_params.get('USER', None),
    'Password': conn_params.get('PASSWORD', None),

# Example of connection_string is "Driver=Tibero7Driver;Server=127.0.0.1;Port=8629;Database=tibero7;User=django;Password=password"
connection_string = odbc_connection_string_from_settings(cstr_parts)

connection = pyodbc.connect(connection_string)
  1. Connection Logic inside Tibero Driver
REQUIRED_KEYWORDS = ['Server', 'IpAddress', 'Port', 'Database', 'User', 'Password']
    
IF Server keyword is in connection string:
    IF NOT all(keyword in connection string for keyword in REQUIRED_KEYWORDS):
        RAISE ConnectionError("Missing required connection parameters")
    ELSE:
        Use REQUIRED_KEYWORDS in the connection string to connect


ELSE IF 'DSN' keyword is in connection_string:
    # find odbc.ini file
    IF environment_variable_exists('ODBCINI'):
        config_file = $ODBCINI
    ELSE IF file_exists('$HOME/.odbc.ini'):
        config_file = '$HOME/.odbc.ini'
    ELSE:
        config_file = '/etc/odbc.ini'

    IF 'Server' or 'IpAddress' exists in config_file:
        REQUIRE 'Port' and ('Database' or 'DB') in config_file
        
        IF 'User' or 'Password' in connection_string:
            Override credentials, otherwise use 'User' and 'Password key/value from config_file 
     
        Connect to Tibero Database 
    ELSE:
        USE credentials from tbdsn.tbr
        
        IF 'User' or 'Password' in connection_string:
            Override credentials, otherwise use 'User' and 'Password key/value from config_file
        Connect to Tibero Database

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_tibero-0.0.1a1.tar.gz (38.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_tibero-0.0.1a1-py3-none-any.whl (40.5 kB view details)

Uploaded Python 3

File details

Details for the file django_tibero-0.0.1a1.tar.gz.

File metadata

  • Download URL: django_tibero-0.0.1a1.tar.gz
  • Upload date:
  • Size: 38.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for django_tibero-0.0.1a1.tar.gz
Algorithm Hash digest
SHA256 463bd3b379da804f0bbaa82d73df44288cab3fb0453ef1b81eed92e783d2c9b0
MD5 8432236875999c5de1460003366fa29f
BLAKE2b-256 bccc8a3195909c18ad8f06c29a34d2340c20e15b47de805658a5d2fbfae33a06

See more details on using hashes here.

File details

Details for the file django_tibero-0.0.1a1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_tibero-0.0.1a1-py3-none-any.whl
Algorithm Hash digest
SHA256 cfe5c428c9fcfead2eff1c3ac90590b99c83a0e27a836096b5bcb7086ae5eba0
MD5 78f7fd57d457d12894ee49c3969ad533
BLAKE2b-256 bf7ef045b1b226a4834d6a33b7ae7f3c80d5089f1501ace769d0ee810e3f25fb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page