private string GenerateFilter(FdoFeature feat) { FdoFeatureTable table = feat.Table; if (table.PrimaryKey.Length > 0) { List <string> filters = new List <string>(); foreach (DataColumn col in table.PrimaryKey) { DataType dt = ExpressUtility.GetFdoDataTypeFromClrType(col.DataType); string f = string.Empty; if (dt == DataType.DataType_DateTime || dt == DataType.DataType_String) { f = col.ColumnName + " = '" + feat[col] + "'"; } else { f = col.ColumnName + " = " + feat[col]; } filters.Add(f); } return("(" + string.Join(" AND ", filters.ToArray()) + ")"); } return(null); }
public FdoUpdateScaffoldPresenter(IFdoUpdateView view, FdoFeature feat, FdoConnection conn, string filter) { _view = view; _conn = conn; _className = feat.Table.TableName; _view.Title = ICSharpCode.Core.ResourceService.GetString("TITLE_UPDATE_FEATURE"); _view.UpdateFilter = filter; _filter = filter; _feature = feat; }
internal void DoUpdate(FdoFeature feat) { FeatureQueryOptions query = null; switch (_view.SelectedQueryMode) { case QueryMode.Aggregate: { query = (_view.QueryView as IFdoAggregateQueryView).QueryObject; } break; case QueryMode.Standard: { query = (_view.QueryView as IFdoStandardQueryView).QueryObject; } break; } if (query != null) { string filter = GenerateFilter(feat); if (string.IsNullOrEmpty(filter)) { _view.ShowError("Unable to generate an update filter. Possibly this result set has no unique identifiers or this result set was produced from a SQL query"); return; } using (FdoFeatureService service = _connection.CreateFeatureService()) { //Update is based on a very simple premise, the filter should produce the //same number of affected results when selecting and updating. // //In our case, the filter should affect exactly one result when selecting and updating. long count = service.GetFeatureCount(feat.Table.TableName, filter, true); if (1 == count) { Workbench wb = Workbench.Instance; FdoUpdateScaffold ctl = new FdoUpdateScaffold(feat, _connection, filter); wb.ShowContent(ctl, ViewRegion.Dialog); } } } else { _view.ShowError("Could not determine the feature class name from the result set"); } }
private void updateThisFeatureToolStripMenuItem_Click(object sender, EventArgs e) { if (grdResults.SelectedRows.Count == 1) { FdoFeature feat = (FdoFeature)((grdResults.SelectedRows[0].DataBoundItem as DataRowView).Row); _presenter.DoUpdate(feat); } else if (grdResults.SelectedRows.Count > 1) { List <FdoFeature> features = new List <FdoFeature>(); foreach (DataGridViewRow row in grdResults.SelectedRows) { features.Add((FdoFeature)(row.DataBoundItem as DataRowView).Row); } string filter = _presenter.GenerateFilter(features.ToArray()); FdoBulkUpdateCtl ctl = new FdoBulkUpdateCtl(_presenter.Connection, _presenter.SelectedClassName, filter); Workbench.Instance.ShowContent(ctl, ViewRegion.Dialog); } }
/// <summary> /// Creates a new <see cref="FdoRow"/> from a <see cref="FdoFeature"/> instance /// </summary> /// <param name="feat">The <see cref="FdoFeature"/> instance</param> /// <returns>A new <see cref="FdoRow"/> instance</returns> public static FdoRow FromFeatureRow(FdoFeature feat) { if (feat.Table == null) { throw new InvalidOperationException(ResourceUtil.GetString("ERR_FEATURE_ROW_HAS_NO_PARENT_TABLE")); } FdoRow row = new FdoRow(); foreach (DataColumn dc in feat.Table.Columns) { if (feat[dc] != null && feat[dc] != DBNull.Value) { FdoGeometry geom = feat[dc] as FdoGeometry; if (geom != null) { if (feat.Table.GeometryColumn == dc.ColumnName) { row.AddGeometry(dc.ColumnName, geom.InternalInstance); row.DefaultGeometryProperty = dc.ColumnName; } else { row.AddGeometry(dc.ColumnName, geom.InternalInstance); } } else { row[dc.ColumnName] = feat[dc]; } if (dc.ReadOnly) { row.MarkReadOnly(dc.ColumnName); } } } return(row); }
private void deleteThisFeatureToolStripMenuItem_Click(object sender, EventArgs e) { if (grdResults.SelectedRows.Count == 1) { if (this.Confirm("Delete Feature", "Are you sure you want to delete this feature?")) { FdoFeature feat = (FdoFeature)((grdResults.SelectedRows[0].DataBoundItem as DataRowView).Row); _presenter.DoDelete(feat); } } else if (grdResults.SelectedRows.Count > 1) { if (this.Confirm("Delete Feature", "Are you sure you want to delete these features?")) { List <FdoFeature> features = new List <FdoFeature>(); foreach (DataGridViewRow row in grdResults.SelectedRows) { features.Add((FdoFeature)(row.DataBoundItem as DataRowView).Row); } _presenter.DoDelete(features.ToArray()); } } }
/// <summary> /// Constructor /// </summary> /// <param name="feature"></param> /// <param name="action"></param> public FdoFeatureChangeEventArgs(FdoFeature feature, DataRowAction action) { _feature = feature; _action = action; }
public FdoUpdateScaffold(FdoFeature feature, FdoConnection conn, string filter) : this() { _presenter = new FdoUpdateScaffoldPresenter(this, feature, conn, filter); }
internal void DoDelete(FdoFeature feat) { this.DeleteFeatures(feat); }
private void DeleteFeatures(FdoFeature feat) { DeleteFeatures(feat.Table, new FdoFeature[] { feat }); }
void DoWork(object sender, DoWorkEventArgs e) { IFdoReader reader = null; using (FdoFeatureService service = _connection.CreateFeatureService()) { try { if (e.Argument is FeatureAggregateOptions) { reader = service.SelectAggregates((FeatureAggregateOptions)e.Argument); } else if (e.Argument is StandardQuery) { var stdArg = (e.Argument as StandardQuery); reader = service.SelectFeatures(stdArg.query, stdArg.Limit, stdArg.UseExtendedSelect); } else if (e.Argument is string) { reader = service.ExecuteSQLQuery(e.Argument.ToString()); } //Init the data grid view FdoFeatureTable table = new FdoFeatureTable(); table.RequestSpatialContext += delegate(object o, string name) { SpatialContextInfo c = service.GetSpatialContext(name); if (c != null) { table.AddSpatialContext(c); } }; ClassDefinition clsDef = null; bool hasJoins = false; if (e.Argument is StandardQuery) { var qry = ((StandardQuery)e.Argument).query; clsDef = service.GetClassByName(qry.ClassName); //TODO: Should really qualify this, but our input parameters do not specify a schema hasJoins = qry.JoinCriteria.Count > 0; } table.InitTable(reader, clsDef, hasJoins); // need to store object class defn outside loop ClassDefinition classDefObject = null; ClassDefinition classDefAssoc = null; while (reader.ReadNext() && !_queryWorker.CancellationPending) { //Pass processed feature to data grid view FdoFeature feat = table.NewRow(); for (int i = 0; i < reader.FieldCount; i++) { string name = reader.GetName(i); if (!reader.IsNull(name)) { FdoPropertyType pt = reader.GetFdoPropertyType(name); switch (pt) { case FdoPropertyType.Association: // TODO: how to handle for non-StandardQuery if (e.Argument is StandardQuery) { // because the original code used an iterator over the reader.FieldCount // it is a bit of a hack to get another definition of the class and check // we are at the right property with a name comparison // TODO: code review to see if this can be implemented better FdoFeatureReader readerFeature = (FdoFeatureReader)reader; ClassDefinition classDef = readerFeature.GetClassDefinition(); foreach (PropertyDefinition propertyDef in classDef.Properties) { // only looking for the right association if ((PropertyType.PropertyType_AssociationProperty == propertyDef.PropertyType) && (propertyDef.Name.Equals(name))) { AssociationPropertyDefinition assocProp = (AssociationPropertyDefinition)propertyDef; ClassDefinition classAssoc = assocProp.AssociatedClass; // get a reader from the association - nice! IFeatureReader readerAssoc = readerFeature.GetFeatureObject(name); if ((null != readerAssoc) && (readerAssoc.ReadNext())) { // TODO: this should be an iterative function // the simple reassignment on each instance fails // (classDefObject.Properties is always zero after first record) // so have set classDefObject higher-up and re-use/ // - problem will occur if more than one association // ClassDefinition classDefObject = readerObject.GetClassDefinition(); if (null == classDefAssoc) { classDefAssoc = readerAssoc.GetClassDefinition(); } foreach (PropertyDefinition propertyDefAssoc in classDefAssoc.Properties) { String nameAssoc = propertyDefAssoc.Name; // TODO: only "data" type handled at present if (PropertyType.PropertyType_DataProperty == propertyDefAssoc.PropertyType) { DataPropertyDefinition datapropertyDefAssoc = (DataPropertyDefinition)propertyDefAssoc; String szFullName = name + "." + nameAssoc; DataType ptAssoc = datapropertyDefAssoc.DataType; // TODO: handle all types switch (ptAssoc) { case DataType.DataType_String: feat[szFullName] = readerAssoc.GetString(nameAssoc); break; case DataType.DataType_Int16: feat[szFullName] = readerAssoc.GetInt16(nameAssoc); break; case DataType.DataType_Int32: feat[szFullName] = readerAssoc.GetInt32(nameAssoc); break; case DataType.DataType_Int64: feat[szFullName] = readerAssoc.GetInt64(nameAssoc); break; } } } readerAssoc.Close(); } } } } break; case FdoPropertyType.BLOB: feat[name] = reader.GetLOB(name).Data; break; case FdoPropertyType.Boolean: feat[name] = reader.GetBoolean(name); break; case FdoPropertyType.Byte: feat[name] = reader.GetByte(name); break; case FdoPropertyType.CLOB: feat[name] = reader.GetLOB(name).Data; break; case FdoPropertyType.DateTime: { try { feat[name] = reader.GetDateTime(name); } catch //Unrepresentable dates { feat[name] = DBNull.Value; } } break; case FdoPropertyType.Decimal: //We should probably remove this as FDO coerces decimals to doubles (otherwise why is there not a GetDecimal() method in FdoIReader?) { double val = reader.GetDouble(name); if (Double.IsNaN(val)) { feat[name] = DBNull.Value; } else { feat[name] = Convert.ToDecimal(val); } } break; case FdoPropertyType.Double: feat[name] = reader.GetDouble(name); break; case FdoPropertyType.Geometry: { try { byte[] fgf = reader.GetGeometry(name); OSGeo.FDO.Geometry.IGeometry geom = service.GeometryFactory.CreateGeometryFromFgf(fgf); var fg = new FdoGeometry(geom); //OGR geometry trap using (var env = fg.Envelope) { } feat[name] = fg; } catch { feat[name] = DBNull.Value; } } break; case FdoPropertyType.Int16: feat[name] = reader.GetInt16(name); break; case FdoPropertyType.Int32: feat[name] = reader.GetInt32(name); break; case FdoPropertyType.Int64: feat[name] = reader.GetInt64(name); break; case FdoPropertyType.Object: // TODO: how to handle for non-StandardQuery if (e.Argument is StandardQuery) { // because the original code used an iterator over the reader.FieldCount // it is a bit of a hack to get another definition of the class and check // we are at the right property with a name comparison // TODO: code review to see if this can be implemented better FdoFeatureReader readerFeature = (FdoFeatureReader)reader; ClassDefinition classDef = readerFeature.GetClassDefinition(); foreach (PropertyDefinition propertyDef in classDef.Properties) { // only looking for the right object if ((PropertyType.PropertyType_ObjectProperty == propertyDef.PropertyType) && (propertyDef.Name.Equals(name))) { ObjectPropertyDefinition assocProp = (ObjectPropertyDefinition)propertyDef; ClassDefinition classAssoc = assocProp.Class; // get a reader from the object - nice! IFeatureReader readerObject = readerFeature.GetFeatureObject(name); if ((null != readerObject) && (readerObject.ReadNext())) { // TODO: this should be an iterative function // the simple reassignment on each instance fails // (classDefObject.Properties is always zero after first record) // so have set classDefObject higher-up and re-use/ // - problem will occur if more than one association // ClassDefinition classDefObject = readerObject.GetClassDefinition(); if (null == classDefObject) { classDefObject = readerObject.GetClassDefinition(); } foreach (PropertyDefinition propertyDefObject in classDefObject.Properties) { String nameObject = propertyDefObject.Name; // TODO: only "data" type handled at present if (PropertyType.PropertyType_DataProperty == propertyDefObject.PropertyType) { DataPropertyDefinition datapropertyDefObject = (DataPropertyDefinition)propertyDefObject; String szFullName = name + "." + nameObject; DataType ptAssoc = datapropertyDefObject.DataType; // TODO: handle all types switch (ptAssoc) { case DataType.DataType_String: feat[szFullName] = readerObject.GetString(nameObject); break; case DataType.DataType_Int16: feat[szFullName] = readerObject.GetInt16(nameObject); break; case DataType.DataType_Int32: feat[szFullName] = readerObject.GetInt32(nameObject); break; case DataType.DataType_Int64: feat[szFullName] = readerObject.GetInt64(nameObject); break; } } } readerObject.Close(); readerObject = null; } } } } break; //case FdoPropertyType.Raster: case FdoPropertyType.Single: feat[name] = reader.GetSingle(name); break; case FdoPropertyType.String: feat[name] = reader.GetString(name); break; } } else { feat[name] = DBNull.Value; } } table.AddRow(feat); if (table.Rows.Count % 50 == 0) { System.Threading.Thread.Sleep(50); } } if (_queryWorker.CancellationPending) { e.Cancel = true; _cancelResult = table; } else { e.Result = table; } } finally { if (reader != null) { reader.Close(); } } } }