Пример #1
0
 public ConstraintSchema(DataModelSchema schema, XmlSchemaIdentityConstraint xmlSchemaIdentityConstraint)
     : base(xmlSchemaIdentityConstraint)
 {
     // Initialize the object.
     this.DataModelSchema             = schema;
     this.xmlSchemaIdentityConstraint = xmlSchemaIdentityConstraint;
 }
Пример #2
0
        /// <summary>
        /// Generate the code from the inputs.
        /// </summary>
        /// <param name="inputFileName">The name of the input file.</param>
        /// <param name="inputFileContent">The contents of the input file.</param>
        /// <returns>A buffer containing the generated code.</returns>
        protected byte[] GenerateCode(DataModelSchema schema)
        {
            // Create a namespace MarkThree.MiddleTier add it to the module.  This namespace MarkThree.MiddleTier all the relavent code in it.
            CodeCompileUnit codeCompileUnit = new CodeCompileUnit();

            codeCompileUnit.Namespaces.Add(new MiddleTierNamespace(schema));

            // If a handler was provided for the generation of the code, then call it with an update.
            if (this.ivsGeneratorProgress != null)
            {
                this.ivsGeneratorProgress.Progress(75, 100);
            }

            // This will generate the source code and return it as an array of bytes.
            StringWriter stringWriter = new StringWriter();

            this.CodeDomProvider.GenerateCodeFromCompileUnit(codeCompileUnit, stringWriter,
                                                             this.CodeGeneratorOptions);

            // If a handler was provided for the progress, then let it know that the task is complete.
            if (this.ivsGeneratorProgress != null)
            {
                this.ThrowOnFailure(this.ivsGeneratorProgress.Progress(100, 100));
            }

            // Return the source code converted to a stream of bytes.
            return(System.Text.Encoding.UTF8.GetBytes(stringWriter.ToString()));
        }
Пример #3
0
 /// <summary>
 /// Creates a description of a class (Complex Type) in a data model.
 /// </summary>
 /// <param name="dataModelSchema">The Schema of the entire data model.</param>
 /// <param name="xmlSchemaComplexType">The description of the complex type.</param>
 public ComplexTypeSchema(DataModelSchema dataModelSchema, XmlSchemaComplexType xmlSchemaComplexType) :
     base(dataModelSchema, xmlSchemaComplexType)
 {
     // Initialize the object.
     this.Name          = GetName(xmlSchemaComplexType);
     this.QualifiedName = GetQualifiedName(xmlSchemaComplexType);
 }
Пример #4
0
 public KeyrefSchema(DataModelSchema dataModelSchema, XmlSchemaKeyref xmlSchemaKeyref)
     : base(dataModelSchema, xmlSchemaKeyref)
 {
     // Initialize the object
     this.xmlSchemaKeyref = xmlSchemaKeyref;
     this.Refer           = GetRefer(xmlSchemaKeyref);
 }
Пример #5
0
 public PersistentStore(DataModelSchema schema)
 {
     this.Comments.Add(new CodeCommentStatement(@"This value is used to map the object to a persistent storage device.  The parameters for the storage", true));
     this.Comments.Add(new CodeCommentStatement(@"are found in the configuration file for this service.", true));
     this.Attributes     = MemberAttributes.Public | MemberAttributes.Static | MemberAttributes.Final;
     this.Name           = "PersistentStore";
     this.Type           = new CodeTypeReference(typeof(string));
     this.InitExpression = new CodePrimitiveExpression(schema.DurableStoreName);
 }
Пример #6
0
 /// <summary>
 /// Create a description of a constraint on a table.
 /// </summary>
 /// <param name="schema">The Schema of the entire data model.</param>
 /// <param name="xmlSchemaIdentityConstraint">The schema of a constraint.</param>
 public ConstraintSchema(DataModelSchema dataModelSchema, XmlSchemaIdentityConstraint xmlSchemaIdentityConstraint)
     : base(dataModelSchema, xmlSchemaIdentityConstraint)
 {
     // Initialize the object.
     this.Name          = xmlSchemaIdentityConstraint.Name;
     this.QualifiedName = xmlSchemaIdentityConstraint.QualifiedName;
     this.Selector      = GetSelector(xmlSchemaIdentityConstraint);
     this.Fields        = GetFields(xmlSchemaIdentityConstraint);
     this.IsPrimaryKey  = GetPrimaryKeyStatus(xmlSchemaIdentityConstraint);
     this.IsNullable    = GetNullableStatus();
 }
Пример #7
0
        /// <summary>
        /// Generate the code from the inputs.
        /// </summary>
        /// <param name="inputFileName">The name of the input file.</param>
        /// <param name="inputFileContent">The contents of the input file.</param>
        /// <returns>A buffer containing the generated code.</returns>
        protected static byte[] GenerateCode(DataModelSchema schema)
        {
            // Create a namespace MarkThree.MiddleTier add it to the module.  This is where all the work is done to generate the
            // CodeDOM specification for the output file.
            CodeCompileUnit codeCompileUnit = new CodeCompileUnit();

            codeCompileUnit.Namespaces.Add(new MiddleTierNamespace(schema));

            // The purpose of this method is to emulate the way the IDE tools would return the data.  This generates the code and
            // then converts it to an UTF8 array of bytes, which is what the IDE expects to get from any code generation tools.
            StringWriter stringWriter = new StringWriter();

            MiddleTierCompiler.CodeProvider.GenerateCodeFromCompileUnit(codeCompileUnit, stringWriter,
                                                                        MiddleTierCompiler.codeGeneratorOptions);
            return(System.Text.Encoding.UTF8.GetBytes(stringWriter.ToString()));
        }
Пример #8
0
        public TableSchema(DataModelSchema dataModelSchema, XmlSchemaElement xmlSchemaElement)
            : base(dataModelSchema, xmlSchemaElement)
        {
            // Initialize the object.
            this.DataModelSchema = dataModelSchema;
            this.Name            = xmlSchemaElement.Name;
            this.QualifiedName   = xmlSchemaElement.QualifiedName;
            this.IsPersistent    = GetIsPersistentAttribute(xmlSchemaElement);
            this.Columns         = new ColumnSchemaCollection();
            this.Constraints     = new ConstraintSchemaCollection();
            this.Keys            = new List <ConstraintSchema>();
            this.ParentKeyrefs   = new List <KeyrefSchema>();
            this.TypeSchema      = GetTypeSchema(xmlSchemaElement);

            GetColumns(xmlSchemaElement.SchemaType, this.Columns);

            Constraints.ItemAdded += new ConstraintEvent(ConstraintAddedHandler);
        }
Пример #9
0
        /// <summary>
        /// Creates a CodeDOM namespace that contains the strongly typed DataSet.
        /// </summary>
        /// <param name="schema">The schema description of the strongly typed DataSet.</param>
        public MiddleTierNamespace(DataModelSchema schema)
        {
            // Initialize the object.
            this.schema = schema;

            //namespace MarkThree.UnitTest
            //{
            //	using MarkThree;
            //	using System;
            //	using System.ComponentModel;
            //	using System.Data;
            this.Name = this.Schema.TargetNamespace;
            this.Imports.Add(new CodeNamespaceImport("MarkThree"));
            this.Imports.Add(new CodeNamespaceImport("System"));
            this.Imports.Add(new CodeNamespaceImport("System.ComponentModel"));
            this.Imports.Add(new CodeNamespaceImport("System.Data"));

            // This is where all the hard work is done.
            this.Types.Add(new TypedDataSetClass(this));
        }
Пример #10
0
        /// <summary>
        /// Generate the code from the custom tool.
        /// </summary>
        /// <param name="wszInputFilePath">The name of the input file.</param>
        /// <param name="bstrInputFileContents">The contents of the input file.</param>
        /// <param name="wszDefaultNamespace">The namespace for the generated code.</param>
        /// <param name="pbstrOutputFileContents">The generated code.</param>
        /// <param name="pbstrOutputFileContentSize">The buffer size of the generated code.</param>
        /// <param name="pGenerateProgress">An indication of the tools progress.</param>
        /// <returns>0 indicates the tool handled the command.</returns>
        public int Generate(string wszInputFilePath, string bstrInputFileContents, string wszDefaultNamespace,
                            IntPtr[] pbstrOutputFileContents, out uint pbstrOutputFileContentSize, IVsGeneratorProgress pGenerateProgress)
        {
            // Throw an execption if there is nothing to process.
            if (bstrInputFileContents == null)
            {
                throw new ArgumentNullException(bstrInputFileContents);
            }

            // This will make the parameters used to invoke this custom tool available to the class that generates the code.
            this.codeFilePath         = wszInputFilePath;
            this.codeFileNameSpace    = wszDefaultNamespace;
            this.ivsGeneratorProgress = pGenerateProgress;

            DataModelSchema schema = new DataModelSchema(bstrInputFileContents);

            schema.TargetNamespace = wszDefaultNamespace;

            // Call the super class to generate the code.
            byte[] generatedBuffer = GenerateCode(schema);
            if (generatedBuffer == null)
            {
                // This will pack up an empty buffer in a format that can be moved across the managed code boundary.
                pbstrOutputFileContents[0] = IntPtr.Zero;
                pbstrOutputFileContentSize = 0;
            }
            else
            {
                // Pack the data back into a form that can be moved across the managed code boundary.
                pbstrOutputFileContents[0] = Marshal.AllocCoTaskMem(generatedBuffer.Length);
                Marshal.Copy(generatedBuffer, 0, pbstrOutputFileContents[0], generatedBuffer.Length);
                pbstrOutputFileContentSize = (uint)generatedBuffer.Length;
            }

            // This indicates that the 'Generate' method was handled.
            return(0);
        }
Пример #11
0
 public ObjectSchema(DataModelSchema dataModelSchema, XmlSchemaObject xmlSchemaObject)
 {
     // Initialize the object.
     this.DataModelSchema = dataModelSchema;
     this.xmlSchemaObject = xmlSchemaObject;
 }
Пример #12
0
 public KeyrefSchema(DataModelSchema schema, XmlSchemaKeyref xmlSchemaKeyref)
     : base(schema, xmlSchemaKeyref)
 {
     // Initialize the object
     this.xmlSchemaKeyref = xmlSchemaKeyref;
 }
Пример #13
0
 public KeySchema(DataModelSchema schema, XmlSchemaKey xmlSchemaKey)
     : base(schema, xmlSchemaKey)
 {
     // Initialize the object
     this.xmlSchemaKey = xmlSchemaKey;
 }
Пример #14
0
        static int Main(string[] args)
        {
            try
            {
                // Defaults
                targetNamespace = "DefaultNamespace";

                // The command line parser is driven by different states that are triggered by the flags read.  Unless a flag has
                // been read, the command line parser assumes that it's reading the file name from the command line.
                argumentState = ArgumentState.InputFileName;

                // Parse the command line for arguments.
                foreach (string argument in args)
                {
                    // Decode the current argument into a state change (or some other action).
                    if (argument == "-i")
                    {
                        argumentState = ArgumentState.InputFileName; continue;
                    }
                    if (argument == "-ns")
                    {
                        argumentState = ArgumentState.TargetNamespace; continue;
                    }
                    if (argument == "-out")
                    {
                        argumentState = ArgumentState.OutputFileName; continue;
                    }

                    // The parsing state will determine which variable is read next.
                    switch (argumentState)
                    {
                    case ArgumentState.InputFileName: inputFileName = argument; break;

                    case ArgumentState.OutputFileName: outputFileName = argument; break;

                    case ArgumentState.TargetNamespace: targetNamespace = argument; break;
                    }

                    // The default state is to look for the input file name on the command line.
                    argumentState = ArgumentState.InputFileName;
                }

                // Throw a usage message back at the user if no file name was given.
                if (inputFileName == null)
                {
                    throw new Exception("Usage: DataSetGenerator -i <InputFileName>");
                }

                // If no output file name was specified, create one from the input file specification.
                if (outputFileName == null)
                {
                    outputFileName = string.Format("{0}.cs", Path.GetFileNameWithoutExtension(inputFileName));
                }

                // Read the schema into a string.  This emulates the way that the IDE would normally call a code generator.  Create
                // the MiddleTierSchema (like a Schema, but with extra helping functions and relations for this type of code
                // generation).
                StreamReader    streamReader = new StreamReader(inputFileName);
                DataModelSchema schema       = new DataModelSchema(streamReader.ReadToEnd());
                streamReader.Close();

                // Transfer the calling parameters into the schema.
                schema.TargetNamespace = targetNamespace;

                // This will generate a buffer of source code from the input schema.
                byte[] buffer = GenerateCode(schema);

                // Write the buffer to the specified UTF8 output file.
                StreamWriter streamWriter = new StreamWriter(outputFileName);
                streamWriter.Write(Encoding.UTF8.GetString(buffer));
                streamWriter.Close();
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
            }

            return(0);
        }
Пример #15
0
        /// <summary>
        /// Create a description of a column in a data model.
        /// </summary>
        /// <param name="dataModelSchema">The Schema of the entire data model.</param>
        /// <param name="xmlSchemaObject">The schema of the column.</param>
        public ColumnSchema(DataModelSchema dataModelSchema, XmlSchemaObject xmlSchemaObject) :
            base(dataModelSchema, xmlSchemaObject)
        {
            // Initialize the object
            this.dataModelSchema = dataModelSchema;
            this.Name            = string.Empty;
            this.QualifiedName   = XmlQualifiedName.Empty;
            this.DataType        = typeof(System.Object);
            this.MinOccurs       = 0.0M;
            this.DefaultValue    = null;
            this.FixedValue      = null;

            // Extract the column properties from an Element.
            if (xmlSchemaObject is XmlSchemaElement)
            {
                XmlSchemaElement xmlSchemaElement = xmlSchemaObject as XmlSchemaElement;
                this.Name          = xmlSchemaElement.Name;
                this.QualifiedName = xmlSchemaElement.QualifiedName;
                this.DataType      = xmlSchemaElement.ElementSchemaType.Datatype.ValueType;
                this.MinOccurs     = xmlSchemaElement.MinOccurs;
                this.DefaultValue  = ConvertValue(xmlSchemaElement.DefaultValue);
                this.FixedValue    = ConvertValue(xmlSchemaElement.FixedValue);
            }

            // Extract the column properties from an Attribute.
            if (xmlSchemaObject is XmlSchemaAttribute)
            {
                XmlSchemaAttribute xmlSchemaAttribute = xmlSchemaObject as XmlSchemaAttribute;
                this.Name          = xmlSchemaAttribute.Name;
                this.QualifiedName = xmlSchemaAttribute.QualifiedName;
                this.DataType      = xmlSchemaAttribute.AttributeSchemaType.Datatype.ValueType;
                this.DefaultValue  = ConvertValue(xmlSchemaAttribute.DefaultValue);
                this.FixedValue    = ConvertValue(xmlSchemaAttribute.FixedValue);
            }

            // Determine the IsIdentityColumn property.
            object autoIncrementAttribute = GetUnhandledAttribute(xmlSchemaObject, "AutoIncrement");

            this.IsAutoIncrement = autoIncrementAttribute == null ? false : Convert.ToBoolean(autoIncrementAttribute);

            // Determine the IsPersistent property.
            object isColumnPersistentAttribute = GetUnhandledAttribute(xmlSchemaObject, "IsPersistent");

            this.IsPersistent = isColumnPersistentAttribute == null ? true : Convert.ToBoolean(isColumnPersistentAttribute);

            // Determine the AutoIncrementSeed property.
            object autoIncrementSeedAttribute = GetUnhandledAttribute(xmlSchemaObject, "AutoIncrementSeed");

            this.AutoIncrementSeed = autoIncrementSeedAttribute == null ? 0 : Convert.ToInt32(autoIncrementSeedAttribute);

            // Determine the AutoIncrementStop property
            object autoIncrementStepAttribute = GetUnhandledAttribute(xmlSchemaObject, "AutoIncrementStep");

            this.AutoIncrementStep = autoIncrementStepAttribute == null ? 0 : Convert.ToInt32(autoIncrementStepAttribute);

            // In an object oriented architecture it is important to know at which level of the hierarchy a column is initially
            // declared.  Just like the equivalent in the object oriented languages, this field indicates which table actually owns
            // the data of a given column.
            XmlSchemaObject parentObject = xmlSchemaObject.Parent;

            while (!(parentObject is XmlSchemaComplexContentExtension) && !(parentObject is XmlSchemaComplexType))
            {
                parentObject = parentObject.Parent;
            }
            this.DeclaringType = FindDeclaringType(parentObject, xmlSchemaObject);
        }
Пример #16
0
 public ColumnSchema(DataModelSchema schema, XmlSchemaObject xmlSchemaObject)
 {
     // Initialize the object
     this.schema          = schema;
     this.xmlSchemaObject = xmlSchemaObject;
 }
Пример #17
0
 public UniqueSchema(DataModelSchema schema, XmlSchemaUnique xmlSchemaUnique)
     : base(schema, xmlSchemaUnique)
 {
     // Initialize the object
     this.xmlSchemaUnique = xmlSchemaUnique;
 }
Пример #18
0
 public ItemEnumerator(DataModelSchema schema, XmlSchemaObjectEnumerator xmlSchemaObjectEnumerator)
 {
     // Initialize the object
     this.schema = schema;
     this.xmlSchemaObjectEnumerator = xmlSchemaObjectEnumerator;
 }
Пример #19
0
 public ItemIterator(DataModelSchema schema, XmlSchemaObjectCollection xmlSchemaObjectCollection)
 {
     this.schema = schema;
     this.xmlSchemaObjectCollection = xmlSchemaObjectCollection;
 }
Пример #20
0
 public DataSetSchema(DataModelSchema schema, XmlSchemaElement xmlSchemaElement) : base(xmlSchemaElement)
 {
     // Initialize the object.
     this.Schema           = schema;
     this.xmlSchemaElement = xmlSchemaElement;
 }
Пример #21
0
        /// <summary>
        /// Generate the code from the inputs.
        /// </summary>
        /// <param name=\"inputFileName\">The name of the input file.</param>
        /// <param name=\"inputFileContent\">The contents of the input file.</param>
        /// <returns>A buffer containing the generated code.</returns>
        private static byte[] GenerateDdl(DataModelSchema schema)
        {
            // The generated data is written to a memeory stream using a writer.
            MemoryStream memoryStream = new MemoryStream();
            StreamWriter streamWriter = new StreamWriter(memoryStream);

            // Generate the file header.
            streamWriter.WriteLine("/*******************************************************************************");
            streamWriter.WriteLine("*	<auto-generated>");
            streamWriter.WriteLine("*	This code was generated by a tool.");
            streamWriter.WriteLine("*	Runtime Version:1.0.0.0");
            streamWriter.WriteLine("*");
            streamWriter.WriteLine("*	Changes to this file may cause incorrect behavior and will be lost if");
            streamWriter.WriteLine("*	the code is regenerated.");
            streamWriter.WriteLine("*	</auto-generated>");
            streamWriter.WriteLine("*******************************************************************************/");
            streamWriter.WriteLine();
            streamWriter.WriteLine("/* Set the environment */");
            streamWriter.WriteLine("set nocount on");
            streamWriter.WriteLine("set quoted_identifier on");
            streamWriter.WriteLine();

            // If this is the first version, then generate the version control schema.
            if (schema.Version == 0.0m)
            {
                GenerateVersionControl(streamWriter);
            }

            // The tables must be written out to the DDL file so that parent tables are written out before child tables.  As tables
            // are emitted into the DDL file, they are removed from the list.  This continues until the list of tables is empty.
            TableSchemaCollection tables = schema.Tables;

            while (tables.Count > 0)
            {
                foreach (TableSchema tableSchema in tables)
                {
                    // This will search the tables to see if the current table has any parent dependancies that haven't been
                    // written yet.
                    bool isParentDefined = true;
                    foreach (KeyrefSchema parentKeyref in tableSchema.ParentKeyrefs)
                    {
                        if (tables.Contains(parentKeyref.Refer.Selector.QualifiedName))
                        {
                            isParentDefined = false;
                            break;
                        }
                    }

                    // If there are parent dependancies that have not yet been generated, then skip this table for now.
                    if (!isParentDefined)
                    {
                        continue;
                    }

                    // The table schema is removed from the list after it is written to the stream.
                    GenerateTable(streamWriter, tableSchema);
                    tables.Remove(tableSchema);
                    break;
                }
            }

            // This will flush all the generated code into an array of bytes that can be written back to a file.  The choice of a
            // byte array as a means of returning the generated data is intended to emulate the interface of the Visual Studio
            // tools.
            streamWriter.Flush();
            return(memoryStream.ToArray());
        }
Пример #22
0
 public TypeSchema(DataModelSchema schema, XmlSchemaComplexType xmlSchemaComplexType)
 {
     // Initialize the object.
     this.Schema = schema;
     this.xmlSchemaComplexType = xmlSchemaComplexType;
 }