Example #1
0
        //dataset is all data...
        public string[] Update(DataSet dataSet, string sTable, bool isReplaceCmd, string replaceCmd, int state, string tableName)
        {
            _updateDataSet = dataSet;
            _updateTable = sTable;
            List<string> sqlSentences = new List<string>();
            OpenConn();
            _needSyncFVs = false;

            if (replaceCmd != null && replaceCmd.Length != 0)
                this.SelectCmd.CommandText = replaceCmd;

            DataTable dataTable = dataSet.Tables[sTable];

            DataTable addedTable = dataTable.GetChanges(DataRowState.Added);
            DataTable modifiedTable = dataTable.GetChanges(DataRowState.Modified);
            DataTable deletedTable = dataTable.GetChanges(DataRowState.Deleted);

            if (addedTable == null && modifiedTable == null && deletedTable == null) return sqlSentences.ToArray();
            DataTable schema = new DataTable();
            if (!isReplaceCmd)
            {
                schema = GetSchema();
            }
            // Get the updatecomp's infotranscations.
            List<IInfoTransaction> infoTransactions = GetInfoTransactions();

            OnBeforeApply(new UpdateComponentBeforeApplyEventArgs());

            List<IAutoNumber> autoNumbersList = null;
            KeyItems keysList = this.SelectCmd.KeyFields;
            ClientType type = DBUtils.GetDatabaseType(conn);
            OdbcDBType odbcType = DBUtils.GetOdbcDatabaseType(conn);
            #region Deleted.
            if (deletedTable != null && (state == 0 || state == 1))
            {
                foreach (DataRow row in deletedTable.Rows)
                {
                    // Set the currect row.
                    _currentRow = row;

                    // 替换Command的SQL
                    if (isReplaceCmd)
                    {
                        schema = GetSchema(isReplaceCmd, row, true);
                    }

                    OnBeforeDelete(new UpdateComponentBeforeDeleteEventArgs());

                    // Sync setfieldvalues
                    if (_needSyncFVs)
                    {
                        SetFieldValueSynchronizer syncer = new SetFieldValueSynchronizer(dataTable, row, schema, _syncFieldValues);
                        if (type == ClientType.ctOracle)
                        {
                            syncer.SyncOracle();
                        }
                        else
                        {
                            syncer.Sync();
                        }
                    }
                    _syncFieldValues.Clear();

                    SQLBuilder builder = new SQLBuilder(this, row, schema);
                    String deleteSQL = "";
                    String writeBackSQLWherePart = "";

                    if (type == ClientType.ctMsSql)
                    {
                        deleteSQL = builder.GetDeleteSQL();
                        writeBackSQLWherePart = builder.GetWriteBackSQLWherePart();
                    }
                    else if (type == ClientType.ctOleDB)
                    {
                        deleteSQL = builder.GetDeleteSybase(tableName);
                        writeBackSQLWherePart = builder.GetWriteBackSybaseWherePart();
                    }
                    else if (type == ClientType.ctOracle)
                    {
                        deleteSQL = builder.GetDeleteOracle();
                        writeBackSQLWherePart = builder.GetWriteBackOracleWherePart();
                    }
                    else if (type == ClientType.ctODBC)
                    {
                        deleteSQL = builder.GetDeleteOdbc(tableName, odbcType);
                        writeBackSQLWherePart = builder.GetWriteBackOdbcWherePart(odbcType);
                    }
                    else if (type == ClientType.ctMySql)
                    {
                        deleteSQL = builder.GetDeleteMySql();
                        writeBackSQLWherePart = builder.GetWriteBackMySqlWherePart();
                    }
                    else if (type == ClientType.ctInformix)
                    {
                        deleteSQL = builder.GetDeleteInformix(tableName);
                        writeBackSQLWherePart = builder.GetWriteBackInformixWherePart();
                    }

                    IDbCommand deleteCommand = conn.CreateCommand();
                    deleteCommand.CommandText = deleteSQL;
                    if (trans != null)
                    {
                        deleteCommand.Transaction = trans;
                    }
                    sqlSentences.Add(deleteSQL);

                    if (_logInfo != null)
                    {
                        _logInfo.Log(row, schema, conn, trans, keysList);
                    }

                    try
                    {
                        Int32 i = deleteCommand.ExecuteNonQuery();
                        if (RowAffectsCheck)
                        {
                            if (i > 1)
                            {
                                String message = SysMsg.GetSystemMessage(((DataModule)this.OwnerComp).Language, "Srvtools", "UpdateComponent", "msg_AffectedMultiRows");
                                throw new ArgumentException(message);
                            }
                            else if (i == 0)
                            {
                                String message = SysMsg.GetSystemMessage(((DataModule)this.OwnerComp).Language, "Srvtools", "UpdateComponent", "msg_AffectedNotRows");
                                throw new ArgumentException(message);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }

                    if (infoTransactions != null && infoTransactions.Count != 0)
                    {
                        foreach (InfoTransaction iT in infoTransactions)
                        {
                            if (iT.Update(row, schema, writeBackSQLWherePart, conn, sqlSentences, trans) == true)
                                continue;
                            else
                                throw new Exception();
                        }
                    }

                    OnAfterDelete(new UpdateComponentAfterDeleteEventArgs());
                }
            }
            #endregion

            #region Modified.
            if (modifiedTable != null && (state == 0 || state == 2))
            {
                foreach (DataRow row in modifiedTable.Rows)
                {
                    // Set the currect row.
                    _currentRow = row;

                    // 替换Command的SQL
                    if (isReplaceCmd)
                    {
                        schema = GetSchema(isReplaceCmd, row);
                    }

                    OnBeforeModify(new UpdateComponentBeforeModifyEventArgs());

                    // Sync setfieldvalues
                    if (_needSyncFVs)
                    {
                        SetFieldValueSynchronizer syncer = new SetFieldValueSynchronizer(dataTable, row, schema, _syncFieldValues);
                        if (type == ClientType.ctOracle)
                        {
                            syncer.SyncOracle();
                        }
                        else
                        {
                            syncer.Sync();
                        }
                    }
                    _syncFieldValues.Clear();

                    SQLBuilder builder = new SQLBuilder(this, row, schema);
                    String updateSQL = "";
                    String writeBackSQLWherePart = "";
                    IDbCommand updateCommand = conn.CreateCommand();
                    if (trans != null)
                    {
                        updateCommand.Transaction = trans;
                    }
                    if (type == ClientType.ctMsSql)
                    {
                        updateSQL = builder.GetUpdateSQL();
                        writeBackSQLWherePart = builder.GetWriteBackSQLWherePart();
                    }
                    else if (type == ClientType.ctOleDB)
                    {
                        updateSQL = builder.GetUpdateSybase(tableName, updateCommand);
                        writeBackSQLWherePart = builder.GetWriteBackSybaseWherePart();
                    }
                    else if (type == ClientType.ctOracle)
                    {
                        updateSQL = builder.GetUpdateOracle(updateCommand);
                        writeBackSQLWherePart = builder.GetWriteBackOracleWherePart();
                    }
                    else if (type == ClientType.ctODBC)
                    {
                        updateSQL = builder.GetUpdateOdbc(tableName, odbcType);
                        writeBackSQLWherePart = builder.GetWriteBackOdbcWherePart(odbcType);
                    }
                    else if (type == ClientType.ctMySql)
                    {
                        updateSQL = builder.GetUpdateMySql(updateCommand);
                        writeBackSQLWherePart = builder.GetWriteBackMySqlWherePart();
                    }
                    else if (type == ClientType.ctInformix)
                    {
                        updateSQL = builder.GetUpdateInformix(tableName);
                        writeBackSQLWherePart = builder.GetWriteBackInformixWherePart();
                    }

                    updateCommand.CommandText = updateSQL;

                    if (updateSQL == null || updateSQL.Length == 0)
                    {
                        // throw new Exception();
                        continue;
                    }

                    sqlSentences.Add(updateSQL);

                    if (_logInfo != null)
                    {
                        _logInfo.Log(row, schema, conn, trans, keysList);
                    }

                    try
                    {
                        Int32 i = updateCommand.ExecuteNonQuery();
                        if (RowAffectsCheck)
                        {
                            if (i > 1)
                            {
                                String message = SysMsg.GetSystemMessage(((DataModule)this.OwnerComp).Language, "Srvtools", "UpdateComponent", "msg_AffectedMultiRows");
                                throw new ArgumentException(message);
                            }
                            else if (i == 0)
                            {
                                String message = SysMsg.GetSystemMessage(((DataModule)this.OwnerComp).Language, "Srvtools", "UpdateComponent", "msg_AffectedNotRows");
                                throw new ArgumentException(message);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }

                    if (infoTransactions != null && infoTransactions.Count != 0)
                    {
                        foreach (InfoTransaction iT in infoTransactions)
                        {
                            if (iT.Update(row, schema, writeBackSQLWherePart, conn, sqlSentences, trans) == true)
                                continue;
                            else
                                throw new Exception();
                        }
                    }

                    OnAfterModify(new UpdateComponentAfterModifyEventArgs());
                }
            }
            #endregion

            #region Added.
            if (addedTable != null && (state == 0 || state == 2))
            {
                foreach (DataRow row in addedTable.Rows)
                {
                    // Set the currect row.
                    _currentRow = row;

                    autoNumbersList = GetAutoNumbersList(true);

                    // 替换Command的SQL
                    if (isReplaceCmd)
                    {
                        schema = GetSchema(isReplaceCmd, row);
                    }

                    // UpdateComponent.ServerModify和InfoDataSet.ServerModify、InfoNavigator.AutoApply配合使用。
                    // if (_serverModify)
                    // {
                    AutoNumberSynchronizer autoNumberSync = new AutoNumberSynchronizer(dataTable, row, schema, autoNumbersList);

                    if (type == ClientType.ctOracle)
                    {
                        autoNumberSync.SyncOracle();
                    }
                    else if (type == ClientType.ctMySql)
                    {
                        autoNumberSync.SyncMySql();
                    }
                    else
                    {
                        autoNumberSync.Sync();
                    }

                    OnBeforeInsert(new UpdateComponentBeforeInsertEventArgs());

                    // Sync setfieldvalues
                    if (_needSyncFVs)
                    {
                        SetFieldValueSynchronizer syncer = new SetFieldValueSynchronizer(dataTable, row, schema, _syncFieldValues);
                        if (type == ClientType.ctOracle)
                        {
                            syncer.SyncOracle();
                        }
                        else
                        {
                            syncer.Sync();
                        }
                    }
                    _syncFieldValues.Clear();

                    SQLBuilder builder = new SQLBuilder(this, row, schema);
                    String insertSQL = "";
                    String writeBackSQLWherePart = "";
                    IDbCommand insertCommand = conn.CreateCommand();
                    if (trans != null)
                    {
                        insertCommand.Transaction = trans;
                    }
                    if (type == ClientType.ctMsSql)
                    {
                        insertSQL = builder.GetInsertSQL();
                        writeBackSQLWherePart = builder.GetWriteBackSQLWherePart();
                    }
                    else if (type == ClientType.ctOleDB)
                    {
                        insertSQL = builder.GetInsertSybase(tableName, insertCommand);
                        writeBackSQLWherePart = builder.GetWriteBackSybaseWherePart();
                    }
                    else if (type == ClientType.ctOracle)
                    {
                        insertSQL = builder.GetInsertOracle(insertCommand);
                        writeBackSQLWherePart = builder.GetWriteBackOracleWherePart();
                    }
                    else if (type == ClientType.ctODBC)
                    {
                        insertSQL = builder.GetInsertOdbc(tableName, odbcType);
                        writeBackSQLWherePart = builder.GetWriteBackOdbcWherePart(odbcType);
                    }
                    else if (type == ClientType.ctMySql)
                    {
                        insertSQL = builder.GetInsertMySql(insertCommand);
                        writeBackSQLWherePart = builder.GetWriteBackMySqlWherePart();
                    }
                    else if (type == ClientType.ctInformix)
                    {
                        insertSQL = builder.GetInsertInformix(tableName);
                        writeBackSQLWherePart = builder.GetWriteBackInformixWherePart();
                    }

                    insertCommand.CommandText = insertSQL;
                    sqlSentences.Add(insertSQL);

                    // Log
                    if (_logInfo != null)
                    {
                        _logInfo.Log(row, schema, conn, trans, keysList);
                    }

                    try
                    {
                        Int32 i = insertCommand.ExecuteNonQuery();
                        if (RowAffectsCheck)
                        {
                            if (i > 1)
                            {
                                //Object[] clientInfos = ((DataModule)OwnerComp).ClientInfo;
                                String message = SysMsg.GetSystemMessage(((DataModule)this.OwnerComp).Language, "Srvtools", "UpdateComponent", "msg_AffectedMultiRows");
                                throw new ArgumentException(message);
                            }
                            else if (i == 0)
                            {
                                //Object[] clientInfos = ((DataModule)OwnerComp).ClientInfo;
                                String message = SysMsg.GetSystemMessage(((DataModule)this.OwnerComp).Language, "Srvtools", "UpdateComponent", "msg_AffectedNotRows");
                                throw new ArgumentException(message);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }

                    if (infoTransactions != null && infoTransactions.Count != 0)
                    {
                        foreach (InfoTransaction iT in infoTransactions)
                        {
                            if (iT.Update(row, schema, writeBackSQLWherePart, conn, sqlSentences, trans) == true)
                                continue;
                            else
                                throw new Exception();
                        }
                    }

                    OnAfterInsert(new UpdateComponentAfterInsertEventArgs());

                    //String tableName = schema.Rows[0]["BaseTableName"].ToString(); ;
                    //DataRow identityRow = GetIdentityRow(tableName, writeBackSQLWherePart);
                    //if (identityRow != null)
                    //{
                    //    IdentitySynchronizer identitySync = new IdentitySynchronizer(dataTable, row, schema, identityRow);
                    //    identitySync.Sync();
                    //}
                }
            }
            #endregion
            OnAfterApply(new UpdateComponentAfterApplyEventArgs());
            return sqlSentences.ToArray();
        }
Example #2
0
        public void IdentitySync(DataSet dataSet, String sTable, String realTableName)
        {
            DataSet insertedDS = dataSet.GetChanges(DataRowState.Added);
            if (insertedDS == null || insertedDS.Tables.Count == 0 || insertedDS.Tables[0].Rows.Count == 0)
            {
                return;
            }

            DataTable schema = GetSchema();
            String tableName = String.Empty;
            if (!String.IsNullOrEmpty(realTableName))
            {
                tableName = realTableName;
            }
            else
            {
                //DataTable schema = GenerateSchema();
                tableName = schema.Rows[0]["BaseTableName"].ToString();
                //给表加上owner by Rei
                String schemaName = schema.Rows[0]["BaseSchemaName"].ToString();
                if (schemaName != null && schemaName != "")
                    tableName = schemaName + "." + tableName;
            }
            DataTable dataTable = dataSet.Tables[sTable];
            ClientType type = DBUtils.GetDatabaseType(conn);
            string origiantable = DBUtils.GetTableName(SelectCmd.CommandText, true);
            foreach (DataRow r in insertedDS.Tables[0].Rows)
            {
                string wherePart = "";
                DataRow identityRow = null;
                if (ServerModifyGetMax)
                {
                    KeyItems keys = SelectCmd.KeyFields;
                    string columnName = "";
                    if (keys == null || keys.Count == 0)
                        columnName = dataTable.Columns[0].ColumnName;
                    else
                        columnName = ((KeyItem)keys[0]).KeyName;

                    wherePart = origiantable + "." + DBUtils.QuoteWords(columnName, type) + " in " + "(select max("
                        + DBUtils.QuoteWords(columnName, type) + ") from " + DBUtils.QuoteWords(tableName, type) + ")";
                }
                else
                {
                    SQLBuilder builder = new SQLBuilder(this, r, schema);

                    if (this.SelectCmd != null)
                    {
                        if (this.conn != null)
                        {
                            if (type == ClientType.ctOracle)
                            {
                                wherePart = builder.GetIdenSelectOracleWherePart();
                            }
                            else if (type == ClientType.ctOleDB)
                            {
                                wherePart = builder.GetIdenSelectSybaseWherePart();
                            }
                            else
                            {
                                wherePart = builder.GetIdenSelectSQLWherePart();
                            }
                        }
                    }
                }

                if (wherePart == null || wherePart.Length == 0)
                    continue;

                identityRow = GetIdentityRow(tableName, wherePart);
                if (identityRow != null)
                {
                    IdentitySynchronizer identitySync = new IdentitySynchronizer(dataTable, r, schema, identityRow);
                    identitySync.Sync();
                }
            }
        }