forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterface.py
133 lines (94 loc) · 4.24 KB
/
interface.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
class Table:
def __init__(self, name, client):
self.__fullname = name
self.__database, self.__name = name.split('.')
self.__client = client
@property
def row_count(self):
count = self.__client.execute(f'SELECT COUNT(*) FROM {self.fullname}')[0][0]
return count
@property
def name(self):
return self.__name
@property
def database_name(self):
return self.__database
@property
def fullname(self):
return self.__fullname
@property
def exists(self):
tables = self.__client.execute(f'SELECT name FROM system.tables WHERE database == \'{self.database_name}\' and name == \'{self.name}\';')
return len(tables) == 1
def create(self):
assert not self.exists, f'Table {self.fullname} already exists'
self.__client.execute(f'CREATE TABLE {self.fullname};')
def rename_table(self, new_name):
assert self.exists, f'Table {self.fullname} does not exists'
self.__client.execute(f'RENAME TABLE {self.fullname} TO {new_name};')
self.__fullname = new_name
self.__name, self.__database = self.__fullname.split('.')
def safe_rename_table(self, new_name):
assert self.exists, f'Table {self.fullname} does not exists'
self.__client.execute(f'CREATE TABLE {new_name} AS {self.fullname};')
self.__client.execute(f'INSERT INTO {new_name} SELECT * FROM {self.fullname};')
self.drop()
self.__fullname = new_name
self.__name, self.__database = self.__fullname.split('.')
def rename_database(self, new_database):
self.rename_table(f'{new_database}.{self.name}')
def safe_rename_database(self, new_database):
self.safe_rename_table(f'{new_database}.{self.name}')
def drop(self):
assert self.exists, f'Table {self.fullname} does not exists'
self.__client.execute(f'DROP TABLE {self.fullname};')
class Database:
def __init__(self, name, client):
self.__name = name
self.__client = client
@property
def name(self):
return self.__name
@property
def exists(self):
dbs = self.__client.execute(f'SELECT name FROM system.databases WHERE name == \'{self.name}\';')
return len(dbs) == 1
@property
def engine(self):
assert self.exists, f'Database {self.name} does not exists'
dbs = self.__client.execute(f'SELECT engine FROM system.databases WHERE name == \'{self.name}\';')
return dbs[0][0]
@property
def tables(self):
assert self.exists, f'Database {self.name} does not exists'
tables = self.__client.execute(f'SELECT name FROM system.tables WHERE database == \'{self.name}\';')
tables = map(lambda row: row[0], tables)
return [self.__construct_table(name) for name in tables]
def __construct_table(self, table_name):
return Table(f'{self.name}.{table_name}', self.__client)
def get_table(self, table_name):
assert self.exists, f'Database {self.name} does not exists'
tables = self.__client.execute(f'SELECT name FROM system.tables WHERE database == \'{self.name}\' and name == \'{table_name}\';')
assert len(tables) == 1, f'Table {table_name} does not exists'
return self.__construct_table(table_name)
def create(self, engine = 'Ordinary'):
assert not self.exists, f'Database {self.name} already exists'
self.__client.execute(f'CREATE DATABASE {self.name} ENGINE={engine};')
def rename(self, new_name):
assert self.exists, f'Database {self.name} does not exists'
self.__client.execute(f'RENAME DATABASE {self.name} TO {new_name};')
self.__name = new_name
def create_table(self, table_name):
assert self.exists, f'Database {self.name} does not exists'
table = self.__construct_table(table_name)
table.create()
return table
def move_tables(self, tables, safe_rename):
for table in tables:
if safe_rename:
table.safe_rename_database(self.name)
else:
table.rename_database(self.name)
def drop(self):
assert self.exists, f'Database {self.name} does not exists'
self.__client.execute(f'DROP DATABASE {self.name};')