Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="model"></param>
        /// <param name="keys"></param>
        /// <returns></returns>
        public static bool Create <TElement>(this IRawSqlSearchService service, string tableName, string primaryKey,
                                             TElement model, params string[] keys) where TElement : class, new()
        {
            var           dic            = ToSqlDictionary(service.GetDictionary(model));
            List <string> primaryKeyList = new List <string>();

            if (keys != null)
            {
                primaryKeyList.AddRange(keys);
            }
            if (keys == null || !keys.Any())
            {
                primaryKeyList.Add(primaryKey);
            }
            var    whereClause = "";
            string sqlClause;

            foreach (var key in primaryKeyList)
            {
                var tempKey = key.ToLower();
                if (dic.ContainsKey(tempKey))
                {
                    whereClause = string.Format("{0}{1}{2}='{3}'", whereClause, string.IsNullOrEmpty(whereClause) ? "" : " AND ", tempKey, dic[tempKey]);
                }
            }
            if (string.IsNullOrEmpty(whereClause))
            {
                return(false);
            }

            sqlClause = BuildCreateSql(tableName, primaryKey, dic);
            if (string.IsNullOrEmpty(sqlClause))
            {
                return(false);
            }

            var propertyList = typeof(TElement).GetProperties().Where(x => x.CanWrite);
            var property     =
                propertyList.FirstOrDefault(
                    x => string.Equals(x.Name, primaryKey, StringComparison.OrdinalIgnoreCase));

            if (property != null && property.PropertyType == typeof(int))
            {
                var value = service.GetSingle(sqlClause);
                property.SetValue(model, value, new object[] { });
            }
            else
            {
                service.ExcuteSql(sqlClause);
            }
            return(true);
        }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="model"></param>
        /// <param name="keys"></param>
        /// <returns></returns>
        public static bool Update <TElement>(this IRawSqlSearchService service, string tableName, string primaryKey, TElement model, params string[] keys) where TElement : class, new()
        {
            var           dic            = ToSqlDictionary(service.GetDictionary(model));
            List <string> primaryKeyList = new List <string>();

            if (keys != null)
            {
                primaryKeyList.AddRange(keys);
            }
            if (keys == null || !keys.Any())
            {
                primaryKeyList.Add(primaryKey);
            }
            var    whereClause = "";
            string sqlClause;

            foreach (var key in primaryKeyList)
            {
                var tempKey = key.ToLower();
                if (dic.ContainsKey(tempKey))
                {
                    whereClause = string.Format("{0}{1}{2}='{3}'", whereClause, string.IsNullOrEmpty(whereClause) ? "" : " AND ", tempKey, dic[tempKey]);
                }
            }
            if (string.IsNullOrEmpty(whereClause))
            {
                return(false);
            }

            sqlClause = BuildUpdateSql(tableName, primaryKey, dic, keys);
            if (string.IsNullOrEmpty(sqlClause))
            {
                return(false);
            }
            service.ExcuteSql(sqlClause);

            return(true);
        }
Пример #3
0
 public virtual IDictionary <string, object> GetDictionary(TElement model)
 {
     return(_searchService.GetDictionary(model));
 }