Exemple #1
0
        public string CreateDeleteSql(TL model, out DBParam[] outparam)
        {
            StringBuilder stringBuilder = new StringBuilder();

            PropertyInfo[] properties   = EntityTypeCache.GetEntityInfo(typeof(TL)).Properties;
            PropertyInfo   propertyInfo = null;
            List <DBParam> list         = new List <DBParam>();

            for (int i = 0; i < properties.Length; i++)
            {
                object[]        customAttributes = properties[i].GetCustomAttributes(typeof(EntityAttribute), inherit: true);
                EntityAttribute entityAttribute  = null;
                if (customAttributes.Length > 0)
                {
                    entityAttribute = (EntityAttribute)customAttributes[0];
                }
                if (properties[i].Name == PrimaryKey)
                {
                    propertyInfo = properties[i];
                }
            }
            stringBuilder.Append("  " + PrimaryKey + "=@" + PrimaryKey);
            list.Add(new DBParam
            {
                ParamDbType = EntityHelper.GetDbType(propertyInfo.PropertyType),
                ParamName   = propertyInfo.Name,
                ParamValue  = propertyInfo.GetValue(model, null)
            });
            outparam = list.ToArray();
            return(CreateDeleteSql(stringBuilder.ToString()));
        }
Exemple #2
0
        public string CreatePageSql(string selector, int pageIndex, int pageSize, string ordering, string colList, params DBParam[] values)
        {
            pageIndex = ((pageIndex <= 0) ? 1 : pageIndex);
            if (pageSize == 0)
            {
                pageSize = PageSize;
            }
            string text = "";

            if (string.IsNullOrEmpty(colList) || colList == "*")
            {
                PropertyInfo[] properties = EntityTypeCache.GetEntityInfo(typeof(TL)).Properties;
                PropertyInfo[] array      = properties;
                foreach (PropertyInfo propertyInfo in array)
                {
                    object[]        customAttributes = propertyInfo.GetCustomAttributes(typeof(EntityAttribute), inherit: true);
                    EntityAttribute entityAttribute  = null;
                    if (customAttributes.Length > 0)
                    {
                        entityAttribute = (EntityAttribute)customAttributes[0];
                    }
                    if (!(entityAttribute?.CustomMember ?? false))
                    {
                        text = text + "[" + propertyInfo.Name + "],";
                    }
                }
                text = text.Substring(0, text.Length - 1);
            }
            else
            {
                text = GetSQLFildList(colList);
            }
            StringBuilder stringBuilder = new StringBuilder();
            string        arg;

            if (string.IsNullOrEmpty(ordering))
            {
                arg = ((!string.IsNullOrEmpty(PrimaryKey)) ? $" order by {PrimaryKey} desc" : string.Format(" order by {0} desc", "newid()"));
            }
            else
            {
                ordering = ordering.Replace("descending", "desc").Replace("ascending", "asc");
                arg      = $" order by {ordering}";
            }
            if (string.IsNullOrEmpty(selector))
            {
                stringBuilder.Append($"select {text} from {TableName} ");
                stringBuilder.Append($" {arg} limit {(pageIndex - 1) * pageSize},{pageSize};");
            }
            else
            {
                stringBuilder.Append($"select {text} from {TableName} ");
                stringBuilder.Append($" where {SqlHelper.ParseSelector(selector, values)}");
                stringBuilder.Append($" {arg} limit {(pageIndex - 1) * pageSize},{pageSize};");
            }
            return(stringBuilder.ToString());
        }
Exemple #3
0
        public string CreateUpdateSql(TL model, string colList, string selector, out DBParam[] outparam, params DBParam[] values)
        {
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append("update  " + TableName + " set ");
            PropertyInfo[] properties   = EntityTypeCache.GetEntityInfo(typeof(TL)).Properties;
            PropertyInfo   propertyInfo = null;
            List <DBParam> list         = new List <DBParam>();

            for (int i = 0; i < properties.Length; i++)
            {
                object[]        customAttributes = properties[i].GetCustomAttributes(typeof(EntityAttribute), inherit: true);
                EntityAttribute entityAttribute  = null;
                if (customAttributes.Length > 0)
                {
                    entityAttribute = (EntityAttribute)customAttributes[0];
                }
                if (properties[i].Name == PrimaryKey)
                {
                    propertyInfo = properties[i];
                }
                else if (properties[i].Name != PrimaryKey && !(entityAttribute?.CustomMember ?? false) && (string.IsNullOrEmpty(colList) || colList.IndexOf(properties[i].Name.Trim()) >= 0) && !EntityHelper.IsTypeMinValue(properties[i].GetValue(model, null), EntityHelper.GetDbType(properties[i].PropertyType)))
                {
                    stringBuilder.Append("[" + properties[i].Name + "]=@" + properties[i].Name + ",");
                    list.Add(new DBParam
                    {
                        ParamDbType = EntityHelper.GetDbType(properties[i].PropertyType),
                        ParamName   = properties[i].Name,
                        ParamValue  = properties[i].GetValue(model, null)
                    });
                }
            }
            stringBuilder = stringBuilder.Replace(",", " ", stringBuilder.Length - 1, 1);
            if (!string.IsNullOrEmpty(selector))
            {
                stringBuilder.Append(" where 1=1  and " + selector);
                if (values != null)
                {
                    for (int i = 0; i < values.Length; i++)
                    {
                        list.Insert(i, values[i]);
                    }
                }
            }
            else
            {
                stringBuilder.Append(" where [" + PrimaryKey + "]=@" + PrimaryKey);
                list.Add(new DBParam
                {
                    ParamDbType = EntityHelper.GetDbType(propertyInfo.PropertyType),
                    ParamName   = propertyInfo.Name,
                    ParamValue  = propertyInfo.GetValue(model, null)
                });
            }
            outparam = list.ToArray();
            return(stringBuilder.ToString());
        }
Exemple #4
0
        public string CreateInsertSql(TL model, out DBParam[] outparam)
        {
            StringBuilder stringBuilder  = new StringBuilder();
            StringBuilder stringBuilder2 = new StringBuilder();

            stringBuilder.Append($"insert into {TableName}(");
            PropertyInfo[] properties = EntityTypeCache.GetEntityInfo(typeof(TL)).Properties;
            List <DBParam> list       = new List <DBParam>();

            for (int i = 0; i < properties.Length; i++)
            {
                object[]        customAttributes = properties[i].GetCustomAttributes(typeof(EntityAttribute), inherit: true);
                EntityAttribute entityAttribute  = null;
                if (customAttributes.Length > 0)
                {
                    entityAttribute = (EntityAttribute)customAttributes[0];
                }
                if (!model.IsAutoID || !(model.PrimaryKey == properties[i].Name) || entityAttribute == null || !entityAttribute.IsDbGenerated)
                {
                    if (properties[i].Name == PrimaryKey && entityAttribute != null && !entityAttribute.IsDbGenerated)
                    {
                        stringBuilder.Append("[" + properties[i].Name + "],");
                        stringBuilder2.Append("@" + properties[i].Name + ",");
                        list.Add(new DBParam
                        {
                            ParamName   = properties[i].Name,
                            ParamDbType = EntityHelper.GetDbType(properties[i].PropertyType),
                            ParamValue  = EntityHelper.GetTypeDefaultValue(properties[i].GetValue(model, null), EntityHelper.GetDbType(properties[i].PropertyType))
                        });
                    }
                    else if (properties[i].Name != PrimaryKey && (entityAttribute == null || (!entityAttribute.IsDbGenerated && !entityAttribute.CustomMember)))
                    {
                        stringBuilder.Append("[" + properties[i].Name + "],");
                        stringBuilder2.Append("@" + properties[i].Name + ",");
                        list.Add(new DBParam
                        {
                            ParamName   = properties[i].Name,
                            ParamDbType = EntityHelper.GetDbType(properties[i].PropertyType),
                            ParamValue  = EntityHelper.GetTypeDefaultValue(properties[i].GetValue(model, null), EntityHelper.GetDbType(properties[i].PropertyType))
                        });
                    }
                }
            }
            stringBuilder  = stringBuilder.Replace(",", ")", stringBuilder.Length - 1, 1);
            stringBuilder2 = stringBuilder2.Replace(",", ")", stringBuilder2.Length - 1, 1);
            stringBuilder.Append(" values (");
            stringBuilder.Append(stringBuilder2.ToString() + ";");
            if (model.IsAutoID)
            {
                stringBuilder.Append($" select ident_current('{TableName}') ");
            }
            outparam = list.ToArray();
            return(stringBuilder.ToString());
        }
Exemple #5
0
        public virtual TL GetModel(IDataReader dr)
        {
            TL val = new TL();

            PropertyInfo[] properties = EntityTypeCache.GetEntityInfo(typeof(TL)).Properties;
            PropertyInfo[] array      = properties;
            foreach (PropertyInfo propertyInfo in array)
            {
                object[]        customAttributes = propertyInfo.GetCustomAttributes(typeof(EntityAttribute), inherit: true);
                EntityAttribute entityAttribute  = null;
                if (customAttributes.Length > 0)
                {
                    entityAttribute = (EntityAttribute)customAttributes[0];
                }
                if (entityAttribute != null && entityAttribute.CustomMember)
                {
                    continue;
                }
                int ordinal;
                try
                {
                    ordinal = dr.GetOrdinal(propertyInfo.Name);
                }
                catch (IndexOutOfRangeException)
                {
                    continue;
                }
                object value;
                if (dr.IsDBNull(ordinal) && EntityHelper.GetDbType(propertyInfo.PropertyType) == DbType.String)
                {
                    value = string.Empty;
                }
                else
                {
                    if (dr.IsDBNull(ordinal))
                    {
                        continue;
                    }
                    value = dr.GetValue(ordinal);
                }
                propertyInfo.SetValue(val, value, null);
            }
            return(val);
        }
Exemple #6
0
        public string CreatePageSql(TL model, out DBParam[] outparam)
        {
            PropertyInfo[] properties = EntityTypeCache.GetEntityInfo(typeof(TL)).Properties;
            List <DBParam> list       = new List <DBParam>();

            for (int i = 0; i < properties.Length; i++)
            {
                if (properties[i].Name == PrimaryKey)
                {
                    list.Add(new DBParam
                    {
                        ParamName   = properties[i].Name,
                        ParamDbType = EntityHelper.GetDbType(properties[i].PropertyType),
                        ParamValue  = properties[i].GetValue(model, null)
                    });
                    break;
                }
            }
            outparam = list.ToArray();
            return(CreatePageSql(string.Format("{0}=@{0}", PrimaryKey), 0, PageSize, string.Empty, string.Empty, outparam));
        }