FTP文件管理項目(本地雲)項目日報(二)

今天我們的“本地雲”練手項目開工第二天,總結了前一天大家的反應,來記錄個日報。

前一日日報彙總

先來彙總一下前一個工作日大家的日報情況:

號碼牌 日報
1號 (組長) https://blog.csdn.net/qq_43762191/article/details/106803860
2號 https://blog.csdn.net/qq_45792305/article/details/106821952
3號 https://blog.csdn.net/blackcamouflage/article/details/106815639
4號 https://blog.csdn.net/luyaozhima/article/details/106807415
5號 https://blog.csdn.net/jxy0219/article/details/106818773
6號 https://blog.csdn.net/weixin_44784684/article/details/106819508
7號 已失聯
8號(組長) https://blog.csdn.net/qq_42151344/article/details/106819026
9號 https://blog.csdn.net/qq_46485161/article/details/106819291

總結:說實話呢,還是有上心的人。

昨日計劃任務完成情況

完成

今日規劃

需求分析。

實際計劃

需求分析,項目整體框架設計,數據庫設計與實現

完成情況:

整體框架(這種還是比較喜歡手繪,雖然字不好看,但是手寫就是有種不一樣的感覺)
在這裏插入圖片描述
數據庫設計(我覺得這個項目兩個表就夠了)
在這裏插入圖片描述


數據庫板塊設計:
在這裏插入圖片描述整個模塊只開放數據庫操作類的接口。

數據庫實現:

初代代碼

//DB_base.h

#ifndef DB_BASE_H
#define DB_BASE_H

#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<sstream>
#include<sqlite3.h>
#include<string.h>



using namespace std;

//數據庫單例
class DB_base
{
public:
    static DB_base* instence(char* name);//獲取數據庫單例

  
    /*對查詢結果進行操作*/
    //這三條函數都可以做公用
    int return_serch_num(char* sql);//返回查詢記錄數
    int ireturn_serch(char* sql);//獲取查詢結果(單一結果)
    char* creturn_serch(char* sql);//獲取查詢結果(單一結果)
    bool excute(char* sql);

private:
    DB_base(char* name);
    ~DB_base();

    static DB_base* My_DB;//實例
    static sqlite3* db;//數據庫文件句柄
};

#endif



//DB_base.cpp

#include "DB_base.h"
#include <stdlib.h>
#include <stdio.h>

DB_base* DB_base::My_DB = NULL;
sqlite3* DB_base::db = NULL;

//我只有一個數據庫,所以不用傳入名字了
DB_base::DB_base(char* name)    //打開數據庫
{
    int rc = sqlite3_open(name, &db);
    if (rc != SQLITE_OK)
    {
        printf("%s", sqlite3_errmsg(db));
        exit(0);
    }
}

DB_base::~DB_base()
{
    sqlite3_close(db);
}

DB_base* DB_base::instence(char* name)
{
    if (!My_DB)
    {
        My_DB = new DB_base(name);
    }
    return My_DB;
}

bool DB_base::excute(char* order)   //這個用來傳創建表的語句
{
    int nRow, nColumn;
    int rc;
    char* errMsg;
    char** dbResult;

    //執行SQL語句
    rc = sqlite3_get_table(db, order, &dbResult, &nRow, &nColumn, &errMsg);

    //判斷是否成功
    if (rc != SQLITE_OK){
        printf("%s", sqlite3_errmsg(db));
        return false;
    }
    else
        return true;
}

//對查詢結果進行操作
int DB_base::return_serch_num(char* sql)
{
    int rc, num;
    char* errMsg;
    char** dbResult;
    int nRow = 0, nColumn = 0;

    rc = sqlite3_get_table(db, sql, &dbResult, &nRow, &nColumn, &errMsg);
    if (rc == SQLITE_OK)
    {
        num = nRow;
        return num;
    }
    else
        return 0;
}

int DB_base::ireturn_serch(char* sql)
{
    char* errMsg;
    char** dbResult;
    int nRow = 0, nColumn = 0;
    int rc;

    int result;
    rc = sqlite3_get_table(db, sql, &dbResult, &nRow, &nColumn, &errMsg);
    if (rc == SQLITE_OK)
    {
        result = atoi(dbResult[nColumn]);
        sqlite3_free_table(dbResult);
        return result;
    }
    else
        return 0;
}

char* DB_base::creturn_serch(char* sql)
{
    char* errMsg;
    char** dbResult;
    int nRow = 0, nColumn = 0;
    int rc;

    rc = sqlite3_get_table(db, sql, &dbResult, &nRow, &nColumn, &errMsg);
    if (rc == SQLITE_OK)
    {
        return dbResult[nColumn];
    }
    else
        return NULL;
}

//DB_init.h

#include"DB_base.h"

class DB_init
{
private:
	DB_base* My_DB;

	void DB_create();
	void table_create();

public:
	void commmand();
};
//DB_init.cpp

#include "DB_init.h"

void DB_init::DB_create() {
	char* name = new char[10];
	sprintf(name, "FTP.db");
	My_DB = DB_base::instence(name);
	delete name;
}

void DB_init::table_create() {
	char* sql = new char[120];

	sprintf(sql, "select count(*) from sqlite_master where type = 'table' and name = 'UserMsg'");
	if (!My_DB->ireturn_serch(sql))
	{
		sprintf(sql, "create table UserMsg(id int primary key,pwd int,tel text,dir_id int)");
		My_DB->excute(sql);

		sprintf(sql, "create table FileMsg(file_name text primary key,owner_id int,mode int,dir_id int,file_id int)");
		My_DB->excute(sql);

		sprintf(sql, "insert into UserMsg values(100000,123456,'18039027069',1)");
		My_DB->excute(sql);

		sprintf(sql, "create table DirMsg(dir_id int primary key,dir_name text,grade int,owner_id int)");
		My_DB->excute(sql);
	}	
	delete sql;
}

void DB_init::commmand() {
	DB_create();
	table_create();
}

//DB_command.h

#include"DB_init.h"

class DB_command
{
private:
	DB_base* My_DB;

public:
	DB_command();

	//增
	bool add_user(int id,int pwd,char* tel);
	bool add_file(char* file_name,int owner_id,int mode,int dir_id);
	bool add_list(int id,int pwd,int father_dir,char* dir_name);
	
	//刪
	bool del_user(int user_id,int pwd);
	bool del_file(int user_id,int pwd,char* file_name);
	bool del_list(int user_id,int pwd,int dir_id);
	
	//查
	bool login(int user_id,int pwd);
	int pwd(int user_id,char* tel);
	void file_list(int user_id,int pwd,int dir_id, char* ret);
    bool download(int user_id,int pwd,char* file_name);
    
	//改
	bool cpwd(int user_id, int old_pwd, int new_id);
	bool cfile_addr(int user_id,int pwd,char* file_name, int old_dir, int new_dir);
	bool cmode(int user_id,int pwd,char* file_name, int new_mode);
};
//DB_command.cpp

#include "DB_command.h"

DB_command::DB_command() {
	DB_init* init = new DB_init();
	init->commmand();
	delete init;

	char* name = new char[10];
	sprintf(name, "FTP.db");
	My_DB = DB_base::instence(name);
	delete name;
}

//增
bool DB_command::add_user(int id, int pwd, char* tel) {
	//先查一下該用戶是否已存在
	char* sql = new char[120];
	int ret = 0;
	sprintf(sql, "select pwd from UserMsg where id = %d", id);
	if (int a = My_DB->return_serch_num(sql) == 1) {
		cout << "user already in" << endl;
	}
	else {
	//查詢目錄id使用情況
		sprintf(sql, "select max(dir_id) from Dir_Msg");
		int max = My_DB->ireturn_serch(sql) + 1;

		sprintf(sql, "insert into UserMsg values(%d,%d,%s,%d)", id, pwd, tel, max);
	    ret = My_DB->excute(sql);
	}
	delete sql;
	return ret;
}

bool DB_command::add_file(char* file_name, int owner_id, int mode, int dir_id) {
	//先確定目錄是否有問題
	char* sql = new char[120];
	int ret = 0;
	sprintf(sql, "select owner_id from Dir_Msg where dir_id = %d", dir_id);
	if (My_DB->return_serch_num(sql)) {
		//再對文件id進行安排
		sprintf(sql, "select max(file_id) from File_Msg where dir_id = %d", dir_id);
		int file_id = My_DB->ireturn_serch(sql) + 1;
		//之後將文件數據寫入
		sprintf(sql, "insert into FileMsg values(%s,%d,%d,%d,%d)", file_name, owner_id, mode, dir_id, file_id);
		ret = My_DB->excute(sql);
	}
	else {
		cout << "dir is not exit" << endl;
	}
	delete sql;
	return ret;
}

bool DB_command::add_list(int id, int pwd, int father_dir, char* dir_name) {
	// 先確定目錄是否有問題
	char* sql = new char[120];
	int ret = 0;
	sprintf(sql, "select owner_id from Dir_Msg where dir_name = %s", dir_name);
	if (My_DB->return_serch_num(sql)) {
		cout << "dir is exit" << endl;
	}
	else {
		//生成新目錄
		sprintf(sql, "select dir_grade from Dir_Msg where dir_id = %d",father_dir);
		int dir_grade = My_DB->ireturn_serch(sql) + 1;

		sprintf(sql, "select max(dir_id) from Dir_Msg");
		int dir_id = My_DB->ireturn_serch(sql) + 1;
		sprintf(sql, "insert into DirMsg values(%d,%s,%d,%d)",dir_id, dir_name,dir_grade,id);
		ret = My_DB->excute(sql);
	}
	delete sql;
	return ret;
}

//刪
bool DB_command::del_user(int user_id,int pwd) {
	char* sql = new char[120];
	sprintf(sql, "delete from FileMsg where owner_id = %d and pwd = %d", user_id,pwd);
	int ret = My_DB->excute(sql);
	if (ret) {
		sprintf(sql, "delete from UserMsg where id = %d", user_id);
		ret = My_DB->excute(sql);
	}
	delete sql;
	return ret;
}

bool DB_command::del_file(int user_id, int pwd, char* file_name) {
	if (login(user_id, pwd)) {
		char* sql = new char[120];
		sprintf(sql, "delete from FileMsg where user_id = %d and file_name = %s", user_id, file_name);
		int ret = My_DB->excute(sql);
		delete sql;
		return ret;
	}
	else {
		cout << "del file defeat" << endl;
		return false;
	}
}

bool DB_command::del_list(int user_id, int pwd, int dir_id) {
	if (login(user_id, pwd)) {
		char* sql = new char[120];
		sprintf(sql, "delete from FileMsg where user_id = %d and dir_id = %d", user_id, dir_id);
		int ret = My_DB->excute(sql);
		if (ret) {
			sprintf(sql, "delete from DirMsg where dir_grade = %d", dir_id);
			ret = My_DB->return_serch_num(sql);
			if (ret) {
				cout << "there still some file under this dir" << endl;
			}
			else {
				sprintf(sql, "delete from DirMsg where dir_id = %d", dir_id);
				ret = My_DB->excute(sql);
			}	
		}
		else {
			cout << "del dir defeat" << endl;
		}
		delete sql;
		return ret;
	}
	else {
		cout << "del dir defeat" << endl;
		return false;
	}
}

//查
bool DB_command::login(int user_id, int pwd) {
	char* sql = new char[120];
	sprintf(sql, "select pwd from UserMsg where id = %d", user_id);
	int ret = My_DB->ireturn_serch(sql);
	delete sql;
	if (pwd == ret)
		return true;
	else
		return false;
}

int DB_command::pwd(int user_id, char* tel) {
	char* sql = new char[120];
	sprintf(sql, "select pwd from UserMsg where id = %d and tel = %s", user_id,tel);
	int ret = My_DB->ireturn_serch(sql);
	delete sql;
	return ret;
}

void DB_command::file_list(int user_id, int pwd, int dir_id, char* ret){
	if (login(user_id, pwd)) {
		char* sql = new char[120];
		sprintf(sql, "select file_name from FileMsg where owner_id = %d and dir_id = %d", user_id, dir_id);
		ret = My_DB->creturn_serch(sql);
		sprintf(sql, "select dir_name from DirMsg where dir_grade = %d",dir_id);
		strcpy(ret, My_DB->creturn_serch(sql));
		delete sql;
	}
	else 
		cout << "can't get file list" << endl;
}

bool DB_command::download(int user_id, int pwd, char* file_name) {
	if (login(user_id, pwd)) {
		char* sql = new char[120];
		sprintf(sql, "select file_id from File_Msg where file_name = %s", file_name);
		int ret = My_DB->ireturn_serch(sql);
		delete sql;
		return ret;
	}
	else {
		cout << "del defeat" << endl;
		return false;
	}
}

//改
bool DB_command::cpwd(int user_id, int old_pwd, int new_id) {
	char* sql = new char[120];
	sprintf(sql, "update UserMsg set pwd = %d where id = %d and pwd = %d", user_id, old_pwd, new_id);
	int ret = My_DB->excute(sql);
	delete sql;
	return ret;
}

bool DB_command::cfile_addr(int user_id, int pwd, char* file_name, int old_dir, int new_dir) {
	//修改目錄前,先判斷目標目錄是否存在
	if (login(user_id, pwd)) {
		char* sql = new char[120];
		sprintf(sql, "select owner_id from Dir_Msg where dir_id = %d", new_dir);

		int id = My_DB->ireturn_serch(sql);
		int ret = 0;
		if (user_id == id) {
			sprintf(sql, "update FileMsg set dir_id = %d where owner_id = %d and dir_id = %d and file_name = %s", new_dir, user_id, old_dir, file_name);
		    ret = My_DB->excute(sql);
		}
		else {
			cout << "aim dir is not exit" << endl;
		}
		delete sql;
		return false;
	}
	else {
		cout << "del dir defeat" << endl;
		return false;
	}
}

bool DB_command::cmode(int user_id, int pwd, char* file_name, int new_mode){
	//確定文件是不是他的
	if (login(user_id, pwd)) {
		char* sql = new char[120];
		sprintf(sql, "update FileMsg set mode = %d where owner_id = %d and file_name = %d", new_mode, user_id, file_name);
		int ret = My_DB->excute(sql);
		delete sql;
		return ret;
	}
	else {
		cout << "have no mode to change mode" << endl;
		return false;
	}
}

下午做一個測試類來進行對數據庫模塊的測試,爲什麼要對每個類進行測試我在
<重構3>
這篇博客裏講的清楚了。

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章