From 6a417aaf45d522515ae909afaeb5f8030caff6ee Mon Sep 17 00:00:00 2001 From: wangxiner55 <47072643+wangxiner55@users.noreply.github.com> Date: Wed, 4 Sep 2024 21:43:10 +0800 Subject: [PATCH] QueryALL --- StudentManager/App.xaml.cs | 5 +- StudentManager/Common/SQLHelper.cs | 132 ++++++++++++++++++-- StudentManager/Data/QuestionData.cs | 38 ++++++ StudentManager/Data/Student.cs | 13 -- StudentManager/Data/StudentData.cs | 25 ++++ StudentManager/Editor/MainEditor.xaml | 18 ++- StudentManager/Editor/MainEditor.xaml.cs | 11 +- StudentManager/Editor/QuestionsView.xaml | 38 ++++++ StudentManager/Editor/QuestionsView.xaml.cs | 28 +++++ StudentManager/Editor/StudentsView.xaml | 37 ++++++ StudentManager/Editor/StudentsView.xaml.cs | 28 +++++ StudentManager/Extensions/PrismManager.cs | 13 ++ StudentManager/Model/Students.cs | 107 ++++++++++++++++ StudentManager/StudentManager.csproj | 1 - StudentManager/StudentManager.csproj.user | 12 ++ 15 files changed, 469 insertions(+), 37 deletions(-) create mode 100644 StudentManager/Data/QuestionData.cs delete mode 100644 StudentManager/Data/Student.cs create mode 100644 StudentManager/Data/StudentData.cs create mode 100644 StudentManager/Editor/QuestionsView.xaml create mode 100644 StudentManager/Editor/QuestionsView.xaml.cs create mode 100644 StudentManager/Editor/StudentsView.xaml create mode 100644 StudentManager/Editor/StudentsView.xaml.cs create mode 100644 StudentManager/Extensions/PrismManager.cs create mode 100644 StudentManager/Model/Students.cs diff --git a/StudentManager/App.xaml.cs b/StudentManager/App.xaml.cs index 8fc4e4b..9932779 100644 --- a/StudentManager/App.xaml.cs +++ b/StudentManager/App.xaml.cs @@ -1,4 +1,5 @@ using StudentManager.Editor; +using StudentManager.Model; using System.Configuration; using System.Data; using System.Windows; @@ -17,7 +18,9 @@ namespace StudentManager protected override void RegisterTypes(IContainerRegistry containerRegistry) { - + containerRegistry.RegisterForNavigation(); + containerRegistry.RegisterForNavigation(); + containerRegistry.RegisterForNavigation(); } } diff --git a/StudentManager/Common/SQLHelper.cs b/StudentManager/Common/SQLHelper.cs index 2870872..09fd088 100644 --- a/StudentManager/Common/SQLHelper.cs +++ b/StudentManager/Common/SQLHelper.cs @@ -1,5 +1,6 @@ using DryIoc.ImTools; using MySql.Data.MySqlClient; +using StudentManager.Common; using StudentManager.Data; using System; using System.Collections.Generic; @@ -10,12 +11,65 @@ 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 T Mapping() + public static void Mapping(MySqlDataReader reader) { - typeof(T).Attributes.ToString(); + typeof(T).GetProperties().ForEach(x => { Debug.WriteLine(x.Name.ToLower()); }); } + + public static void Mapping(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 @@ -27,35 +81,91 @@ namespace StudentManager.Common // update ** set ** = '**',... where ** = **; // insert into ** values('**','**'...); // delete from ** where ** = **; - public static T Query(ref T table, params string[] queryParams) where T : IDataCommon + public static List Query(string tableName, int id = -1, params string[] queryParams) where T : IDataCommon, new() { connection = new MySqlConnection(config); - + + List result = new List(); + + 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};"; } - string queryStr = $"select * from {table.TableName};"; cmd = new MySqlCommand(queryStr, connection); MySqlDataReader reader = cmd.ExecuteReader(); - + + + while (reader.Read()) { - for (int i = 0; i < reader.FieldCount; ++i) - { - Debug.WriteLine(reader[i].ToString()); - } + 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 UnionQuery(int id = -1, string tableName = "questions", string otherTable = "user_data") where T : IDataCommon, new() + { + connection = new MySqlConnection(config); + + List result = new List(); + + 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 { diff --git a/StudentManager/Data/QuestionData.cs b/StudentManager/Data/QuestionData.cs new file mode 100644 index 0000000..47c0d4e --- /dev/null +++ b/StudentManager/Data/QuestionData.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace StudentManager.Data +{ + public enum DifficultyLevel + { + easy, + medium, + hard, + } + + public enum QStatus + { + published, + pending_review, + deprecated, + } + + public class QuestionData : IDataCommon + { + public int Id { get; set; } = 0; + public string Type { get; set; } = string.Empty; + public string Stem { get; set; } = string.Empty; + public string Answer { get; set; } = String.Empty; + public DifficultyLevel DifficultyLevel { get; set; } = DifficultyLevel.easy; + public string Category { get; set; } = string.Empty; + public string Tags { get; set; } = string.Empty; + public string Source { get; set; } = string.Empty; + public QStatus Status { get; set; } = QStatus.published; + + + public string TableName { get => "questions"; set => throw new NotImplementedException(); } + } +} diff --git a/StudentManager/Data/Student.cs b/StudentManager/Data/Student.cs deleted file mode 100644 index 7632455..0000000 --- a/StudentManager/Data/Student.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace StudentManager.Data -{ - public class Student - { - public int UId { get; set; } - } -} diff --git a/StudentManager/Data/StudentData.cs b/StudentManager/Data/StudentData.cs new file mode 100644 index 0000000..7f9a08d --- /dev/null +++ b/StudentManager/Data/StudentData.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace StudentManager.Data +{ + public class StudentData : IDataCommon + { + public int UID { get; set; } = 1; + public string Name { get; set; } = "Name"; + public string Gender { get; set; } = "Gender"; + public string Contact { get; set; } = "Contact"; + public string Address { get; set; } = "Address"; + public int Grade { get; set; } = 1; + public int TotalsQuestions { get; set; } = 0; + public int CorrectionCount { get; set; } = 0; + + + + + public string TableName { get => "users"; set => throw new NotImplementedException(); } + } +} diff --git a/StudentManager/Editor/MainEditor.xaml b/StudentManager/Editor/MainEditor.xaml index fff6ee7..c2f09e7 100644 --- a/StudentManager/Editor/MainEditor.xaml +++ b/StudentManager/Editor/MainEditor.xaml @@ -4,9 +4,25 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:StudentManager.Editor" + xmlns:prism="http://prismlibrary.com/" + xmlns:extent="clr-namespace:StudentManager.Extensions" mc:Ignorable="d" Title="MainEditor" Height="450" Width="800"> + - + + + + + + + +