def initializeInstance(self, instance, item=None, container=None): if (self.is_initialized(instance) or getattr(instance, '_at_is_fake_instance', None)): # duh, we don't need to be initialized twice return factory = getToolByName(instance, 'portal_factory') if factory.isTemporary(instance): return fields = instance.Schema().fields() fields = [ f for f in fields if IObjectField.providedBy(f) and f.getStorage().__class__ is self.__class__ ] columns = [] args = {} for field in fields: type = self.db_type_map.get(field.type, field.type) name = field.getName() # MySQL supports escape for columns names! if self.__class__.__name__ == 'MySQLSQLStorage': columns.append('`%s` %s' % (name, type)) else: columns.append('%s %s' % (name, type)) parent = container or aq_parent(aq_inner(instance)) args['PARENTUID'] = getattr(aq_base(parent), 'UID', lambda: None)() args['table'] = instance.portal_type args['UID'] = instance.UID() # args['db_encoding']=kwargs.get('db_encoding',None) args['columns'] = ', ' + ', '.join(columns) if not self.table_exists(instance): self._query(instance, self.query_create, args) log('created table %s\n' % args['table']) try: self._query(instance, self.query_insert, args) except ConflictError: raise except: # usually, duplicate key # raise SQLInitException(msg) raise try: instance.__initialized += (self.getName(), ) except AttributeError: instance.__initialized = (self.getName(), ) # now, if we find an attribute called _v_$classname_temps, it # means the object was moved and we can initialize the fields # with those values temps_var = '_v_%s_temps' % self.getName() if hasattr(aq_base(instance), temps_var): temps = getattr(instance, temps_var) for key, value in temps.items(): instance.Schema()[key].set(instance, value) delattr(instance, temps_var) try: del instance.__cleaned except (AttributeError, KeyError): pass
def initializeInstance(self, instance, item=None, container=None): if (self.is_initialized(instance) or getattr(instance, '_at_is_fake_instance', None)): # duh, we don't need to be initialized twice return factory = getToolByName(instance, 'portal_factory') if factory.isTemporary(instance): return fields = instance.Schema().fields() fields = [f for f in fields if IObjectField.providedBy(f) and f.getStorage().__class__ is self.__class__] columns = [] args = {} for field in fields: type = self.db_type_map.get(field.type, field.type) name = field.getName() # MySQL supports escape for columns names! if self.__class__.__name__ == 'MySQLSQLStorage': columns.append('`%s` %s' % (name, type)) else: columns.append('%s %s' % (name, type)) parent = container or aq_parent(aq_inner(instance)) args['PARENTUID'] = getattr(aq_base(parent), 'UID', lambda: None)() args['table'] = instance.portal_type args['UID'] = instance.UID() # args['db_encoding']=kwargs.get('db_encoding',None) args['columns'] = ', ' + ', '.join(columns) if not self.table_exists(instance): self._query(instance, self.query_create, args) log('created table %s\n' % args['table']) try: self._query(instance, self.query_insert, args) except ConflictError: raise except: # usually, duplicate key # raise SQLInitException(msg) raise try: instance.__initialized += (self.getName(),) except AttributeError: instance.__initialized = (self.getName(),) # now, if we find an attribute called _v_$classname_temps, it # means the object was moved and we can initialize the fields # with those values temps_var = '_v_%s_temps' % self.getName() if hasattr(aq_base(instance), temps_var): temps = getattr(instance, temps_var) for key, value in temps.items(): instance.Schema()[key].set(instance, value) delattr(instance, temps_var) try: del instance.__cleaned except (AttributeError, KeyError): pass
def getConfigurableTypes(self): """ Get a list of types that can be configured for SQL Storage. """ c_types = [] ti = self.getInstalledTypes() for t in ti: for field in t['schema'].fields(): if IObjectField.providedBy(field) and \ ISQLStorage.providedBy(field.getStorage()): c_types.append(t) break return c_types
def getConfigurableTypes(self): """ Get a list of types that can be configured for SQL Storage. """ c_types = [] ti = self.getInstalledTypes() for t in ti: for field in t['schema'].fields(): if IObjectField.providedBy(field) and \ ISQLStorage.providedBy(field.getStorage()): c_types.append(t) break return c_types
def cleanupInstance(self, instance, item=None, container=None): if (self.is_cleaned(instance) or getattr(instance, '_at_is_fake_instance', None)): # duh, we don't need to be cleaned twice return # the object is being deleted. remove data from sql. but # first, made a temporary copy of the field values in case we # are being moved fields = instance.Schema().fields() fields = [ f for f in fields if IObjectField.providedBy(f) and f.getStorage().__class__ is self.__class__ ] temps = {} for f in fields: temps[f.getName()] = f.get(instance) setattr(instance, '_v_%s_temps' % self.getName(), temps) # now, remove data from sql c_tool = getToolByName(instance, TOOL_NAME) connection_id = c_tool.getConnFor(instance) args = {} args['table'] = instance.portal_type args['UID'] = instance.UID() # args['db_encoding']=kwargs.get('db_encoding',None) method = SQLMethod(instance) method.edit(connection_id, ' '.join(args.keys()), self.query_delete) try: query, result = method(test__=1, **args) except ConflictError: raise except: # dunno what could happen here raise # SQLCleanupException(msg) raise BeforeDeleteException try: instance.__cleaned += (self.getName(), ) except AttributeError: instance.__cleaned = (self.getName(), ) try: del instance.__initialized except (AttributeError, KeyError): pass
def cleanupInstance(self, instance, item=None, container=None): if (self.is_cleaned(instance) or getattr(instance, '_at_is_fake_instance', None)): # duh, we don't need to be cleaned twice return # the object is being deleted. remove data from sql. but # first, made a temporary copy of the field values in case we # are being moved fields = instance.Schema().fields() fields = [f for f in fields if IObjectField.providedBy(f) and f.getStorage().__class__ is self.__class__] temps = {} for f in fields: temps[f.getName()] = f.get(instance) setattr(instance, '_v_%s_temps' % self.getName(), temps) # now, remove data from sql c_tool = getToolByName(instance, TOOL_NAME) connection_id = c_tool.getConnFor(instance) args = {} args['table'] = instance.portal_type args['UID'] = instance.UID() # args['db_encoding']=kwargs.get('db_encoding',None) method = SQLMethod(instance) method.edit(connection_id, ' '.join(args.keys()), self.query_delete) try: query, result = method(test__=1, **args) except ConflictError: raise except: # dunno what could happen here raise # SQLCleanupException(msg) raise BeforeDeleteException try: instance.__cleaned += (self.getName(),) except AttributeError: instance.__cleaned = (self.getName(),) try: del instance.__initialized except (AttributeError, KeyError): pass