2010年3月18日木曜日

SQLite-型のないカラム

SQLiteはデータ型を指定せずにカラムを作成することがで、様々な型を適切(?)に格納することができる。
これは、オブジェクトをプロパティで分割して格納する際に非常に都合がよい。

というわけで、検証。
DecimalがDoubleとして格納されるために有効桁数が小さくなってしまってるので困りもの。どうにか回避方法はないものか。

  1. static void Main(string[] args)  
  2. {  
  3. ConnectionStringSettings setting = ConfigurationManager.ConnectionStrings["SQLiteConnection"];  
  4. DbProviderFactory factory = DbProviderFactories.GetFactory(setting.ProviderName);  
  5.   
  6. using (DbConnection connection = factory.CreateConnection())  
  7. {  
  8. connection.ConnectionString = setting.ConnectionString;  
  9. connection.Open();  
  10. DbCommand insertCommand = connection.CreateCommand();  
  11. insertCommand.CommandText = "Insert into Data Values (?, ?, ?)";  
  12. {  
  13. DbParameter p1 = factory.CreateParameter();  
  14. p1.DbType = DbType.Int32;  
  15. insertCommand.Parameters.Add(p1);  
  16. DbParameter p2 = factory.CreateParameter();  
  17. p2.DbType = DbType.String;  
  18. insertCommand.Parameters.Add(p2);  
  19. DbParameter p3 = factory.CreateParameter();  
  20. p3.DbType = DbType.Object;  
  21. insertCommand.Parameters.Add(p3);  
  22. }  
  23. PropertyInfo[] props = typeof(Data).GetProperties();  
  24. var propDic = props.ToDictionary(x => x.Name);  
  25. using (DbTransaction tran = connection.BeginTransaction())  
  26. {  
  27. foreach (var data in GetPersonList().Take(2))  
  28. {  
  29. insertCommand.Parameters[0].Value = data.IntVal;  
  30. foreach (var prop in props)  
  31. {  
  32. insertCommand.Parameters[1].Value = prop.Name;  
  33. insertCommand.Parameters[2].Value = prop.GetValue(data, null);  
  34. insertCommand.ExecuteNonQuery();  
  35. }  
  36. }  
  37. tran.Commit();  
  38. }  
  39.   
  40. DbCommand selectCommand = connection.CreateCommand();  
  41. selectCommand.CommandText = "Select * From Data";  
  42. using (DbDataReader reader = selectCommand.ExecuteReader())  
  43. {  
  44. while (reader.Read())  
  45. {  
  46. var prop = propDic[reader.GetString(1)];  
  47. Console.Write(prop.Name + ":" + reader.GetFieldType(2) + ":");  
  48. if (prop.PropertyType == typeof(int))  
  49. {  
  50. Console.WriteLine(reader.GetInt32(2));  
  51. }  
  52. else if (prop.PropertyType == typeof(long))  
  53. {  
  54. Console.WriteLine(reader.GetInt64(2));  
  55. }  
  56. else if (prop.PropertyType == typeof(string))  
  57. {  
  58. Console.WriteLine(reader.GetString(2));  
  59. }  
  60. else if (prop.PropertyType == typeof(bool))  
  61. {  
  62. Console.WriteLine(reader.GetBoolean(2));  
  63. }  
  64. else if (prop.PropertyType == typeof(DateTime))  
  65. {  
  66. Console.WriteLine(reader.GetDateTime(2));  
  67. }  
  68. else if (prop.PropertyType == typeof(double))  
  69. {  
  70. Console.WriteLine(reader.GetDouble(2));  
  71. }  
  72. else if (prop.PropertyType == typeof(decimal))  
  73. {  
  74. Console.WriteLine(reader.GetDecimal(2));  
  75. }  
  76. }  
  77. }  
  78. }  
  79. }  
  80.   
  81. static IEnumerable<Data> GetPersonList()  
  82. {  
  83. int id = 0;  
  84. while (true)  
  85. {  
  86. yield return new Data  
  87. {  
  88. IntVal = id,  
  89. LongVal = long.MaxValue,  
  90. Text = Path.GetRandomFileName(),  
  91. Flag = id % 2 == 0,  
  92. Date = new DateTime(2010, 1, 1).AddDays(id),  
  93. DoubleVal = double.MaxValue,  
  94. DecimalVal = decimal.MaxValue,  
  95. };  
  96. id++;  
  97. }  
  98. }  
  99.   
  100. private class Data  
  101. {  
  102. public int IntVal { getset; }  
  103. public long LongVal { getset; }  
  104. public string Text { getset; }  
  105. public bool Flag { getset; }  
  106. public DateTime Date { getset; }  
  107. public double DoubleVal { getset; }  
  108. public decimal DecimalVal { getset; }  
  109. }  

0 件のコメント: