private void Export(XmlWriter writer, List <string> tablenames, EvaluateAttributesDelegate eval, bool encryptPatientId, int userExportId) { ExportXml( writer, tablenames, delegate(string tablename, SqlConnection connection) { List <string> columns = GetExportColumns(tablename, eval, connection); string parentKey = GetParentKey(tablename); List <KeyValuePair <string, string> > parentTableKeyMap = GetParentTableKeyMap(tablename); List <KeyValuePair <string, string> > restrictions = new List <KeyValuePair <string, string> >(); restrictions.Add(new KeyValuePair <string, string>("UserExportPatients.UserExportId", userExportId.ToString())); return(SqlBuilder.BuildExportSelectAllStatement(columns, parentTableKeyMap, restrictions)); }, delegate(IEnumerable <string> selectedTables) { StringBuilder union = new StringBuilder(); bool prependUnion = false; foreach (string tablename in selectedTables) { IEnumerable <KeyValuePair <string, string> > map = GetParentTableKeyMap(tablename); string primaryKeyName = BusinessObject.GetPrimaryKeyName(tablename); string sql = SqlBuilder.BuildVirtualValuesExportStatement(tablename, primaryKeyName, map, userExportId); if (prependUnion) { union.AppendLine("UNION"); } else { prependUnion = true; } union.AppendLine(sql); } return(union.ToString()); }, encryptPatientId); }
//private void Export(XmlWriter writer, List<string> tablenames, EvaluateAttributesDelegate eval, bool encryptPatientId) //{ // ExportXml( // writer, // tablenames, // delegate(string tablename) // { // List<string> columns = GetExportColumns(tablename, eval); // string parentKey = GetParentKey(tablename); // List<KeyValuePair<string, string>> parentTableKeyMap = GetParentTableKeyMap(tablename); // return SqlBuilder.BuildDatasetSelectAllStatement(columns, parentTableKeyMap); // }, // encryptPatientId); //} private string GetLookupSql(string tablename, int userExportId, SqlConnection connection) { EvaluateAttributesDelegate eval = null; switch (privacyLevel) { case "Identified": eval = EvaluateAttributesForIdentified; break; case "Deidentified": eval = EvaluateAttributesForDeidentified; break; case "Limited": eval = EvaluateAttributesForLimited; break; } string lookupTable = GetLookupTablename(tablename); string lookupKey = GetLookupKeyName(tablename); List <string> columns = GetExportColumns(lookupTable, eval, connection); List <KeyValuePair <string, string> > lookupMap = new List <KeyValuePair <string, string> >(); lookupMap.Add(new KeyValuePair <string, string>(lookupTable, lookupKey)); lookupMap.Add(new KeyValuePair <string, string>(tablename, GetParentKey(tablename))); lookupMap.Add(new KeyValuePair <string, string>("UserExportPatients", "PatientId")); List <KeyValuePair <string, string> > restrictions = new List <KeyValuePair <string, string> >(); restrictions.Add(new KeyValuePair <string, string>("UserExportPatients.UserExportId", userExportId.ToString())); return(SqlBuilder.BuildExportDistinctStatement(columns, lookupMap, restrictions)); }
private List <string> GetExportColumns(string tablename, EvaluateAttributesDelegate eval, SqlConnection connection) { List <string> exportColumns = new List <string>(); BusinessObject b = GetBizObject(tablename); FieldInfo[] fields = b.GetType().GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy); string sql = @"SELECT COUNT(COLUMN_NAME) AS Count FROM information_schema.columns WHERE TABLE_NAME = @TableName AND COLUMN_NAME = @ColumnName" ; using (SqlCommand validationCommand = new SqlCommand(sql, connection)) { foreach (FieldInfo f in fields) { string fieldname = f.GetValue(b).ToString(); string maskedFieldname; if ((!b.UseEnteredBy && fieldname == BusinessObject.EnteredBy) || (!b.UseEnteredTime && fieldname == BusinessObject.EnteredTime) || (!b.UseUpdatedBy && fieldname == BusinessObject.UpdatedBy) || (!b.UseUpdatedTime && fieldname == BusinessObject.UpdatedTime) || (!b.UseLockedBy && fieldname == BusinessObject.LockedBy) || (!b.UseLockedTime && fieldname == BusinessObject.LockedTime)) { continue; } // validate each field (tablename, fieldname) vs database // can we combine these fields into one query (per table) or one big query? validationCommand.Parameters.Clear(); validationCommand.Parameters.AddWithValue("@TableName", tablename); validationCommand.Parameters.AddWithValue("@ColumnName", fieldname); int valid = (int)validationCommand.ExecuteScalar(); // skip invalid fields if (valid == 0) { continue; } object[] atts = f.GetCustomAttributes(true); if (eval(atts, tablename, fieldname, out maskedFieldname)) { if (maskedFieldname != null) { exportColumns.Add(maskedFieldname); } else { StringBuilder temp = new StringBuilder(tablename); temp.Append("."); temp.Append(fieldname); exportColumns.Add(temp.ToString()); } } } } return(exportColumns); }