diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-05-16 13:01:19 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-05-16 13:01:19 -0400 |
commit | acb13668c2988179dbee68f599492a533e817d0e (patch) | |
tree | deaa9f304088a9abd9f1a1afbd37fc0de5d6e300 /lib/sqlalchemy/dialects/postgresql/base.py | |
parent | 0f925ad430fd482d77520bb5de62949f9c75ac65 (diff) | |
parent | 57b6da9d402f164605c7bfe270654725cb604f1f (diff) | |
download | sqlalchemy-acb13668c2988179dbee68f599492a533e817d0e.tar.gz |
Merge branch 'zero_indexes-param-for-postgresql-ARRAY-type' of https://bitbucket.org/LevonXXL/sqlalchemy/overview into t
Diffstat (limited to 'lib/sqlalchemy/dialects/postgresql/base.py')
-rw-r--r-- | lib/sqlalchemy/dialects/postgresql/base.py | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index f69a6e010..50918c31e 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -702,11 +702,20 @@ class ARRAY(sqltypes.Concatenable, sqltypes.TypeEngine): """Define comparison operations for :class:`.ARRAY`.""" def __getitem__(self, index): + shift_indexes = 1 if self.expr.type.zero_indexes else 0 if isinstance(index, slice): + if shift_indexes: + index = slice( + index.start + shift_indexes, + index.stop + shift_indexes, + index.step + ) index = _Slice(index, self) return_type = self.type else: + index += shift_indexes return_type = self.type.item_type + return self._binary_operate(self.expr, operators.getitem, index, result_type=return_type) @@ -797,7 +806,8 @@ class ARRAY(sqltypes.Concatenable, sqltypes.TypeEngine): comparator_factory = Comparator - def __init__(self, item_type, as_tuple=False, dimensions=None): + def __init__(self, item_type, as_tuple=False, dimensions=None, + zero_indexes=False): """Construct an ARRAY. E.g.:: @@ -824,6 +834,10 @@ class ARRAY(sqltypes.Concatenable, sqltypes.TypeEngine): meaning they can store any number of dimensions no matter how they were declared. + :param zero_indexes=False: True allow to work with field like with + python's list - use indexes starts with zero, but not starts with + 1 like in ARRAY + """ if isinstance(item_type, ARRAY): raise ValueError("Do not nest ARRAY types; ARRAY(basetype) " @@ -833,6 +847,7 @@ class ARRAY(sqltypes.Concatenable, sqltypes.TypeEngine): self.item_type = item_type self.as_tuple = as_tuple self.dimensions = dimensions + self.zero_indexes = zero_indexes @property def python_type(self): |