def execute(my): assert my.db_resource assert my.table database = my.db_resource.get_database() from pyasm.search import Insert, Select, DbContainer, Search, Sql # get the data if not my.sobjects: search = Search("sthpw/search_object") # BAD assumption #search.add_filter("table", my.table) # create a search_type. This is bad assumption cuz it assumes project-specific search_type # should call set_search_type() if not my.search_type: my.search_type = "%s/%s" % (my.database, my.table) search.add_filter("search_type", my.search_type) my.search_type_obj = search.get_sobject() if not my.search_type_obj: if my.no_exception == False: raise SqlException("Table [%s] does not have a corresponding search_type" % my.table) else: return search_type = my.search_type_obj.get_base_key() search = Search(my.search_type) search.set_show_retired(True) my.sobjects = search.get_sobjects() # get the info for the table column_info = SearchType.get_column_info(my.search_type) for sobject in my.sobjects: print my.delimiter insert = Insert() insert.set_database(my.database) insert.set_table(my.table) data = sobject.data for name, value in data.items(): if name in my.ignore_columns: continue if not my.include_id and name == "id": insert.set_value("id", '"%s_id_seq".nextval' % table, quoted=False) #insert.set_value(name, value, quoted=False) elif value == None: continue else: # replace all of the \ with double \\ insert.set_value(name, value) print "%s" % insert.get_statement() print my.end_delimiter print
def execute(my): search_type = my.kwargs.get("search_type") column_info = SearchType.get_column_info(search_type) values = my.kwargs.get("values") # get the definition config for this search_type from pyasm.search import WidgetDbConfig config = WidgetDbConfig.get_by_search_type(search_type, "definition") if not config: config = SearchType.create("config/widget_config") config.set_value("search_type", search_type) config.set_value("view", "definition") config.commit() config._init() for data in values: name = data.get("name") name = name.strip() if name == '': continue try: name.encode('ascii') except UnicodeEncodeError: raise TacticException('Column name needs to be in English. Non-English characters can be used in Title when performing [Edit Column Definition] afterwards.') if column_info.get(name): raise CommandException("Column [%s] is already defined" % name) format = data.get("format") fps = data.get("fps") data_type = data.get("data_type") from pyasm.command import ColumnAddCmd cmd = ColumnAddCmd(search_type, name, data_type) cmd.execute() #(my, search_type, attr_name, attr_type, nullable=True): class_name = 'tactic.ui.table.FormatElementWdg' options = { 'format': format, 'type': data_type, 'fps': fps } # add a new widget to the definition config.append_display_element(name, class_name, options=options) config.commit_config()
def execute(my): search_type = my.kwargs.get("search_type") column_info = SearchType.get_column_info(search_type) values = my.kwargs.get("values") # get the definition config for this search_type from pyasm.search import WidgetDbConfig config = WidgetDbConfig.get_by_search_type(search_type, "definition") if not config: config = SearchType.create("config/widget_config") config.set_value("search_type", search_type) config.set_value("view", "definition") config.commit() config._init() for data in values: name = data.get("name") name = name.strip() if name == '': continue try: name.encode('ascii') except UnicodeEncodeError: raise TacticException( 'Column name needs to be in English. Non-English characters can be used in Title when performing [Edit Column Definition] afterwards.' ) if column_info.get(name): raise CommandException("Column [%s] is already defined" % name) format = data.get("format") fps = data.get("fps") data_type = data.get("data_type") from pyasm.command import ColumnAddCmd cmd = ColumnAddCmd(search_type, name, data_type) cmd.execute() #(my, search_type, attr_name, attr_type, nullable=True): class_name = 'tactic.ui.table.FormatElementWdg' options = {'format': format, 'type': data_type, 'fps': fps} # add a new widget to the definition config.append_display_element(name, class_name, options=options) config.commit_config()
def dump_tactic_inserts(my, path, mode='sql'): assert my.db_resource assert my.table database = my.db_resource.get_database() assert mode in ['sql', 'sobject'] if path: import os dirname = os.path.dirname(path) if not os.path.exists(dirname): os.makedirs(dirname) #f = open(path, 'w') #f = codecs.open(path, 'a', 'utf-8') UTF8Writer = codecs.getwriter('utf8') f = UTF8Writer(open(path, 'ab')) else: import sys f = sys.stdout from pyasm.search import Insert, Select, DbContainer, Search, Sql # get the data if not my.sobjects: search = Search("sthpw/search_object") search.add_filter("table_name", my.table) search.add_order_by("id") my.search_type_obj = search.get_sobject() if not my.search_type_obj: if my.no_exception == False: raise Exception("Table [%s] does not have a corresponding search_type" % my.table) else: return search_type = my.search_type_obj.get_base_key() search = Search(search_type) search.set_show_retired(True) my.sobjects = search.get_sobjects() # get the info for the table from pyasm.search import SearchType, Sql column_info = SearchType.get_column_info(my.search_type) for sobject in my.sobjects: f.write( "%s\n" % my.delimiter ) if mode == 'sobject': search_type = sobject.get_base_search_type() f.write("insert = SearchType.create('%s')\n" % search_type) else: f.write("insert.set_table('%s')\n" % my.table) data = sobject.get_data() for name, value in data.items(): if name in my.ignore_columns: continue if name == '_tmp_spt_rownum': continue if not my.include_id and name == "id": #insert.set_value("id", '"%s_id_seq".nextval' % table, quoted=False) pass elif value == None: continue else: # This is not strong enough #if value.startswith("{") and value.endswith("}"): # f.write("insert.set_expr_value('%s', \"\"\"%s\"\"\")\n" % (name, value)) if type(value) == types.IntType or \ type(value) == types.FloatType or \ type(value) == types.BooleanType or \ type(value) == types.LongType: f.write("insert.set_value('%s', %s)\n" % (name, value)) else: # if the value contains triple double quotes, convert to # triple quotes if isinstance(value, datetime.datetime): value = str(value) elif isinstance(value, unicode): #value = str(value) value = value.encode("UTF-8") # this fixes a problem with non-ascii characters if isinstance(value, basestring): quoted = value.startswith('"') and value.endswith('"') value = repr(value) quoted2 = value.startswith('"') and value.endswith('"') if not quoted and quoted2: value = value.strip('"') # repr puts single quotes at the start and end if value.startswith("'") and value.endswith("'"): value = value[1:-1] # and it puts a slash in front value = value.replace(r"\'", "'") # replace literal \n with newline (comes from repr) value = value.replace(r"\n", "\n") value = value.replace('"""', "'''") #value = value.replace("\\", "\\\\") # handle the case where the value starts with a quote if value.startswith('"'): value = '\\%s' % value # handle the case where the value ends starts with a quote if value.endswith('"'): value = '%s\\"' % value[:-1] f.write("insert.set_value('%s', \"\"\"%s\"\"\")\n" % (name, value)) # Disable commit for now #if mode == 'sobject': # f.write("insert.commit()\n") f.write( "%s\n" % my.end_delimiter ) f.write( "\n" ) if path: f.close()