DataProvider.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // Authors:
  2. // Rafael Mizrahi <[email protected]>
  3. // Erez Lotan <[email protected]>
  4. // Oren Gurfinkel <[email protected]>
  5. // Ofer Borstein
  6. //
  7. // Copyright (c) 2004 Mainsoft Co.
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Data;
  30. using System.Data.OleDb ;
  31. using System.IO;
  32. using System.Collections;
  33. // Provide All Data required by the diffderent tests e.g.DataTable, DataRow ...
  34. namespace MonoTests.System.Data.Utils
  35. {
  36. public class DataProvider
  37. {
  38. #region Constatntas
  39. #region Private
  40. //A string containing all printable charachters.
  41. private const string SAMPLE_STRING = "abcdefghijklmnopqrstuvwxyz1234567890~!@#$%^&*()_+-=[]\\|;:,./<>? ";
  42. #endregion
  43. #endregion
  44. public static DataTable CreateChildDataTable()
  45. {
  46. DataTable dtChild = new DataTable("Child");
  47. dtChild.Columns.Add("ParentId",typeof(int));
  48. dtChild.Columns.Add("ChildId",typeof(int));
  49. dtChild.Columns.Add("String1",typeof(string));
  50. dtChild.Columns.Add("String2",typeof(string));
  51. dtChild.Columns.Add("ChildDateTime",typeof(DateTime));
  52. dtChild.Columns.Add("ChildDouble",typeof(double));
  53. dtChild.Rows.Add(new object[] {1,1,"1-String1","1-String2",new DateTime(2000,1,1,0,0,0,0),1.534});
  54. dtChild.Rows.Add(new object[] {1,2,"2-String1","2-String2",DateTime.MaxValue ,-1.534});
  55. dtChild.Rows.Add(new object[] {1,3,"3-String1","3-String2",DateTime.MinValue,double.MaxValue/10000});
  56. dtChild.Rows.Add(new object[] {2,1,"1-String1","1-String2",new DateTime(1973,6,20,0,0,0,0),double.MinValue*10000});
  57. dtChild.Rows.Add(new object[] {2,2,"2-String1","2-String2",new DateTime(2008,12,1,13,59,59,59),0.45});
  58. dtChild.Rows.Add(new object[] {2,3,"3-String1","3-String2",new DateTime(2003,1,1,1,1,1,1),0.55});
  59. dtChild.Rows.Add(new object[] {5,1,"1-String1","1-String2",new DateTime(2002,1,1,1,1,1,1),0});
  60. dtChild.Rows.Add(new object[] {5,2,"2-String1","2-String2",new DateTime(2001,1,1,1,1,1,1),10});
  61. dtChild.Rows.Add(new object[] {5,3,"3-String1","3-String2",new DateTime(2000,1,1,1,1,1,1),20});
  62. dtChild.Rows.Add(new object[] {6,1,"1-String1","1-String2",new DateTime(2000,1,1,1,1,1,0),25});
  63. dtChild.Rows.Add(new object[] {6,2,"2-String1","2-String2",new DateTime(2000,1,1,1,1,0,0),30});
  64. dtChild.Rows.Add(new object[] {6,3,"3-String1","3-String2",new DateTime(2000,1,1,0,0,0,0),35});
  65. dtChild.AcceptChanges();
  66. return dtChild;
  67. }
  68. public static DataTable CreateParentDataTable()
  69. {
  70. DataTable dtParent = new DataTable("Parent");
  71. dtParent.Columns.Add("ParentId",typeof(int));
  72. dtParent.Columns.Add("String1",typeof(string));
  73. dtParent.Columns.Add("String2",typeof(string));
  74. dtParent.Columns.Add("ParentDateTime",typeof(DateTime));
  75. dtParent.Columns.Add("ParentDouble",typeof(double));
  76. dtParent.Columns.Add("ParentBool",typeof(bool));
  77. dtParent.Rows.Add(new object[] {1,"1-String1","1-String2",new DateTime(2005,1,1,0,0,0,0),1.534,true});
  78. dtParent.Rows.Add(new object[] {2,"2-String1","2-String2",new DateTime(2004,1,1,0,0,0,1),-1.534,true});
  79. dtParent.Rows.Add(new object[] {3,"3-String1","3-String2",new DateTime(2003,1,1,0,0,1,0),double.MinValue*10000,false});
  80. dtParent.Rows.Add(new object[] {4,"4-String1","4-String2",new DateTime(2002,1,1,0,1,0,0),double.MaxValue/10000,true});
  81. dtParent.Rows.Add(new object[] {5,"5-String1","5-String2",new DateTime(2001,1,1,1,0,0,0),0.755,true});
  82. dtParent.Rows.Add(new object[] {6,"6-String1","6-String2",new DateTime(2000,1,1,0,0,0,0),0.001,false});
  83. dtParent.AcceptChanges();
  84. return dtParent;
  85. }
  86. //This method replace the DataSet GetXmlSchema method
  87. //used to compare DataSets
  88. //Created by Ofer (13-Nov-03) becuase DataSet GetXmlSchema method is not yet implemented in java
  89. public static string GetDSSchema(DataSet ds)
  90. {
  91. string strSchema = "DataSet Name=" + ds.DataSetName + "\n";
  92. //Get relations
  93. foreach (DataRelation dl in ds.Relations)
  94. {
  95. strSchema += "\t" + "DataRelation Name=" + dl.RelationName ;
  96. foreach (DataColumn dc in dl.ParentColumns)
  97. strSchema += "\t" + "ParentColummn=" + dc.ColumnName ;
  98. foreach (DataColumn dc in dl.ChildColumns )
  99. strSchema += "\t" + "ChildColumn=" + dc.ColumnName ;
  100. strSchema += "\n";
  101. }
  102. //Get teables
  103. foreach (DataTable dt in ds.Tables)
  104. {
  105. strSchema += "Table=" + dt.TableName + "\t";
  106. //Get Constraints
  107. strSchema += "Constraints =";
  108. foreach (Constraint cs in dt.Constraints )
  109. strSchema += cs.GetType().Name + ", ";
  110. strSchema += "\n";
  111. //Get PrimaryKey Columns
  112. strSchema += "PrimaryKey Columns index:=";
  113. foreach (DataColumn dc in dt.PrimaryKey)
  114. strSchema += dc.Ordinal + ", ";
  115. strSchema += "\n";
  116. //Get Columns
  117. foreach (DataColumn dc in dt.Columns)
  118. {
  119. strSchema += "ColumnName=" + dc.ColumnName + "\t" +
  120. "ColumnType=" + dc.DataType.Name + "\t" +
  121. "AllowDBNull=" + dc.AllowDBNull.ToString() + "\t" +
  122. "DefaultValue=" + dc.DefaultValue.ToString() + "\t" +
  123. "Unique=" + dc.Unique.ToString() + "\t" +
  124. "ReadOnly=" + dc.ReadOnly.ToString() + "\n" ;
  125. }
  126. strSchema += "\n";
  127. }
  128. return strSchema;
  129. }
  130. public static DataTable CreateUniqueConstraint()
  131. {
  132. DataTable dt = DataProvider.CreateParentDataTable();
  133. return CreateUniqueConstraint(dt);
  134. }
  135. public static DataTable CreateUniqueConstraint(DataTable dt)
  136. {
  137. Constraint con = new UniqueConstraint(dt.Columns["ParentId"]);
  138. dt.Constraints.Add(con);
  139. return dt;
  140. }
  141. public static void TryToBreakUniqueConstraint()
  142. {
  143. //Create the constraint
  144. DataTable dt = CreateUniqueConstraint();
  145. //Try to violate the constraint
  146. DataRow dr1 = dt.NewRow();
  147. dr1[0] = 1;
  148. dt.Rows.Add(dr1);
  149. }
  150. public static DataSet CreateForigenConstraint()
  151. {
  152. DataTable parent = DataProvider.CreateParentDataTable();
  153. DataTable child = DataProvider.CreateChildDataTable();
  154. DataSet ds = new DataSet();
  155. ds.Tables.Add(parent);
  156. ds.Tables.Add(child);
  157. Constraint con1 = new ForeignKeyConstraint(parent.Columns[0],child.Columns[0]);
  158. child.Constraints.Add(con1);
  159. return ds;
  160. }
  161. public static void TryToBreakForigenConstraint()
  162. {
  163. DataSet ds = CreateForigenConstraint();
  164. //Code to break:
  165. DataRow dr = ds.Tables[1].NewRow();
  166. dr[0]=7;
  167. ds.Tables[1].Rows.Add(dr);
  168. ds.AcceptChanges();
  169. ds.EnforceConstraints=true;
  170. }
  171. }
  172. }