193 lines
4.6 KiB
C#
193 lines
4.6 KiB
C#
using DryIoc.ImTools;
|
|
using MySql.Data.MySqlClient;
|
|
using StudentManager.Common;
|
|
using StudentManager.Data;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace StudentManager.Common
|
|
{
|
|
static class Tables
|
|
{
|
|
static public string UserTable { get; } = "users";
|
|
static public string QuesTable { get; } = "questions";
|
|
static public string UQTable { get; } = "user_data";
|
|
}
|
|
|
|
|
|
public static class SQLMapping
|
|
{
|
|
public static void Mapping<T>(MySqlDataReader reader)
|
|
{
|
|
typeof(T).GetProperties().ForEach(x => { Debug.WriteLine(x.Name.ToLower()); });
|
|
}
|
|
|
|
public static void Mapping<T>(T data, MySqlDataReader reader)
|
|
{
|
|
if (typeof(T) == typeof(StudentData))
|
|
{
|
|
(data as StudentData).UID = (int)reader[0];
|
|
(data as StudentData).Name = reader[1].ToString();
|
|
(data as StudentData).Gender = reader[2].ToString();
|
|
(data as StudentData).Contact = reader[3].ToString();
|
|
(data as StudentData).Address = reader[4].ToString();
|
|
(data as StudentData).Grade = (int)reader[5];
|
|
}
|
|
else if(typeof(T) == typeof(QuestionData))
|
|
{
|
|
DifficultyLevel dLevel = DifficultyLevel.easy;
|
|
QStatus qStatus = QStatus.published;
|
|
(data as QuestionData).Id = (int)reader[0];
|
|
(data as QuestionData).Type = reader[1].ToString();
|
|
(data as QuestionData).Stem = reader[2].ToString();
|
|
(data as QuestionData).Answer = reader[3].ToString();
|
|
Enum.TryParse(reader[4].ToString(), true, out dLevel);
|
|
(data as QuestionData).DifficultyLevel = dLevel;
|
|
(data as QuestionData).Category = reader[5].ToString();
|
|
(data as QuestionData).Tags = reader[6].ToString();
|
|
(data as QuestionData).Source = reader[7].ToString();
|
|
Enum.TryParse(reader[8].ToString(), true, out qStatus);
|
|
(data as QuestionData).Status = qStatus;
|
|
}
|
|
|
|
|
|
|
|
//for (int i = 0; i < reader.FieldCount; ++i)
|
|
//{
|
|
// data?.GetType().GetProperties().ForEach(x => { x.SetValue(reader[i].GetType(), reader[i]); });
|
|
//}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
struct userTable
|
|
{
|
|
|
|
}
|
|
|
|
public static class SQLHelper
|
|
{
|
|
static string config = "server=8.137.125.29;port=3306;user=StudentManager;password=wangxin55;database=studentmanager";
|
|
static MySqlConnection connection = null;
|
|
static MySqlCommand cmd = null;
|
|
|
|
// update ** set ** = '**',... where ** = **;
|
|
// insert into ** values('**','**'...);
|
|
// delete from ** where ** = **;
|
|
public static List<T> Query<T>(string tableName, int id = -1, params string[] queryParams) where T : IDataCommon, new()
|
|
{
|
|
connection = new MySqlConnection(config);
|
|
|
|
List<T> result = new List<T>();
|
|
|
|
string idName = typeof(T) == typeof(StudentData) ? Tables.UserTable : Tables.QuesTable;
|
|
try
|
|
{
|
|
connection.Open();
|
|
string queryStr = "";
|
|
if (queryParams.Length > 0)
|
|
{
|
|
string filedList = string.Join(",", queryParams);
|
|
queryStr = $"select {filedList} from {tableName};";
|
|
|
|
if (id != -1)
|
|
queryStr = $@"select {filedList} from {tableName} WHERE {idName} = {id};";
|
|
}
|
|
else
|
|
{
|
|
queryStr = $"select * from {tableName};";
|
|
if (id != -1)
|
|
queryStr = $@"select * from {tableName} WHERE {idName} = {id};";
|
|
}
|
|
|
|
cmd = new MySqlCommand(queryStr, connection);
|
|
MySqlDataReader reader = cmd.ExecuteReader();
|
|
|
|
|
|
|
|
while (reader.Read())
|
|
{
|
|
T row = new T();
|
|
SQLMapping.Mapping(row, reader);
|
|
result.Add(row);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
return null;
|
|
}
|
|
finally
|
|
{
|
|
cmd.Dispose();
|
|
connection.Close();
|
|
}
|
|
}
|
|
|
|
public static List<T> UnionQuery<T>(int id = -1, string tableName = "questions", string otherTable = "user_data") where T : IDataCommon, new()
|
|
{
|
|
connection = new MySqlConnection(config);
|
|
|
|
List<T> result = new List<T>();
|
|
|
|
try
|
|
{
|
|
connection.Open();
|
|
string queryStr = "";
|
|
if(id == -1)
|
|
queryStr = $@"SELECT u.user_id, q.* FROM {otherTable} u JOIN {tableName} q ON u.problem_id = q.problem_id";
|
|
else
|
|
queryStr = $@"SELECT q.* FROM {tableName} q JOIN {otherTable} u ON q.problem_id = u.problem_id WHERE u.user_id = {id}";
|
|
|
|
cmd = new MySqlCommand(queryStr, connection);
|
|
MySqlDataReader reader = cmd.ExecuteReader();
|
|
|
|
|
|
|
|
while (reader.Read())
|
|
{
|
|
T row = new T();
|
|
SQLMapping.Mapping(row, reader);
|
|
result.Add(row);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
return null;
|
|
}
|
|
finally
|
|
{
|
|
cmd.Dispose();
|
|
connection.Close();
|
|
}
|
|
}
|
|
|
|
public static void Add()
|
|
{
|
|
try
|
|
{
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
}
|
|
finally
|
|
{
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|