Esempio n. 1
0
        internal ImportQueries(DIQueries DBQueries)
        {
            this.DBQueries = DBQueries;

            // Set IUpdateQuery object, (By Default: Traditional)
            this.UpdateQueries = new TraditionalUpdateQuery(this.DBQueries);
        }
Esempio n. 2
0
        private bool SaveUpdate(ITransaction transaction)
        {
            SchemaObject schemaObject = Schema.Schema.GetSchemaObject(GetType());

            IUpdateQuery updateQuery = SQLProviderFactory.GetUpdateQuery();

            updateQuery.Table = new Table(schemaObject.SchemaName, schemaObject.ObjectName);

            foreach (Schema.Field field in schemaObject.GetFields().Where(f => f != schemaObject.PrimaryKeyField))
            {
                FieldValue fieldValue = new FieldValue();
                fieldValue.FieldName = field.FieldName;
                fieldValue.Value     = field.GetValue(this);
                updateQuery.FieldValueList.Add(fieldValue);
            }

            updateQuery.Condition = new Condition()
            {
                Left          = (Base.Data.Operand.Field)schemaObject.PrimaryKeyField.FieldName,
                ConditionType = Condition.ConditionTypes.Equal,
                Right         = new Literal(schemaObject.PrimaryKeyField.GetValue(this))
            };

            updateQuery.Execute(transaction);

            return(true);
        }
Esempio n. 3
0
        public static IUpdateQuery CreateUpdateQuery(string db, DbProviderType provider)
        {
            IUpdateQuery query = CreateUpdateQuery(db);

            query.DbProviderType = provider;
            return(query);
        }
Esempio n. 4
0
 private UpdateStore.EntityVersionQuery ToDbQuery(IUpdateQuery q, int?defaultCount) => new UpdateStore.EntityVersionQuery
 {
     Before = q.Before,
     After  = q.After,
     Order  = q.Order,
     Page   = q.Page,
     Count  = q.Count ?? defaultCount
 };
Esempio n. 5
0
        public override async Task <UpdateQueryResult> QueryAsync(IUpdateQuery query)
        {
            var b   = Batch.CreateSimple(this);
            var res = b.QueryAsync(query);
            await b.Execute();

            return(res.Result);
        }
Esempio n. 6
0
        public override async Task <UpdateQueryResult> QueryAsync(IUpdateQuery updateQuery)
        {
            var b   = Batch.CreateUsingTransaction(this);
            var res = b.QueryAsync(updateQuery);
            await b.Execute();

            return(res.Result);
        }
Esempio n. 7
0
        public static IQuery Update(this IUpdateQuery query, string?comment = null)
        {
            var upd = string.Join(", ", query.Updates.Select(pair => $"`{pair.Item1}` = {pair.Item2}"));

            string where = "";
            if (query.Condition.Condition != "1")
            {
                where = $" WHERE {query.Condition.Condition}";
            }
            return(new Query(query.Condition.Table, $"UPDATE `{query.Condition.Table.TableName}` SET {upd}{where};" + (comment == null ? "" : " -- " + comment)));
        }
Esempio n. 8
0
        public override Task <UpdateQueryResult> QueryAsync(IUpdateQuery updateQuery)
        {
            _combinedRawQuery.Append(updateQuery.GetRawQuery());
            _combinedRawQuery.Append(";\n");
            IsEmpty = false;

            var tcs = new TaskCompletionSource <UpdateQueryResult>();

            _resultProcessors.Add(async reader => {
                tcs.SetResult(await updateQuery.ReadResultAsync(reader));
            });

            return(tcs.Task);
        }
Esempio n. 9
0
 public static IUpdateQuery Set(this IUpdateQuery query, string key, object?value)
 {
     return(new UpdateQuery(query, key, value.ToSql()));
 }
Esempio n. 10
0
 public UpdateQuery(IUpdateQuery update, string key, string value)
 {
     this.condition = update.Condition;
     updates.AddRange(update.Updates);
     updates.Add((key, value));
 }
Esempio n. 11
0
        private async Task <ActionResult <ApiResponsePaginated <ApiBasicVersion> > > BasicUpdateHandler(UpdateType type,
                                                                                                        IUpdateQuery q, int defaultCount)
        {
            var versions = await _updateStore
                           .ExportAllUpdatesRaw(type, ToDbQuery(q, defaultCount))
                           .ToListAsync();

            return(Ok(new ApiResponsePaginated <ApiBasicUpdate>
            {
                Data = versions
                       .Select(v => new ApiBasicUpdate(v)),
                NextPage = versions.LastOrDefault()?.NextPage
            }));
        }
Esempio n. 12
0
 /// <summary>
 /// Sets the IUpdateQuery object on the basis of DBServer type.
 /// <para>Client program need to call this method explicitly.</para>
 /// </summary>
 /// <param name="serverType">Enum value for DIServer type.</param>
 internal void SetUpdateQueryObject(DIServerType serverType)
 {
     switch (serverType)
     {
         // In case of MSACCESS & MYSQL, use Traditional Update queries
         case DIServerType.Excel:
         case DIServerType.MsAccess:
         case DIServerType.MySql:
             this.UpdateQueries = new TraditionalUpdateQuery(this.DBQueries);
             break;
         // In case of SqlServer 2005 & Oracle, use new Update Queries.
         case DIServerType.Oracle:
         case DIServerType.SqlServer:
             this.UpdateQueries = new SqlServerUpdateQuery(this.DBQueries);
             break;
     }
 }
Esempio n. 13
0
        private void HandleFKConstraintConflicts(List <FKConstraintConflict> conflicts, ITransaction transaction)
        {
            HashSet <FKConstraintConflict> handled = new HashSet <FKConstraintConflict>();
            IEnumerable <IGrouping <Type, FKConstraintConflict> > constraintsByType = conflicts.GroupBy(conf => conf.ConflictType);

            foreach (IGrouping <Type, FKConstraintConflict> constraintGroup in constraintsByType)
            {
                SchemaObject foreignSchemaObject = Schema.Schema.GetSchemaObject(constraintGroup.Key);
                foreach (IGrouping <string, FKConstraintConflict> constraintGroupByField in constraintGroup.GroupBy(fk => fk.ForeignKeyName))
                {
                    List <long> autoDeleteReferenceKeys = new List <long>();
                    List <long> autoRemoveReferenceKeys = new List <long>();

                    foreach (FKConstraintConflict fKConstraintConflict in constraintGroup)
                    {
                        switch (fKConstraintConflict.ActionType)
                        {
                        case FKConstraintConflict.ActionTypes.AutoDeleteReference:
                            autoDeleteReferenceKeys.Add(fKConstraintConflict.ForeignKey.Value);
                            handled.Add(fKConstraintConflict);
                            break;

                        case FKConstraintConflict.ActionTypes.AutoRemoveReference:
                            autoRemoveReferenceKeys.Add(fKConstraintConflict.ForeignKey.Value);
                            handled.Add(fKConstraintConflict);
                            break;
                        }
                    }

                    if (autoDeleteReferenceKeys.Any())
                    {
                        IDeleteQuery deleteQuery = SQLProviderFactory.GetDeleteQuery();
                        deleteQuery.Table     = new Table(foreignSchemaObject.SchemaName, foreignSchemaObject.ObjectName);
                        deleteQuery.Condition = new Condition()
                        {
                            Left          = (Base.Data.Operand.Field)foreignSchemaObject.PrimaryKeyField.FieldName,
                            ConditionType = Condition.ConditionTypes.List,
                            Right         = (CSV <long>)autoDeleteReferenceKeys
                        };

                        deleteQuery.Execute(transaction);
                    }

                    if (autoRemoveReferenceKeys.Any())
                    {
                        IUpdateQuery updateQuery = SQLProviderFactory.GetUpdateQuery();
                        updateQuery.Table = new Table(foreignSchemaObject.SchemaName, foreignSchemaObject.ObjectName);
                        updateQuery.FieldValueList.Add(new FieldValue()
                        {
                            FieldName = constraintGroupByField.Key,
                            Value     = null
                        });
                        updateQuery.Condition = new Condition()
                        {
                            Left          = (Base.Data.Operand.Field)foreignSchemaObject.PrimaryKeyField.FieldName,
                            ConditionType = Condition.ConditionTypes.List,
                            Right         = (CSV <long>)autoRemoveReferenceKeys
                        };

                        updateQuery.Execute(transaction);
                    }
                }
            }

            conflicts.RemoveAll(fk => handled.Contains(fk));
        }
Esempio n. 14
0
 public abstract Task <UpdateQueryResult> QueryAsync(IUpdateQuery updateQuery);
        private IQuery GenerateUpdateQuery(IDatabaseTableData tableData)
        {
            IMultiQuery query = Queries.BeginTransaction();

            foreach (var entity in tableData.Entities)
            {
                Dictionary <string, List <IDatabaseField> > fieldsByTable = entity.Fields
                                                                            .Select(ef => (ef, tableData.TableDefinition.TableColumns[ef.FieldName]))
                                                                            .Where(pair => !pair.Item2.IsMetaColumn && !pair.Item2.IsConditionColumn)
                                                                            .GroupBy(pair => pair.Item2.ForeignTable ?? tableData.TableDefinition.TableName)
                                                                            .ToDictionary(g => g.Key, g => g.Select(f => f.ef).ToList());

                if (tableData.TableDefinition.ForeignTable != null)
                {
                    foreach (var foreign in tableData.TableDefinition.ForeignTable)
                    {
                        fieldsByTable[foreign.TableName].Insert(0, new DatabaseField <long>(foreign.ForeignKey, new ValueHolder <long>(entity.Key, false)));
                    }
                }

                if (entity.ExistInDatabase)
                {
                    foreach (var table in fieldsByTable)
                    {
                        if (!table.Value.Any(f => f.IsModified))
                        {
                            continue;
                        }

                        var updates = table.Value
                                      .Where(f => f.IsModified)
                                      .ToList();

                        string primaryKeyColumn = tableData.TableDefinition.TablePrimaryKeyColumnName;
                        if (table.Key != tableData.TableDefinition.TableName)
                        {
                            primaryKeyColumn = tableData.TableDefinition.ForeignTableByName[table.Key].ForeignKey;
                            query.Table(table.Key)
                            .InsertIgnore(new Dictionary <string, object?>()
                            {
                                { primaryKeyColumn, entity.Key }
                            });
                        }

                        IUpdateQuery update = query.Table(table.Key)
                                              .Where(row => row.Column <uint>(primaryKeyColumn) == entity.Key)
                                              .Set(updates[0].FieldName, updates[0].Object);
                        for (int i = 1; i < updates.Count; ++i)
                        {
                            update = update.Set(updates[i].FieldName, updates[i].Object);
                        }

                        update.Update();
                    }
                }
                else
                {
                    foreach (var table in fieldsByTable)
                    {
                        string primaryKeyColumn = tableData.TableDefinition.TablePrimaryKeyColumnName;
                        if (table.Key != tableData.TableDefinition.TableName)
                        {
                            primaryKeyColumn = tableData.TableDefinition.ForeignTableByName[table.Key].ForeignKey;
                        }

                        query.Table(table.Key)
                        .Where(row => row.Column <uint>(primaryKeyColumn) == entity.Key)
                        .Delete();

                        query.Table(table.Key)
                        .Insert(table.Value.ToDictionary(t => t.FieldName, t => t.Object));
                    }
                }
            }

            return(query.Close());
        }