public void TestConnection() { FdoProviderInfo provider = _view.SelectedProvider; string connStr = ExpressUtility.ConvertFromNameValueCollection(_view.ConnectProperties); FdoConnection conn = new FdoConnection(provider.Name, connStr); try { FdoConnectionState state = conn.Open(); if (state == FdoConnectionState.Open || state == FdoConnectionState.Pending) { _view.ShowMessage(null, "Test successful"); conn.Close(); } else { _view.ShowError("Connection test failed"); } } catch (FdoException ex) { _view.ShowError(ex.InnerException.Message); } finally { conn.Dispose(); } }
/// <summary> /// Removes all connections /// </summary> public void Clear() { List <string> names = new List <string>(GetConnectionNames()); foreach (string name in names) { //Yes we're inlining RemoveConnection(), but we want any cancel action to cancel //the Clear() method if (_ConnectionDict.ContainsKey(name)) { ConnectionBeforeRemoveEventArgs e = new ConnectionBeforeRemoveEventArgs(name); this.BeforeConnectionRemove(this, e); if (e.Cancel) { return; } FdoConnection conn = _ConnectionDict[name]; conn.Close(); _ConnectionDict.Remove(name); conn.Dispose(); if (this.ConnectionRemoved != null) { this.ConnectionRemoved(this, new EventArgs <string>(name)); } } } }
/// <summary> /// Gets all class names from the specified flat-file data source /// </summary> /// <param name="sourceFile"></param> /// <returns></returns> public static string[] GetClassNames(string sourceFile) { List <string> classnames = new List <string>(); FdoConnection source = null; try { source = CreateFlatFileConnection(sourceFile); source.Open(); using (FdoFeatureService svc = source.CreateFeatureService()) { using (FeatureSchemaCollection schemas = svc.DescribeSchema()) { foreach (FeatureSchema sch in schemas) { foreach (ClassDefinition cd in sch.Classes) { classnames.Add(cd.Name); } } } } } catch (Exception ex) { } finally { if (source != null) { source.Dispose(); } } return(classnames.ToArray()); }
public bool CreateSqlite() { if (_view.CreateConnection && string.IsNullOrEmpty(_view.ConnectionName)) { _view.ShowError("Specify a connection name"); return(false); } if (ExpressUtility.CreateFlatFileDataSource("OSGeo.SQLite", _view.SQLiteFile)) { FdoConnection conn = ExpressUtility.CreateFlatFileConnection("OSGeo.SQLite", _view.SQLiteFile); if (FileService.FileExists(_view.FeatureSchemaDefinition)) { conn.Open(); using (FdoFeatureService service = conn.CreateFeatureService()) { service.LoadSchemasFromXml(_view.FeatureSchemaDefinition, _view.FixIncompatibilities); } } if (_view.CreateConnection) { _connMgr.AddConnection(_view.ConnectionName, conn); } else { conn.Dispose(); } } return(true); }
/// <summary> /// Releases unmanaged and - optionally - managed resources /// </summary> /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param> protected override void Dispose(bool disposing) { if (disposing) { _outConn.Close(); _outConn.Dispose(); } base.Dispose(disposing); }
/// <summary> /// Removes the connection. /// </summary> /// <param name="name">The name of the connection.</param> public void RemoveConnection(string name) { if (_ConnectionDict.ContainsKey(name)) { ConnectionBeforeRemoveEventArgs e = new ConnectionBeforeRemoveEventArgs(name); this.BeforeConnectionRemove(this, e); if (e.Cancel) { return; } FdoConnection conn = _ConnectionDict[name]; conn.Close(); _ConnectionDict.Remove(name); conn.Dispose(); if (this.ConnectionRemoved != null) { this.ConnectionRemoved(this, new EventArgs <string>(name)); } } }
private void btnSave_Click(object sender, EventArgs e) { var schema = _schema; //Remove elements that have been unchecked. foreach (TreeNode clsNode in treeSchema.Nodes) { string className = clsNode.Name; int index = schema.Classes.IndexOf(className); if (!clsNode.Checked) { if (index >= 0) { schema.Classes.RemoveAt(index); } } else { if (index >= 0) { ClassDefinition clsDef = schema.Classes[index]; foreach (TreeNode propNode in clsNode.Nodes) { if (!propNode.Checked) { string propName = propNode.Text; int pidx = clsDef.Properties.IndexOf(propName); if (pidx >= 0) { clsDef.Properties.RemoveAt(pidx); if (clsDef.IdentityProperties.Contains(propName)) { int idpdx = clsDef.IdentityProperties.IndexOf(propName); clsDef.IdentityProperties.RemoveAt(idpdx); } if (clsDef.ClassType == ClassType.ClassType_FeatureClass) { FeatureClass fc = (FeatureClass)clsDef; if (fc.GeometryProperty.Name == propName) { fc.GeometryProperty = null; } } } } } } } } if (rdXml.Checked) { using (var ios = new IoFileStream(txtXml.Text, "w")) { using (var writer = new XmlWriter(ios, false, XmlWriter.LineFormat.LineFormat_Indent)) { schema.WriteXml(writer); writer.Close(); } ios.Close(); } MessageService.ShowMessage("Schema saved to: " + txtXml.Text); this.DialogResult = DialogResult.OK; } else if (rdFile.Checked) { string fileName = txtFile.Text; if (ExpressUtility.CreateFlatFileDataSource(fileName)) { FdoConnection conn = ExpressUtility.CreateFlatFileConnection(fileName); bool disposeConn = true; using (FdoFeatureService svc = conn.CreateFeatureService()) { svc.ApplySchema(schema); if (MessageService.AskQuestion("Schema saved to: " + txtFile.Text + " connect to it?", "Saved")) { FdoConnectionManager mgr = ServiceManager.Instance.GetService <FdoConnectionManager>(); string name = MessageService.ShowInputBox(ResourceService.GetString("TITLE_CONNECTION_NAME"), ResourceService.GetString("PROMPT_ENTER_CONNECTION"), ""); if (name == null) { return; } while (name == string.Empty || mgr.NameExists(name)) { MessageService.ShowError(ResourceService.GetString("ERR_CONNECTION_NAME_EMPTY_OR_EXISTS")); name = MessageService.ShowInputBox(ResourceService.GetString("TITLE_CONNECTION_NAME"), ResourceService.GetString("PROMPT_ENTER_CONNECTION"), name); if (name == null) { return; } } disposeConn = false; mgr.AddConnection(name, conn); } } if (disposeConn) { conn.Close(); conn.Dispose(); } this.DialogResult = DialogResult.OK; } } }
public bool CreateSdf() { if (_view.CreateConnection && string.IsNullOrEmpty(_view.ConnectionName)) { _view.ShowError("Specify a connection name"); return(false); } if (ExpressUtility.CreateFlatFileDataSource("OSGeo.SDF", _view.SdfFile)) { FdoDataStoreConfiguration dstore = null; if (FileService.FileExists(_view.FeatureSchemaDefinition)) { dstore = FdoDataStoreConfiguration.FromFile(_view.FeatureSchemaDefinition); //SDF only permits the following: // 1 feature schema // 1 spatial context if (dstore.Schemas.Count > 1) { _view.ShowError("Multiple schemas were found in this document. SDF only allows 1 feature schema"); return(false); } if (dstore.SpatialContexts.Length > 1) { _view.ShowError("Multiple spatial contexts were found in this doucment. SDF only allows 1 spatial context"); return(false); } } FdoConnection conn = ExpressUtility.CreateFlatFileConnection("OSGeo.SDF", _view.SdfFile); if (dstore != null) { using (var svc = conn.CreateFeatureService()) { if (dstore.SpatialContexts.Length == 1) { //Overwrite existing spatial context if it exists var sc = dstore.SpatialContexts[0]; var asc = svc.GetActiveSpatialContext(); if (asc != null) { sc.Name = asc.Name; } svc.CreateSpatialContext(sc, (asc != null)); } var schema = dstore.Schemas[0]; if (_view.FixIncompatibilities) { IncompatibleSchema incS; if (!svc.CanApplySchema(schema, out incS)) { schema = svc.AlterSchema(schema, incS); } } svc.ApplySchema(schema); } } if (_view.CreateConnection) { _connMgr.AddConnection(_view.ConnectionName, conn); } else { conn.Dispose(); } } return(true); }
public bool Create() { bool ok = true; FdoConnection conn = new FdoConnection(_view.Provider, GetBaseConnectionString()); bool created = false; bool cleanup = true; try { using (var svc = conn.CreateFeatureService()) { try { CreateDataStore(svc); created = true; } catch (Exception ex) { _view.ShowError(ex); created = false; ok = false; } } if (created) { //Amend the connection string to include the data store var builder = new DbConnectionStringBuilder(); builder.ConnectionString = conn.ConnectionString; builder[_view.DataStoreParameter] = _view.DataStoreName; conn.ConnectionString = builder.ConnectionString; conn.Open(); using (var svc = conn.CreateFeatureService()) { CreateDefaultSpatialContext(svc); } } } finally { if (created) { if (File.Exists(_view.SchemaFile)) { ApplySchemas(conn); } if (_view.ConnectOnCreate) { _connMgr.AddConnection(_view.ConnectionName, conn); cleanup = false; } } if (cleanup) { conn.Close(); conn.Dispose(); } } return(ok); }
/// <summary> /// Creates a FDO bulk copy task. The target file will be created as part of /// this method call. If the target path is a directory, it is assumed that /// SHP files are to be created and copied to. /// </summary> /// <param name="sourceFile">The path to the source file.</param> /// <param name="targetPath"> /// The path to the target file/directory. If it is a directory, it is assumed /// that SHP files are to be created and copied to. /// </param> /// <param name="copySpatialContexts">If true, will also copy spatial contexts</param> /// <param name="fixIncompatibleSchema">If true, will try to fix the source schema to make it compatible with the target connection. If false, an exception will be thrown</param> /// <param name="flattenGeometries">If true, will strip all Z and M coordinates from all geometries that are copied</param> /// <returns></returns> public static FdoBulkCopy CreateBulkCopy(string sourceFile, string targetPath, bool copySpatialContexts, bool fixIncompatibleSchema, bool flattenGeometries) { FdoBulkCopyOptions options = null; FdoConnection source = null; FdoConnection target = null; try { //Is a directory. Implies a SHP connection if (IsShp(targetPath)) { //SHP doesn't actually support CreateDataStore. We use the following technique: // - Connect to base directory // - Clone source schema and apply to SHP connection. // - A SHP file and related files are created for each feature class. string shpdir = Directory.Exists(targetPath) ? targetPath : Path.GetDirectoryName(targetPath); source = CreateFlatFileConnection(sourceFile); target = new FdoConnection("OSGeo.SHP", "DefaultFileLocation=" + shpdir); source.Open(); //Verify source has only classes with single geometry storage and only one geometry using (FdoFeatureService svc = source.CreateFeatureService()) { using (FeatureSchemaCollection schemas = svc.DescribeSchema()) { foreach (FeatureSchema sch in schemas) { foreach (ClassDefinition cd in sch.Classes) { int geomProps = 0; foreach (PropertyDefinition pd in cd.Properties) { if (pd.PropertyType == PropertyType.PropertyType_GeometricProperty) { GeometricPropertyDefinition gp = pd as GeometricPropertyDefinition; GeometricType[] types = FdoGeometryUtil.GetGeometricTypes(gp.GeometryTypes); if (types.Length != 1 || (types.Length == 1 && types[0] == GeometricType.GeometricType_All)) { throw new FdoETLException(string.Format("Source file cannot be copied to a SHP file. {0}:{1}.{2} has more than one geometry storage type", sch.Name, cd.Name, pd.Name)); } geomProps++; } } if (geomProps > 1) { throw new FdoETLException("Source file cannot be copied to a SHP file. One or more feature classes have more than one geometry property"); } } } } } } else { if (!CreateFlatFileDataSource(targetPath)) { throw new FdoException("Unable to create data source on: " + targetPath); } source = CreateFlatFileConnection(sourceFile); target = CreateFlatFileConnection(targetPath); } //Source and target connections may have been opened before this point if (source.State == FdoConnectionState.Closed) { source.Open(); } if (target.State == FdoConnectionState.Closed) { target.Open(); } string srcName = "SOURCE"; string dstName = "TARGET"; Dictionary <string, FdoConnection> connections = new Dictionary <string, FdoConnection>(); connections.Add(srcName, source); connections.Add(dstName, target); options = new FdoBulkCopyOptions(connections, true); if (copySpatialContexts) { CopyAllSpatialContexts(source, target, true); } using (FdoFeatureService srcService = source.CreateFeatureService()) using (FdoFeatureService destService = target.CreateFeatureService()) { FeatureSchemaCollection schemas = srcService.DescribeSchema(); //Assume single-schema FeatureSchema fs = schemas[0]; //Clone and apply to target FeatureSchema targetSchema = FdoSchemaUtil.CloneSchema(fs); IncompatibleSchema incSchema; string sourceSchemaName = fs.Name; string targetSchemaName = string.Empty; bool canApply = destService.CanApplySchema(targetSchema, out incSchema); if (canApply) { destService.ApplySchema(targetSchema); targetSchemaName = targetSchema.Name; } else { if (fixIncompatibleSchema) { FeatureSchema fixedSchema = destService.AlterSchema(targetSchema, incSchema); destService.ApplySchema(fixedSchema); targetSchemaName = fixedSchema.Name; } else { throw new Exception(incSchema.ToString()); } } //Copy all classes foreach (ClassDefinition cd in fs.Classes) { FdoClassCopyOptions copt = new FdoClassCopyOptions(srcName, dstName, sourceSchemaName, cd.Name, targetSchemaName, cd.Name); copt.Name = "Copy source to target [" + cd.Name + "]"; copt.FlattenGeometries = flattenGeometries; options.AddClassCopyOption(copt); //Flick on batch support if we can if (destService.SupportsBatchInsertion()) { copt.BatchSize = 300; //Madness? THIS IS SPARTA! } } } } catch (Exception) { if (source != null) { source.Dispose(); } if (target != null) { target.Dispose(); } throw; } return(new FdoBulkCopy(options)); }
public bool CreateShp() { if (_view.CreateConnection && string.IsNullOrEmpty(_view.ConnectionName)) { _view.ShowError("Specify a connection name"); return(false); } //Creating SHP files is as follows // // 1. Connect to the *parent* directory of the shape file we want to create // 2. Apply the schema to this connection if (FileService.FileExists(_view.FeatureSchemaDefinition)) { try { FdoConnection conn = ExpressUtility.CreateFlatFileConnection("OSGeo.SHP", _view.ShpDirectory); FdoDataStoreConfiguration config = FdoDataStoreConfiguration.FromFile(_view.FeatureSchemaDefinition); //SHP allows the following: // 1 feature schema // Multiple spatial contexts if (config.Schemas.Count > 1) { _view.ShowError("More than 1 feature schema was found in the document. SHP only allows 1 feature schema"); return(false); } var schema = config.Schemas[0]; using (var svc = conn.CreateFeatureService()) { foreach (var sc in config.SpatialContexts) { svc.CreateSpatialContext(sc, true); } if (_view.FixIncompatibilities) { IncompatibleSchema incs; if (!svc.CanApplySchema(schema, out incs)) { schema = svc.AlterSchema(schema, incs); } } svc.ApplySchema(schema); } conn.Dispose(); if (_view.CreateConnection) { conn = ExpressUtility.CreateFlatFileConnection("OSGeo.SHP", _view.ShpDirectory); conn.Open(); _connMgr.AddConnection(_view.ConnectionName, conn); } } catch (OSGeo.FDO.Common.Exception ex) { _view.ShowError(ex); LoggingService.Error("Failed to create SHP", ex); return(false); } } return(true); }
public bool Connect() { if (string.IsNullOrEmpty(_view.ConnectionName)) { _view.FlagNameError("Required"); return(false); } FdoConnection conn = _manager.GetConnection(_view.ConnectionName); if (conn != null) { _view.FlagNameError("A connection named " + _view.ConnectionName + " already exists"); return(false); } FdoProviderInfo provider = _view.SelectedProvider; //string connStr = ExpressUtility.ConvertFromNameValueCollection(_view.ConnectProperties); NameValueCollection cp = new NameValueCollection(_view.ConnectProperties); if (_pendingProperties.Count > 0) { NameValueCollection extra = new NameValueCollection(); cp.Add(extra); } string connStr = ExpressUtility.ConvertFromNameValueCollection(cp); conn = new FdoConnection(provider.Name, connStr); if (FileService.FileExists(_view.ConfigFile)) { try { conn.SetConfiguration(_view.ConfigFile); } catch (Exception ex) { conn.Dispose(); _view.FlagConfigError(ex.Message); return(false); } } try { FdoConnectionState state = conn.Open(); if (state == FdoConnectionState.Open) { _manager.AddConnection(_view.ConnectionName, conn); return(true); } else if (state == FdoConnectionState.Pending) { //Re-query the pending parameters and re-prompt in a new dialog if (_pendingProperties.Count > 0) { List <DictionaryProperty> pend = new List <DictionaryProperty>(); foreach (DictionaryProperty p in _pendingProperties) { pend.Add(conn.GetConnectTimeProperty(p.Name)); } NameValueCollection extra = PendingParameterDialog.GetExtraParameters(pend); //Cancelled action if (extra == null) { return(false); } cp.Add(extra); conn.ConnectionString = ExpressUtility.ConvertFromNameValueCollection(cp); if (conn.Open() == FdoConnectionState.Open) { _manager.AddConnection(_view.ConnectionName, conn); return(true); } } } else { return(false); } } catch (Exception ex) { _view.ShowError(ex); conn.Dispose(); return(false); } return(false); }
public override int Execute() { CommandStatus retCode; FdoConnection srcConn = new FdoConnection(_srcProvider, _srcConnStr); FdoConnection destConn = null; //Directory given, assume SHP if (Directory.Exists(_destPath)) { destConn = new FdoConnection("OSGeo.SHP", "DefaultFileLocation=" + _destPath); } else { if (ExpressUtility.CreateFlatFileDataSource(_destPath)) { destConn = ExpressUtility.CreateFlatFileConnection(_destPath); } else { throw new FdoException("Could not create data source: " + _destPath); } } try { srcConn.Open(); destConn.Open(); string srcName = "SOURCE"; string dstName = "TARGET"; FdoBulkCopyOptions options = new FdoBulkCopyOptions(); options.RegisterConnection(srcName, srcConn); options.RegisterConnection(dstName, destConn); using (FdoFeatureService srcService = srcConn.CreateFeatureService()) using (FdoFeatureService destService = destConn.CreateFeatureService()) { //See if spatial context needs to be copied to target if (!string.IsNullOrEmpty(_srcSpatialContext)) { SpatialContextInfo srcCtx = srcService.GetSpatialContext(_srcSpatialContext); if (srcCtx != null) { Console.WriteLine("Copying spatial context: " + srcCtx.Name); ExpressUtility.CopyAllSpatialContexts(new SpatialContextInfo[] { srcCtx }, destConn, true); } } else { //Copy all ExpressUtility.CopyAllSpatialContexts(srcConn, destConn, true); } FeatureSchema srcSchema = null; //See if partial class list is needed if (_srcClasses.Count > 0) { WriteLine("Checking if partial schema discovery is supported: " + srcService.SupportsPartialSchemaDiscovery()); srcSchema = srcService.PartialDescribeSchema(_srcSchema, _srcClasses); } else //Full copy { WriteLine("No classes specified, reading full source schema"); srcSchema = srcService.GetSchemaByName(_srcSchema); } if (srcSchema == null) { WriteError("Could not find source schema: " + _srcSchema); retCode = CommandStatus.E_FAIL_SCHEMA_NOT_FOUND; } else { WriteLine("Checking source schema for incompatibilities"); FeatureSchema targetSchema = null; IncompatibleSchema incSchema; if (destService.CanApplySchema(srcSchema, out incSchema)) { int clsCount = srcSchema.Classes.Count; WriteLine("Applying source schema (containing " + clsCount + " classes) to target"); destService.ApplySchema(srcSchema, null, true); targetSchema = srcSchema; } else { WriteWarning("Incompatibilities were detected in source schema. Applying a modified version to target"); FeatureSchema fixedSchema = destService.AlterSchema(srcSchema, incSchema); int clsCount = fixedSchema.Classes.Count; WriteLine("Applying modified source schema (containing " + clsCount + " classes) to target"); destService.ApplySchema(fixedSchema, null, true); targetSchema = fixedSchema; } //Now set class copy options foreach (ClassDefinition cd in srcSchema.Classes) { FdoClassCopyOptions copt = new FdoClassCopyOptions(srcName, dstName, srcSchema.Name, cd.Name, targetSchema.Name, cd.Name); copt.FlattenGeometries = _flatten; options.AddClassCopyOption(copt); } if (_flatten) { WriteWarning("The switch -flatten has been defined. Geometries that are copied will have any Z or M coordinates removed"); } FdoBulkCopy copy = new FdoBulkCopy(options); copy.ProcessMessage += new MessageEventHandler(OnMessage); copy.ProcessCompleted += new EventHandler(OnCompleted); Console.WriteLine("Executing bulk copy"); copy.Execute(); List <Exception> errors = new List <Exception>(copy.GetAllErrors()); if (errors.Count > 0) { string file = GenerateLogFileName("bcp-error-"); LogErrors(errors, file); base.WriteError("Errors were encountered during bulk copy."); retCode = CommandStatus.E_FAIL_BULK_COPY_WITH_ERRORS; } else { retCode = CommandStatus.E_OK; } retCode = CommandStatus.E_OK; } } } catch (Exception ex) { WriteException(ex); retCode = CommandStatus.E_FAIL_UNKNOWN; } finally { srcConn.Dispose(); destConn.Dispose(); } return((int)retCode); }