Esempio n. 1
0
 internal static ValueRow FirstRow(this ValueTable dt)
 {
     if (dt == null)
     {
         return(null);
     }
     if (dt.Rows.Count == 0)
     {
         return(null);
     }
     return(dt.Rows[0]);
 }
Esempio n. 2
0
        public static Dictionary <TKey, TValue> ToDictionary <TKey, TValue>(this ValueTable dt)
            where TValue : ValueObject
        {
            Dictionary <TKey, TValue> tKeys = new Dictionary <TKey, TValue>(dt.Rows.Count);
            Type         type         = typeof(TValue);
            PropertyInfo propertyInfo = null;
            string       empty        = string.Empty;

            PropertyInfo[] properties = type.GetProperties();
            for (int i = 0; i < (int)properties.Length; i++)
            {
                PropertyInfo    propertyInfo1   = properties[i];
                ColumnAttribute customAttribute = propertyInfo1.GetCustomAttribute <ColumnAttribute>();
                if (customAttribute != null && (customAttribute.ColumnType & SqlColumnType.PrimaryKey) == SqlColumnType.PrimaryKey)
                {
                    propertyInfo = propertyInfo1;
                    empty        = customAttribute.Name;
                }
            }
            if (propertyInfo == null)
            {
                throw new Exception(string.Concat(type.ToString(), " not exist primary key property"));
            }
            string columnName = string.Empty;

            foreach (ValueColumn column in dt.Columns)
            {
                if (!column.ColumnName.MatchDataName(empty))
                {
                    continue;
                }
                columnName = column.ColumnName;
            }
            if (string.IsNullOrEmpty(columnName))
            {
                throw new Exception(string.Concat(type.ToString(), " not exist primary key column"));
            }
            if (dt != null)
            {
                foreach (ValueRow row in dt.Rows)
                {
                    TKey item = (TKey)row[columnName];
                    if (tKeys.ContainsKey(item))
                    {
                        throw new Exception(string.Concat(type.ToString(), " duplicate key:", tKeys[item]));
                    }
                    TValue tValue = default(TValue);
                    tKeys.Add(item, row.ToVo <TValue>(tValue, false));
                }
            }
            return(tKeys);
        }
Esempio n. 3
0
        private void BuildSPCommand()
        {
            object value;

            string[] strArrays = Regex.Replace(Regex.Replace(this.Text, "[\\s]+", " "), " *, *", ",").Trim().Split(new char[] { ' ' });
            if ((int)strArrays.Length < 1)
            {
                throw new Exception("sentense is wrong");
            }
            this.Text = strArrays[0];
            if ((int)strArrays.Length > 1)
            {
                string[] strArrays1 = strArrays[1].Split(new char[] { ',' });
                for (int i = 0; i < (int)strArrays1.Length; i++)
                {
                    string              str                  = strArrays1[i];
                    string[]            strArrays2           = str.Split(new char[] { '=' });
                    MapperParameterItem mapperParameterItem  = this.Parameter.Add(strArrays2[0]);
                    MapperParameterItem mapperParameterItem1 = mapperParameterItem;
                    ParameterSource     parameterSource      = this.parameterSource.Find((ParameterSource p) => p.Name.MatchDataName(strArrays2[0]));
                    if (parameterSource != null)
                    {
                        value = parameterSource.Value;
                    }
                    else
                    {
                        value = null;
                    }
                    mapperParameterItem1.Value = value;
                    if (!strArrays2[1].StartsWith("@"))
                    {
                        mapperParameterItem.Direction = ParameterDirection.Input;
                        mapperParameterItem.Value     = strArrays2[1].Trim(new char[] { '\'' });
                    }
                    else if (strArrays2[1].LastIndexOf("<table>") > 0)
                    {
                        mapperParameterItem.IsTable = true;
                        ValueTable dataTable = mapperParameterItem.Value.ToString().ToDataTable();
                        dataTable.TableName       = strArrays2[0];
                        mapperParameterItem.Value = dataTable;
                    }
                    else if (strArrays2[1].LastIndexOf("<out>") <= 0)
                    {
                        mapperParameterItem.Direction = ParameterDirection.Input;
                    }
                    else
                    {
                        mapperParameterItem.Direction = ParameterDirection.Output;
                    }
                }
            }
        }
Esempio n. 4
0
        public static List <T> ToList <T>(this ValueTable dt)
            where T : ValueObject
        {
            List <T> ts = new List <T>(dt.Rows.Count);

            if (dt != null)
            {
                foreach (ValueRow row in dt.Rows)
                {
                    ts.Add(row.ToVo <T>(default(T), false));
                }
            }
            return(ts);
        }
Esempio n. 5
0
        public static string GetDataSetQuery(ValueTable dt, string tablename)
        {
            string        str;
            StringBuilder stringBuilder = new StringBuilder();

            foreach (ValueRow row in dt.Rows)
            {
                stringBuilder.AppendFormat("INSERT INTO {0} VALUES(", tablename);
                foreach (ValueColumn column in dt.Columns)
                {
                    str = (column.DataType != typeof(DateTime) ? row[column.ColumnName].ToString().Replace("'", "''") : SourceBuilder.GetDBDateString((DateTime)row[column.ColumnName]));
                    stringBuilder.AppendFormat("'{0}',", str);
                }
                stringBuilder.Remove(stringBuilder.Length - 1, 1);
                stringBuilder.AppendFormat(")\n", Array.Empty <object>());
            }
            return(stringBuilder.ToString());
        }
Esempio n. 6
0
        public void Generate()
        {
            ValueTable valueTable = MapperProvider.DefaultProvider.QueryForValueTable("select * from sys.all_objects where type_desc='USER_TABLE' and object_id>0", null, CommandType.Text);
            string     path       = this.webPath.Path;

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            if (!Directory.Exists(string.Concat(path, "Command/")))
            {
                Directory.CreateDirectory(string.Concat(path, "Command/"));
            }
            if (!Directory.Exists(string.Concat(path, "Entity/")))
            {
                Directory.CreateDirectory(string.Concat(path, "Entity/"));
            }
            if (!Directory.Exists(string.Concat(path, "Mapper/")))
            {
                Directory.CreateDirectory(string.Concat(path, "Mapper/"));
            }
            if (!Directory.Exists(string.Concat(path, "Service/")))
            {
                Directory.CreateDirectory(string.Concat(path, "Service/"));
            }
            if (!Directory.Exists(string.Concat(path, "Controller/")))
            {
                Directory.CreateDirectory(string.Concat(path, "Controller/"));
            }
            foreach (ValueRow row in valueTable.Rows)
            {
                string item       = row["name"] as string;
                string pascalCase = item.Substring(3);
                pascalCase = SourceBuilder.ToPascalCase(pascalCase);
                File.WriteAllText(string.Concat(path, "Command/", pascalCase, ".xml"), this.BuildCommand(item));
                File.WriteAllText(string.Concat(path, "Entity/", pascalCase, "Entity.cs"), this.BuildEntity(item));
                File.WriteAllText(string.Concat(path, "Mapper/I", pascalCase, "Mapper.cs"), this.BuildMapper(item));
                File.WriteAllText(string.Concat(path, "Service/", pascalCase, "Service.cs"), this.BuildService(item));
                File.WriteAllText(string.Concat(path, "Controller/", pascalCase, "Controller.cs"), this.BuildController(item));
            }
        }
Esempio n. 7
0
        public static void Fill(this DbDataReader sdr, ValueSet ds)
        {
            ValueTable valueTable = ds.Tables.Add();

            for (int i = 0; i < sdr.FieldCount; i++)
            {
                valueTable.Columns.Add(sdr.GetName(i), sdr.GetFieldType(i), sdr.GetDataTypeName(i));
            }
            while (sdr.Read())
            {
                ValueRow item = valueTable.NewRow();
                for (int j = 0; j < sdr.FieldCount; j++)
                {
                    item.ItemArray[j] = sdr[j];
                }
                valueTable.Rows.Add(item);
            }
            if (sdr.NextResult())
            {
                sdr.Fill(ds);
            }
        }
Esempio n. 8
0
        internal static T _ToVo <T>(ValueRow dr, T vo, bool useAlias)
            where T : ValueObject
        {
            Type reflectedType;
            Type type = typeof(T);

            if (dr == null)
            {
                if (vo != null)
                {
                    return(vo);
                }
                return(default(T));
            }
            if (vo == null)
            {
                vo = Activator.CreateInstance <T>();
            }
            BaseEntity baseEntity = (object)vo as BaseEntity;

            if (baseEntity != null)
            {
                baseEntity.SetValueRow(dr);
            }
            else
            {
            }
            ValueTable table = dr.Table;

            foreach (ValueColumn column in dr.Table.Columns)
            {
                string       columnName   = column.ColumnName;
                PropertyInfo propertyInfo = null;
                if (table.mappingOrinal != null)
                {
                    foreach (Tuple <Type, bool> tuple in table.mappingOrinal)
                    {
                        Type item1 = tuple.Item1;
                        ((tuple.Item2 ? ValueObject.DataMappingAlias[item1] : ValueObject.DataMapping[item1])).TryGetValue(columnName, out propertyInfo);
                        if (propertyInfo == null)
                        {
                            continue;
                        }
                        goto Label0;
                    }
                }
                else
                {
                    ((useAlias ? ValueObject.DataMappingAlias[type] : ValueObject.DataMapping[type])).TryGetValue(columnName, out propertyInfo);
                }
Label0:
                Type type1 = typeof(T);
                if (propertyInfo != null)
                {
                    reflectedType = propertyInfo.ReflectedType;
                }
                else
                {
                    reflectedType = null;
                }
                if (type1 != reflectedType)
                {
                    continue;
                }
                object item = dr[column.ColumnOrdinal.Value];
                vo.Invoke(item, propertyInfo);
            }
            return(vo);
        }
Esempio n. 9
0
 public static T ToFirstVo <T>(this ValueTable dt, T vo)
     where T : ValueObject
 {
     return(dt.FirstRow().ToVo <T>(vo, false));
 }
Esempio n. 10
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. 11
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());
        }