public void ReadXmlSchema (XmlReader reader) { #if true XmlSchemaDataImporter xsdImporter = new XmlSchemaDataImporter (this, reader, true); xsdImporter.Process (); tableAdapterSchemaInfo = xsdImporter.CurrentAdapter; #else XmlSchemaMapper SchemaMapper = new XmlSchemaMapper (this); SchemaMapper.Read (reader); #endif }
private void CreateShortCommandMethods (CodeTypeDeclaration t, TableAdapterSchemaInfo taInfo) { }
private void ProcessTableAdapter (XmlElement el, DbProviderFactory provider, string connStr) { XmlElement e; string datasetTableName = null; if (el == null) return; // #325464 debugging //Console.WriteLine ("in ProcessTableAdapters..."); currentAdapter = new TableAdapterSchemaInfo (provider); currentAdapter.ConnectionString = connStr; //Console.WriteLine ("Provider: {0}, connection: {1}, adapter: {2}", // provider, currentAdapter.Connection, currentAdapter.Adapter); currentAdapter.BaseClass = el.GetAttribute ("BaseClass"); datasetTableName = el.GetAttribute ("Name"); currentAdapter.Name = el.GetAttribute ("GeneratorDataComponentClassName"); if (String.IsNullOrEmpty (currentAdapter.Name)) currentAdapter.Name = el.GetAttribute ("DataAccessorName"); //Console.WriteLine ("Name: "+currentAdapter.Name); foreach (XmlNode n in el.ChildNodes) { e = n as XmlElement; //Console.WriteLine ("Children of Tables: "+e.LocalName); if (e == null) continue; switch (e.LocalName) { case "MainSource": case "Sources": foreach (XmlNode msn in e.ChildNodes) ProcessDbSource (msn as XmlElement); break; case "Mappings": DataTableMapping tableMapping = new DataTableMapping (); tableMapping.SourceTable = "Table"; tableMapping.DataSetTable = datasetTableName; foreach (XmlNode mps in e.ChildNodes) ProcessColumnMapping (mps as XmlElement, tableMapping); currentAdapter.Adapter.TableMappings.Add (tableMapping); break; } } }
private void CreateDbSourceMethods (CodeTypeDeclaration t, TableAdapterSchemaInfo taInfo) { string tmp = null; CodeMemberMethod m = null; CodeExpression expr, expr1, expr2; CodeStatement stmt; string tmpScalarVal = null; expr = PropRef (PropRef ("Adapter"), "SelectCommand"); // loop through the collection and generate the code for (int idx = 0; idx < taInfo.Commands.Count; idx++) { DbCommandInfo cmdInfo = (DbCommandInfo)taInfo.Commands[idx]; foreach (DbSourceMethodInfo mInfo in cmdInfo.Methods) { //Console.WriteLine ("Generating code for {0} method", mInfo.Name); // TODO: Add support for Fill methods if (mInfo.MethodType == GenerateMethodsType.Fill) continue; m = new CodeMemberMethod (); m.Name = mInfo.Name; stmt = Let (expr, IndexerRef (PropRef ("CommandCollection"), Const (idx))); m.Statements.Add (stmt); switch ((MemberAttributes) Enum.Parse (typeof (MemberAttributes), mInfo.Modifier)) { case MemberAttributes.Public: m.Attributes = MemberAttributes.Public; break; case MemberAttributes.Private: m.Attributes = MemberAttributes.Private; break; case MemberAttributes.Assembly: m.Attributes = MemberAttributes.Assembly; break; case MemberAttributes.Family: m.Attributes = MemberAttributes.Family; break; } //Console.WriteLine ("QueryType: {0}", mInfo.QueryType); QueryType qType = (QueryType) Enum.Parse (typeof (QueryType), mInfo.QueryType); switch (qType) { case QueryType.Scalar: case QueryType.NoData: // executes non query and returns status m.ReturnType = TypeRef (typeof (int)); AddGeneratedMethodParametersAndStatements (m, expr, cmdInfo.Command); // store connection state tmp = typeof (System.Data.ConnectionState).FullName; expr1 = PropRef (Local ("command"), "Connection"); expr2 = PropRef (PropRef (Local ("System"), "Data"), "ConnectionState"); stmt = VarDecl (tmp, "previousConnectionState", PropRef (expr1, "State")); m.Statements.Add (stmt); // Open connection, if its not already CodeExpression expr3 = BitOps (PropRef (expr1, "State"), PropRef (expr2, "Open"), CodeBinaryOperatorType.BitwiseAnd); stmt = new CodeConditionStatement (Inequals (expr3, PropRef (expr2, "Open")), new CodeStatement[] {Eval (MethodInvoke (expr1, "Open", new CodeExpression[] {}))}, new CodeStatement[] {}); m.Statements.Add (stmt); // declare return variable and execute query CodeTryCatchFinallyStatement try1 = new CodeTryCatchFinallyStatement (); if (qType == QueryType.NoData) { m.Statements.Add (VarDecl (typeof (int), "returnValue", null)); expr3 = MethodInvoke (Local ("command"), "ExecuteNonQuery", new CodeExpression[] {}); } else { tmpScalarVal = mInfo.ScalarCallRetval.Substring (0, mInfo.ScalarCallRetval.IndexOf (',')); m.Statements.Add (VarDecl (TypeRef (tmpScalarVal).BaseType, "returnValue", null)); expr3 = MethodInvoke (Local ("command"), "ExecuteScalar", new CodeExpression[] {}); } // Execute query try1.TryStatements.Add (Let (Local ("returnValue"), expr3)); // fill finally block stmt = new CodeConditionStatement (Equals (Local ("previousConnectionState"), PropRef (expr2, "Closed")), new CodeStatement[] {Eval (MethodInvoke (expr1, "Close", new CodeExpression[] {}))}, new CodeStatement[] {}); try1.FinallyStatements.Add (stmt); m.Statements.Add (try1); // return the value if (qType == QueryType.NoData) { m.Statements.Add (Return (Local ("returnValue"))); } else { expr2 = Equals (Local ("returnValue"), Const (null)); expr3 = Equals (MethodInvoke (Local ("returnValue"), "GetType", new CodeExpression[] {}), TypeOfRef ("System.DBNull")); stmt = new CodeConditionStatement (BooleanOps (expr2, expr3, CodeBinaryOperatorType.BooleanOr), new CodeStatement[] {Return (Const (null))}, new CodeStatement[] {Return (Cast (tmpScalarVal, Local ("returnValue")))}); m.Statements.Add (stmt); } break; case QueryType.Rowset: // returns DataTable // TODO: Handle multiple DataTables tmp = opts.DataSetName (ds.DataSetName) + "." + opts.TableTypeName (ds.Tables[0].TableName); m.ReturnType = TypeRef (tmp); AddGeneratedMethodParametersAndStatements (m, expr, cmdInfo.Command); stmt = VarDecl (tmp, "dataTable", New (tmp)); m.Statements.Add (stmt); // fill datatable expr = PropRef ("Adapter"); stmt = Eval (MethodInvoke (expr, "Fill", Local ("dataTable"))); m.Statements.Add (stmt); // return statement m.Statements.Add (Return (Local ("dataTable"))); break; } t.Members.Add (m); } } }
private void CreateCommandCollectionInitializeMethod (CodeTypeDeclaration t, TableAdapterSchemaInfo taInfo) { //string tmp = null; Type type; CodeMemberMethod m = new CodeMemberMethod (); m.Name = "InitCommandCollection"; m.Attributes = MemberAttributes.Private; // code statements CodeExpression expr, expr1; CodeStatement stmt; type = ((DbCommandInfo)taInfo.Commands[0]).Command.GetType(); // initialize connection expr = FieldRef ("_commandCollection"); stmt = Let (expr, NewArray (type, taInfo.Commands.Count)); m.Statements.Add (stmt); // loop through the collection and generate the code for (int idx = 0; idx < taInfo.Commands.Count; idx++) { DbCommand cmd = ((DbCommandInfo)taInfo.Commands[idx]).Command; // Allocate expr1 = IndexerRef (expr, Const (idx)); stmt = Let (expr1, New (type)); m.Statements.Add (stmt); // Initialize cmd members stmt = Let (PropRef (expr1, "Connection"), PropRef ("Connection")); m.Statements.Add (stmt); stmt = Let (PropRef (expr1, "CommandText"), Const (cmd.CommandText)); m.Statements.Add (stmt); stmt = Let (PropRef (expr1, "CommandType"), PropRef (Local(typeof (CommandType).FullName), cmd.CommandType.ToString ())); m.Statements.Add (stmt); expr1 = PropRef (expr1, "Parameters"); foreach (DbParameter param in cmd.Parameters) { AddDbParameterStatements (m, expr1, param); } } t.Members.Add (m); }
private void CreateConnectionInitializeMethod (CodeTypeDeclaration t, TableAdapterSchemaInfo taInfo) { CodeMemberMethod m = new CodeMemberMethod (); m.Name = "InitConnection"; m.Attributes = MemberAttributes.Private; // code statements CodeExpression expr, expr1; CodeStatement stmt; // initialize connection expr = FieldRef ("_connection"); stmt = Let (expr, New (taInfo.Connection.GetType ())); m.Statements.Add (stmt); // assign connection string expr = PropRef (FieldRef ("_connection"), "ConnectionString"); expr1 = IndexerRef (PropRef (Local (typeof (System.Configuration.ConfigurationManager).ToString()), "ConnectionStrings"), Const (taInfo.ConnectionString)); stmt = Let (expr, PropRef (expr1, "ConnectionString")); m.Statements.Add (stmt); t.Members.Add (m); }
private void CreateAdapterInitializeMethod (CodeTypeDeclaration t, TableAdapterSchemaInfo taInfo) { CodeMemberMethod m = new CodeMemberMethod (); m.Name = "InitAdapter"; m.Attributes = MemberAttributes.Private; // code statements CodeExpression expr; CodeStatement stmt; // initialize adapter expr = FieldRef ("_adapter"); stmt = Let (expr, New (taInfo.Adapter.GetType ())); m.Statements.Add (stmt); // populate tableMapping stmt = VarDecl (typeof (DataTableMapping), "tableMapping", null); m.Statements.Add (stmt); foreach (DataTableMapping tblMap in taInfo.Adapter.TableMappings) { expr = Local ("tableMapping"); stmt = Let (expr, New (tblMap.GetType ())); m.Statements.Add (stmt); stmt = Let (PropRef (expr, "SourceTable"), Const (tblMap.SourceTable)); m.Statements.Add (stmt); stmt = Let (PropRef (expr, "DataSetTable"), Const (tblMap.DataSetTable)); m.Statements.Add (stmt); foreach (DataColumnMapping colMap in tblMap.ColumnMappings) { stmt = Eval (MethodInvoke (PropRef (expr, "ColumnMappings"), "Add", new CodeExpression [] {Const (colMap.SourceColumn), Const (colMap.DataSetColumn)})); m.Statements.Add (stmt); } expr = PropRef (FieldRef ("_adapter"), "TableMappings"); stmt = Eval (MethodInvoke (expr, "Add", Local ("tableMapping"))); m.Statements.Add (stmt); } // Generate code for adapter's deletecommand expr = PropRef (FieldRef ("_adapter"), "DeleteCommand"); DbCommand cmd = taInfo.Adapter.DeleteCommand; AddDbCommandStatements (m, expr, cmd); // Generate code for adapter's insertcommand expr = PropRef (FieldRef ("_adapter"), "InsertCommand"); cmd = taInfo.Adapter.InsertCommand; AddDbCommandStatements (m, expr, cmd); // Generate code for adapter's updatecommand expr = PropRef (FieldRef ("_adapter"), "UpdateCommand"); cmd = taInfo.Adapter.UpdateCommand; AddDbCommandStatements (m, expr, cmd); t.Members.Add (m); }
private CodeTypeDeclaration GenerateTableAdapterType (TableAdapterSchemaInfo taInfo) { CodeTypeDeclaration t = new CodeTypeDeclaration (); t.Name = opts.TableAdapterName (taInfo.Name); t.BaseTypes.Add (TypeRef (taInfo.BaseClass)); t.Members.Add (CreateTableAdapterDefaultCtor ()); // table adapter fields/properties CreateDBAdapterFieldAndProperty (t, taInfo.Adapter); CreateDBConnectionFieldAndProperty (t, taInfo.Connection); DbCommand cmd = null; if (taInfo.Commands.Count > 0) cmd = ((DbCommandInfo)taInfo.Commands[0]).Command; else cmd = taInfo.Provider.CreateCommand (); CreateDBCommandCollectionFieldAndProperty (t, cmd); CreateAdapterClearBeforeFillFieldAndProperty (t); CreateAdapterInitializeMethod (t, taInfo); CreateConnectionInitializeMethod (t, taInfo); CreateCommandCollectionInitializeMethod (t, taInfo); CreateDbSourceMethods (t, taInfo); if (taInfo.ShortCommands) CreateShortCommandMethods (t, taInfo); return t; }