def parse_end(self): if self.m_is_help is True: return if self.m_file_path is None: print("please input filepath") return isExist = os.path.exists(self.m_file_path) if isExist is False: print("file is not exist") return # 判断输出目录是否存在 obj_flag = False cpp_obj_flag = False h_obj_flag = False self.__create_dirs(self.m_obj) h_obj = self.m_obj if self.m_h_obj is not None: h_obj = self.m_h_obj self.__create_dirs(h_obj) cpp_obj = self.m_obj if self.m_cpp_obj is not None: cpp_obj = self.m_cpp_obj self.__create_dirs(cpp_obj) parser = CSqlParse(self.m_file_path) parser.read() info_dict = parser.get_info_dict() namespace = info_dict.get(CSqlParse.NAMESPACE) # 写参数类 writer = CWriteParamClass(parser.get_file_path(), root=h_obj) writer.write(info_dict) if self.m_mode == CCmdHandle.MODE_UPDATE: # 更新实现的h文件 basename = os.path.basename(parser.get_file_path()) name = os.path.splitext(basename)[0] imp_h_path = os.path.join(h_obj, "{0}_db_handler.h".format(name)) if os.path.exists(imp_h_path) is False: raise RuntimeError(imp_h_path + "is not exist") imp_cpp_path = os.path.join(cpp_obj, "{0}_db_handler.cpp".format(name)) if os.path.exists(imp_cpp_path) is False: raise RuntimeError(imp_cpp_path + "is not exist") updater = CUpdateSqliteImpH(imp_h_path) updater.update(info_dict) updater = CUpdateSqliteImpCpp(imp_cpp_path) updater.update(info_dict) elif self.m_mode == CCmdHandle.MODE_CREATE: # 写头文件 writer = CWriteSqliteImpH(parser, parser.get_file_path(), root=h_obj) writer.write(info_dict) # 写源文件 writer = CWriteSqliteImpCpp(parser, parser.get_file_path(), root=cpp_obj) writer.write(info_dict)
def parse_end(self): if self.m_is_help is True: return if self.m_is_exe_sql is True: return if self.m_is_gen_cfg is True: return if self.m_file_path is None: print("please input filepath") return isExist = os.path.exists(self.m_file_path) if isExist is False: print("file is not exist") return # 判断输出目录是否存在 obj_flag = False cpp_obj_flag = False h_obj_flag = False isExist = os.path.exists(self.m_obj) if isExist is False: print("dir is not exist") return parser = CSqlParse(self.m_file_path) parser.read() info_dict = parser.get_info_dict() # 创建命名空间命名的目录 namespace = info_dict.get(CSqlParse.NAMESPACE) if namespace is None: raise RuntimeError("no namesapce") path = os.path.join(self.m_obj, namespace) if os.path.exists(path) is False: os.makedirs(path) # 写参数类 writer = CWriteParamClass(parser.get_file_path(), root=path) writer.write(info_dict) # 写数据库接口类 writer = CWriteInterface(parser, parser.get_file_path(), root=path) writer.write(info_dict)
self.read() def namespace(self): return self.m_namespace def class_name(self): return "CDbHandler" def __get_content(self): content = "" method_list = self.m_info_dict.get(CSqlParse.METHOD_LIST) for method_info in method_list: func_name = method_info.get(CSqlParse.FUNC_NAME) define = "uint32_t {0}::{1}({2})".format( self.class_name(), func_name, self.get_method_param_list(func_name, method_info)) is_exist = CStringTools.is_exist( r'(?:^|[ |\s]*?){0}'.format( CStringTools.filter_reg_keyword(define)), self.m_content) if is_exist is False: content += self.write_method_implement(method_info) return content if __name__ == '__main__': parse = CSqlParse("./file/user_info.sql") parse.read() info_dict = parse.get_info_dict() update = CUpdateSqliteImpCpp("./obj/user_info_db_handler.cpp") update.update(info_dict)
content += "\t" * 1 + self.write_set_method( param_type, param_name) + "\n" content += "\t" * 1 + self.write_get_method( param_type, param_name) + "\n" content += "\t" * 1 + self.write_get_method( "bool", param_name + "Used") + "\n" return content def __write_private_member(self, param_list): content = "" content += "\n" content += "private:\n" for param in param_list: param_type = param.get(CSqlParse.PARAM_TYPE) param_name = param.get(CSqlParse.PARAM_NAME) if param_type is None or param_name is None: continue content += "\t" * 1 + self.write_member_var( param_type, param_name) + "\n" content += "\t" * 1 + self.write_member_var( "bool", param_name + "Used") + "\n" return content if __name__ == "__main__": parser = CSqlParse("./file/user_info.sql") parser.read() info_dict = parser.get_info_dict() writer = CWriteParamClass(parser.get_file_path(), root="./obj") writer.write(info_dict)
content += self.__write_private_member() return content def __write_methods(self, method_list): content = "" content += "\n" content += "public:\n" content += "\t" * 1 + "/*@@start@@*/" + "\n" for method_info in method_list: content += self.write_method_define(method_info) content += "\npublic:\n" content += "\t" * 1 + "sql::IConnect *connect() { return m_connPool.connect(m_dial); }\n" content += "\t" * 1 + "void freeConnect(sql::IConnect *conn) { m_connPool.freeConnect(conn); }\n" return content def __write_private_member(self): content = "" content += "\n" content += "private:\n" content += "\t" * 1 + "sql::CConnPool m_connPool;\n" content += "\t" * 1 + "std::string m_dial;\n" return content if __name__ == "__main__": parser = CSqlParse("./example_sql/user_info.sql") parser.read() info_dict = parser.get_info_dict() writer = CWriteSqliteImpH(parser.get_file_path(), root="./obj") writer.write(info_dict)
content += err_content() for sql in create_functions: content += "\t" * 1 + "_, err = this.m_db.Exec(`{0}`)\n".format( sql) content += err_content() content += "\t" * 1 + "return nil\n" content += "}\n\n" return content def __write_header(self, namespace): self.m_content += "package {0}\n\n".format(namespace) self.m_content += "import (\n" self.m_content += "\t" * 1 + '"{0}"\n'.format("bufio") self.m_content += "\t" * 1 + '"{0}"\n'.format("bytes") self.m_content += "\t" * 1 + '"{0}"\n'.format("database/sql") self.m_content += "\t" * 1 + '"{0}"\n'.format("io") self.m_content += "\t" * 1 + '"{0}"\n'.format("os") self.m_content += "\t" * 1 + '"{0}"\n'.format("regexp") self.m_content += "\t" * 1 + '"{0}"\n'.format("strconv") self.m_content += "\t" * 1 + '"{0}"\n'.format("fmt") self.m_content += "\t" * 1 + '"{0}"\n'.format("errors") self.m_content += ")\n\n" if __name__ == "__main__": parser = CSqlParse("./file/user_info.sql") parser.read() info_dict = parser.get_info_dict() writer = CWriteInterface(parser, parser.get_file_path(), root="./obj") writer.write(info_dict)