Skip to content

Fix "connection shutdown" exceptions. Fix #72. #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 53 additions & 32 deletions sqlalchemy_firebird/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from packaging import version

from typing import List
from typing import Any, List, TypedDict
from typing import Optional

from sqlalchemy import __version__ as SQLALCHEMY_VERSION
Expand Down Expand Up @@ -377,19 +377,18 @@ def visit_boolean(self, type_, **kw):
def visit_datetime(self, type_, **kw):
return self.visit_TIMESTAMP(type_, **kw)

def _render_string_type(self, type_, name, length_override=None):
def _render_firebird_string_type(
self,
name: str,
length: Optional[int]=None,
collation: Optional[str]=None,
charset: Optional[str]=None,
) -> str:
firebird_3_or_lower = (
self.dialect.server_version_info
and self.dialect.server_version_info < (4,)
)

length = coalesce(
length_override,
getattr(type_, "length", None),
)
charset = getattr(type_, "charset", None)
collation = getattr(type_, "collation", None)

if name in ["BINARY", "VARBINARY", "NCHAR", "NVARCHAR"]:
charset = None
collation = None
Expand Down Expand Up @@ -432,11 +431,33 @@ def _render_string_type(self, type_, name, length_override=None):

return text

def visit_BINARY(self, type_, **kw):
return self._render_string_type(type_, "BINARY")
def visit_CHAR(self, type_: fb_types.FBCHAR, **kw: Any) -> str:
return self._render_firebird_string_type(
"CHAR",
type_.length,
type_.collation,
getattr(type_, "charset", None),
)

def visit_NCHAR(self, type_: fb_types.FBNCHAR, **kw: Any) -> str:
return self._render_firebird_string_type("NCHAR", type_.length, type_.collation)

def visit_VARBINARY(self, type_, **kw):
return self._render_string_type(type_, "VARBINARY")
def visit_VARCHAR(self, type_: fb_types.FBVARCHAR, **kw: Any) -> str:
return self._render_firebird_string_type(
"VARCHAR",
type_.length,
type_.collation,
getattr(type_, "charset", None),
)

def visit_NVARCHAR(self, type_: fb_types.FBNCHAR, **kw: Any) -> str:
return self._render_firebird_string_type("NVARCHAR", type_.length, type_.collation)

def visit_BINARY(self, type_: fb_types.FBBINARY, **kw) -> str:
return self._render_firebird_string_type("BINARY", type_.length)

def visit_VARBINARY(self, type_: fb_types.FBVARBINARY, **kw) -> str:
return self._render_firebird_string_type("VARBINARY", type_.length)

def visit_TEXT(self, type_, **kw):
return self.visit_BLOB(type_, override_subtype=1, **kw)
Expand Down Expand Up @@ -490,9 +511,11 @@ def visit_TIMESTAMP(self, type_, **kw):
return super().visit_TIMESTAMP(type_, **kw)

return "TIMESTAMP%s %s" % (
"(%d)" % type_.precision
if getattr(type_, "precision", None) is not None
else "",
(
"(%d)" % type_.precision
if getattr(type_, "precision", None) is not None
else ""
),
(type_.timezone and "WITH" or "WITHOUT") + " TIME ZONE",
)

Expand All @@ -501,9 +524,11 @@ def visit_TIME(self, type_, **kw):
return super().visit_TIME(type_, **kw)

return "TIME%s %s" % (
"(%d)" % type_.precision
if getattr(type_, "precision", None) is not None
else "",
(
"(%d)" % type_.precision
if getattr(type_, "precision", None) is not None
else ""
),
(type_.timezone and "WITH" or "WITHOUT") + " TIME ZONE",
)

Expand All @@ -528,7 +553,7 @@ def fire_sequence(self, seq, type_):
)


class ReflectedDomain(util.typing.TypedDict):
class ReflectedDomain(TypedDict):
"""Represents a reflected domain."""

name: str
Expand Down Expand Up @@ -601,7 +626,7 @@ class FBDialect(default.DefaultDialect):

colspecs = {
sa_types.String: fb_types._FBString,
sa_types.Numeric: fb_types._FBNumeric,
sa_types.Numeric: fb_types.FBNUMERIC,
sa_types.Float: fb_types.FBFLOAT,
sa_types.Double: fb_types.FBDOUBLE_PRECISION,
sa_types.Date: fb_types.FBDATE,
Expand All @@ -611,14 +636,14 @@ class FBDialect(default.DefaultDialect):
sa_types.BigInteger: fb_types.FBBIGINT,
sa_types.Integer: fb_types.FBINTEGER,
sa_types.SmallInteger: fb_types.FBSMALLINT,
sa_types.BINARY: fb_types._FBLargeBinary,
sa_types.VARBINARY: fb_types._FBLargeBinary,
sa_types.LargeBinary: fb_types._FBLargeBinary,
sa_types.BINARY: fb_types.FBBINARY,
sa_types.VARBINARY: fb_types.FBVARBINARY,
sa_types.LargeBinary: fb_types.FBBLOB,
}

# SELECT TRIM(rdb$type_name) FROM rdb$types WHERE rdb$field_name = 'RDB$FIELD_TYPE' ORDER BY 1
ischema_names = {
"BLOB": fb_types._FBLargeBinary,
"BLOB": fb_types.FBBLOB,
# "BLOB_ID": unused
"BOOLEAN": fb_types.FBBOOLEAN,
"CSTRING": fb_types.FBVARCHAR,
Expand Down Expand Up @@ -853,7 +878,7 @@ def get_columns( # noqa: C901
charset=row.character_set_name,
collation=row.collation_name,
)
elif issubclass(colclass, fb_types._FBNumeric):
elif colclass in (fb_types.FBFLOAT, fb_types.FBDOUBLE_PRECISION, fb_types.FBDECFLOAT):
# FLOAT, DOUBLE PRECISION or DECFLOAT
coltype = colclass(row.field_precision)
elif issubclass(colclass, fb_types._FBInteger):
Expand All @@ -874,13 +899,9 @@ def get_columns( # noqa: C901
elif issubclass(colclass, sa_types.DateTime):
has_timezone = "WITH TIME ZONE" in row.field_type
coltype = colclass(timezone=has_timezone)
elif issubclass(colclass, fb_types._FBLargeBinary):
elif issubclass(colclass, fb_types.FBBLOB):
if row.field_sub_type == 1:
coltype = fb_types.FBTEXT(
row.segment_length,
row.character_set_name,
row.collation_name,
)
coltype = fb_types.FBTEXT(row.segment_length, row.character_set_name, row.collation_name)
else:
coltype = fb_types.FBBLOB(row.segment_length)
else:
Expand Down
9 changes: 8 additions & 1 deletion sqlalchemy_firebird/firebird.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import firebird.driver
from firebird.driver import driver_config
from firebird.driver import get_timezone
from firebird.driver.types import DatabaseError;


class FBDialect_firebird(FBDialect):
Expand Down Expand Up @@ -63,7 +64,13 @@ def get_deferrable(self, connection):
return connection.deferrable

def do_terminate(self, dbapi_connection) -> None:
dbapi_connection.terminate()
try:
dbapi_connection.close()
except DatabaseError as err:
# Ignore errors during connection termination, as the connection may already be in an invalid state.
# https://github.com/pauldex/sqlalchemy-firebird/issues/72
if not self.is_disconnect(err, None, None):
raise

def create_connect_args(self, url):
opts = url.translate_connect_args(username="user")
Expand Down
12 changes: 6 additions & 6 deletions sqlalchemy_firebird/infrastructure.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
#

if os_name == "nt":
FB50_URL = "https://github.com/FirebirdSQL/firebird/releases/download/v5.0.0-RC2/Firebird-5.0.0.1304-RC2-windows-x64.zip"
FB40_URL = "https://github.com/FirebirdSQL/firebird/releases/download/v4.0.4/Firebird-4.0.4.3010-0-x64.zip"
FB30_URL = "https://github.com/FirebirdSQL/firebird/releases/download/v3.0.11/Firebird-3.0.11.33703-0_x64.zip"
FB50_URL = "https://github.com/FirebirdSQL/firebird/releases/download/v5.0.2/Firebird-5.0.2.1613-0-windows-x64.zip"
FB40_URL = "https://github.com/FirebirdSQL/firebird/releases/download/v4.0.5/Firebird-4.0.5.3140-0-x64.zip"
FB30_URL = "https://github.com/FirebirdSQL/firebird/releases/download/v3.0.12/Firebird-3.0.12.33787-0-x64.zip"
FB25_URL = "https://github.com/FirebirdSQL/firebird/releases/download/R2_5_9/Firebird-2.5.9.27139-0_x64_embed.zip"
FB25_EXTRA_URL = "https://github.com/FirebirdSQL/firebird/releases/download/R2_5_9/Firebird-2.5.9.27139-0_x64.zip"
else:
FB50_URL = "https://github.com/FirebirdSQL/firebird/releases/download/v5.0.0-RC2/Firebird-5.0.0.1304-RC2-linux-x64.tar.gz"
FB40_URL = "https://github.com/FirebirdSQL/firebird/releases/download/v4.0.4/Firebird-4.0.4.3010-0.amd64.tar.gz"
FB30_URL = "https://github.com/FirebirdSQL/firebird/releases/download/v3.0.11/Firebird-3.0.11.33703-0.amd64.tar.gz"
FB50_URL = "https://github.com/FirebirdSQL/firebird/releases/download/v5.0.2/Firebird-5.0.2.1613-0-linux-arm64.tar.gz"
FB40_URL = "https://github.com/FirebirdSQL/firebird/releases/download/v4.0.5/Firebird-4.0.5.3140-0.amd64.tar.gz"
FB30_URL = "https://github.com/FirebirdSQL/firebird/releases/download/v3.0.12/Firebird-3.0.12.33787-0.amd64.tar.gz"
FB25_URL = "https://github.com/FirebirdSQL/firebird/releases/download/R2_5_9/FirebirdCS-2.5.9.27139-0.amd64.tar.gz"

TEMP_PATH = gettempdir()
Expand Down
Loading