1
1
# MySQL Connector/Python - MySQL driver written in Python.
2
- # Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
2
+ # Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
3
3
4
4
# MySQL Connector/Python is licensed under the terms of the GPLv2
5
5
# <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
23
23
24
24
"""Implementation of Statements."""
25
25
26
+ import copy
26
27
import json
27
28
import re
28
29
@@ -580,8 +581,7 @@ def get_sql(self):
580
581
self ._limit_offset ) if self ._has_limit else ""
581
582
582
583
stmt = ("SELECT {select} FROM {schema}.{table}{where}{group}{having}"
583
- "{order}{limit}" .format (
584
- select = getattr (self , '_projection_str' , "*" ),
584
+ "{order}{limit}" .format (select = self ._projection_str or "*" ,
585
585
schema = self .schema .name , table = self .target .name , limit = limit ,
586
586
where = where , group = group_by , having = having , order = order_by ))
587
587
@@ -852,7 +852,11 @@ def defined_as(self, statement):
852
852
Returns:
853
853
mysqlx.CreateViewStatement: CreateViewStatement object.
854
854
"""
855
- self ._defined_as = statement
855
+ if not isinstance (statement , SelectStatement ) and \
856
+ not isinstance (statement , STRING_TYPES ):
857
+ raise ProgrammingError ("The statement must be an instance of "
858
+ "SelectStatement or a SQL string." )
859
+ self ._defined_as = copy .copy (statement ) # Prevent modifications
856
860
return self
857
861
858
862
def with_check_option (self , check_option ):
@@ -878,6 +882,9 @@ def execute(self):
878
882
if self ._definer else ""
879
883
columns = " ({0})" .format (", " .join (self ._columns )) \
880
884
if self ._columns else ""
885
+ defined_as = self ._defined_as .get_sql () \
886
+ if isinstance (self ._defined_as , SelectStatement ) \
887
+ else self ._defined_as
881
888
view_name = quote_multipart_identifier ((self ._schema .name , self ._name ))
882
889
check_option = " WITH {0} CHECK OPTION" .format (self ._check_option ) \
883
890
if self ._check_option else ""
@@ -887,8 +894,7 @@ def execute(self):
887
894
"" .format (replace = replace , algorithm = self ._algorithm ,
888
895
definer = definer , security = self ._security ,
889
896
view_name = view_name , columns = columns ,
890
- defined_as = self ._defined_as ,
891
- check_option = check_option ))
897
+ defined_as = defined_as , check_option = check_option ))
892
898
893
899
self ._connection .execute_nonquery ("sql" , sql )
894
900
return self ._view
@@ -913,6 +919,9 @@ def execute(self):
913
919
if self ._definer else ""
914
920
columns = " ({0})" .format (", " .join (self ._columns )) \
915
921
if self ._columns else ""
922
+ defined_as = self ._defined_as .get_sql () \
923
+ if isinstance (self ._defined_as , SelectStatement ) \
924
+ else self ._defined_as
916
925
view_name = quote_multipart_identifier ((self ._schema .name , self ._name ))
917
926
check_option = " WITH {0} CHECK OPTION" .format (self ._check_option ) \
918
927
if self ._check_option else ""
@@ -921,7 +930,7 @@ def execute(self):
921
930
"AS {defined_as}{check_option}"
922
931
"" .format (algorithm = self ._algorithm , definer = definer ,
923
932
security = self ._security , view_name = view_name ,
924
- columns = columns , defined_as = self . _defined_as ,
933
+ columns = columns , defined_as = defined_as ,
925
934
check_option = check_option ))
926
935
927
936
self ._connection .execute_nonquery ("sql" , sql )
0 commit comments