def db_type(self, connection): if connection.vendor == 'oracle': data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_") type = 'NUMBER(1)' if self.default != NOT_PROVIDED: type += (' DEFAULT %s ' % str(int(self.default))) type += ' CHECK (%(qn_column)s IN (0,1)) ' return (type % data) if connection.vendor == 'postgresql': data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_") type = 'smallint' if self.default != NOT_PROVIDED: type += (' DEFAULT %s ' % str(int(self.default))) type += ' CHECK (%(column)s IN (0,1)) ' return (type % data) if connection.vendor == 'mysql': type = 'bool' if self.default != NOT_PROVIDED: type += (' DEFAULT %s ' % str(int(self.default))) return type if connection.vendor == 'sqlite': type = 'BOOL' if self.default != NOT_PROVIDED: type += (' DEFAULT %s ' % str(int(self.default))) return type return super(ChemblBooleanField, self).db_type(connection)
def db_type(self, connection): default = '' if self.default != NOT_PROVIDED: default = (' DEFAULT %s ' % str(int(self.default))) if connection.vendor == 'oracle': data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_") field_type = 'NUMBER(1,0)' field_type += default field_type += ' CHECK (%(qn_column)s IN (0,1,-1)) ' return field_type % data if connection.vendor == 'postgresql': data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_") field_type = 'smallint' field_type += default field_type += ' CHECK ("%(column)s" in (0,1,-1))' return field_type % data if connection.vendor == 'mysql': field_type = 'int(1)' field_type += default return field_type if connection.vendor == 'sqlite': field_type = 'tinyint' field_type += default return field_type return super(ChemblNullBooleanField, self).db_type(connection)
def db_type(self, connection): if connection.vendor == 'oracle': data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_") type = 'NUMBER(%(max_digits)s, %(decimal_places)s) CHECK (%(qn_column)s >= 0)' return (type % data) if connection.vendor == 'postgresql': data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_") type = 'numeric(%(max_digits)s, %(decimal_places)s) CHECK ("%(column)s" >= 0)' return (type % data) return super(ChemblPositiveDecimalField, self).db_type(connection)
def db_type(self, connection): default = '' choices = '' if self.default != NOT_PROVIDED: default = (' DEFAULT %s ' % str(int(self.default))) if connection.vendor == 'oracle': data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_") type = 'NUMBER(%s,0)' % (self.length) type += default if self.choices: choices = ' AND %(qn_column)s IN (' + ', '.join( map(lambda x: str(x[0]), self.choices)) + ' ) ' type += ' CHECK (%(qn_column)s >= 0' + choices + ') ' return (type % data) if connection.vendor == 'postgresql': data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_") type = 'integer' if self.length <= 4: type = 'smallint' if self.length > 9: type = 'bigint' type += default if self.choices: choices = ' AND %(column)s IN (' + ', '.join( map(lambda x: str(x[0]), self.choices)) + ' ) ' type += ' CHECK (%(column)s >= 0' + choices + ') ' return (type % data) if connection.vendor == 'mysql': type = 'int(%s) UNSIGNED' % (self.length) type += default return type if connection.vendor == 'sqlite': type = 'integer' if self.length <= 4: type = 'smallint' if self.length > 9: type = 'bigint' type += ' unsigned ' type += default return type return super(ChemblPositiveIntegerField, self).db_type(connection)
def db_type(self): """ Returns the database column data type for this field, taking into account the DATABASE_ENGINE setting. """ # The default implementation of this method looks at the # backend-specific DATA_TYPES dictionary, looking up the field by its # "internal type". # # A Field class can implement the get_internal_type() method to specify # which *preexisting* Django Field class it's most similar to -- i.e., # an XMLField is represented by a TEXT column type, which is the same # as the TextField Django field type, which means XMLField's # get_internal_type() returns 'TextField'. # # But the limitation of the get_internal_type() / data_types approach # is that it cannot handle database column types that aren't already # mapped to one of the built-in Django field types. In this case, you # can implement db_type() instead of get_internal_type() to specify # exactly which wacky database column type you want to use. data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_") try: return connection.creation.data_types[ self.get_internal_type()] % data except KeyError: return None
def db_type(connection, primary_key=False): c = connection cengine = c.settings_dict['ENGINE'] #print("\n------------------\n[share] cengine = %s" % cengine) cengine_id = -1 if cengine in psql_engines: cengine_id = psql_engines.index(cengine) if cengine_id > -1: if primary_key: return 'bigserial' else: return 'bigint' data = DictWrapper(self.__dict__, c.ops.quote_name, "qn_") #print("\n------------------\n[share] data = %s" % data) if cengine_id == 0: # postgresql try: return c.creation.data_types[self.get_internal_type()] % data except KeyError: return None elif cengine_id == 1: # postGIS try: #print("\n------------------\n[share] c.creation.connection = %s" % c.creation.connection) return c.creation.connection.data_types[ self.get_internal_type()] % data except KeyError: return None return None
def related_db_type(self, connection): data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_") try: return connection.creation.data_types['RelatedAutoField'] % data except KeyError: return IntegerField().db_type(connection=connection)
def test_dictwrapper(self): def f(x): return "*%s" % x d = DictWrapper({"a": "a"}, f, "xx_") self.assertEqual("Normal: %(a)s. Modified: %(xx_a)s" % d, "Normal: a. Modified: *a")
def db_type_suffix(self, connection): data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_") try: return connection.creation.data_types_suffix[ self.get_internal_type()] % data except (KeyError, AttributeError): return False
def _db_type(self, field, internal_type): data = DictWrapper(field.__dict__, self.connection.ops.quote_name, "qn_") try: return self.connection.creation.data_types[internal_type] % data except KeyError: return None
def test_dictwrapper(self): def f(x): return "*%s" % x d = DictWrapper({'a': 'a'}, f, 'xx_') self.assertEqual("Normal: %(a)s. Modified: %(xx_a)s" % d, 'Normal: a. Modified: *a')
def db_type(self, connection): """ Returns the database column data type for this field, for the provided connection. """ # The default implementation of this method looks at the # backend-specific DATA_TYPES dictionary, looking up the field by its # "internal type". # # A Field class can implement the get_internal_type() method to specify # which *preexisting* Django Field class it's most similar to -- i.e., # a custom field might be represented by a TEXT column type, which is # the same as the TextField Django field type, which means the custom # field's get_internal_type() returns 'TextField'. # # But the limitation of the get_internal_type() / data_types approach # is that it cannot handle database column types that aren't already # mapped to one of the built-in Django field types. In this case, you # can implement db_type() instead of get_internal_type() to specify # exactly which wacky database column type you want to use. # from django.utils.datastructures import DictWrapper data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_") try: return (connection.creation.data_types[self.get_internal_type()] % data) except KeyError: return None
def db_type(self, connection): data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_") try: typ = connection.creation.data_types[self.get_internal_type()] % data if self.default is not models.NOT_PROVIDED and not callable(self.default): typ += ' default %s' % escape(self.default) return typ except KeyError: return None
def db_type(self, connection): engine = connection.settings_dict['ENGINE'] # Use the native 'inet' type for Postgres. if 'postgres' in engine: return 'inet' # Or 'varbinary' for everyone else. data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_") return 'varbinary(%(max_length)s)' % data
def db_type(self, connection): """Returns the correct field type for a given database vendor.""" # Use 'bytea' type for PostgreSQL. if connection.vendor == "postgresql": return "bytea" # Or 'varbinary' for everyone else. max_length = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_") return "varbinary(%(max_length)s)" % max_length
def db_type(self, connection): """Returns the correct field type for a given database engine.""" engine = connection.settings_dict["ENGINE"] # Use 'bytea' type for Postgres. if "postgres" in engine: return super().db_type(connection) # Or 'varbinary' for everyone else. max_length = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_") return "varbinary(%(max_length)s)" % max_length
def db_type(self, connection): default = '' if self.default != NOT_PROVIDED: default = (' DEFAULT %s ' % str(int(self.default))) if connection.vendor == 'oracle': data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_") field_type = 'NUMBER(%s,0)' % self.length field_type += default if self.choices: choices = ', '.join([str(x[0]) for x in self.choices]) field_type += ' CHECK (%(qn_column)s IN (' + choices + ')) ' return field_type % data if connection.vendor == 'postgresql': data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_") field_type = 'integer' if self.length <= 4: field_type = 'smallint' if self.length > 9: field_type = 'bigint' field_type += default if self.choices: choices = ', '.join([str(x[0]) for x in self.choices]) field_type += ' CHECK (%(column)s IN (' + choices + ')) ' return field_type % data if connection.vendor == 'mysql': field_type = 'int(%s)' % self.length return field_type + default if connection.vendor == 'sqlite': field_type = 'integer' if self.length <= 4: field_type = 'smallint' if self.length > 9: field_type = 'bigint' return field_type + default return super(ChemblIntegerField, self).db_type(connection)
def db_type(self, connection): data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_") default = '' if self.default != NOT_PROVIDED and not self.novalidate_default: if connection.vendor != 'postgresql': default += (" DEFAULT '%s' " % str(self.default)) if connection.vendor == 'postgresql': if self.max_length >= 2712: self.db_index = False self._unique = False type = 'varchar(%(max_length)s)' type += default if self.choices and not self.novalidate: choices = ', '.join( map(lambda x: "'%s'" % x[0].replace("'", "''"), self.choices)) #type += ' CHECK (%(column)s IN (' + choices + ')) ' return (type % data) if connection.vendor == 'mysql': if self.max_length > 767: self.db_index = False self._unique = False type = 'varchar(%(max_length)s)' type += default return (type % data) if connection.vendor == 'sqlite': type = 'varchar(%(max_length)s)' type += default return (type % data) if connection.vendor == 'oracle': type = 'VARCHAR2(%(max_length)s BYTE)' type += default if self.choices and not self.novalidate: choices = ', '.join( map(lambda x: "'%s'" % x[0].replace("'", "''"), self.choices)) type += ' CHECK (%(qn_column)s IN (' + choices + ')) ' return (type % data) return super(ChemblCharField, self).db_type(connection)
def db_type(self, connection): data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_") return 'char(%(max_length)s)' % data
def db_type_parameters(self, connection): return DictWrapper(self.__dict__, connection.ops.quote_name, 'qn_')
def db_type(self, connection): data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_") try: return connection.creation.data_types['CharField'] % data except KeyError: return None