custom postgres adapter

19 views
Skip to first unread message

Massimiliano

unread,
Jun 22, 2016, 4:36:31 AM6/22/16
to web2py-users
Hi,

I'm working on legacy databases where boolean are defined as smallint.

The following adapter doesn't work anymore. Could you give me some hint on how can I write a new one for the "new" pydal.
Thank you

The adapter that was working till now:

# -*- coding: utf-8 -*-


from pydal.adapters import ADAPTERS, PostgreSQLAdapter

class BoolAsIntPostgreSQLAdapter(PostgreSQLAdapter):


   
# specify a diver to use
    drivers = ('psycopg2','pg8000')
    TRUE
= -1
    FALSE
= 0


    types
= {
       
'boolean': 'smallint',
       
'string': 'VARCHAR(%(length)s)',
       
'text': 'TEXT',
       
'json': 'TEXT',
       
'password': 'VARCHAR(%(length)s)',
       
'blob': 'BYTEA',
       
'upload': 'VARCHAR(%(length)s)',
       
'integer': 'INTEGER',
       
'bigint': 'BIGINT',
       
'float': 'FLOAT',
       
'double': 'FLOAT8',
       
'decimal': 'NUMERIC(%(precision)s,%(scale)s)',
       
'date': 'DATE',
       
'time': 'TIME',
       
'datetime': 'TIMESTAMP',
       
'id': 'SERIAL PRIMARY KEY',
       
'reference': 'INTEGER REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s',
       
'list:integer': 'TEXT',
       
'list:string': 'TEXT',
       
'list:reference': 'TEXT',
       
'geometry': 'GEOMETRY',
       
'geography': 'GEOGRAPHY',
       
'big-id': 'BIGSERIAL PRIMARY KEY',
       
'big-reference': 'BIGINT REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s',
       
'reference FK': ', CONSTRAINT  "FK_%(constraint_name)s" FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s',
       
'reference TFK': ' CONSTRAINT  "FK_%(foreign_table)s_PK" FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_table)s (%(foreign_key)s) ON DELETE %(on_delete_action)s',
   
}
   
def parse_boolean(self, value, field_type):
       
return value == self.TRUE or False
   


ADAPTERS
.update( {
   
'pgsql': BoolAsIntPostgreSQLAdapter
})



Reply all
Reply to author
Forward
0 new messages