|
| 1 | +title: sqlalchemy.dialects.postgresql.base PGCompiler Example Code |
| 2 | +category: page |
| 3 | +slug: sqlalchemy-dialects-postgresql-base-pgcompiler-examples |
| 4 | +sortorder: 500031015 |
| 5 | +toc: False |
| 6 | +sidebartitle: sqlalchemy.dialects.postgresql.base PGCompiler |
| 7 | +meta: Example code for understanding how to use the PGCompiler class from the sqlalchemy.dialects.postgresql.base module of the SQLAlchemy project. |
| 8 | + |
| 9 | + |
| 10 | +PGCompiler is a class within the sqlalchemy.dialects.postgresql.base module of the SQLAlchemy project. |
| 11 | + |
| 12 | + |
| 13 | +## Example 1 from sqlalchemy-clickhouse |
| 14 | +[sqlalchemy-clickhouse](https://github.com/cloudflare/sqlalchemy-clickhouse) |
| 15 | +is a [SQLAlchemy Dialect](https://docs.sqlalchemy.org/en/13/dialects/) |
| 16 | +for communicating with the open source [ClickHouse](https://clickhouse.tech/) |
| 17 | +database management system. ClickHouse is column-oriented and therefore |
| 18 | +better for some use cases and worse for others compared to a traditional |
| 19 | +[relational database](/databases.html). |
| 20 | + |
| 21 | +The code for this project is open sourced under the |
| 22 | +[MIT license](https://github.com/cloudflare/sqlalchemy-clickhouse/blob/master/LICENSE.txt) |
| 23 | +while ClickHouse is provided as open source under the |
| 24 | +[Apache License 2.0](https://github.com/ClickHouse/ClickHouse/blob/master/LICENSE). |
| 25 | + |
| 26 | +[**sqlalchemy-clickhouse / base.py**](https://github.com/cloudflare/sqlalchemy-clickhouse/blob/master/././base.py) |
| 27 | + |
| 28 | +```python |
| 29 | +# base.py |
| 30 | + |
| 31 | +import re |
| 32 | + |
| 33 | +import sqlalchemy.types as sqltypes |
| 34 | +from sqlalchemy import exc as sa_exc |
| 35 | +from sqlalchemy import util as sa_util |
| 36 | +from sqlalchemy.engine import default, reflection |
| 37 | +from sqlalchemy.sql import compiler, expression |
| 38 | +from sqlalchemy.sql.elements import quoted_name |
| 39 | +~~from sqlalchemy.dialects.postgresql.base import PGCompiler, PGIdentifierPreparer |
| 40 | +from sqlalchemy.types import ( |
| 41 | + CHAR, DATE, DATETIME, INTEGER, SMALLINT, BIGINT, DECIMAL, TIME, |
| 42 | + TIMESTAMP, VARCHAR, BINARY, BOOLEAN, FLOAT, REAL) |
| 43 | + |
| 44 | +VERSION = (0, 1, 0, None) |
| 45 | + |
| 46 | +colspecs = {} |
| 47 | + |
| 48 | +class ARRAY(sqltypes.TypeEngine): |
| 49 | + __visit_name__ = 'ARRAY' |
| 50 | + |
| 51 | +ischema_names = { |
| 52 | + 'Int64': INTEGER, |
| 53 | + 'Int32': INTEGER, |
| 54 | + 'Int16': INTEGER, |
| 55 | + 'Int8': INTEGER, |
| 56 | + 'UInt64': INTEGER, |
| 57 | + 'UInt32': INTEGER, |
| 58 | + 'UInt16': INTEGER, |
| 59 | + 'UInt8': INTEGER, |
| 60 | + 'Date': DATE, |
| 61 | + 'DateTime': DATETIME, |
| 62 | + 'Float64': FLOAT, |
| 63 | + 'Float32': FLOAT, |
| 64 | + 'String': VARCHAR, |
| 65 | + 'FixedString': VARCHAR, |
| 66 | + 'Enum': VARCHAR, |
| 67 | + 'Enum8': VARCHAR, |
| 68 | + 'Enum16': VARCHAR, |
| 69 | + 'Array': ARRAY, |
| 70 | +} |
| 71 | + |
| 72 | +class ClickHouseIdentifierPreparer(PGIdentifierPreparer): |
| 73 | + def quote_identifier(self, value): |
| 74 | + return self._escape_identifier(value) |
| 75 | + def quote(self, ident, force=None): |
| 76 | + if self._requires_quotes(ident): |
| 77 | + return '"{}"'.format(ident) |
| 78 | + return ident |
| 79 | + |
| 80 | +~~class ClickHouseCompiler(PGCompiler): |
| 81 | + def visit_count_func(self, fn, **kw): |
| 82 | + return 'count{0}'.format(self.process(fn.clause_expr, **kw)) |
| 83 | + |
| 84 | + def visit_random_func(self, fn, **kw): |
| 85 | + return 'rand()' |
| 86 | + |
| 87 | + def visit_now_func(self, fn, **kw): |
| 88 | + return 'now()' |
| 89 | + |
| 90 | + def visit_current_date_func(self, fn, **kw): |
| 91 | + return 'today()' |
| 92 | + |
| 93 | + def visit_true(self, element, **kw): |
| 94 | + return '1' |
| 95 | + |
| 96 | + def visit_false(self, element, **kw): |
| 97 | + return '0' |
| 98 | + |
| 99 | + def visit_cast(self, cast, **kwargs): |
| 100 | + if self.dialect.supports_cast: |
| 101 | + return super(ClickHouseCompiler, self).visit_cast(cast, **kwargs) |
| 102 | + else: |
| 103 | + return self.process(cast.clause, **kwargs) |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | +## ... source file continues with no further PGCompiler examples... |
| 108 | + |
| 109 | +``` |
| 110 | + |
0 commit comments