From c6c9f13e5114c5fd31f1b119d8f3230f7c1a9752 Mon Sep 17 00:00:00 2001 From: Itai Shirav Date: Thu, 30 Jun 2016 11:36:54 +0300 Subject: [PATCH] Support dashes and other special characters in database names and table names --- src/infi/clickhouse_orm/database.py | 8 ++++---- src/infi/clickhouse_orm/models.py | 4 ++-- tests/test_database.py | 8 ++++---- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/infi/clickhouse_orm/database.py b/src/infi/clickhouse_orm/database.py index b8c2e9f..2db3294 100644 --- a/src/infi/clickhouse_orm/database.py +++ b/src/infi/clickhouse_orm/database.py @@ -14,7 +14,7 @@ class Database(object): self.db_url = db_url self.username = username self.password = password - self._send('CREATE DATABASE IF NOT EXISTS ' + db_name) + self._send('CREATE DATABASE IF NOT EXISTS `%s`' % db_name) def create_table(self, model_class): # TODO check that model has an engine @@ -24,7 +24,7 @@ class Database(object): self._send(model_class.drop_table_sql(self.db_name)) def drop_database(self): - self._send('DROP DATABASE ' + self.db_name) + self._send('DROP DATABASE `%s`' % self.db_name) def insert(self, model_instances): i = iter(model_instances) @@ -34,7 +34,7 @@ class Database(object): return # model_instances is empty model_class = first_instance.__class__ def gen(): - yield 'INSERT INTO %s.%s FORMAT TabSeparated\n' % (self.db_name, model_class.table_name()) + yield 'INSERT INTO `%s`.`%s` FORMAT TabSeparated\n' % (self.db_name, model_class.table_name()) yield first_instance.to_tsv() yield '\n' for instance in i: @@ -43,7 +43,7 @@ class Database(object): self._send(gen()) def count(self, model_class, conditions=None): - query = 'SELECT count() FROM %s.%s' % (self.db_name, model_class.table_name()) + query = 'SELECT count() FROM `%s`.`%s`' % (self.db_name, model_class.table_name()) if conditions: query += ' WHERE ' + conditions r = self._send(query) diff --git a/src/infi/clickhouse_orm/models.py b/src/infi/clickhouse_orm/models.py index ece5557..e71a8e9 100644 --- a/src/infi/clickhouse_orm/models.py +++ b/src/infi/clickhouse_orm/models.py @@ -92,7 +92,7 @@ class Model(object): ''' Returns the SQL command for creating a table for this model. ''' - parts = ['CREATE TABLE IF NOT EXISTS %s.%s (' % (db_name, cls.table_name())] + parts = ['CREATE TABLE IF NOT EXISTS `%s`.`%s` (' % (db_name, cls.table_name())] cols = [] for name, field in cls._fields: default = field.get_db_prep_value(field.default) @@ -107,7 +107,7 @@ class Model(object): ''' Returns the SQL command for deleting this model's table. ''' - return 'DROP TABLE IF EXISTS %s.%s' % (db_name, cls.table_name()) + return 'DROP TABLE IF EXISTS `%s`.`%s`' % (db_name, cls.table_name()) @classmethod def from_tsv(cls, line, field_names=None): diff --git a/tests/test_database.py b/tests/test_database.py index fa6b7cc..46cf3b2 100644 --- a/tests/test_database.py +++ b/tests/test_database.py @@ -9,7 +9,7 @@ from infi.clickhouse_orm.engines import * class DatabaseTestCase(unittest.TestCase): def setUp(self): - self.database = Database('test_db') + self.database = Database('test-db') self.database.create_table(Person) def tearDown(self): @@ -41,7 +41,7 @@ class DatabaseTestCase(unittest.TestCase): def test_select(self): self._insert_and_check(self._sample_data(), len(data)) - query = "SELECT * FROM test_db.person WHERE first_name = 'Whitney' ORDER BY last_name" + query = "SELECT * FROM `test-db`.person WHERE first_name = 'Whitney' ORDER BY last_name" results = list(self.database.select(query, Person)) self.assertEquals(len(results), 2) self.assertEquals(results[0].last_name, 'Durham') @@ -51,7 +51,7 @@ class DatabaseTestCase(unittest.TestCase): def test_select_partial_fields(self): self._insert_and_check(self._sample_data(), len(data)) - query = "SELECT first_name, last_name FROM test_db.person WHERE first_name = 'Whitney' ORDER BY last_name" + query = "SELECT first_name, last_name FROM `test-db`.person WHERE first_name = 'Whitney' ORDER BY last_name" results = list(self.database.select(query, Person)) self.assertEquals(len(results), 2) self.assertEquals(results[0].last_name, 'Durham') @@ -61,7 +61,7 @@ class DatabaseTestCase(unittest.TestCase): def test_select_ad_hoc_model(self): self._insert_and_check(self._sample_data(), len(data)) - query = "SELECT * FROM test_db.person WHERE first_name = 'Whitney' ORDER BY last_name" + query = "SELECT * FROM `test-db`.person WHERE first_name = 'Whitney' ORDER BY last_name" results = list(self.database.select(query)) self.assertEquals(len(results), 2) self.assertEquals(results[0].__class__.__name__, 'AdHocModel')