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);
        }
Esempio n. 2
0
        /// <summary>
        /// Builds the params.
        /// </summary>
        void BuildStatement(MethodStatementType statementType)
        {
            StringBuilder buff = new StringBuilder();
            CodeParameterDeclarationExpression unitParam;
            string      paramName;
            string      dbType;
            string      indentSpace    = "            ";//12 chars
            string      indentSpaceSub = "                ";
            PagingFlags pagingFlags    = PagingFlags.None;

            buff.Append(indentSpace);
            buff.AppendFormat("using(DataAccess db = new DataAccess(\"{0}\"))", _sqlName);
            buff.AppendLine("{");
            foreach (DataRow dr in FetchParamList().Rows)
            {
                paramName = DbDataConvert.ToString(dr[1]);
                dbType    = DbDataConvert.ToString(dr[2]);
                unitParam = new CodeParameterDeclarationExpression(
                    DalUtil.MapDbType(dbType),
                    DalUtil.CreateMemberName(paramName, MapMemberType.Parameter));
                switch (unitParam.Name.ToLower())
                {
                case "rowindex":
                    if (statementType == MethodStatementType.Select ||
                        statementType == MethodStatementType.SelectCount)
                    {
                        ///
                        pagingFlags   |= PagingFlags.RowIndex;
                        unitParam.Type = new CodeTypeReference(typeof(int?));
                        buff.Append(indentSpaceSub)
                        .AppendFormat("if ({0}.HasValue)", unitParam.Name)
                        .AppendLine()
                        .Append(indentSpaceSub).Append("    ")
                        .AppendFormat("db.AddParameter(\"{0}\", {1}.Value);", paramName, unitParam.Name);
                    }
                    else
                    {
                        buff.Append(indentSpaceSub)
                        .AppendFormat("db.AddParameter(\"{0}\", {1});", paramName, unitParam.Name);
                    }
                    _unitMethod.Parameters.Add(unitParam);
                    break;

                case "pagesize":
                    if (statementType == MethodStatementType.Select ||
                        statementType == MethodStatementType.SelectCount)
                    {
                        ///
                        pagingFlags   |= PagingFlags.PageSize;
                        unitParam.Type = new CodeTypeReference(typeof(int?));
                        buff.Append(indentSpaceSub)
                        .AppendFormat("if ({0}.HasValue)", unitParam.Name)
                        .AppendLine()
                        .Append(indentSpaceSub).Append("    ")
                        .AppendFormat("db.AddParameter(\"{0}\", {1}.Value);", paramName, unitParam.Name);
                    }
                    else
                    {
                        buff.Append(indentSpaceSub)
                        .AppendFormat("db.AddParameter(\"{0}\", {1});", paramName, unitParam.Name);
                    }
                    _unitMethod.Parameters.Add(unitParam);
                    break;

                case "keyword":
                    if (statementType == MethodStatementType.Select ||
                        statementType == MethodStatementType.SelectCount)
                    {
                        ///
                        buff.Append(indentSpaceSub)
                        .AppendLine("if (!string.IsNullOrEmpty(keyword))")
                        .Append(indentSpaceSub).Append("    ")
                        .AppendFormat("db.AddParameter(\"{0}\", {1});", paramName, unitParam.Name);
                    }
                    else
                    {
                        buff.Append(indentSpaceSub)
                        .AppendFormat("db.AddParameter(\"{0}\", {1});", paramName, unitParam.Name);
                    }
                    _unitMethod.Parameters.Add(unitParam);
                    break;

                case "criteria":
                    if (statementType == MethodStatementType.Select ||
                        statementType == MethodStatementType.SelectCount)
                    {
                        ///
                        buff.Append(indentSpaceSub)
                        .AppendLine("if (!string.IsNullOrEmpty(criteria))")
                        .Append(indentSpaceSub).Append("    ")
                        .AppendFormat("db.AddParameter(\"{0}\", {1});", paramName, unitParam.Name);
                    }
                    else
                    {
                        buff.Append(indentSpaceSub)
                        .AppendFormat("db.AddParameter(\"{0}\", {1});", paramName, unitParam.Name);
                    }
                    _unitMethod.Parameters.Add(unitParam);
                    break;

                case "operator":
                    buff.Append(indentSpaceSub)
                    .AppendFormat("Artem.Data.Access.Build.DalUtil.AddOperatorParam(db);");
                    break;

                default:
                    DalUtil.AddParamInStatement(buff, dbType, paramName, unitParam.Name, indentSpaceSub);
                    //buff.Append(indentSpaceSub)
                    //    .AppendFormat("db.AddParameter(\"{0}\", {1});", paramName, unitParam.Name);
                    _unitMethod.Parameters.Add(unitParam);
                    break;
                }
                buff.AppendLine();
            }
            switch (statementType)
            {
            case MethodStatementType.Common:
            case MethodStatementType.Delete:
            case MethodStatementType.Insert:
            case MethodStatementType.Update:
                buff.Append(indentSpaceSub).Append("db.ExecuteNonQuery();");
                break;

            case MethodStatementType.Get:
                buff.Append(indentSpaceSub).AppendFormat("return db.FetchObject<{0}>();", _unitClass.Name);
                break;

            case MethodStatementType.Select:
                buff.Append(indentSpaceSub).AppendFormat("return db.FetchCollection<{0}>();", _unitClass.Name);
                break;

            case MethodStatementType.SelectCount:
                buff.Append(indentSpaceSub).Append("return db.ExecuteScalar<int>();");
                break;
            }
            buff.AppendLine();
            buff.Append(indentSpace).Append("}");
            _unitMethod.Statements.Add(new CodeSnippetStatement(buff.ToString()));
            // if paging enabled add count method before go back
            //if (pagingFlags == PagingFlags.Enabled) {
            //    GenSelectCount();
            //}
        }
Esempio n. 3
0
        /// <summary>
        /// Generates the fields.
        /// </summary>
        /// <param name="unitClass">The unit class.</param>
        /// <param name="dt">The dt.</param>
        void GenFields(CodeTypeDeclaration unitClass, DataTable dt)
        {
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                DataColumn column          = dt.Columns[j];
                string     colName         = column.ColumnName;
                Type       colType         = column.DataType;
                bool       colIsPrimatyKey = false;
                foreach (DataColumn col in column.Table.PrimaryKey)
                {
                    if (col.ColumnName == colName)
                    {
                        colIsPrimatyKey = true;
                        break;
                    }
                }
                string fieldName = DalUtil.CreateMemberName(colName, MapMemberType.Field);
                //
                // Add the private field to store the data
                //
                CodeMemberField unitField = new CodeMemberField(colType, fieldName);
                unitClass.Members.Add(unitField);
                //
                // Add property declaration and get/set accessors
                //
                CodeMemberProperty unitProperty = new CodeMemberProperty();
                unitProperty.Name       = DalUtil.CreateMemberName(colName, MapMemberType.Property);
                unitProperty.Type       = new CodeTypeReference(colType);
                unitProperty.Attributes = MemberAttributes.Public;
                //
                // Custom Attributes
                //
                CodeAttributeDeclaration attr = new CodeAttributeDeclaration(
                    "Artem.Data.Access.DbFieldAttribute",
                    new CodeAttributeArgument(new CodePrimitiveExpression(colName)));
                unitProperty.CustomAttributes.Add(attr);
                attr = new CodeAttributeDeclaration(
                    "System.ComponentModel.DataObjectField",
                    new CodeAttributeArgument(new CodePrimitiveExpression(colIsPrimatyKey)),
                    new CodeAttributeArgument(new CodePrimitiveExpression(column.AutoIncrement)),
                    new CodeAttributeArgument(new CodePrimitiveExpression(column.AllowDBNull)),
                    new CodeAttributeArgument(new CodePrimitiveExpression(column.MaxLength)));
                unitProperty.CustomAttributes.Add(attr);
                //
                // Define the codeDOM reference for the property's private field
                //
                CodeFieldReferenceExpression unitFieldRef = new CodeFieldReferenceExpression();
                unitFieldRef.TargetObject = new CodeThisReferenceExpression();
                unitFieldRef.FieldName    = fieldName;
                //
                // Get
                //
                CodeMethodReturnStatement unitReturn = new CodeMethodReturnStatement(unitFieldRef);
                unitProperty.GetStatements.Add(unitReturn);
                //
                // Set
                //
                CodeAssignStatement unitAssign = new CodeAssignStatement();
                unitAssign.Left  = unitFieldRef;
                unitAssign.Right = new CodePropertySetValueReferenceExpression();
                unitProperty.SetStatements.Add(unitAssign);

                unitClass.Members.Add(unitProperty);
            }
        }