/// ///////////////////////////////////////////////////////////////////////////// public int Fill(DataSet dsDest) { DataTable[] tablesDest = FillSchema(dsDest, SchemaType.Mapped); if (tablesDest.Length == 0) { return(0); } DataTable tableDest = tablesDest[0]; string strFiltre = m_strWhere; if (m_filtre != null) { string strConvFiltre = new CFormatteurFiltreDataToStringDataTable().GetString(m_filtre); if (strConvFiltre != "") { if (strFiltre != "") { strFiltre = "(" + strFiltre + ") and "; } strFiltre += strConvFiltre; } } DataView view = new DataView(m_connexion.DataSet.Tables[tableDest.TableName]); view.RowFilter = strFiltre; foreach (DataRowView row in view) { tableDest.ImportRow(row.Row); /*DataRow newRow = tableDest.NewRow(); * foreach ( string strChamp in m_strChamps ) * { * if ( tableDest.Columns[strChamp] != null ) * newRow[strChamp] = row[strChamp]; * } * tableDest.Rows.Add ( newRow );*/ } return(view.Count); }
///////////////////////////////////////////////////////// public CResultAErreur SetValeurChamp(string strNomTableInDb, string[] strChamps, object[] valeurs, CFiltreData filtre) { CResultAErreur result = CResultAErreur.True; try { string strFiltre = new CFormatteurFiltreDataToStringDataTable().GetString(filtre); DataRow[] rows = DataSet.Tables[strNomTableInDb].Select(strFiltre); foreach (DataRow row in rows) { for (int n = 0; n < strChamps.Length; n++) { row[strChamps[n]] = valeurs[n]; } } } catch (Exception e) { result.EmpileErreur(new CErreurException(e)); result.EmpileErreur(I.T("Error SetValeurChamp(@1)|175", strNomTableInDb)); } return(result); }
///////////////////////////////////////////////////////// public string GetStringForRequete(object valeur) { return(CFormatteurFiltreDataToStringDataTable.GetStringFor(valeur)); }
//--------------------------------- public override CResultAErreur EndInsertData(System.Data.DataSet ds) { CResultAErreur result = base.EndInsertData(ds); if (NePasCalculer) { return(result); } if (!result) { return(result); } try { //Renomme la table de travail avec un _ derrière //Table qui a été créé et sur laquelle les tables filles ont un lien DataTable tableDeLien = ds.Tables[NomTable]; tableDeLien.TableName = NomTable + "_WK"; //TAble qui va stocker les données finales DataTable tableDest = new DataTable(NomTable); ds.Tables.Add(tableDest); #region Création de la table finale //Crée le champ d'id C2iStructureExport.CreateChampInTableAIdAuto(tableDest, 0); DataColumn colLienFinale = null; //Crée le champ de lien avec la table parente //Recherche de la table parente string strFK = ""; foreach (Constraint contrainte in tableDeLien.Constraints) { ForeignKeyConstraint fk = contrainte as ForeignKeyConstraint; if (fk != null) { if (fk.Table == tableDeLien) { strFK = fk.Columns[0].ColumnName; colLienFinale = C2iStructureExport.CreateForeignKeyInTable(fk.RelatedColumns[0], tableDest, 0); break; } } } if (colLienFinale == null && ChampOrigine != null) { result.EmpileErreur(I.T("Cannot establish a link for Union table @1|20008", NomTable)); return(result); } //Création de l'ancienne colonne Id DataColumn colOldId = null; if (ChampOrigine != null && !(ChampOrigine is CDefinitionProprieteDynamiqueThis)) { string strColOldId = DynamicClassAttribute.GetNomConvivial(ChampOrigine.TypeDonnee.TypeDotNetNatif) + "_Id_0";; colOldId = new DataColumn(strColOldId, typeof(int)); colOldId.AllowDBNull = true; tableDest.Columns.Add(colOldId); } #endregion //Met en cache les clés filles vers les clés parentes Dictionary <int, int> dicIdsFillesToParent = new Dictionary <int, int>(); string strPrimKey = tableDeLien.PrimaryKey[0].ColumnName; if (colLienFinale != null) { foreach (DataRow row in tableDeLien.Rows) { dicIdsFillesToParent[(int)row[strPrimKey]] = (int)row[strFK]; } } #region Création des champs Dictionary <string, int> dicChampsDesTables = new Dictionary <string, int>(); List <DataColumn> listeColonnesACreer = new List <DataColumn>(); Dictionary <string, Type> typesDesChamps = new Dictionary <string, Type>(); Dictionary <string, DataColumn> colsDeLien = new Dictionary <string, DataColumn>(); Dictionary <string, List <string> > listeColsToCopyParTable = new Dictionary <string, List <string> >(); foreach (ITableExport uneTableSource in TablesFilles) { DataTable tableSource = ds.Tables[uneTableSource.NomTable]; if (tableSource == null) { result.EmpileErreur(I.T("Table @1 doesn't exist|20004", uneTableSource.NomTable)); return(result); } DataColumn colDeLien = null; //Trouve la colonne de lien avec la table de lien foreach (Constraint contrainte in tableSource.Constraints) { ForeignKeyConstraint fk = contrainte as ForeignKeyConstraint; if (fk != null && fk.RelatedTable == tableDeLien) { colDeLien = fk.Columns[0]; break; } } if (colDeLien == null) { result.EmpileErreur(I.T("Cannot find a link between @1 and @2|20007", tableDeLien.TableName, tableSource.TableName)); return(result); } colsDeLien[tableSource.TableName] = colDeLien; List <string> colsToCopy = new List <string>(); listeColsToCopyParTable[tableSource.TableName] = colsToCopy; foreach (DataColumn col in tableSource.Columns) { //Ignore les clés if (tableSource.PrimaryKey.Length > 0 && tableSource.PrimaryKey[0] == col) { continue; } if (col == colDeLien) { continue; } int nNb = 0; colsToCopy.Add(col.ColumnName); if (!dicChampsDesTables.TryGetValue(col.ColumnName.ToUpper(), out nNb)) { dicChampsDesTables[col.ColumnName.ToUpper()] = 1; listeColonnesACreer.Add(col); } else { nNb++; dicChampsDesTables[col.ColumnName.ToUpper()] = nNb; } } } foreach (DataColumn col in listeColonnesACreer) { DataColumn newCol = new DataColumn(col.ColumnName, col.DataType); newCol.AllowDBNull = true; tableDest.Columns.Add(newCol); } #endregion //Trouve les clés : les clés sont les champs communs à toutes les tables //ou les champs explicites List <string> champsCles = new List <string>(); if (m_listeChampsClesExplicites.Count > 0) { foreach (string strChampTmp in m_listeChampsClesExplicites) { if (dicChampsDesTables.ContainsKey(strChampTmp.ToUpper())) { champsCles.Add(strChampTmp); } } if (champsCles.Count != m_listeChampsClesExplicites.Count) { result.EmpileErreur(I.T("Some keys are not in source tables|20012")); return(result); } } else { int nNbTablesSource = TablesFilles.Length; foreach (KeyValuePair <string, int> tableNb in dicChampsDesTables) { if (tableNb.Value == nNbTablesSource) { champsCles.Add(tableNb.Key); } } } string[] strChampsCles = champsCles.ToArray(); //Il faut traiter les tables filles de celle qui a le plus de clés //à celle qui en a le moins List <string> lstNomTablesDansOrdre = new List <string>(); for (int nNbClesTest = strChampsCles.Length; nNbClesTest >= 1; nNbClesTest--) { foreach (ITableExport tableSource in TablesFilles) { int nNbClesDansTable = 0; //Compte les champs clés présents foreach (string strCol in listeColsToCopyParTable[tableSource.NomTable]) { if (champsCles.Contains(strCol)) { nNbClesDansTable++; } } if (nNbClesDansTable == nNbClesTest) { lstNomTablesDansOrdre.Add(tableSource.NomTable); } } } if (strChampsCles.Length == 0) { foreach (ITableExport table in TablesFilles) { lstNomTablesDansOrdre.Add(table.NomTable); } } ///Index : clé (texte)->DataRow Dictionary <string, DataRow> dicIndex = new Dictionary <string, DataRow>(); CFormatteurFiltreDataToStringDataTable formateurFiltre = new CFormatteurFiltreDataToStringDataTable(); //Insere les données table par table foreach (string strNomTableSource in lstNomTablesDansOrdre) { DataTable tableSource = ds.Tables[strNomTableSource]; DataColumn colDeLien = colsDeLien[tableSource.TableName]; bool bHasAllKeys = false; List <string> strClesDansTable = new List <string>(); foreach (string strColKey in champsCles) { if (tableSource.Columns[strColKey] != null) { strClesDansTable.Add(strColKey); } } if (strClesDansTable.Count == champsCles.Count) { bHasAllKeys = true; } /*if (strClesDansTable.Count == 0) * { * //Aucune clé pour cette table, on ne sait pas quoi en faire ! * result.EmpileErreur(I.T("Table @1 doesn't have any key|20013", tableSource.TableName)); * return result; * }*/ CFiltreData filtreDonnees = null; if (!bHasAllKeys) { StringBuilder blFiltre = new StringBuilder(); int nParametre = 2; blFiltre.Append(colDeLien.ColumnName); blFiltre.Append("=@"); blFiltre.Append(1); blFiltre.Append(" and "); foreach (string strCle in strClesDansTable) { blFiltre.Append(strCle); blFiltre.Append("=@"); blFiltre.Append(nParametre); blFiltre.Append(" and "); nParametre++; } blFiltre.Remove(blFiltre.Length - 5, 5); filtreDonnees = new CFiltreData(blFiltre.ToString()); } foreach (DataRow rowSource in tableSource.Rows) { StringBuilder blKey = new StringBuilder(); blKey.Append(rowSource[colDeLien].ToString() + "¤"); if (filtreDonnees != null) { filtreDonnees.Parametres.Clear(); filtreDonnees.Parametres.Add(rowSource[colDeLien]); } for (int nKey = 0; nKey < strClesDansTable.Count; nKey++) { object val = rowSource[strClesDansTable[nKey]]; if (filtreDonnees != null) { filtreDonnees.Parametres.Add(val); } else { if (val == null) { blKey.Append("@NULL@"); } else { blKey.Append(val.ToString()); } blKey.Append("¤"); } } DataRow[] rowsDest = null; bool bIsNew = false; string strKey = ""; if (filtreDonnees == null) { strKey = blKey.ToString(); DataRow rowTmp = null; bIsNew = !dicIndex.TryGetValue(strKey, out rowTmp); if (bIsNew) { rowTmp = tableDest.NewRow(); } rowsDest = new DataRow[] { rowTmp }; } else { string strFiltre = formateurFiltre.GetString(filtreDonnees); rowsDest = tableDest.Select(strFiltre); if (rowsDest.Length == 0) { rowsDest = new DataRow[] { tableDest.NewRow() }; bIsNew = true; } } foreach (DataRow rowDest in rowsDest) { foreach (string strCol in listeColsToCopyParTable[tableSource.TableName]) { rowDest[strCol] = rowSource[strCol]; } if (colOldId != null) { rowDest[colOldId] = rowSource[colDeLien]; } if (colLienFinale != null) { int nIdLink = dicIdsFillesToParent[(int)rowSource[colDeLien]]; rowDest[colLienFinale] = nIdLink; } } if (bIsNew) { tableDest.Rows.Add(rowsDest[0]); if (bHasAllKeys) { dicIndex[strKey] = rowsDest[0]; } } } } if (SupprimerTablesTravail) { SupprimeTableEtDependances(ds, tableDeLien.TableName); } } catch (Exception e) { result.EmpileErreur(new CErreurException(e)); } return(result); }