Between the equivalent of a query in an App Engine datastore?

I have a model containing ranges of IP addresses similar to this:

class Country(db.Model):
  begin_ipnum = db.IntegerProperty()
  end_ipnum = db.IntegerProperty()

In the SQL database, I could find rows containing IP in a specific range, for example:

SELECT * FROM Country WHERE ipnum BETWEEN begin_ipnum AND end_ipnum

or that:

SELECT * FROM Country WHERE begin_ipnum < ipnum AND end_ipnum > ipnum

Unfortunately, GQL allows only inequality filters by one property and does not support syntax BETWEEN. How can I get around this and build a query equivalent for them in App Engine?

Also, can it ListPropertybe "live" or should it be computed when creating the record?

question updated with first strike to resolve:

So, based on David's answer below and articles such as:

http://appengine-cookbook.appspot.com/recipe/custom-model-properties-are-cute/

I am trying to add a custom field to my model as follows:

class IpRangeProperty(db.Property):
  def __init__(self, begin=None, end=None, **kwargs):
    if not isinstance(begin, db.IntegerProperty) or not isinstance(end, db.IntegerProperty):
        raise TypeError('Begin and End must be Integers.')
    self.begin = begin
    self.end = end
    super(IpRangeProperty, self).__init__(self.begin, self.end, **kwargs)

  def get_value_for_datastore(self, model_instance):
    begin = self.begin.get_value_for_datastore(model_instance)
    end = self.end.get_value_for_datastore(model_instance)
    if begin is not None and end is not None:
      return range(begin, end)

class Country(db.Model):
  begin_ipnum = db.IntegerProperty()
  end_ipnum = db.IntegerProperty()
  ip_range = IpRangeProperty(begin=begin_ipnum, end=end_ipnum)

, , ListProperty, :

q = Country.gql('WHERE ip_range = :1', my_num_ipaddress)

, , :

...
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/db/__init__.py", line 619, in _attr_name
return '_' + self.name
TypeError: cannot concatenate 'str' and 'IntegerProperty' objects

attr_name self.name, , , . ?

+5
3

: . , , , : . , . , , . :

class M(db.Model):
    r = db.ListProperty(int)

# create an instance of M which has a range from `begin` to `end` (inclusive)
M(r=range(begin, end+1)).put()

# query to find instances of M which contain a value `v`
q = M.gql('WHERE r = :1', v)

( - - (. issue 798). , , , , , db.ListProperty. , , ( , IP- ). , , - v (.. ), - , , v , , , v , .

, :

class M(db.Model):
    r = db.ListProperty(int)

# create an instance of M which has a rnage from `begin` to `end` (inclusive)
M(r=[begin, end]).put()

# query to find instances of M which contain a value `v`
q = M.gql('WHERE r >= :1 AND r <= :1', v)
+2

, , , . CIDR IP .

from google.appengine.ext import db    
class Country(db.Model):
    subnets = db.StringListProperty()
    country_code = db.StringProperty()

c = Country()
c.subnets = ['1.2.3.0/24', '1.2.0.0/16', '1.3.4.0/24']
c.country_code = 'US'
c.put()

c = Country()
c.subnets = ['2.2.3.0/24', '2.2.0.0/16', '2.3.4.0/24']
c.country_code = 'CA'
c.put()

# Search for 1.2.4.5 starting with most specific block and then expanding until found    
result = Country.all().filter('subnets =', '1.2.4.5/32').fetch(1)
result = Country.all().filter('subnets =', '1.2.4.4/31').fetch(1)
result = Country.all().filter('subnets =', '1.2.4.4/30').fetch(1)
result = Country.all().filter('subnets =', '1.2.4.0/29').fetch(1)
# ... repeat until found
# optimize by starting with the largest routing prefix actually found in your data (probably not 32)
+2

, ( 130 , , MaxMind Geo Country). , . Google:

-, , , , . , [1, 2] x, WHERE x > 1 AND x < 2. x, , .

, listproperty, .

!

0

All Articles