Exemple #1
0
        internal static string BuildInsertSql(EntityMeta meta, List <string> list)
        {
            if (list == null || list.Count == 0)
            {
                return(string.Empty);
            }

            StringBuilder sqlbuilder = new StringBuilder();

            sqlbuilder.AppendFormat("INSERT INTO [{0}] (", meta.TableName);
            for (int i = 0, j = 0; i < meta.Columns.Count; i++)
            {
                if (meta.Columns[i].Identity || !list.Contains(meta.Columns[i].ColumnName))
                {
                    continue;
                }
                if (j > 0)
                {
                    sqlbuilder.Append(",");
                }
                sqlbuilder.Append("[" + meta.Columns[i].ColumnName + "]");
                j++;
            }
            sqlbuilder.Append(") VALUES (");
            for (int i = 0, j = 0; i < meta.Columns.Count; i++)
            {
                if (meta.Columns[i].Identity || !list.Contains(meta.Columns[i].ColumnName))
                {
                    continue;
                }
                if (j > 0)
                {
                    sqlbuilder.Append(",");
                }
                sqlbuilder.Append("@" + meta.Columns[i].ColumnName + "");
                j++;
            }
            sqlbuilder.Append(");");

            if (meta.Columns.Exists(x => x.Identity))
            {
                sqlbuilder.Append(" SELECT CAST(SCOPE_IDENTITY() as bigint) as Id;"); //sqlbuilder.Append("SELECT SCOPE_IDENTITY();");
            }
            else
            {
                sqlbuilder.Append(" SELECT  CAST(0 as bigint) as Id;");
            }

            return(sqlbuilder.ToString());
        }
Exemple #2
0
        internal static string BuildUpdateSql(EntityMeta meta, List <string> list)
        {
            if (list == null || list.Count == 0)
            {
                return(string.Empty);
            }


            var keys = meta.Columns.FindAll(_ => _.PrimaryKey);

            StringBuilder sqlbuilder = new StringBuilder();

            sqlbuilder.AppendFormat("UPDATE [{0}] SET ", meta.TableName);

            for (int i = 0, j = 0; i < meta.Columns.Count; i++)
            {
                if (!meta.Columns[i].PrimaryKey && list.Contains(meta.Columns[i].ColumnName))
                {
                    if (j > 0)
                    {
                        sqlbuilder.Append(",");
                    }
                    j++;
                    sqlbuilder.Append("[" + meta.Columns[i].ColumnName + "]=@" + meta.Columns[i].ColumnName + "");
                }
            }
            sqlbuilder.Append(" WHERE ");
            for (int i = 0; i < keys.Count; i++)
            {
                if (i > 0)
                {
                    sqlbuilder.Append(" AND ");
                }
                sqlbuilder.Append("[" + keys[i].ColumnName + "]=@" + keys[i].ColumnName);
            }

            return(sqlbuilder.ToString());
        }
Exemple #3
0
        public static string EntityMetaToUpdateSQL(EntityMeta meta)
        {
            if (meta.Columns == null || meta.Columns.Count == 0)
            {
                return(string.Empty);
            }


            var keys = meta.Columns.FindAll(_ => _.PrimaryKey);

            StringBuilder sqlbuilder = new StringBuilder();

            sqlbuilder.AppendFormat("UPDATE [{0}] SET ", meta.TableName);

            for (int i = 0, j = 0; i < meta.Columns.Count; i++)
            {
                if (!meta.Columns[i].PrimaryKey)
                {
                    if (j > 0)
                    {
                        sqlbuilder.Append(",");
                    }
                    j++;
                    sqlbuilder.Append("[" + meta.Columns[i].ColumnName + "]=@" + meta.Columns[i].ColumnName + "");
                }
            }
            sqlbuilder.Append(" WHERE ");
            for (int i = 0; i < keys.Count; i++)
            {
                if (i > 0)
                {
                    sqlbuilder.Append(" AND ");
                }
                sqlbuilder.Append("[" + keys[i].ColumnName + "]=@" + keys[i].ColumnName);
            }

            return(sqlbuilder.ToString());
        }
Exemple #4
0
        public static EntityMeta GetDefineInfoFromType(Type type)
        {
            EntityMeta tdefine = null;

            lock (lockobject)
            {
                if (_cache.ContainsKey(type))
                {
                    tdefine = _cache[type];
                }
            }
            if (tdefine != null)
            {
                return(tdefine);
            }
            else
            {
                tdefine = new EntityMeta();
            }

            TableNameAttribute tableAttribute = (TableNameAttribute)Attribute.GetCustomAttribute(type, typeof(TableNameAttribute));

            if (tableAttribute != null)
            {
                tdefine.TableName = tableAttribute.TableName;
            }
            else
            {
                tdefine.TableName = type.FullName.Split('.').Last();
            }

            PropertyInfo[] pinfos = type.GetProperties();
            foreach (PropertyInfo p in pinfos)
            {
                Attribute[]      attrs  = Attribute.GetCustomAttributes(p);
                EntityColumnMeta ecmeta = new EntityColumnMeta();
                ecmeta.ColumnName = p.Name;

                foreach (Attribute cusattr in attrs)
                {
                    if (cusattr is IgnoreAttribute)
                    {
                        ecmeta = null;
                        break;
                    }
                    if (cusattr is PrimaryKeyAttribute)
                    {
                        ecmeta.PrimaryKey = true;
                    }
                    if (cusattr is MapFieldAttribute)
                    {
                        ecmeta.ColumnName = ((MapFieldAttribute)cusattr).MapFieldName;
                    }
                    if (cusattr is IdentityAttribute)
                    {
                        ecmeta.Identity = true;
                    }
                    if (cusattr is NullableAttribute)
                    {
                        ecmeta.Nullable = true;
                    }
                }
                if (ecmeta != null)
                {
                    tdefine.Columns.Add(ecmeta);
                }
            }
            lock (lockobject)
            {
                if (!_cache.ContainsKey(type))
                {
                    _cache.Add(type, tdefine);
                }
            }
            return(tdefine);
        }