Skip to main content

SQLAlchemy dialect for FairCom Database via JSON API

Project description

SQLAlchemy FairCom

A pure Python SQLAlchemy dialect for FairCom Database using the JSON/REST API. This driver works cross-platform without requiring native C libraries.

License: MIT Python 3.7+

Features

  • ✅ Pure Python implementation (no native libraries required)
  • ✅ Cross-platform: Works on macOS, Linux, Windows
  • ✅ SQLAlchemy compatible (1.4+)
  • ✅ DB-API 2.0 compliant
  • ✅ Uses FairCom JSON API over HTTP/HTTPS
  • ✅ Compatible with Apache Superset, Pandas, and other SQLAlchemy-based tools

Installation

From PyPI

pip install sqlalchemy-faircom

From Source

git clone https://github.com/toddstoffel/sqlalchemy-faircom.git
cd sqlalchemy-faircom
pip install -e .

For Development

git clone https://github.com/toddstoffel/sqlalchemy-faircom.git
cd sqlalchemy-faircom
pip install -e .[dev]
pytest

Connection String Format

faircom://username:password@host:port/database?protocol=http

Parameters

  • username: Database username (e.g., ADMIN)
  • password: Database password
  • host: Database server hostname
  • port: JSON API port (typically 8080 for HTTP, 8443 for HTTPS)
  • database: Database name (e.g., ctreeSQL)
  • protocol: Either http or https (default: http)

Environment Variables

Create a .env file in your project root:

HOST=your-server.example.com
PORT=8080
USERNAME=your_username
PASSWORD=your_password
PROTOCOL=http

Note: PORT should be set to the JSON API port: 8080 for HTTP or 8443 for HTTPS.

Usage Examples

Basic SQLAlchemy Usage

from sqlalchemy import create_engine, text

# Create connection
connection_string = 'faircom://username:password@your-server:8080/ctreeSQL?protocol=http'
engine = create_engine(connection_string, echo=True)

# Execute queries
with engine.connect() as conn:
    result = conn.execute(text("SELECT 1 as test"))
    print(result.fetchone())

Using Environment Variables

import os
from dotenv import load_dotenv
from sqlalchemy import create_engine, text

load_dotenv()

connection_string = f"faircom://{os.getenv('USERNAME')}:{os.getenv('PASSWORD')}@{os.getenv('HOST')}:{os.getenv('PORT')}/{os.getenv('DATABASE', 'ctreeSQL')}?protocol={os.getenv('PROTOCOL', 'http')}"
engine = create_engine(connection_string)

with engine.connect() as conn:
    result = conn.execute(text("SELECT * FROM my_table"))
    for row in result:
        print(row)

Direct DB-API Usage

from faircom_jsonapi.dbapi import connect

# Connect to database
conn = connect(
    host='your-server.example.com',
    port=8080,
    username='your_username',
    password='your_password',
    database='ctreeSQL',
    protocol='http'
)

# Create cursor and execute
cursor = conn.cursor()
cursor.execute("SELECT 1 as test")
print(cursor.fetchone())

cursor.close()
conn.close()

Testing

Run the test suite:

pytest tests/

Project Structure

sqlalchemy-faircom/
├── LICENSE                       # MIT License
├── README.md                     # This file
├── pyproject.toml               # Project metadata and dependencies
├── setup.py                     # Package setup
├── MANIFEST.in                  # Package manifest
├── faircom_jsonapi/
│   ├── __init__.py              # Package initialization
│   ├── client.py                # FairCom JSON API client
│   ├── dbapi.py                 # DB-API 2.0 implementation
│   └── sqlalchemy_dialect.py   # SQLAlchemy dialect
└── tests/
    └── test_dialect.py          # Unit tests

Compatibility

  • Python 3.7+
  • SQLAlchemy 1.4+
  • Apache Superset - Full pagination support
  • Works with any tool that uses SQLAlchemy (e.g., Pandas, Jupyter notebooks, etc.)

Pagination Support

FairCom uses SKIP syntax instead of the standard OFFSET. This driver automatically converts SQLAlchemy pagination:

  • LIMIT only: Generates SELECT TOP n ... (optimized)

    query.limit(10)  # → SELECT TOP 10 ...
    
  • LIMIT + OFFSET: Generates SELECT ... SKIP n FETCH FIRST m ROWS ONLY

    query.limit(10).offset(20)  # → SELECT ... SKIP 20 FETCH FIRST 10 ROWS ONLY
    
  • OFFSET only: Generates SELECT ... SKIP n

    query.offset(5)  # → SELECT ... SKIP 5
    

Apache Superset Compatibility

Full pagination support - Apache Superset's data exploration features work seamlessly with this driver. The automatic OFFSET→SKIP conversion ensures all Superset queries execute correctly.

Limitations

Database Limitations

  • Parameterized TOP/SKIP Values: FairCom requires literal integer values in TOP and SKIP clauses (not bind parameters). This driver automatically extracts literal values from SQLAlchemy queries.

API Limitations

  • Read operations are fully supported
  • Write operations may have limited support (depends on JSON API capabilities)
  • Some advanced SQLAlchemy features may not be implemented

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Publishing

To publish a new version to PyPI:

# Update version in pyproject.toml
# Clean and build
rm -rf dist/ build/ *.egg-info
python -m build

# Upload to PyPI
twine upload dist/*

Troubleshooting

Connection Refused

  • Ensure the JSON API is enabled on your FairCom server
  • Verify the correct port (8080 for HTTP, 8443 for HTTPS)
  • Check firewall settings

SSL Warnings

The driver currently disables SSL verification for HTTPS connections. For production use, you may want to enable proper certificate verification in faircom_jsonapi/client.py.

Reserved Keywords

Some SQL keywords like "number" are reserved in FairCom. Use different column aliases if you encounter syntax errors.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Links

Acknowledgments

  • Built with SQLAlchemy
  • Uses FairCom Database JSON API
  • Pure Python implementation for maximum compatibility

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

sqlalchemy_faircom-0.1.19.tar.gz (17.7 kB view details)

Uploaded Source

Built Distribution

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

sqlalchemy_faircom-0.1.19-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

Details for the file sqlalchemy_faircom-0.1.19.tar.gz.

File metadata

  • Download URL: sqlalchemy_faircom-0.1.19.tar.gz
  • Upload date:
  • Size: 17.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for sqlalchemy_faircom-0.1.19.tar.gz
Algorithm Hash digest
SHA256 4dbcfcea638be4cc85026ea7947db5505f6999e50c06b26cb4d4ec8346d3fac7
MD5 32bf9090c140a342cb4b6179db50455c
BLAKE2b-256 f1691c64469ca9dbfd3f33b0564b8cec41237658e22e409cc8f937b420df9cac

See more details on using hashes here.

File details

Details for the file sqlalchemy_faircom-0.1.19-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlalchemy_faircom-0.1.19-py3-none-any.whl
Algorithm Hash digest
SHA256 d72fbbc7cf89810414b8c8671e4c41d581b94bc031c5f0a6128507ac9b52fd64
MD5 22a537c3ac40175da13525caa40d6aa6
BLAKE2b-256 d02f6613e0b9586ecde896b172b68414a7c20a94b80acc06fb75d29fe92edb2b

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