public void InsertOrUpdate(string tablename, Guid storeId, IRawSqlSearchService service, DataTable table, List <string> primaryKey)
        {
            primaryKey = primaryKey.Where(x => table.Columns.Contains(x)).ToList();
            if (primaryKey.Count == 0)
            {
                return;
            }

            List <DataRow> willRemoveIndexs = new List <DataRow>();

            if (primaryKey.Count == 1)
            {
                string currentPrimaryKey = primaryKey[0];
                if (string.IsNullOrEmpty(currentPrimaryKey))
                {
                    return;
                }
                var whereClause = BuildWhereCluase(table, storeId, currentPrimaryKey);
                if (string.IsNullOrEmpty(whereClause))
                {
                    return;
                }
                var existsData = service.GetListWithSingleValue(tablename, whereClause, currentPrimaryKey, string.Empty, short.MaxValue)
                                 .Select(x => Convert.ToString(x).ToLower()).ToList();
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    var item = table.Rows[i];
                    var currentPrimaryKeyValue = Convert.ToString(item[currentPrimaryKey]) ?? string.Empty;
                    if (existsData.Contains(currentPrimaryKeyValue.ToLower()))
                    {
                        willRemoveIndexs.Add(item);
                        whereClause = BuildWhereCluase(item, storeId, primaryKey);
                        if (string.IsNullOrEmpty(whereClause))
                        {
                            continue;
                        }
                        Update(service, tablename, item, primaryKey, whereClause);
                    }
                    continue;
                }
            }
            else
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    var item        = table.Rows[i];
                    var whereClause = BuildWhereCluase(item, storeId, primaryKey);
                    if (service.Exists(tablename, whereClause))
                    {
                        willRemoveIndexs.Add(item);
                        if (string.IsNullOrEmpty(whereClause))
                        {
                            continue;
                        }
                        Update(service, tablename, item, primaryKey, whereClause);
                    }
                }
            }
            foreach (var row in willRemoveIndexs)
            {
                table.Rows.Remove(row);
            }
            if (table.Rows.Count > 0)
            {
                SyncBigDataServiceHelper.BatchInsert(service.DbProviderConfig.DbConnectionString, tablename, table, table.Rows.Count);
            }
        }
Exemplo n.º 2
0
 public bool Exists(string whereClause)
 {
     return(_searchService.Exists(TableName, whereClause));
 }
Exemplo n.º 3
0
        public static bool CreateOrUpdate <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);
            }
            if (service.Exists(tableName, whereClause))
            {
                sqlClause = BuildUpdateSql(tableName, primaryKey, dic, keys);
                if (string.IsNullOrEmpty(sqlClause))
                {
                    return(false);
                }
                service.ExcuteSql(sqlClause);
            }
            else
            {
                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);
        }