2 // Rafael Mizrahi <rafim@mainsoft.com>
3 // Erez Lotan <erezl@mainsoft.com>
4 // Oren Gurfinkel <oreng@mainsoft.com>
7 // Copyright (c) 2004 Mainsoft Co.
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
31 using System.Data.OleDb ;
33 using System.Collections;
35 // Provide All Data required by the diffderent tests e.g.DataTable, DataRow ...
36 namespace MonoTests.System.Data.Utils
38 public class DataProvider
43 //A string containing all printable charachters.
44 private const string SAMPLE_STRING = "abcdefghijklmnopqrstuvwxyz1234567890~!@#$%^&*()_+-=[]\\|;:,./<>? ";
49 public static DataTable CreateChildDataTable()
51 DataTable dtChild = new DataTable("Child");
52 dtChild.Columns.Add("ParentId",typeof(int));
53 dtChild.Columns.Add("ChildId",typeof(int));
54 dtChild.Columns.Add("String1",typeof(string));
55 dtChild.Columns.Add("String2",typeof(string));
56 dtChild.Columns.Add("ChildDateTime",typeof(DateTime));
57 dtChild.Columns.Add("ChildDouble",typeof(double));
59 dtChild.Rows.Add(new object[] {1,1,"1-String1","1-String2",new DateTime(2000,1,1,0,0,0,0),1.534});
60 dtChild.Rows.Add(new object[] {1,2,"2-String1","2-String2",DateTime.MaxValue ,-1.534});
61 dtChild.Rows.Add(new object[] {1,3,"3-String1","3-String2",DateTime.MinValue,double.MaxValue/10000});
62 dtChild.Rows.Add(new object[] {2,1,"1-String1","1-String2",new DateTime(1973,6,20,0,0,0,0),double.MinValue*10000});
63 dtChild.Rows.Add(new object[] {2,2,"2-String1","2-String2",new DateTime(2008,12,1,13,59,59,59),0.45});
64 dtChild.Rows.Add(new object[] {2,3,"3-String1","3-String2",new DateTime(2003,1,1,1,1,1,1),0.55});
65 dtChild.Rows.Add(new object[] {5,1,"1-String1","1-String2",new DateTime(2002,1,1,1,1,1,1),0});
66 dtChild.Rows.Add(new object[] {5,2,"2-String1","2-String2",new DateTime(2001,1,1,1,1,1,1),10});
67 dtChild.Rows.Add(new object[] {5,3,"3-String1","3-String2",new DateTime(2000,1,1,1,1,1,1),20});
68 dtChild.Rows.Add(new object[] {6,1,"1-String1","1-String2",new DateTime(2000,1,1,1,1,1,0),25});
69 dtChild.Rows.Add(new object[] {6,2,"2-String1","2-String2",new DateTime(2000,1,1,1,1,0,0),30});
70 dtChild.Rows.Add(new object[] {6,3,"3-String1","3-String2",new DateTime(2000,1,1,0,0,0,0),35});
71 dtChild.AcceptChanges();
75 public static DataTable CreateParentDataTable()
77 DataTable dtParent = new DataTable("Parent");
79 dtParent.Columns.Add("ParentId",typeof(int));
80 dtParent.Columns.Add("String1",typeof(string));
81 dtParent.Columns.Add("String2",typeof(string));
83 dtParent.Columns.Add("ParentDateTime",typeof(DateTime));
84 dtParent.Columns.Add("ParentDouble",typeof(double));
85 dtParent.Columns.Add("ParentBool",typeof(bool));
87 dtParent.Rows.Add(new object[] {1,"1-String1","1-String2",new DateTime(2005,1,1,0,0,0,0),1.534,true});
88 dtParent.Rows.Add(new object[] {2,"2-String1","2-String2",new DateTime(2004,1,1,0,0,0,1),-1.534,true});
89 dtParent.Rows.Add(new object[] {3,"3-String1","3-String2",new DateTime(2003,1,1,0,0,1,0),double.MinValue*10000,false});
90 dtParent.Rows.Add(new object[] {4,"4-String1","4-String2",new DateTime(2002,1,1,0,1,0,0),double.MaxValue/10000,true});
91 dtParent.Rows.Add(new object[] {5,"5-String1","5-String2",new DateTime(2001,1,1,1,0,0,0),0.755,true});
92 dtParent.Rows.Add(new object[] {6,"6-String1","6-String2",new DateTime(2000,1,1,0,0,0,0),0.001,false});
93 dtParent.AcceptChanges();
97 //This method replace the DataSet GetXmlSchema method
98 //used to compare DataSets
99 //Created by Ofer (13-Nov-03) becuase DataSet GetXmlSchema method is not yet implemented in java
100 public static string GetDSSchema(DataSet ds)
102 string strSchema = "DataSet Name=" + ds.DataSetName + "\n";
104 foreach (DataRelation dl in ds.Relations)
106 strSchema += "\t" + "DataRelation Name=" + dl.RelationName ;
107 foreach (DataColumn dc in dl.ParentColumns)
108 strSchema += "\t" + "ParentColummn=" + dc.ColumnName ;
109 foreach (DataColumn dc in dl.ChildColumns )
110 strSchema += "\t" + "ChildColumn=" + dc.ColumnName ;
114 foreach (DataTable dt in ds.Tables)
116 strSchema += "Table=" + dt.TableName + "\t";
118 strSchema += "Constraints =";
119 foreach (Constraint cs in dt.Constraints )
120 strSchema += cs.GetType().Name + ", ";
122 //Get PrimaryKey Columns
123 strSchema += "PrimaryKey Columns index:=";
124 foreach (DataColumn dc in dt.PrimaryKey)
125 strSchema += dc.Ordinal + ", ";
128 foreach (DataColumn dc in dt.Columns)
130 strSchema += "ColumnName=" + dc.ColumnName + "\t" +
131 "ColumnType=" + dc.DataType.Name + "\t" +
132 "AllowDBNull=" + dc.AllowDBNull.ToString() + "\t" +
133 "DefaultValue=" + dc.DefaultValue.ToString() + "\t" +
134 "Unique=" + dc.Unique.ToString() + "\t" +
135 "ReadOnly=" + dc.ReadOnly.ToString() + "\n" ;
142 public static DataTable CreateUniqueConstraint()
144 DataTable dt = DataProvider.CreateParentDataTable();
145 return CreateUniqueConstraint(dt);
148 public static DataTable CreateUniqueConstraint(DataTable dt)
150 Constraint con = new UniqueConstraint(dt.Columns["ParentId"]);
151 dt.Constraints.Add(con);
155 public static void TryToBreakUniqueConstraint()
157 //Create the constraint
158 DataTable dt = CreateUniqueConstraint();
159 //Try to violate the constraint
161 DataRow dr1 = dt.NewRow();
166 public static DataSet CreateForigenConstraint()
168 DataTable parent = DataProvider.CreateParentDataTable();
169 DataTable child = DataProvider.CreateChildDataTable();
170 DataSet ds = new DataSet();
171 ds.Tables.Add(parent);
172 ds.Tables.Add(child);
174 Constraint con1 = new ForeignKeyConstraint(parent.Columns[0],child.Columns[0]);
175 child.Constraints.Add(con1);
180 public static void TryToBreakForigenConstraint()
182 DataSet ds = CreateForigenConstraint();
185 DataRow dr = ds.Tables[1].NewRow();
187 ds.Tables[1].Rows.Add(dr);
190 ds.EnforceConstraints=true;