thirdparty/google_appengine/google/appengine/ext/gql/__init__.py
changeset 3031 7678f72140e6
parent 2864 2e0b0af889be
equal deleted inserted replaced
3030:09cae668b536 3031:7678f72140e6
   102 
   102 
   103   - Positional parameters
   103   - Positional parameters
   104   Execute('SELECT * FROM Story WHERE Author = :1 AND Date > :2')
   104   Execute('SELECT * FROM Story WHERE Author = :1 AND Date > :2')
   105   - Named parameters
   105   - Named parameters
   106   Execute('SELECT * FROM Story WHERE Author = :author AND Date > :date')
   106   Execute('SELECT * FROM Story WHERE Author = :author AND Date > :date')
   107   - Literals (numbers, and strings)
   107   - Literals (numbers, strings, booleans, and NULL)
   108   Execute('SELECT * FROM Story WHERE Author = \'James\'')
   108   Execute('SELECT * FROM Story WHERE Author = \'James\'')
   109 
   109 
   110   Users are also given the option of doing type conversions to other datastore
   110   Users are also given the option of doing type conversions to other datastore
   111   types (e.g. db.Email, db.GeoPt). The language provides a conversion function
   111   types (e.g. db.Email, db.GeoPt). The language provides a conversion function
   112   which allows the caller to express conversions of both literals and
   112   which allows the caller to express conversions of both literals and
   341     return datastore_types.GeoPt(*values)
   341     return datastore_types.GeoPt(*values)
   342 
   342 
   343   def __CastUser(self, values):
   343   def __CastUser(self, values):
   344     """Cast to User() class using the email address in values[0]."""
   344     """Cast to User() class using the email address in values[0]."""
   345     if len(values) != 1:
   345     if len(values) != 1:
   346       self.__CastError(values, 'user', 'requires one and only one value')
   346       self.__CastError('user', values, 'requires one and only one value')
       
   347     elif values[0] is None:
       
   348       self.__CastError('user', values, 'must be non-null')
   347     else:
   349     else:
   348       return users.User(email=values[0], _auth_domain=self.__auth_domain)
   350       return users.User(email=values[0], _auth_domain=self.__auth_domain)
   349 
   351 
   350   def __EncodeIfNeeded(self, value):
   352   def __EncodeIfNeeded(self, value):
   351     """Simple helper function to create an str from possibly unicode strings.
   353     """Simple helper function to create an str from possibly unicode strings.
   418         self.__CastError('TIME', values,
   420         self.__CastError('TIME', values,
   419                          'Single input value not a string or integer hour')
   421                          'Single input value not a string or integer hour')
   420     elif len(values) <= 4:
   422     elif len(values) <= 4:
   421       time_tuple = (1970, 1, 1) + tuple(values)
   423       time_tuple = (1970, 1, 1) + tuple(values)
   422     else:
   424     else:
   423       self.__CastError('TIME', values, err)
   425       self.__CastError('TIME', values,
       
   426                        'function takes 1 to 4 integers or 1 string')
   424 
   427 
   425     try:
   428     try:
   426       return datetime.datetime(*time_tuple)
   429       return datetime.datetime(*time_tuple)
   427     except ValueError, err:
   430     except ValueError, err:
   428       self.__CastError('TIME', values, err)
   431       self.__CastError('TIME', values, err)
   927     if identifier.lower() == 'ancestor':
   930     if identifier.lower() == 'ancestor':
   928       self.__has_ancestor = True
   931       self.__has_ancestor = True
   929       filter_rule = (self.__ANCESTOR, 'is')
   932       filter_rule = (self.__ANCESTOR, 'is')
   930       assert condition.lower() == 'is'
   933       assert condition.lower() == 'is'
   931 
   934 
   932     if condition.lower() != 'in' and operator == 'list':
   935     if operator == 'list' and condition.lower() != 'in':
   933       self.__Error('Only IN can process a list of values')
   936       self.__Error('Only IN can process a list of values')
   934 
   937 
   935     self.__filters.setdefault(filter_rule, []).append((operator, parameters))
   938     self.__filters.setdefault(filter_rule, []).append((operator, parameters))
   936     return True
   939     return True
   937 
   940 
  1006       elif self.__Accept('FALSE'):
  1009       elif self.__Accept('FALSE'):
  1007         literal = False
  1010         literal = False
  1008 
  1011 
  1009     if literal is not None:
  1012     if literal is not None:
  1010       return Literal(literal)
  1013       return Literal(literal)
       
  1014 
       
  1015     if self.__Accept('NULL'):
       
  1016       return Literal(None)
  1011     else:
  1017     else:
  1012       return None
  1018       return None
  1013 
  1019 
  1014   def __TypeCast(self):
  1020   def __TypeCast(self):
  1015     """Check if the next operation is a type-cast and return the cast if so.
  1021     """Check if the next operation is a type-cast and return the cast if so.