databend-sqlalchemy
===================
Databend dialect for SQLAlchemy.
Installation
------------
The package is installable through PIP::
pip install databend-sqlalchemy
Usage
-----
The DSN format is similar to that of regular Postgres::
from sqlalchemy import create_engine, text
from sqlalchemy.engine.base import Connection, Engine
engine = create_engine(
f"databend://{username}:{password}@{host_port_name}/{database_name}?sslmode=disable"
)
connection = engine.connect()
result = connection.execute(text("SELECT 1"))
assert len(result.fetchall()) == 1
import connector
cursor = connector.connect('databend://root:@localhost:8000?sslmode=disable').cursor()
cursor.execute('SELECT * FROM test')
# print(cursor.fetchone())
# print(cursor.fetchall())
for row in cursor:
print(row)
Merge Command Support
---------------------
Databend SQLAlchemy supports upserts via its `Merge` custom expression.
See [Merge](https://docs.databend.com/sql/sql-commands/dml/dml-merge) for full documentation.
The Merge command can be used as below::
from sqlalchemy.orm import sessionmaker
from sqlalchemy import MetaData, create_engine
from databend_sqlalchemy.databend_dialect import Merge
engine = create_engine(db.url, echo=False)
session = sessionmaker(bind=engine)()
connection = engine.connect()
meta = MetaData()
meta.reflect(bind=session.bind)
t1 = meta.tables['t1']
t2 = meta.tables['t2']
merge = Merge(target=t1, source=t2, on=t1.c.t1key == t2.c.t2key)
merge.when_matched_then_delete().where(t2.c.marked == 1)
merge.when_matched_then_update().where(t2.c.isnewstatus == 1).values(val = t2.c.newval, status=t2.c.newstatus)
merge.when_matched_then_update().values(val=t2.c.newval)
merge.when_not_matched_then_insert().values(val=t2.c.newval, status=t2.c.newstatus)
connection.execute(merge)
Table Options
---------------------
Databend SQLAlchemy supports databend specific table options for Engine, Cluster Keys and Transient tables
The table options can be used as below::
from sqlalchemy import Table, Column
from sqlalchemy import MetaData, create_engine
engine = create_engine(db.url, echo=False)
meta = MetaData()
# Example of Transient Table
t_transient = Table(
"t_transient",
meta,
Column("c1", Integer),
databend_transient=True,
)
# Example of Engine
t_engine = Table(
"t_engine",
meta,
Column("c1", Integer),
databend_engine='Memory',
)
# Examples of Table with Cluster Keys
t_cluster_1 = Table(
"t_cluster_1",
meta,
Column("c1", Integer),
databend_cluster_by=[c1],
)
#
c = Column("id", Integer)
c2 = Column("Name", String)
t_cluster_2 = Table(
't_cluster_2',
meta,
c,
c2,
databend_cluster_by=[cast(c, String), c2],
)
meta.create_all(engine)
Compatibility
---------------
- If databend version >= v0.9.0 or later, you need to use databend-sqlalchemy version >= v0.1.0.
- The databend-sqlalchemy use [databend-py](https://github.com/datafuselabs/databend-py) as internal driver when version < v0.4.0, but when version >= v0.4.0 it use [databend driver python binding](https://github.com/datafuselabs/bendsql/blob/main/bindings/python/README.md) as internal driver. The only difference between the two is that the connection parameters provided in the DSN are different. When using the corresponding version, you should refer to the connection parameters provided by the corresponding Driver.
Raw data
{
"_id": null,
"home_page": "https://github.com/datafuselabs/databend-sqlalchemy",
"name": "databend-sqlalchemy",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": null,
"keywords": null,
"author": "Databend Cloud",
"author_email": "hanshanjie@datafuselabs.com",
"download_url": "https://files.pythonhosted.org/packages/68/0f/547ec6375ee9a9b1584aed374854466fdf9d10de5f57fc5ca05e611ada7c/databend_sqlalchemy-0.4.7.tar.gz",
"platform": null,
"description": "databend-sqlalchemy\n===================\n\nDatabend dialect for SQLAlchemy.\n\nInstallation\n------------\n\nThe package is installable through PIP::\n\n pip install databend-sqlalchemy\n\nUsage\n-----\n\nThe DSN format is similar to that of regular Postgres::\n\n from sqlalchemy import create_engine, text\n from sqlalchemy.engine.base import Connection, Engine\n engine = create_engine(\n f\"databend://{username}:{password}@{host_port_name}/{database_name}?sslmode=disable\"\n )\n connection = engine.connect()\n result = connection.execute(text(\"SELECT 1\"))\n assert len(result.fetchall()) == 1\n\n import connector\n cursor = connector.connect('databend://root:@localhost:8000?sslmode=disable').cursor()\n cursor.execute('SELECT * FROM test')\n # print(cursor.fetchone())\n # print(cursor.fetchall())\n for row in cursor:\n print(row)\n\n\nMerge Command Support\n---------------------\n\nDatabend SQLAlchemy supports upserts via its `Merge` custom expression.\nSee [Merge](https://docs.databend.com/sql/sql-commands/dml/dml-merge) for full documentation.\n\nThe Merge command can be used as below::\n\n from sqlalchemy.orm import sessionmaker\n from sqlalchemy import MetaData, create_engine\n from databend_sqlalchemy.databend_dialect import Merge\n\n engine = create_engine(db.url, echo=False)\n session = sessionmaker(bind=engine)()\n connection = engine.connect()\n\n meta = MetaData()\n meta.reflect(bind=session.bind)\n t1 = meta.tables['t1']\n t2 = meta.tables['t2']\n\n merge = Merge(target=t1, source=t2, on=t1.c.t1key == t2.c.t2key)\n merge.when_matched_then_delete().where(t2.c.marked == 1)\n merge.when_matched_then_update().where(t2.c.isnewstatus == 1).values(val = t2.c.newval, status=t2.c.newstatus)\n merge.when_matched_then_update().values(val=t2.c.newval)\n merge.when_not_matched_then_insert().values(val=t2.c.newval, status=t2.c.newstatus)\n connection.execute(merge)\n\n\nTable Options\n---------------------\n\nDatabend SQLAlchemy supports databend specific table options for Engine, Cluster Keys and Transient tables\n\nThe table options can be used as below::\n\n from sqlalchemy import Table, Column\n from sqlalchemy import MetaData, create_engine\n\n engine = create_engine(db.url, echo=False)\n\n meta = MetaData()\n # Example of Transient Table\n t_transient = Table(\n \"t_transient\",\n meta,\n Column(\"c1\", Integer),\n databend_transient=True,\n )\n\n # Example of Engine\n t_engine = Table(\n \"t_engine\",\n meta,\n Column(\"c1\", Integer),\n databend_engine='Memory',\n )\n\n # Examples of Table with Cluster Keys\n t_cluster_1 = Table(\n \"t_cluster_1\",\n meta,\n Column(\"c1\", Integer),\n databend_cluster_by=[c1],\n )\n #\n c = Column(\"id\", Integer)\n c2 = Column(\"Name\", String)\n t_cluster_2 = Table(\n 't_cluster_2',\n meta,\n c,\n c2,\n databend_cluster_by=[cast(c, String), c2],\n )\n\n meta.create_all(engine)\n\n\n\nCompatibility\n---------------\n\n- If databend version >= v0.9.0 or later, you need to use databend-sqlalchemy version >= v0.1.0.\n- The databend-sqlalchemy use [databend-py](https://github.com/datafuselabs/databend-py) as internal driver when version < v0.4.0, but when version >= v0.4.0 it use [databend driver python binding](https://github.com/datafuselabs/bendsql/blob/main/bindings/python/README.md) as internal driver. The only difference between the two is that the connection parameters provided in the DSN are different. When using the corresponding version, you should refer to the connection parameters provided by the corresponding Driver.\n\n\n",
"bugtrack_url": null,
"license": "Apache-2.0",
"summary": "Sqlalchemy adapter for Databend",
"version": "0.4.7",
"project_urls": {
"Bug Tracker": "https://github.com/datafuselabs/databend-sqlalchemy",
"Homepage": "https://github.com/datafuselabs/databend-sqlalchemy"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "4a302d16b96d5632f0513bb654655c2b71526dd039634347bf8247be39e9e8b1",
"md5": "ddbe7da9621b434fcb29bd313a45a24d",
"sha256": "d3c0c2c1667bb40ba2debca0fc9ba995fcc0416b03750ef8458c8aa55b016c0d"
},
"downloads": -1,
"filename": "databend_sqlalchemy-0.4.7-py3-none-any.whl",
"has_sig": false,
"md5_digest": "ddbe7da9621b434fcb29bd313a45a24d",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 24567,
"upload_time": "2024-08-26T07:11:28",
"upload_time_iso_8601": "2024-08-26T07:11:28.501177Z",
"url": "https://files.pythonhosted.org/packages/4a/30/2d16b96d5632f0513bb654655c2b71526dd039634347bf8247be39e9e8b1/databend_sqlalchemy-0.4.7-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "680f547ec6375ee9a9b1584aed374854466fdf9d10de5f57fc5ca05e611ada7c",
"md5": "8c1675dc428c50ff5d90902c28b0f400",
"sha256": "020a4ac840389e695f9d959b96902a82e90491fd1d146311c8532b1c4cd67128"
},
"downloads": -1,
"filename": "databend_sqlalchemy-0.4.7.tar.gz",
"has_sig": false,
"md5_digest": "8c1675dc428c50ff5d90902c28b0f400",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 28237,
"upload_time": "2024-08-26T07:11:29",
"upload_time_iso_8601": "2024-08-26T07:11:29.866083Z",
"url": "https://files.pythonhosted.org/packages/68/0f/547ec6375ee9a9b1584aed374854466fdf9d10de5f57fc5ca05e611ada7c/databend_sqlalchemy-0.4.7.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-26 07:11:29",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "datafuselabs",
"github_project": "databend-sqlalchemy",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "databend-sqlalchemy"
}