コード例 #1
0
        private object ReplicateItem(DbDataReader reader, IUnityContainer container, Constants constants)
        {
            var fields    = new Dictionary <string, object>();
            int numCampos = reader.FieldCount;

            for (int i = 0; i < numCampos; i++)
            {
                fields.Add(reader.GetName(i).ToLower(), reader.GetValue(i));
            }
            if (fields.Count > 0)
            {
                var replicator = container.Resolve <IReplicator>(Constants.replicator);
                if (this is MysqlDefault)
                {
                    replicator.insert(fields[PKField], table, JsonDocument(fields), constants);
                }
                else
                {
                    CoreModel instance = (CoreModel)Activator.CreateInstance(this.GetType());
                    instance.Fill(fields);
                    replicator.insert(fields[PKField], table, instance.JsonDocument(fields), constants);
                }
            }
            return(fields[PKField]);
        }
コード例 #2
0
        private List <CoreModel> FillInsertOrUpdate(Dictionary <string, object> fields, Constants constants)
        {
            if (fields == null)
            {
                fields = new Dictionary <string, object>();
            }
            List <CoreModel> list    = new List <CoreModel>();
            object           pkvalue = null;

            while (true)
            {
                var queryFields = string.Join(",", (from f in defaultFields select f.Key).ToList());
                var whereFields = fields.Count == 0 ? string.Empty : "WHERE " + string.Join(" AND ", (from f in fields select string.Format("{0} = @{0}", f.Key)).ToList());
                if (pkvalue != null)
                {
                    whereFields = string.Format("{0} {1} {2} > @{3}", whereFields, string.IsNullOrWhiteSpace(whereFields) ? "WHERE" : "AND", PKField);
                }
                var command = database.CreateCommand(string.Format(sqlGetLimit, queryFields, table, whereFields, PKField, constants.limitUpdate));
                foreach (var field in fields)
                {
                    command.Parameters.Add(database.CreateParameter(field.Key, field.Value));
                }
                if (pkvalue != null)
                {
                    command.Parameters.Add(database.CreateParameter(PKField, pkvalue));
                }
                using (var reader = command.ExecuteReader())
                {
                    int count = 0;
                    while (reader.Read())
                    {
                        count++;
                        var fieldsDB  = new Dictionary <string, object>();
                        int numCampos = reader.FieldCount;
                        for (int i = 0; i < numCampos; i++)
                        {
                            fieldsDB.Add(reader.GetName(i).ToLower(), reader.GetValue(i));
                        }
                        CoreModel model = (CoreModel)Activator.CreateInstance(this.GetType());
                        if (model is MysqlDefault)
                        {
                            ((MysqlDefault)model).setPK(this.PKField);
                        }
                        model.table = this.table;
                        model.Fill(fieldsDB);
                        list.Add(model);
                        pkvalue = model.PKValue;
                        if (pkvalue == null)
                        {
                            throw new KeyNotFoundException("pk not fill on update");
                        }
                    }
                    if (count == 0 || count != constants.limitUpdate)
                    {
                        break;
                    }
                }
            }
            return(list);
        }