Esempio n. 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:MethodBuilder"/> class.
        /// </summary>
        /// <param name="desc">The desc.</param>
        /// <param name="sqlName">Name of the SQL.</param>
        public MethodBuilder(CodeTypeDeclaration unitClass, MapDescriptor desc, string sqlName)
        {
            _unitClass     = unitClass;
            _unitMethod    = new CodeMemberMethod();
            _mapDescriptor = desc;
            _sqlName       = sqlName;
            int index = _sqlName.IndexOf('_') + 1;

            if (!string.IsNullOrEmpty(_mapDescriptor.Prefix))
            {
                index = _sqlName.IndexOf('_', index) + 1;
            }
            _memberName = _sqlName.Substring(index);
            _memberName = DalUtil.CreateMemberName(_memberName, MapMemberType.Method);
        }
        /// <summary>
        /// Extracts this instance.
        /// </summary>
        /// <returns></returns>
        public MapDescriptor Extract()
        {
            if (_disposed)
            {
                throw new ObjectDisposedException("DalExtractor");
            }

            Load();
            ///
            /// get map descriptor
            ///
            XmlNode       root = _doc.DocumentElement;
            MapDescriptor map  = new MapDescriptor(root);

            foreach (XmlNode node in _doc.SelectNodes("dal/mapping"))
            {
                map.TableDescriptors.Add(new MapTableDescriptor(node));
            }
            // return
            return(map);
        }
Esempio n. 3
0
        /// <summary>
        /// Builds the specified unit class.
        /// </summary>
        /// <param name="unitClass">The unit class.</param>
        /// <param name="desc">The desc.</param>
        /// <param name="sqlName">Name of the SQL.</param>
        /// <returns></returns>
        public static CodeMemberMethod Build(CodeTypeDeclaration unitClass, MapDescriptor desc, string sqlName)
        {
            MethodBuilder builder = new MethodBuilder(unitClass, desc, sqlName);

            return(builder.Build());
        }
Esempio n. 4
0
        /// <summary>
        /// Generates this instance.
        /// </summary>
        /// <returns></returns>
        public CodeCompileUnit Generate()
        {
            ///
            /// init
            ///
            _mapDescriptor = _extractor.Extract();
            CodeCompileUnit code = new CodeCompileUnit();
            ///
            /// Create the namespace
            ///
            CodeNamespace unitNamespace = new CodeNamespace(_mapDescriptor.Namespace);

            unitNamespace.Imports.AddRange(new CodeNamespaceImport[] {
                new CodeNamespaceImport("System.Collections.Generic"),
                new CodeNamespaceImport("Artem.Data.Access")
            });
            code.Namespaces.Add(unitNamespace);
            ///
            /// Loop through the mappings and add classes
            ///
            for (int i = 0; i < _mapDescriptor.TableDescriptors.Count; i++)
            {
                // Get the descriptor
                MapTableDescriptor tblDescriptor = _mapDescriptor.TableDescriptors[i];
                ///
                /// Create the class to represent the table
                ///
                CodeTypeDeclaration unitClass = new CodeTypeDeclaration(tblDescriptor.ClassName);
                unitClass.IsPartial = tblDescriptor.IsPartial;
                unitNamespace.Types.Add(unitClass);
                ///
                /// Custom Attributes
                ///
                CodeAttributeDeclaration attr = new CodeAttributeDeclaration(
                    "System.ComponentModel.DataObjectAttribute",
                    new CodeAttributeArgument(new CodePrimitiveExpression(true)));
                unitClass.CustomAttributes.Add(attr);
                attr = new CodeAttributeDeclaration("System.SerializableAttribute");
                unitClass.CustomAttributes.Add(attr);
                ///
                /// Loop through the selected table columns and add members
                ///
                DataTable dt = new DataTable();
                using (DataAccess db = new DataAccess(tblDescriptor.CommandText)) {
                    if (_mapDescriptor.ConnectionString != null)
                    {
                        db.ConnectionString = _mapDescriptor.ConnectionString;
                    }
                    db.CommandType = tblDescriptor.CommandType;// CommandType.Text;
                    db.FillSchema(dt);
                }
                ///
                /// Create Fields & Properties
                ///
                GenFields(unitClass, dt);
                ///
                /// Create Methods
                ///
                if (_mapDescriptor.GenerateMethods)
                {
                    GenMethods(unitClass);
                }
            }

            return(code);
        }