Skip to main content

pymssql-utils is a small library that wraps pymssql to make your life easier.

Project description

pymssql-utils (ALPHA)

pymssql-utils is a small library that wraps pymssql to make your life easier. It provides a higher-level API, as well as a some utility methods, so that you can think less about connections and cursors.

This module's features:

  • Higher-level API that reduces the amount of boilerplate required.
  • Baked-in sensible defaults and usage patterns.
  • Provides optional execution batching, similar to pyodbc's fast_executemany.
  • Parses the SQL Types that pymssql misses to native Python types, and vice versa.
  • Makes it easy to serialize your data with orjson.
  • Provides you with simple and clear options for error handling.
  • Extra utility functions, e.g. for building dynamic SQL queries.
  • Fixing various edge case bugs that arise when using pymssql.
  • Fully type hinted.

This module's enforced opinions (check these work for you):

  • Each execution opens and closes a connection using pymssql's context management.
  • Execution data is returned as a dictionary, as accessing data by column name is clearer and simpler than by index.
  • Converts numeric data to float as this is easier to work with than Decimal and for the vast majority of cases 'good enough'.

When you shouldn't use this module:

  • If you need fine-grained control over your cursors.
  • If performance is a must (use pyodbc's)

Please raise any suggestions or issues via GitHub.

Usage

Installation

This library can be installed via pip: pip install --upgrade pymssql-utils. This library requires Python >= 3.6 and Pip >= 19.3.

Quickstart

This library provides two high-level methods:

  • Query: non-committing, fetches data
  • Execute: committing, optionally fetches data

Running a simple query, accessing the returned data and serialising to JSON:

>>> import pymssqlutils as sql
>>> result = sql.query(
      "SELECT SYSDATETIMEOFFSET() as now",
      server="..."
    )
>>> result.ok
True
>>> result.data
[{'now': datetime.datetime(2021, 1, 21, 23, 31, 11, 272299, tzinfo=datetime.timezone.utc)}]
>>> result.data[0]['now']
datetime.datetime(2021, 1, 21, 23, 31, 11, 272299, tzinfo=datetime.timezone.utc)
>>> result.to_json()
'[{"now":"2021-01-21T23:31:11.272299+00:00"}]'

Running a simple execution:

TODO

Specifying Connection

There are two ways of specifying the connection parameters to the SQL Server:

  1. Passing the required parameters (see pymssql docs) to query or execute like in the quickstart example above. Note: All extra kwargs passed to these methods are passed on to the pymssql.connection().
  2. Specify the connection parameters in the environment like the example below. Note: that parameters given explicitly will take precedence over connection parameters specified in the environment.
import os
import pymssqlutils as sql

os.environ["MSSQL_SERVER"] = "sqlserver.mycompany.com"
os.environ["MSSQL_USER"] = "my_login"
os.environ["MSSQL_PASSWORD"] = "my_password123"

result = sql.execute("INSERT INTO mytable VALUES (1, 'test)")

Executing SQL

This library provides four functions for executing SQL code: query, execute, execute_many & execute_batched. These functions call pymssql's execute or executemany functions with varying behaviour to fetching result data or committing the transaction, see table below.

Function Uses commits fetches
query execute False True
execute execute True Optional
execute_many executemany True False
execute_batched execute True False

Splitting query & execute into two functions based on whether the execution commits or not is intended to make your code clearer and more explicit.

Error handling (TODO)

Utility Functions (TODO)

Testing (TODO)

Must install pytest to run main tests, that mock cursor results. To test on_database tests against an MSSQL instance "TEST_ON_DATABASE" must be set in the environment as well as any of the normal env variables to connect to the MSSQL server, pytest-dotenv can help with this.

Notes

Why pymssql when Microsoft officially recommends pyodbc (opinion)?

The main difference between pyodbc and pymssql is the drivers they use. The ODBC are newer and have various levels of support on differing linux distributions, and if you develop for containers or distribute code onto different platforms you can run into ODBC driver-related issues that FreeTDS tends to not have.

There are other minor reasons someone might prefer pymssql, e.g.:

  • pymssql's parameter subsitution is done client-side improving operation visibility.
  • pymssql also has support for MSSQL specific data types such as Datetimeoffset.

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

pymssql-utils-0.0.13.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

pymssql_utils-0.0.13-py3-none-any.whl (18.9 kB view details)

Uploaded Python 3

File details

Details for the file pymssql-utils-0.0.13.tar.gz.

File metadata

  • Download URL: pymssql-utils-0.0.13.tar.gz
  • Upload date:
  • Size: 9.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1

File hashes

Hashes for pymssql-utils-0.0.13.tar.gz
Algorithm Hash digest
SHA256 294f11585a7a9398fb1aacb69ad39abc6ac25ffea429dccef7f04b2770288649
MD5 cb2e732a58d50e685d969fd4d2cdece2
BLAKE2b-256 f934102bbc137b6939ed55168b56548ab17b16f5355259daeea49110bdddce3b

See more details on using hashes here.

File details

Details for the file pymssql_utils-0.0.13-py3-none-any.whl.

File metadata

  • Download URL: pymssql_utils-0.0.13-py3-none-any.whl
  • Upload date:
  • Size: 18.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1

File hashes

Hashes for pymssql_utils-0.0.13-py3-none-any.whl
Algorithm Hash digest
SHA256 2a690307e532f8ce68fed3ae5dcda8d0d8146164232302afffe291041dd6adbe
MD5 52b3fab6148723616f2456a9c5058072
BLAKE2b-256 e636367fa95059b409dc7e62af51b237e451e3ca10c4c6ed8204bdd933f89afc

See more details on using hashes here.

Supported by

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