@添加数据库
(self, args)
| 424 | public.WriteFile(dbuser_info_path, '', mode='w') |
| 425 | |
| 426 | def AddDatabase(self, args): |
| 427 | """ |
| 428 | @添加数据库 |
| 429 | """ |
| 430 | if not hasattr(args, "name"): |
| 431 | return public.returnMsg(False, "缺少参数!name") |
| 432 | if not hasattr(args, "sid"): |
| 433 | return public.returnMsg(False, "缺少参数!sid") |
| 434 | |
| 435 | db_name = args.name |
| 436 | sid = args.sid |
| 437 | |
| 438 | if re.search(r"\W", db_name): |
| 439 | return public.returnMsg(False, "数据库名不能包含特殊字符,请重新设置") |
| 440 | if not str(sid).isdigit(): |
| 441 | return public.returnMsg(False, "参数错误!sid") |
| 442 | sid = int(sid) |
| 443 | |
| 444 | dtype = "pgsql" |
| 445 | res = self.add_base_database(args, dtype) |
| 446 | if not res['status']: return res |
| 447 | |
| 448 | data_name = res['data_name'] |
| 449 | username = res['username'] |
| 450 | password = res['data_pwd'] |
| 451 | listen_ip= args.listen_ip |
| 452 | # 添加对 listen_ip 的格式检查 |
| 453 | if not re.match(r"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/\d{1,2}\b", listen_ip): |
| 454 | return public.returnMsg(False, "ip格式错误!") |
| 455 | |
| 456 | pgsql_obj = self.get_sql_obj_by_sid(sid) |
| 457 | status, err_msg = pgsql_obj.connect() |
| 458 | if status is False: |
| 459 | return public.returnMsg(False, "连接数据库失败!") |
| 460 | result = pgsql_obj.execute("""CREATE DATABASE "{}";""".format(data_name)) |
| 461 | isError = self.IsSqlError(result) |
| 462 | if isError != None: return isError |
| 463 | if str(result).find("permission denied to create database") != -1: |
| 464 | return public.returnMsg(False, "创建数据库权限拒绝!") |
| 465 | |
| 466 | # 添加用户 |
| 467 | self.__CreateUsers(sid, data_name, username, password, '127.0.0.1') |
| 468 | |
| 469 | if not hasattr(args, 'ps'): args['ps'] = public.getMsg('INPUT_PS') |
| 470 | addTime = time.strftime('%Y-%m-%d %X', time.localtime()) |
| 471 | |
| 472 | pid = 0 |
| 473 | if hasattr(args, 'pid'): pid = args.pid |
| 474 | |
| 475 | if hasattr(args, 'contact'): |
| 476 | site = public.M('sites').where("id=?", (args.contact,)).field('id,name').find() |
| 477 | if site: |
| 478 | pid = int(args.contact) |
| 479 | args['ps'] = site['name'] |
| 480 | |
| 481 | db_type = 0 |
| 482 | if sid: db_type = 2 |
| 483 |
no test coverage detected