Esempio n. 1
0
        public string BuildCommand(string tablename)
        {
            MapperProvider defaultProvider = MapperProvider.DefaultProvider;
            string         pascalCase      = tablename.Substring(3);

            pascalCase = SourceBuilder.ToPascalCase(pascalCase);
            string   str      = string.Concat("SELECT * FROM ", tablename);
            ValueSet valueSet = defaultProvider.QueryForValueSet(str, null, CommandType.Text);

            str = string.Concat("select object_id from sys.all_objects where type_desc='USER_TABLE' and name='", tablename, "'");
            string str1 = defaultProvider.QueryForScalar(str, null, CommandType.Text).ToString();

            str = string.Concat("select * from sys.index_columns where object_id='", str1, "'");
            ValueTable    item = defaultProvider.QueryForValueSet(str, null, CommandType.Text).Tables[0];
            List <string> strs = new List <string>();

            foreach (ValueRow row in item.Rows)
            {
                str = string.Concat(new object[] { "select name from sys.columns where object_id='", str1, "' AND column_id='", row["column_id"], "'" });
                string str2 = defaultProvider.QueryForScalar(str, null, CommandType.Text).ToString();
                strs.Add(str2);
            }
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n<mapper>\r\n\t<commands>\r\n");
            stringBuilder.AppendFormat("\t\t<post id=\"{0}.Create\">\r\n\t\t", pascalCase);
            stringBuilder.Append("<![CDATA[\r\n\t\t\t");
            stringBuilder.AppendFormat("INSERT INTO {0}", tablename);
            stringBuilder.Append("(");
            int num = 0;

            foreach (ValueColumn column in valueSet.Tables[0].Columns)
            {
                stringBuilder.AppendFormat("{0}{1}", (num > 0 ? ", " : " "), column.ColumnName);
                num++;
            }
            stringBuilder.AppendFormat(" )\r\n\t\t\tVALUES(", Array.Empty <object>());
            num = 0;
            foreach (ValueColumn valueColumn in valueSet.Tables[0].Columns)
            {
                stringBuilder.AppendFormat("{0}#{{{1}}}", (num > 0 ? ", " : " "), SourceBuilder.ToPascalCase(valueColumn.ColumnName));
                num++;
            }
            stringBuilder.AppendFormat(" )", Array.Empty <object>());
            stringBuilder.Append("\r\n\t\t\t]]>\r\n");
            stringBuilder.Append("\t\t</post>\r\n\r\n");
            stringBuilder.AppendFormat("\t\t<get id=\"{0}.Get\">\r\n\t\t", pascalCase);
            stringBuilder.Append("<![CDATA[\r\n\t\t\t");
            stringBuilder.AppendFormat("SELECT ", Array.Empty <object>());
            num = 0;
            foreach (ValueColumn column1 in valueSet.Tables[0].Columns)
            {
                stringBuilder.AppendFormat("{0}{1}", (num > 0 ? ", " : string.Empty), column1.ColumnName);
                num++;
            }
            stringBuilder.AppendFormat(" FROM {0} \r\n\t\t\tWHERE ", tablename);
            num = 0;
            foreach (string str3 in strs)
            {
                stringBuilder.AppendFormat("{0}{1}=@{2}", (num > 0 ? " AND " : string.Empty), str3, SourceBuilder.ToPascalCase(str3));
                num++;
            }
            stringBuilder.Append("\r\n\t\t\t]]>\r\n");
            stringBuilder.Append("\t\t</get>\r\n\r\n");
            stringBuilder.AppendFormat("\t\t<get id=\"{0}.GetList\">\r\n\t\t", pascalCase);
            stringBuilder.Append("<![CDATA[\r\n\t\t\t");
            stringBuilder.AppendFormat("SELECT ", Array.Empty <object>());
            num = 0;
            foreach (ValueColumn valueColumn1 in valueSet.Tables[0].Columns)
            {
                stringBuilder.AppendFormat("{0}{1}", (num > 0 ? ", " : string.Empty), valueColumn1.ColumnName);
                num++;
            }
            stringBuilder.AppendFormat(" FROM {0} \r\n\t\t\tWHERE ", tablename);
            num = 0;
            foreach (string str4 in strs)
            {
                stringBuilder.AppendFormat("{0}{1}=@{2}", (num > 0 ? " AND " : string.Empty), str4, SourceBuilder.ToPascalCase(str4));
                num++;
            }
            stringBuilder.Append("\r\n\t\t\t]]>\r\n");
            stringBuilder.Append("\t\t</get>\r\n\r\n");
            stringBuilder.AppendFormat("\t\t<patch id=\"{0}.Update\">\r\n\t\t\t", pascalCase);
            stringBuilder.Append("<![CDATA[\r\n\t\t\t");
            stringBuilder.AppendFormat("UPDATE {0} SET \r\n\t\t\t", tablename);
            num = 0;
            foreach (ValueColumn column2 in valueSet.Tables[0].Columns)
            {
                stringBuilder.AppendFormat("{0}{1}=@{2}", (num > 0 ? ", " : string.Empty), column2.ColumnName, SourceBuilder.ToPascalCase(column2.ColumnName));
                stringBuilder.AppendFormat("\r\n\t\t\t", Array.Empty <object>());
                num++;
            }
            stringBuilder.AppendFormat("WHERE ", Array.Empty <object>());
            num = 0;
            foreach (string str5 in strs)
            {
                stringBuilder.AppendFormat("{0}{1}=@{2}", (num > 0 ? " AND " : string.Empty), str5, SourceBuilder.ToPascalCase(str5));
                num++;
            }
            stringBuilder.Append("\r\n\t\t\t]]>\r\n");
            stringBuilder.Append("\t\t</patch>\r\n\r\n");
            stringBuilder.AppendFormat("\t\t<delete id=\"{0}.Delete\">\r\n\t\t\t", pascalCase);
            stringBuilder.Append("<![CDATA[\r\n\t\t\t");
            stringBuilder.AppendFormat("DELETE FROM {0} WHERE ", tablename);
            num = 0;
            foreach (string str6 in strs)
            {
                stringBuilder.AppendFormat("{0}{1}=@{2}", (num > 0 ? " AND " : string.Empty), str6, SourceBuilder.ToPascalCase(str6));
                num++;
            }
            stringBuilder.Append("\r\n\t\t\t]]>\r\n");
            stringBuilder.Append("\t\t</delete>\r\n\r\n");
            stringBuilder.Append("\t</commands>\r\n\r\n");
            stringBuilder.Append("\t<matches>\r\n");
            stringBuilder.Append("\t</matches>\r\n");
            stringBuilder.Append("</mapper>");
            return(stringBuilder.ToString());
        }
Esempio n. 2
0
        public string BuildEntity(string tablename)
        {
            MapperProvider defaultProvider = MapperProvider.DefaultProvider;
            string         str             = string.Concat("SELECT * FROM ", tablename);
            ValueSet       valueSet        = defaultProvider.QueryForValueSet(str, null, CommandType.Text);
            StringBuilder  stringBuilder   = new StringBuilder();

            stringBuilder.AppendLine("using Test.Core;");
            stringBuilder.AppendLine("using System;");
            stringBuilder.AppendLine();
            stringBuilder.AppendLine(string.Concat("namespace ", this._namespace));
            stringBuilder.AppendLine("{");
            str = string.Concat("select object_id from sys.all_objects where type_desc='USER_TABLE' and name='", tablename, "'");
            string        str1 = defaultProvider.QueryForScalar(str, null, CommandType.Text).ToString();
            List <string> strs = new List <string>();

            str = string.Concat("select * from sys.columns where object_id='", str1, "'");
            ValueTable item = defaultProvider.QueryForValueSet(str, null, CommandType.Text).Tables[0];

            str = string.Concat("select * from sys.index_columns where object_id='", str1, "'");
            ValueTable    valueTable = defaultProvider.QueryForValueSet(str, null, CommandType.Text).Tables[0];
            List <string> strs1      = new List <string>();

            foreach (ValueRow row in valueTable.Rows)
            {
                str = string.Concat(new object[] { "select name from sys.columns where object_id='", str1, "' AND column_id='", row["column_id"], "'" });
                string str2 = defaultProvider.QueryForScalar(str, null, CommandType.Text).ToString();
                strs1.Add(str2);
            }
            stringBuilder.AppendLine(string.Format("\tpublic class {0}Entity : BaseEntity", this.GetCName(tablename, "TB_")));
            stringBuilder.AppendLine("\t{");
            foreach (ValueColumn column in valueSet.Tables[0].Columns)
            {
                ValueRow valueRow = item.Rows.FirstOrDefault <ValueRow>((ValueRow p) => p["name"].ToString() == column.ColumnName);
                string   str3     = "string";
                string   str4     = column.DataType.ToString();
                if (str4 == "System.Int32")
                {
                    str3 = "int";
                }
                else if (str4 == "System.Int64")
                {
                    str3 = "long";
                }
                else if (str4 == "System.Double")
                {
                    str3 = "double";
                }
                else if (str4 == "System.Single")
                {
                    str3 = "float";
                }
                else if (str4 == "System.DateTime")
                {
                    str3 = "DateTime";
                }
                else
                {
                    str3 = (str4 == "System.Boolean" ? "bool" : "string");
                }
                stringBuilder.AppendFormat(string.Concat("\t\t[Column(\"", column.ColumnName, "\", "), Array.Empty <object>());
                str = string.Concat("select name from sys.types where system_type_id='", valueRow["system_type_id"], "'");
                string       name    = defaultProvider.QueryForScalar(str, null, CommandType.Text).ToString();
                MemberInfo[] members = typeof(SqlDataType).GetMembers();
                for (int i = 0; i < (int)members.Length; i++)
                {
                    MemberInfo memberInfo = members[i];
                    if (memberInfo.Name.ToLower() == name)
                    {
                        name = memberInfo.Name;
                    }
                }
                stringBuilder.AppendFormat(", SqlDataType.{0}", name);
                if ((!(bool)valueRow["is_nullable"] || column.Unique || strs1.Contains(column.ColumnName)) && strs1.Contains(column.ColumnName))
                {
                    stringBuilder.AppendFormat("SqlColumnType.PrimaryKey", Array.Empty <object>());
                }
                stringBuilder.AppendLine(")]");
                stringBuilder.AppendLine(string.Format("\t\tpublic {0} {1} {{ get; set; }}", str3, this.GetCName(column.ColumnName, "")));
                stringBuilder.AppendLine();
            }
            stringBuilder.AppendLine("\t}");
            stringBuilder.AppendLine("}");
            return(stringBuilder.ToString());
        }