summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/dialects/postgresql/base.py
diff options
context:
space:
mode:
authorAlexey Terentev <alexey@terentyev.su>2014-05-13 13:10:39 +0400
committerAlexey Terentev <alexey@terentyev.su>2014-05-13 15:48:29 +0400
commit57b6da9d402f164605c7bfe270654725cb604f1f (patch)
treee913d44cb0838bc8bdb84092ddec50f73c2573bd /lib/sqlalchemy/dialects/postgresql/base.py
parent1abd53a3556b9593d9eba868d69c13bae3c3a7ab (diff)
downloadsqlalchemy-57b6da9d402f164605c7bfe270654725cb604f1f.tar.gz
zero_indexes-param-for-postgresql-ARRAY-type
Diffstat (limited to 'lib/sqlalchemy/dialects/postgresql/base.py')
-rw-r--r--lib/sqlalchemy/dialects/postgresql/base.py17
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):