
from . import connector
from ..base import (
    ClickHouseDialect, ClickHouseExecutionContextBase, ClickHouseCompiler,
)

# Export connector version
VERSION = (0, 0, 2, None)


class ClickHouseExecutionContext(ClickHouseExecutionContextBase):
    def pre_exec(self):
        # Always do executemany on INSERT with VALUES clause.
        if self.isinsert and self.compiled.statement.select is None:
            self.executemany = True


class ClickHouseNativeCompiler(ClickHouseCompiler):

    def visit_insert(self, insert_stmt, asfrom=False, **kw):
        rv = super(ClickHouseNativeCompiler, self).visit_insert(
            insert_stmt, asfrom=asfrom, **kw)

        pos = rv.rfind('VALUES (')
        # Remove (%s)-templates from VALUES clause if exists.
        # ClickHouse server since version 19.3.3 parse query after VALUES and
        # allows inplace parameters.
        # Example: INSERT INTO test (x) VALUES (1), (2).
        if pos != -1:
            rv = rv[:pos + 6]
        return rv


class ClickHouseDialect_native(ClickHouseDialect):
    driver = 'native'
    execution_ctx_cls = ClickHouseExecutionContext
    statement_compiler = ClickHouseNativeCompiler

    @classmethod
    def dbapi(cls):
        return connector

    def create_connect_args(self, url):
        url.drivername = 'clickhouse'

        return (str(url), ), {}

    def _execute(self, connection, sql):
        return connection.execute(sql)

    def _query_server_version_string(self, connection):
        return connection.scalar('select version()')


dialect = ClickHouseDialect_native
