private void BuildScriptWithKeyValuePair(Table table, IDataWrapper data, IDataFilter filter, out object script)
        {
            List <KeyValuePair <RedisKey, RedisValue> > lst = new List <KeyValuePair <RedisKey, RedisValue> >();
            int r = 1;

            if (data.GetValueByOriName(KeyField) is RedisKey rk)
            {
                object obj = filter.GetValue(data, -1, KeyField);

                if (obj != DBNull.Value && obj != null)
                {
                    rk = (RedisKey)obj;
                }
                obj = filter.GetValue(data, -1, ValueField);
                if (obj == DBNull.Value || obj == null)
                {
                    obj = data.GetValueByOriName(ValueField);
                }

                lst.Add(new KeyValuePair <RedisKey, RedisValue>(rk, (RedisValue)obj));
                while (r < table.PageSize && data.Read())
                {
                    r++;
                    obj = filter.GetValue(data, -1, KeyField);
                    if (obj != DBNull.Value && obj != null)
                    {
                        rk = (RedisKey)obj;
                    }
                    obj = filter.GetValue(data, -1, ValueField);
                    if (obj == DBNull.Value || obj == null)
                    {
                        obj = data.GetValueByOriName(ValueField);
                    }

                    lst.Add(new KeyValuePair <RedisKey, RedisValue>(rk, (RedisValue)obj));
                }
            }
            else
            {
                RedisKey   key   = GetFmtValue(filter.GetValue(data, 0, KeyField));
                RedisValue value = GetFmtValue(filter.GetValue(data, 1, ValueField));

                lst.Add(new KeyValuePair <RedisKey, RedisValue>(key, value));
                while (r < table.PageSize && data.Read())
                {
                    r++;
                    key   = GetFmtValue(filter.GetValue(data, 0, KeyField));
                    value = GetFmtValue(filter.GetValue(data, 1, ValueField));

                    lst.Add(new KeyValuePair <RedisKey, RedisValue>(key, value));
                }
            }

            script = lst;
        }
Exemplo n.º 2
0
        private void BuildScriptWithInsert(Table table, IDataWrapper data, IDataFilter filter, out object script)
        {
            InsertScript ins = new InsertScript();
            int          r   = 1;

            if (data.GetValueByOriName(DocumentField) is BsonDocument doc)
            {
                ins.Data.Add(doc);
                while (r < table.PageSize && data.Read())
                {
                    r++;

                    object obj = filter.GetValue(data, -1, DocumentField);

                    if (obj == DBNull.Value || obj == null)
                    {
                        doc = data.GetValueByOriName(DocumentField) as BsonDocument;
                    }
                    else
                    {
                        doc = obj as BsonDocument;
                    }

                    ins.Data.Add(doc);
                }
            }
            else
            {
                data.MapFields(null); // 取消字段映射
                doc = new BsonDocument();
                for (int i = 0; i < data.FieldCount; i++)
                {
                    doc[data.GetFieldName(i)] = GetValue(filter.GetValue(data, i, data.GetFieldName(i)));
                }
                ins.Data.Add(doc);
                while (r < table.PageSize && data.Read())
                {
                    r++;
                    doc = new BsonDocument();
                    for (int i = 0; i < data.FieldCount; i++)
                    {
                        doc[data.GetFieldName(i)] = GetValue(filter.GetValue(data, i, data.GetFieldName(i)));
                    }
                    ins.Data.Add(doc);
                }
            }

            script = ins;
        }
Exemplo n.º 3
0
        private void BuildScriptWithUpdate(IDataWrapper data, IDataFilter filter, out object script)
        {
            UpdateScript upd = new UpdateScript();

            if (data.GetValueByOriName(DocumentField) is BsonDocument doc)
            {
                object obj = filter.GetValue(data, -1, DocumentField);

                if (obj != DBNull.Value && obj != null)
                {
                    doc = obj as BsonDocument;
                }

                upd.Filter = $"{{\"{IDField}\":\"{doc["_id"]}\"}}";
                upd.Data.AddRange(doc);
                upd.Data.Remove(IDField);
            }
            else
            {
                for (int i = 0; i < data.FieldCount; i++)
                {
                    string field = data.GetFieldName(i);

                    if (IDField.Equals(field))
                    {
                        upd.Filter = $"{{\"{IDField}\":\"{filter.GetValue(data, i, data.GetFieldName(i))}\"}}";
                    }
                    else
                    {
                        upd.Data[data.GetFieldName(i)] = GetValue(filter.GetValue(data, i, data.GetFieldName(i)));
                    }
                }
            }

            script = upd;
        }