diff --git a/TimeTable/TimeTable.csproj b/TimeTable/TimeTable.csproj index 0ddf874..ceb5a96 100644 --- a/TimeTable/TimeTable.csproj +++ b/TimeTable/TimeTable.csproj @@ -9,7 +9,9 @@ - + + + \ No newline at end of file diff --git a/TimeTable/controll/SqliteCommands.cs b/TimeTable/controll/SqliteCommands.cs new file mode 100644 index 0000000..91d245f --- /dev/null +++ b/TimeTable/controll/SqliteCommands.cs @@ -0,0 +1,160 @@ +using Microsoft.Data.Sqlite; +using System.Globalization; +using TimeTable.model; + +namespace TimeTable.controll { + internal static class SqliteCommands { + + public static string select = "select uid, start, end, text, time from timetable"; + private static CultureInfo ci = System.Globalization.CultureInfo.GetCultureInfo("de-de"); + private static string insertPrefix = "insert into timetable ("; + private static string insertIntermediate = ") values("; + private static string insertPostfix = ");"; + + private static string updatePrefix = "update timetable set "; + private static string updateIntermediate = " where uid = "; + private static string updatePostfix = ";"; + + public static UidCommand InsertCmd(DateTime start) { + string uid = Guid.NewGuid().ToString(); + return BuildInsertCommand(uid, "uid, start", uid + "," + start.ToString()); + } + + private static UidCommand BuildInsertCommand(string uid, string fields, string values) { + string command = insertPrefix + fields + insertIntermediate + values + insertPostfix; + UidCommand uidCommand = new UidCommand(); + uidCommand.uid = uid; + uidCommand.command = command; + return uidCommand; + } + + + private static string BuildUpdateCmd(string uid, string fieldValues) { + return updatePrefix + fieldValues + updateIntermediate + uid + updatePostfix; + } + + + public static UidCommand InsertCmd(DateTime start, string text) { + string uid = Guid.NewGuid().ToString(); + return BuildInsertCommand(uid, "uid, start", uid + "," + start.ToString()); + } + + + public static string UpdateCmdCreator(string uid, string[] keys, string[] values) { + if (keys.Length != values.Length) { + throw new ArgumentException(); + } + + string fieldValues = ""; + for (int i = 0; i < keys.Length; i++) { + fieldValues += keys[i] + " = " + values[i] + ","; + } + + fieldValues = fieldValues.TrimEnd(','); + + + return BuildUpdateCmd(uid, fieldValues); + } + + + public static string UpdateCmd(string uid, DateTime start, DateTime end, string text) { + string[] keys = new string[3]; + keys[0] = "uid"; + keys[1] = "start"; + keys[2] = "end"; + keys[3] = "text"; + string[] values = new string[3]; + values[0] = uid; + values[1] = start.ToString(); + values[2] = end.ToString(); + values[3] = text; + return UpdateCmdCreator(uid, keys, values); + } + + + public static string UpdateStartCmd(string uid, DateTime start) { + string[] keys = new string[2]; + keys[0] = "uid"; + keys[1] = "start"; + string[] values = new string[2]; + values[0] = uid; + values[1] = start.ToString(); + return UpdateCmdCreator(uid, keys, values); + } + public static string UpdateEndCmd(string uid, DateTime end) { + string[] keys = new string[2]; + keys[0] = uid; + keys[1] = "end"; + string[] values = new string[2]; + values[0] = uid; + values[1] = end.ToString(); + return UpdateCmdCreator(uid, keys, values); + } + + + public static string UpdateTextCmd(string uid, string text) { + string[] keys = new string[2]; + keys[0] = uid; + keys[1] = "text"; + string[] values = new string[2]; + values[0] = uid; + values[1] = text; + return UpdateCmdCreator(uid, keys, values); + } + + + public static string UpdateStartTextCmd(string uid, DateTime start, string text) { + string[] keys = new string[3]; + keys[0] = "uid"; + keys[1] = "start"; + keys[2] = "text"; + string[] values = new string[3]; + values[0] = uid; + values[1] = start.ToString(); + values[2] = text; + return UpdateCmdCreator(uid, keys, values); + } + public static string UpdateEndTextCmd(string uid, DateTime end, string text) { + string[] keys = new string[3]; + keys[0] = "uid"; + keys[1] = "start"; + keys[2] = "text"; + string[] values = new string[3]; + values[0] = uid; + values[1] = end.ToString(); + values[2] = text; + return UpdateCmdCreator(uid, keys, values); + } + + + public static string UpdateTimeCmd(string uid, TimeSpan timespan) { + string[] keys = new string[2]; + keys[0] = uid; + keys[1] = "time"; + string[] values = new string[2]; + values[0] = uid; + values[1] = timespan.ToString(); + return UpdateCmdCreator(uid, keys, values); + } + + + + public static void CreateTableIfNotExists(string sqlConnection) { + SqliteConnection con = new SqliteConnection(sqlConnection); + con.Open(); + SqliteCommand sqliteCmd = con.CreateCommand(); + sqliteCmd.CommandText = @"SELECT name FROM sqlite_master WHERE type='table' AND name='timetable';"; + + var name = sqliteCmd.ExecuteScalar(); + if (name != null && name.ToString() == "timetable") { + return; + } + var cols = string.Join(" VARCHAR,", "uid, start, end, text, time"); + cols = cols + " Varchar"; + sqliteCmd.CommandText = "CREATE TABLE timetable (" + cols + ")"; + con.Open(); + sqliteCmd.ExecuteNonQuery(); + con.Close(); + } + } +} diff --git a/TimeTable/model/Basic.cs b/TimeTable/model/Basic.cs index 616b51a..0637dc2 100644 --- a/TimeTable/model/Basic.cs +++ b/TimeTable/model/Basic.cs @@ -1,5 +1,8 @@ namespace TimeTable.model { internal class Basic { + + + string uid { get; set; } DateTime time { get; set; } string name { get; set; } } diff --git a/TimeTable/model/UidCommand.cs b/TimeTable/model/UidCommand.cs new file mode 100644 index 0000000..d06ae11 --- /dev/null +++ b/TimeTable/model/UidCommand.cs @@ -0,0 +1,6 @@ +namespace TimeTable.model { + internal class UidCommand { + public string uid { get; set; } + public string command { get; set; } + } +} diff --git a/TimeTable/view/Main.Designer.cs b/TimeTable/view/Main.Designer.cs index 02dd764..339ada0 100644 --- a/TimeTable/view/Main.Designer.cs +++ b/TimeTable/view/Main.Designer.cs @@ -1,4 +1,10 @@ -namespace TimeTable +using Microsoft.Data.SqlClient; +using Microsoft.Data.Sqlite; +using System.Drawing; +using TimeTable.controll; +using TimeTable.model; + +namespace TimeTable { partial class Main { @@ -33,11 +39,17 @@ btnStopResume = new Button(); btnStart = new Button(); tableView = new Panel(); - dataGridView1 = new DataGridView(); + taskTimeGridView = new DataGridView(); btnPanel.SuspendLayout(); tableView.SuspendLayout(); - ((System.ComponentModel.ISupportInitialize)dataGridView1).BeginInit(); + ((System.ComponentModel.ISupportInitialize)taskTimeGridView).BeginInit(); SuspendLayout(); + + + sqlConnectionString = "Data Source=" + Directory.GetCurrentDirectory() + "\\sqllite.db"; + SqliteCommands.CreateTableIfNotExists(sqlConnectionString); + dataAdapter = new SqlDataAdapter("SELECT * from timetable", sqlConnectionString); + // // btnPanel // @@ -96,22 +108,23 @@ // tableView // tableView.AutoSizeMode = AutoSizeMode.GrowAndShrink; - tableView.Controls.Add(dataGridView1); + tableView.Controls.Add(taskTimeGridView); tableView.Dock = DockStyle.Right; tableView.Location = new Point(239, 0); tableView.Name = "tableView"; tableView.Size = new Size(477, 171); tableView.TabIndex = 1; // - // dataGridView1 + // taskTimeGridView // - dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; - dataGridView1.Dock = DockStyle.Fill; - dataGridView1.Location = new Point(0, 0); - dataGridView1.Name = "dataGridView1"; - dataGridView1.RowHeadersWidth = 51; - dataGridView1.Size = new Size(477, 171); - dataGridView1.TabIndex = 0; + taskTimeGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + taskTimeGridView.Dock = DockStyle.Fill; + taskTimeGridView.Location = new Point(0, 0); + taskTimeGridView.Name = "taskTimeGridView"; + taskTimeGridView.RowHeadersWidth = 51; + taskTimeGridView.Size = new Size(477, 171); + taskTimeGridView.TabIndex = 0; + taskTimeGridView.DataSource = dataAdapter; // // Main // @@ -124,14 +137,24 @@ Text = "TimeTable"; btnPanel.ResumeLayout(false); tableView.ResumeLayout(false); - ((System.ComponentModel.ISupportInitialize)dataGridView1).EndInit(); + ((System.ComponentModel.ISupportInitialize)taskTimeGridView).EndInit(); ResumeLayout(false); } + private void executeSql(string command) { + SqliteConnection con = new SqliteConnection(sqlConnectionString); + SqliteCommand sqliteCmd = con.CreateCommand(); + sqliteCmd.CommandText = command; + con.Open(); + sqliteCmd.ExecuteNonQuery(); + con.Close(); + } private void btnStartClick(object sender, EventArgs e) { - + UidCommand uidCmd = SqliteCommands.InsertCmd(DateTime.Now); + uid = uidCmd.uid; + executeSql(uidCmd.command); } private void btnTaskStartClick(object sender, EventArgs e) { @@ -139,6 +162,9 @@ } private void btnStopResumeClick(object sender, EventArgs e) { + String cmd = SqliteCommands.UpdateEndCmd(uid, DateTime.Now); + executeSql(cmd); + taskTimeGridView.DataSource = dataAdapter; } private void btnTaskStopClick(object sender, EventArgs e) { @@ -155,6 +181,9 @@ private Button btnStopResume; private Button btnStart; private Panel tableView; - private DataGridView dataGridView1; + private DataGridView taskTimeGridView; + private string uid; + string sqlConnectionString; + private SqlDataAdapter dataAdapter; } }