10
10
from .base import SKIP , CompileError , DbPath , Schema , args_as_tuple
11
11
12
12
13
-
14
13
class ExprNode (Compilable ):
15
14
type : Any = None
16
15
@@ -129,7 +128,7 @@ def __getitem__(self, column):
129
128
def count (self ):
130
129
return Select (self , [Count ()])
131
130
132
- def union (self , other : ' ITable' ):
131
+ def union (self , other : " ITable" ):
133
132
return Union (self , other )
134
133
135
134
@@ -172,11 +171,13 @@ def compile(self, c: Compiler) -> str:
172
171
args = ", " .join (c .compile (e ) for e in self .args )
173
172
return f"{ self .name } ({ args } )"
174
173
174
+
175
175
def _expr_type (e : Expr ):
176
176
if isinstance (e , ExprNode ):
177
177
return e .type
178
178
return type (e )
179
179
180
+
180
181
@dataclass
181
182
class CaseWhen (ExprNode ):
182
183
cases : Sequence [Tuple [Expr , Expr ]]
@@ -190,12 +191,12 @@ def compile(self, c: Compiler) -> str:
190
191
191
192
@property
192
193
def type (self ):
193
- when_types = {_expr_type (w ) for _c ,w in self .cases }
194
+ when_types = {_expr_type (w ) for _c , w in self .cases }
194
195
if self .else_ :
195
196
when_types |= _expr_type (self .else_ )
196
197
if len (when_types ) > 1 :
197
198
raise RuntimeError (f"Non-matching types in when: { when_types } " )
198
- t , = when_types
199
+ ( t ,) = when_types
199
200
return t
200
201
201
202
@@ -252,6 +253,7 @@ def compile(self, c: Compiler) -> str:
252
253
a , b = self .args
253
254
return f"({ c .compile (a )} { self .op } { c .compile (b )} )"
254
255
256
+
255
257
class BinBoolOp (BinOp ):
256
258
type = bool
257
259
@@ -334,8 +336,8 @@ def source_table(self):
334
336
335
337
@property
336
338
def schema (self ):
337
- assert self .columns # TODO Implement SELECT *
338
- s = self .source_tables [0 ].schema # XXX
339
+ assert self .columns # TODO Implement SELECT *
340
+ s = self .source_tables [0 ].schema # XXX
339
341
return type (s )({c .name : c .type for c in self .columns })
340
342
341
343
def on (self , * exprs ):
@@ -390,6 +392,7 @@ class GroupBy(ITable):
390
392
def having (self ):
391
393
pass
392
394
395
+
393
396
@dataclass
394
397
class Union (ExprNode , ITable ):
395
398
table1 : ITable
@@ -441,7 +444,7 @@ def source_table(self):
441
444
return self
442
445
443
446
def compile (self , parent_c : Compiler ) -> str :
444
- c = parent_c .replace (in_select = True ) # .add_table_context(self.table)
447
+ c = parent_c .replace (in_select = True ) # .add_table_context(self.table)
445
448
446
449
columns = ", " .join (map (c .compile , self .columns )) if self .columns else "*"
447
450
select = f"SELECT { columns } "
@@ -547,7 +550,6 @@ def name(self):
547
550
return self .resolved .name
548
551
549
552
550
-
551
553
class This :
552
554
def __getattr__ (self , name ):
553
555
return _ResolveColumn (name )
@@ -593,9 +595,11 @@ def compile(self, c: Compiler) -> str:
593
595
594
596
# DDL
595
597
598
+
596
599
class Statement (Compilable ):
597
600
type = None
598
601
602
+
599
603
def to_sql_type (t ):
600
604
if isinstance (t , str ):
601
605
return t
@@ -612,18 +616,20 @@ class CreateTable(Statement):
612
616
if_not_exists : bool = False
613
617
614
618
def compile (self , c : Compiler ) -> str :
615
- schema = ', ' .join (f'{ k } { to_sql_type (v )} ' for k , v in self .path .schema .items ())
616
- ne = 'IF NOT EXISTS ' if self .if_not_exists else ''
617
- return f'CREATE TABLE { ne } { c .compile (self .path )} ({ schema } )'
619
+ schema = ", " .join (f"{ k } { to_sql_type (v )} " for k , v in self .path .schema .items ())
620
+ ne = "IF NOT EXISTS " if self .if_not_exists else ""
621
+ return f"CREATE TABLE { ne } { c .compile (self .path )} ({ schema } )"
622
+
618
623
619
624
@dataclass
620
625
class DropTable (Statement ):
621
626
path : TablePath
622
627
if_exists : bool = False
623
628
624
629
def compile (self , c : Compiler ) -> str :
625
- ie = 'IF EXISTS ' if self .if_exists else ''
626
- return f'DROP TABLE { ie } { c .compile (self .path )} '
630
+ ie = "IF EXISTS " if self .if_exists else ""
631
+ return f"DROP TABLE { ie } { c .compile (self .path )} "
632
+
627
633
628
634
@dataclass
629
635
class InsertToTable (Statement ):
@@ -632,4 +638,4 @@ class InsertToTable(Statement):
632
638
expr : Expr
633
639
634
640
def compile (self , c : Compiler ) -> str :
635
- return f' INSERT INTO { c .compile (self .path )} { c .compile (self .expr )} '
641
+ return f" INSERT INTO { c .compile (self .path )} { c .compile (self .expr )} "
0 commit comments