def _db_format(self, type, text): if type is None: raise TypeError("No type found for value: %s." % text) type = type.lower() if text == '' or text is None: raise TypeError("No value provided for element: %s." % type) if type in self.DB_NON_STRING_TYPES: #dmyung :: some additional input validation if self.DB_NUMERIC_TYPES.has_key(type): typefunc = self.DB_NUMERIC_TYPES[type] try: val = typefunc(text.strip()) return str(val) except: raise TypeError("Error validating type %s with value %s (is not %s)" % \ (type,text,str(typefunc)) ) elif type == "datetime" : text = string.replace(text,'T',' ') if is_configured_mysql(): # truncate microseconds index = text.rfind('.') if index != -1: text = text[0:index] return text.strip() else: return text.strip() else: return text.strip()
def testSaveFormData_2(self): """ Test types created and data saved. Currently only supported in MYSQL. """ cursor = connection.cursor() create_xsd_and_populate("2_types.xsd", "2_types.xml", self.domain) if is_configured_mysql(): cursor.execute("DESCRIBE schema_basicdomain_xml_types") row = cursor.fetchall() self.assertEquals(row[9][1],"varchar(255)") self.assertEquals(row[10][1],"int(11)") self.assertEquals(row[11][1],"int(11)") self.assertEquals(row[12][1],"decimal(5,2)") self.assertEquals(row[13][1],"double") self.assertEquals(row[14][1],"date") self.assertEquals(row[15][1],"time") self.assertEquals(row[16][1],"datetime") self.assertEquals(row[17][1],"tinyint(1)") self.assertEquals(row[18][1],"tinyint(1)") else: pass cursor.execute("SELECT * FROM schema_basicdomain_xml_types") row = cursor.fetchone() self.assertEquals(row[9],"userid0") self.assertEquals(row[10],111) self.assertEquals(row[11],222) if is_configured_realsql(): self.assertEquals(row[12],Decimal("3.20")) else: self.assertEquals( str(float(row[8])), "3.2" ) self.assertEquals(row[13],2002.09) if is_configured_realsql(): self.assertEquals(row[14],date(2002,9,24) ) self.assertEquals(row[15],time(12,24,48)) self.assertEquals(row[16],datetime(2007,12,31,23,59,59) ) else: self.assertEquals(row[14],"2002-09-24" ) self.assertEquals(row[15],"12:24:48") self.assertEquals(row[16],"2007-12-31 23:59:59" ) self.assertEquals(row[17],None ) self.assertEquals(row[18],None ) self.assertEquals(row[19],1 ) self.assertEquals(row[20],None ) self.assertEquals(row[21],1 ) self.assertEquals(row[22],None ) self.assertEquals(row[23],1 ) self.assertEquals(row[24],1 ) self.assertEquals(row[25],None ) self.assertEquals(row[26],None ) self.assertEquals(row[27],None ) self.assertEquals(row[28],None )
def _get_db_type(self, type): type = type.lower() if is_configured_mysql(): if type in self.XSD_TO_MYSQL_TYPES: return self.XSD_TO_MYSQL_TYPES[type] return self.XSD_TO_MYSQL_TYPES['default'] elif is_configured_postgres(): if type in self.XSD_TO_PGSQL_TYPES: return self.XSD_TO_PGSQL_TYPES[type] return self.XSD_TO_PGSQL_TYPES['default'] else: if type in self.XSD_TO_DEFAULT_TYPES: return self.XSD_TO_DEFAULT_TYPES[type] return self.XSD_TO_DEFAULT_TYPES['default']
def _execute(self, queries, values): # todo - rollback on fail if queries is None or len(queries) == 0: logging.error("xformmanager: storageutility - xform " + self.formdef.target_namespace + " could not be parsed") return cursor = connection.cursor() cursor.execute(queries, values) if is_configured_mysql(): query = "SELECT LAST_INSERT_ID();" elif is_configured_postgres(): # hat tips: # http://archives.postgresql.org/pgsql-general/2008-08/msg01044.php # http://en.wikibooks.org/wiki/Converting_MySQL_to_PostgreSQL query = "SELECT CURRVAL(pg_get_serial_sequence('%s','id'));" % self.table_name else: query = "SELECT LAST_INSERT_ROWID()" cursor.execute(query) row = cursor.fetchone() if row is not None: return row[0] return -1
def queries_to_create_instance_tables(self, elementdef, parent_id, parent_name='', parent_table_name='', domain=None): table_name = format_table_name( formatted_join(parent_name, elementdef.name), self.formdef.version, self.formdef.domain_name ) (next_query, fields) = self._create_instance_tables_query_inner_loop(elementdef, parent_id, parent_name, parent_table_name ) # add this later - should never be called during unit tests if not fields: return next_query queries = '' if is_configured_mysql(): queries = "CREATE TABLE "+ table_name +" ( id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, " else: queries = "CREATE TABLE "+ table_name +" ( id SERIAL NOT NULL, " if len(fields[0]) == 1: queries = queries + str(fields) else: for field in fields: if len(field)>0: queries = queries + str(field) # remove ? for encoding test queries = queries.replace("?", "__") # we don't really need a parent_id in our top-level table... # should be NOT NULL? if parent_name is not '': if is_configured_mysql(): queries = queries + " parent_id INT(11), " queries = queries + " FOREIGN KEY (parent_id) REFERENCES " + \ format_table_name(parent_table_name, self.formdef.version, self.formdef.domain_name) + \ "(id) ON DELETE SET NULL" elif is_configured_postgres(): queries = queries + " parent_id INTEGER " queries = queries + " REFERENCES " + \ format_table_name(parent_table_name, self.formdef.version, self.formdef.domain_name) + \ "(id) ON DELETE SET NULL" else: queries = queries + " parent_id REFERENCES " + \ format_table_name(parent_table_name, self.formdef.version, self.formdef.domain_name) + \ "(id) ON DELETE SET NULL" else: queries = self._trim2chars(queries) # most of the time, we rely on global mysql config in my.conf/ini if is_configured_postgres(): queries = queries + ", CONSTRAINT %s_pkey PRIMARY KEY (id)" % table_name end_query = ");" # we only specify default engine and character set if it's clear that # we are already doing something against the global config # (i.e. we're settings database_options in settings.py) if hasattr(settings,'DATABASE_OPTIONS') and \ 'init_command' in settings.DATABASE_OPTIONS: if 'innodb' in settings.DATABASE_OPTIONS['init_command'].lower(): end_query = ") ENGINE=InnoDB;" elif 'myisam' in settings.DATABASE_OPTIONS['init_command'].lower(): end_query = ") ENGINE=MyISAM;" queries = queries + end_query + next_query return queries