site_cubicweb.py 1.48 KB
Newer Older
1
2
3
4
import json

from logilab.database import get_db_helper, FunctionDescr
from logilab.database.sqlgen import SQLExpression
5

6
from yams import register_base_type
7

8
from rql.utils import register_function
9
10


11
12
register_base_type('IntArray', ('size',))
register_base_type('FloatArray')
13

14
pghelper = get_db_helper('postgres')
15
sqlitehelper = get_db_helper('sqlite')
16

17
18
19
20
21
22
def convert_array(values):
    """converter for Geometry"""
    return SQLExpression('ARRAY[%s]' % ', '.join([str(x) for x in values]))

# Yams -> SQL type mapping
pghelper.TYPE_MAPPING['IntArray'] = 'integer[]'
23
pghelper.TYPE_MAPPING['FloatArray'] = 'double precision[]'
24
25
sqlitehelper.TYPE_MAPPING['IntArray'] = 'TEXT'
sqlitehelper.TYPE_MAPPING['FloatArray'] = 'TEXT'
26

27
28
29
30

# Python -> SQL conversion
pghelper.TYPE_CONVERTERS['IntArray'] = convert_array
pghelper.TYPE_CONVERTERS['FloatArray'] = convert_array
31

32
33
sqlitehelper.TYPE_CONVERTERS['IntArray'] = json.dumps
sqlitehelper.TYPE_CONVERTERS['FloatArray'] = json.dumps
34
35
sqlitehelper.PYTHON_CONVERTERS['IntArray'] = json.loads
sqlitehelper.PYTHON_CONVERTERS['FloatArray'] = json.loads
36
37


38
39
40
41
42
43
## from cubicweb.schema import CONSTRAINTS
## from cubes.postgis.schema import params
## CONSTRAINTS.update((v.__name__, v) for v in params.values())

from rql.utils import register_function

44
class ARRAY_GET(FunctionDescr):
45
46
47
48
49
    minargs = 2
    maxargs = 2
    supported_backends = ('postgres',)
    rtype = 'Float'

50
51
    def as_sql_postgres(self, args):
        return "%s[%s]" % (args[0], args[1])
52

53
54

register_function(ARRAY_GET)