Esempio n. 1
0
 public MtInsertableProvider()
 {
     Type t = typeof(T);
     _tableName = t.Name;
     SugarTable sugarTable = t.GetAttributeInfo<SugarTable>();
     if (sugarTable != null)
     {
         if (!string.IsNullOrWhiteSpace(sugarTable.TableName))
         {
             _tableName = sugarTable.TableName;
         }
     }
     _dbColumnInfos = new List<DbColumnInfo>();
     _dbColumnInfos.ConcatWith(t.GetProperties(), prop =>
     {
         SugarColumn sugarColumn = prop.GetAttributeInfo<SugarColumn>();
         if (sugarColumn != null)
         {
             return !sugarColumn.IsOnlyIgnoreInsert;
         }
         return true;
     }, (column, prop) =>
      {
          column.DbColumnName = prop.Name;
          column.PropertyName = prop.Name;
          column.PropertyType = prop.PropertyType;
          SugarColumn sugarColumn = prop.GetAttributeInfo<SugarColumn>();
          if (sugarColumn != null)
          {
              column.DbColumnName = sugarColumn.ColumnName;
          }
      });
 }
Esempio n. 2
0
        private void InitPrimaryKeyList()
        {
            Type type = base.GetType();

            if (!BaseEntity.m_lstKeys.ContainsKey(type) || BaseEntity.m_lstKeys[type].Count <= 0)
            {
                lock (BaseEntity.m_lstKeys)
                {
                    if (!BaseEntity.m_lstKeys.ContainsKey(type) || BaseEntity.m_lstKeys[type].Count <= 0)
                    {
                        List <string>  list       = new List <string>();
                        PropertyInfo[] properties = base.GetType().GetProperties();
                        PropertyInfo[] array      = properties;
                        for (int i = 0; i < array.Length; i++)
                        {
                            PropertyInfo propertyInfo = array[i];
                            SugarColumn  sugarColumn  = (from it in propertyInfo.GetCustomAttributes(typeof(SugarColumn), true)
                                                         where it is SugarColumn
                                                         select(SugarColumn) it).FirstOrDefault <SugarColumn>();
                            if (sugarColumn != null)
                            {
                                if (sugarColumn.IsIdentity || sugarColumn.IsPrimaryKey)
                                {
                                    list.Add(propertyInfo.Name);
                                }
                            }
                        }
                        BaseEntity.m_lstKeys[type] = list;
                    }
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 获取当前字段的字段名
        /// </summary>
        /// <param name="property">当前处理的列属性</param>
        /// <returns>字段名</returns>
        public static string GetColName(PropertyInfo property)
        {
            SugarColumn columnConfig = property.GetCustomAttribute <SugarColumn>();

            if (columnConfig == null || string.IsNullOrEmpty(columnConfig.ColumnName))
            {
                return(property.Name);
            }
            return(columnConfig.ColumnName);
        }
Esempio n. 4
0
        /// <summary>
        /// 判断是否为可空字段
        /// </summary>
        /// <param name="property">当前处理的列属性</param>
        /// <returns>SQL指令</returns>
        public static string ColIsNullable(PropertyInfo property)
        {
            SugarColumn columnConfig = property.GetCustomAttribute <SugarColumn>();

            if (columnConfig == null)
            {
                return("NOT NULL");
            }
            else
            {
                return(columnConfig.IsNullable ? "" : "NOT NULL");
            }
        }
Esempio n. 5
0
        /// <summary>
        /// 判断是否为自增字段
        /// </summary>
        /// <param name="property">当前处理的列属性</param>
        /// <returns>SQL指令</returns>
        public static string ColIsIdentity(PropertyInfo property)
        {
            SugarColumn columnConfig = property.GetCustomAttribute <SugarColumn>();

            if (columnConfig == null)
            {
                return("");
            }
            else
            {
                return(columnConfig.IsIdentity ? "PRIMARY KEY AUTOINCREMENT" : "");
            }
        }
Esempio n. 6
0
        /// <summary>
        /// 判断是否为主键
        /// </summary>
        /// <param name="property"></param>
        /// <returns>是否为主键</returns>
        public static bool ColIsPrimaryKey(PropertyInfo property)
        {
            SugarColumn columnConfig = property.GetCustomAttribute <SugarColumn>();

            if (columnConfig == null)
            {
                return(false);
            }
            else
            {
                return(columnConfig.IsPrimaryKey);
            }
        }
        /// <summary>
        /// 获取每行数据的参数
        /// </summary>
        /// <returns></returns>
        private List <List <SugarParameter> > GetParametersByRow()
        {
            List <List <SugarParameter> > parametersList = new List <List <SugarParameter> >();

            switch (InsertBy)
            {
            case InsertBy.SelectQuery:
                parametersList.Add(QueryBuilder.Parameters);
                break;

            case InsertBy.ProvidedValues:
                Type type = typeof(T);
                List <PropertyInfo> props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly).ToList();
                for (int i = 0; i < Values.Count; i++)
                {
                    List <SugarParameter> parameters = new List <SugarParameter>();
                    foreach (DbColumnInfo colInfo in Columns)
                    {
                        SugarParameter sugarParameter = new SugarParameter(PrefixParams + colInfo.DbColumnName + i, null);
                        PropertyInfo   prop           = props.FirstOrDefault(x =>
                        {
                            string colName = x.Name;
                            if (x.GetCustomAttributes().Any(x => x is SugarColumn))
                            {
                                SugarColumn column = x.GetCustomAttributes().First(x => x is SugarColumn) as SugarColumn;
                                if (!string.IsNullOrWhiteSpace(column.ColumnName))
                                {
                                    colName = column.ColumnName;
                                }
                            }
                            return(colName.Equals(colInfo.DbColumnName, StringComparison.OrdinalIgnoreCase));
                        });
                        if (prop != null)
                        {
                            sugarParameter.Value = prop.GetValue(Values[i]);
                        }
                        parameters.Add(sugarParameter);
                    }
                    parametersList.Add(parameters);
                }
                break;
            }
            return(parametersList);
        }
Esempio n. 8
0
        /// <summary>
        /// 判断字段类型
        /// </summary>
        /// <param name="property">当前处理的列属性</param>
        /// <returns>SQL指令</returns>
        public static string GetColType(PropertyInfo property)
        {
            SugarColumn columnConfig = property.GetCustomAttribute <SugarColumn>();

            if (columnConfig?.ColumnDataType == null)
            {
                //整数类型
                if (property.PropertyType == typeof(sbyte) ||
                    property.PropertyType == typeof(byte) ||
                    property.PropertyType == typeof(short) ||
                    property.PropertyType == typeof(ushort) ||
                    property.PropertyType == typeof(int) ||
                    property.PropertyType == typeof(uint) ||
                    property.PropertyType == typeof(long) ||
                    property.PropertyType == typeof(ulong) ||
                    property.PropertyType == typeof(char) ||
                    property.PropertyType == typeof(bool))
                {
                    return("INTEGER");
                }

                //浮点数类型
                if (property.PropertyType == typeof(double) ||
                    property.PropertyType == typeof(float) ||
                    property.PropertyType == typeof(decimal))
                {
                    return("REAL");
                }

                //字符串类型
                if (property.PropertyType == typeof(string))
                {
                    return("TEXT");
                }

                //其他类型
                return("BLOB");
            }

            return(columnConfig.ColumnDataType);
        }
        public string BuildSql()
        {
            StringBuilder sb = new StringBuilder();

            switch (InsertType)
            {
            case ModifyType.UpdateExisted:
                sb.Append($"INSERT INTO {GetTableSql()}{GetColumnsSql()} {GetInsertValuesSql()} ON DUPLICATE KEY UPDATE {GetUpdateValuesSql()};");
                break;

            case ModifyType.IgnoreExisted:
                sb.Append($"INSERT IGNORE INTO {GetTableSql()}{GetColumnsSql()} {GetInsertValuesSql()};");
                break;

            case ModifyType.Replace:
                sb.Append($"REPLACE INTO {GetTableSql()}{GetColumnsSql()} {GetInsertValuesSql()};");
                break;

            case ModifyType.DirectInsert:
            default:
                sb.Append($"INSERT INTO {GetTableSql()}{GetColumnsSql()} {GetInsertValuesSql()};");
                break;
            }
            switch (InsertedResultType)
            {
            case InsertedResultType.AffectedLastId:
                sb.Append("SELECT LAST_INSERT_ID();");
                break;

            case InsertedResultType.AffectedAllId:
                if (this.InsertBy == InsertBy.ProvidedValues)
                {
                    int?           count         = Values?.Count;
                    string         primaryKey    = string.Empty;
                    PropertyInfo[] properties    = typeof(T).GetProperties();
                    PropertyInfo   sugarProperty = properties.FirstOrDefault(p => p.GetCustomAttributes().Any(a => a is SqlSugar.SugarColumn sc && sc.IsPrimaryKey));
                    primaryKey = sugarProperty.Name;
                    if (sugarProperty != null)
                    {
                        SugarColumn sugarColumn = sugarProperty.GetCustomAttributes().First(a => a is SqlSugar.SugarColumn sc && sc.IsPrimaryKey) as SugarColumn;
                        if (!string.IsNullOrWhiteSpace(sugarColumn.ColumnName))
                        {
                            primaryKey = sugarColumn.ColumnName;
                        }
                    }
                    if (primaryKey == string.Empty || count == null || count == 0)
                    {
                        sb.Append("SELECT -1 as Id;");
                    }
                    else
                    {
                        sb.Append($"SELECT `{primaryKey}` AS Id FROM {GetTableSql()} ORDER BY {primaryKey} DESC LIMIT {count};");
                    }
                }
                break;

            case InsertedResultType.AffectedAllEntity:
                if (this.InsertBy == InsertBy.ProvidedValues)
                {
                    int?           count         = Values?.Count;
                    string         primaryKey    = string.Empty;
                    PropertyInfo[] properties    = typeof(T).GetProperties();
                    PropertyInfo   sugarProperty = properties.FirstOrDefault(p => p.GetCustomAttributes().Any(a => a is SqlSugar.SugarColumn sc && sc.IsPrimaryKey));
                    primaryKey = sugarProperty.Name;
                    if (sugarProperty != null)
                    {
                        SugarColumn sugarColumn = sugarProperty.GetCustomAttributes().First(a => a is SqlSugar.SugarColumn sc && sc.IsPrimaryKey) as SugarColumn;
                        if (!string.IsNullOrWhiteSpace(sugarColumn.ColumnName))
                        {
                            primaryKey = sugarColumn.ColumnName;
                        }
                    }
                    if (primaryKey == string.Empty || count == null || count == 0)
                    {
                        sb.Append("SELECT -1 as Id;");
                    }
                    else
                    {
                        sb.Append($"SELECT * FROM {GetTableSql()} ORDER BY {primaryKey} DESC LIMIT {count};");
                    }
                    sb.Append($"SELECT * FROM {GetTableSql()} ORDER BY {primaryKey} DESC LIMIT {count};");
                }
                break;
            }
            return(sb.ToString());
        }