예제 #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
        public virtual bool CreateOrUpdate(TElement model, params string[] keys)
        {
            var           dic            = ToSqlDictionary(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);
            }
            if (Exists(whereClause))
            {
                sqlClause = BuildUpdateSql(dic, keys);
                if (string.IsNullOrEmpty(sqlClause))
                {
                    return(false);
                }
                _searchService.ExcuteSql(sqlClause);
            }
            else
            {
                sqlClause = BuildCreateSql(dic);
                if (string.IsNullOrEmpty(sqlClause))
                {
                    return(false);
                }


                if (IsIdentity())
                {
                    var propertyList = _searchService.GetPropertyInfos(typeof(TElement));
                    var property     =
                        propertyList.FirstOrDefault(
                            x => string.Equals(x.Name, PrimaryKey, StringComparison.OrdinalIgnoreCase));
                    if (property != null)
                    {
                        var value = _searchService.GetSingle(sqlClause);
                        property.SetValue(model, value, new object[] { });
                    }
                }
                else
                {
                    _searchService.ExcuteSql(sqlClause);
                }
            }
            return(true);
        }