Advertisement
GeorgePashev_88

Untitled

Oct 31st, 2023
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. using System.Data.SQLite;
  2. using System.Dynamic;
  3.  
  4. namespace BananaStore
  5. {
  6. public delegate void DbCallback(dynamic message);
  7.  
  8. public abstract class DatabaseCommand
  9. {
  10. public string TableName { get; set; }
  11. public ExpandoObject Record { get; set; }
  12. public DbCallback Callback { get; set; }
  13.  
  14. protected DatabaseCommand(string tableName,
  15. ExpandoObject record, DbCallback callback)
  16. {
  17. TableName = tableName;
  18. Record = record;
  19. Callback = callback;
  20. }
  21.  
  22. public abstract void Execute(SQLiteConnection connection);
  23. }
  24.  
  25. /// <summary>
  26. /// insert command
  27. /// </summary>
  28.  
  29. public class InsertCommand : DatabaseCommand
  30. {
  31. public InsertCommand(string tableName, ExpandoObject record,
  32. DbCallback callback)
  33. : base(tableName, record, callback) { }
  34.  
  35. public override void Execute(SQLiteConnection connection)
  36. {
  37. var dict = Record as IDictionary<string, object>;
  38. if (dict != null)
  39. {
  40. string columns = string.Join(",", dict.Keys);
  41. string values = string.Join(",", dict.Values);
  42. string paramNames = string.Join(",", dict.Keys.Select(k => "@" + k));
  43.  
  44. string sql = $"INSERT INTO {TableName}({columns}) VALUES({paramNames})";
  45.  
  46. using(var cmd = new SQLiteCommand(sql, connection))
  47. {
  48. foreach(var pair in dict)
  49. {
  50. cmd.Parameters.AddWithValue("@"+pair.Key, pair.Value);
  51. }
  52. try
  53. {
  54. cmd.ExecuteNonQuery();
  55. Callback("Insert Success");
  56.  
  57. }catch(Exception ex)
  58. {
  59. Callback($"Exception: {ex.Message}");
  60. }
  61. }
  62.  
  63.  
  64. }
  65. }
  66. }
  67.  
  68. /// <summary>
  69. /// create command
  70. /// </summary>
  71. ///
  72.  
  73. public class CreateCommand : DatabaseCommand
  74. {
  75. public CreateCommand(string tableName, ExpandoObject recordTemplate,
  76. DbCallback callback)
  77. : base(tableName, recordTemplate, callback) { }
  78.  
  79. private string GetTypeForValue(object value)
  80. {
  81. if (value is int) return "INTEGER";
  82. else if (value is string) return "TEXT";
  83. else if (value is double || value is float) return "REAL";
  84. else if (value is byte[]) return "BLOB";
  85. else
  86. throw new
  87. NotSupportedException($"Type {value.GetType().Name} not supported.");
  88.  
  89.  
  90. }
  91.  
  92. public override void Execute(SQLiteConnection connection)
  93. {
  94. var dict = Record as IDictionary<string, object> ;
  95. if(dict != null)
  96. {
  97. var columnDefinitions = dict.Select(pair => {
  98. string type = GetTypeForValue(pair.Value);
  99. return $"{pair.Key} {type}";
  100. });
  101.  
  102. string columns = string.Join(", ", columnDefinitions) ;
  103. string sql = $"CREATE TABLE IF NOT EXISTS " +
  104. $" {TableName}(ID integer PRIMARY KEY AUTOINCREMENT, " +
  105. $"{columns})";
  106. }
  107.  
  108. }
  109. }
  110.  
  111.  
  112. internal class Program
  113. {
  114. static void Main(string[] args)
  115. {
  116. Console.WriteLine("Hello, World!");
  117. }
  118. }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement