def import_one(self, table): self.query_stmt("USE `%s`;" % table["schema"]) name = table["name"] struct = { "name": table["name"], "comment": table["comment"], "camel_case_name": strutil.camel_case(table["name"]), "source": table["schema"], } options = {} primary_keys = [] unique_keys = [] index_keys = [] fields = [] prev_field = None for column in self.get_table_column_status(name): field = { "name": column["name"], "comment": column["comment"], "is_vector": False, "original_type_name": column["original_type_name"], } type_name = convert_mysql_type(field["original_type_name"]) field["camel_case_name"] = strutil.camel_case(field["name"]) field["type_name"] = type_name field["type"] = types.get_type_by_name(type_name) key = column.get("key", "") if key == "PRI": primary_keys.append(column["name"]) if key == "UNI": unique_keys.append(column["name"]) if key == "MUL": index_keys.append(column["name"]) if prev_field is not None and strutil.is_vector_fields( prev_field, field): prev_field["is_vector"] = True field["is_vector"] = True prev_field = field fields.append(field) options["primary_keys"] = primary_keys options["unique_keys"] = unique_keys options["index_keys"] = index_keys struct["options"] = options struct["fields"] = fields return struct
def gen_kv_parse_method(self, struct): rows = struct['data_rows'] keycol = struct['options'][predef.PredefKeyColumn] valcol = struct['options'][predef.PredefValueColumn] typcol = int(struct['options'][predef.PredefValueTypeColumn]) assert keycol > 0 and valcol > 0 and typcol > 0 keyidx, keyfield = genutil.get_field_by_column_index(struct, keycol) validx, valfield = genutil.get_field_by_column_index(struct, valcol) typeidx, typefield = genutil.get_field_by_column_index(struct, typcol) array_delim = struct['options'].get(predef.OptionArrayDelimeter, predef.DefaultArrayDelimiter) map_delims = struct['options'].get(predef.OptionMapDelimeters, predef.DefaultMapDelimiters) content = '' content += '%s// parse object fields from text rows\n' % self.TAB_SPACE content += '%spublic void ParseFromRows(string[][] rows)\n' % self.TAB_SPACE content += '%s{\n' % self.TAB_SPACE content += '%sif (rows.Length < %d) {\n' % (self.TAB_SPACE * 2, len(rows)) content += '%sthrow new ArgumentException(string.Format("%s: row length out of index, {0} < %d", rows.Length));\n' % ( self.TAB_SPACE * 3, struct['name'], len(rows)) content += '%s}\n' % (self.TAB_SPACE * 2) idx = 0 prefix = 'this.' for row in rows: name = rows[idx][keyidx].strip() name = strutil.camel_case(name) origin_typename = rows[idx][typeidx].strip() typename = lang.map_cs_type(origin_typename) valuetext = 'rows[%d][%d]' % (idx, validx) # print('kv', name, origin_typename, valuetext) if origin_typename.startswith('array'): content += '%s{\n' % (self.TAB_SPACE * 2) content += self.gen_field_array_assign_stmt( prefix, origin_typename, name, valuetext, array_delim, 3) content += '%s}\n' % (self.TAB_SPACE * 2) elif origin_typename.startswith('map'): content += '%s{\n' % (self.TAB_SPACE * 2) content += self.gen_field_map_assign_stmt( prefix, origin_typename, name, valuetext, map_delims, 3) content += '%s}\n' % (self.TAB_SPACE * 2) else: content += '%sif (rows[%d][%d].Length > 0) {\n' % ( self.TAB_SPACE * 2, idx, validx) content += self.gen_field_assgin_stmt(prefix + name, typename, valuetext, 3) content += '%s}\n' % (self.TAB_SPACE * 2) idx += 1 content += '%s}\n\n' % self.TAB_SPACE return content
def gen_kv_parse_method(self, struct): content = '' rows = struct['data_rows'] keycol = struct['options'][predef.PredefKeyColumn] valcol = struct['options'][predef.PredefValueColumn] typcol = int(struct['options'][predef.PredefValueTypeColumn]) assert keycol > 0 and valcol > 0 and typcol > 0 keyidx, keyfield = genutil.get_field_by_column_index(struct, keycol) validx, valfield = genutil.get_field_by_column_index(struct, valcol) typeidx, typefield = genutil.get_field_by_column_index(struct, typcol) array_delim = struct['options'].get(predef.OptionArrayDelimeter, predef.DefaultArrayDelimiter) map_delims = struct['options'].get(predef.OptionMapDelimeters, predef.DefaultMapDelimiters) content += 'func (p *%s) ParseFromRows(rows [][]string) error {\n' % struct[ 'camel_case_name'] content += '\tif len(rows) < %d {\n' % len(rows) content += '\t\tlog.Panicf("%s:row length out of index, %%d < %d", len(rows))\n' % ( struct['name'], len(rows)) content += '\t}\n' idx = 0 for row in rows: content += '\tif rows[%d][%d] != "" {\n' % (idx, validx) name = rows[idx][keyidx].strip() name = strutil.camel_case(name) origin_typename = rows[idx][typeidx].strip() typename = lang.map_go_type(origin_typename) valuetext = 'rows[%d][%d]' % (idx, validx) # print('kv', name, origin_typename, valuetext) if origin_typename.startswith('array'): content += self.gen_field_array_assign_stmt( 'p.', origin_typename, name, valuetext, array_delim, 2) elif origin_typename.startswith('map'): content += self.gen_field_map_assign_stmt( 'p.', origin_typename, name, valuetext, map_delims, 2) else: content += self.gen_field_assgin_stmt('p.' + name, typename, valuetext, 2, idx) content += '%s}\n' % self.TAB_SPACE idx += 1 content += '%sreturn nil\n' % self.TAB_SPACE content += '}\n\n' return content
def get_struct_kv_fields(struct): rows = struct["data_rows"] keycol = struct["options"][predef.PredefKeyColumn] valcol = struct["options"][predef.PredefValueColumn] typecol = int(struct['options'][predef.PredefValueTypeColumn]) assert keycol > 0 and valcol > 0 and typecol > 0 comment_idx = -1 if predef.PredefCommentColumn in struct["options"]: commentcol = int(struct["options"][predef.PredefCommentColumn]) assert commentcol > 0 comment_field = {} comment_idx, comment_field = get_field_by_column_index( struct, commentcol) key_idx, key_field = get_field_by_column_index(struct, keycol) value_idx, value_field = get_field_by_column_index(struct, valcol) type_idx, type_field = get_field_by_column_index(struct, typecol) fields = [] for i in range(len(rows)): # print(rows[i]) name = rows[i][key_idx].strip() typename = rows[i][type_idx].strip() assert len(name) > 0, (rows[i], key_idx) comment = '' if comment_idx >= 0: comment = rows[i][comment_idx].strip() field = { 'name': name, 'camel_case_name': strutil.camel_case(name), 'type_name': typename, 'original_type_name': typename, 'comment': comment, } fields.append(field) return fields
def gen_go_struct(self, struct, params): content = '' fields = struct['fields'] if struct['options'][predef.PredefParseKVMode]: fields = genutil.get_struct_kv_fields(struct) json_decorate = params.get(predef.OptionJsonDecorate, False) inner_class_done = False inner_typename = '' inner_var_name = '' inner_field_names, inner_fields = genutil.get_inner_class_mapped_fields( struct) if len(inner_fields) > 0: content += self.gen_go_inner_struct(struct, json_decorate) inner_type_class = struct["options"][predef.PredefInnerTypeClass] inner_var_name = struct["options"][predef.PredefInnerTypeName] inner_typename = '[]%s' % inner_type_class vec_done = False vec_names, vec_name = genutil.get_vec_field_range(struct) content += '// %s, %s\n' % (struct['comment'], struct['file']) content += 'type %s struct\n{\n' % struct['camel_case_name'] for field in fields: field_name = field['name'] if field_name in inner_field_names: if not inner_class_done: if json_decorate: content += ' %s %s `json:"%s"` //\n' % ( strutil.camel_case(inner_var_name), inner_typename, inner_var_name) else: content += ' %s %s //\n' % ( strutil.camel_case(inner_var_name), inner_typename) inner_class_done = True else: typename = lang.map_go_type(field['original_type_name']) assert typename != "", field['original_type_name'] if field_name not in vec_names: if json_decorate: content += ' %s %s `json:"%s"` // %s\n' % ( field['camel_case_name'], typename, field['comment'], field['name']) else: content += ' %s %s // %s\n' % ( field['camel_case_name'], typename, field['comment']) elif not vec_done: vec_done = True if json_decorate: content += ' %s [%d]%s `json:"%s"` // %s\n' % ( strutil.camel_case(vec_name), len(vec_names), typename, field['comment'], vec_name) else: content += ' %s [%d]%s // %s\n' % ( vec_name, len(vec_names), typename, field['comment']) return content