|
| 1 | +title: sqlalchemy.databases mysql Example Code |
| 2 | +category: page |
| 3 | +slug: sqlalchemy-databases-mysql-examples |
| 4 | +sortorder: 500031000 |
| 5 | +toc: False |
| 6 | +sidebartitle: sqlalchemy.databases mysql |
| 7 | +meta: Python example code that shows how to use the mysql callable from the sqlalchemy.databases module of the SQLAlchemy project. |
| 8 | + |
| 9 | + |
| 10 | +`mysql` is a callable within the `sqlalchemy.databases` module of the SQLAlchemy project. |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +## Example 1 from PyHive |
| 15 | +[PyHive](https://github.com/dropbox/PyHive) |
| 16 | +([PyPI package information](https://pypi.org/project/PyHive/)) |
| 17 | +is a set of [DB-API](https://www.python.org/dev/peps/pep-0249/) |
| 18 | +and |
| 19 | +[SQLAlchemy](/sqlalchemy.html) |
| 20 | +interfaces that make it easier to use [Presto](https://prestodb.io/) |
| 21 | +and [Apache Hive](http://hive.apache.org/) with Python. |
| 22 | +[Dropbox's engineering team](https://www.dropbox.com/jobs/teams/engineering) |
| 23 | +created this code library, open sourced it and put it out under |
| 24 | +the [Apache 2.0 license](https://github.com/dropbox/PyHive/blob/master/LICENSE). |
| 25 | + |
| 26 | +[**PyHive / pyhive / sqlalchemy_presto.py**](https://github.com/dropbox/PyHive/blob/master/pyhive/./sqlalchemy_presto.py) |
| 27 | + |
| 28 | +```python |
| 29 | +# sqlalchemy_presto.py |
| 30 | + |
| 31 | +from __future__ import absolute_import |
| 32 | +from __future__ import unicode_literals |
| 33 | + |
| 34 | +import re |
| 35 | +from sqlalchemy import exc |
| 36 | +from sqlalchemy import types |
| 37 | +from sqlalchemy import util |
| 38 | +~~from sqlalchemy.databases import mysql |
| 39 | +from sqlalchemy.engine import default |
| 40 | +from sqlalchemy.sql import compiler |
| 41 | +from sqlalchemy.sql.compiler import SQLCompiler |
| 42 | + |
| 43 | +from pyhive import presto |
| 44 | +from pyhive.common import UniversalSet |
| 45 | + |
| 46 | + |
| 47 | +class PrestoIdentifierPreparer(compiler.IdentifierPreparer): |
| 48 | + reserved_words = UniversalSet() |
| 49 | + |
| 50 | + |
| 51 | +_type_map = { |
| 52 | + 'boolean': types.Boolean, |
| 53 | +~~ 'tinyint': mysql.MSTinyInteger, |
| 54 | + 'smallint': types.SmallInteger, |
| 55 | + 'integer': types.Integer, |
| 56 | + 'bigint': types.BigInteger, |
| 57 | + 'real': types.Float, |
| 58 | + 'double': types.Float, |
| 59 | + 'varchar': types.String, |
| 60 | + 'timestamp': types.TIMESTAMP, |
| 61 | + 'date': types.DATE, |
| 62 | + 'varbinary': types.VARBINARY, |
| 63 | +} |
| 64 | + |
| 65 | + |
| 66 | +class PrestoCompiler(SQLCompiler): |
| 67 | + def visit_char_length_func(self, fn, **kw): |
| 68 | + return 'length{}'.format(self.function_argspec(fn, **kw)) |
| 69 | + |
| 70 | + |
| 71 | +class PrestoTypeCompiler(compiler.GenericTypeCompiler): |
| 72 | + def visit_CLOB(self, type_, **kw): |
| 73 | + raise ValueError("Presto does not support the CLOB column type.") |
| 74 | + |
| 75 | + def visit_NCLOB(self, type_, **kw): |
| 76 | + raise ValueError("Presto does not support the NCLOB column type.") |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +## ... source file continues with no further mysql examples... |
| 81 | + |
| 82 | +``` |
| 83 | + |
0 commit comments