/// <summary> /// Connect to a stream of flat file data /// </summary> /// <param name="stream">The stream of data to connect to</param> /// <returns>If the data was valid and is a stream</returns> public override Boolean Connect(DataItemDefinition definition, DataConnection connection, Stream stream) { this.Connection = connection; this.definition = definition; // Assign the definition to use this.memoryData = new DataTable(); // Blank data by default // Do we have a stream and a definition if (stream != null && definition == null) { base.connected = true; return(true); } else if (stream != null && definition != null) { stream.Position = 0; // Reset back to the start again in case someone else has read it base.MarkLastAction(); // Tell the provider base class that it did something // Read the data from the stream provided using (StreamReader textReader = new StreamReader(stream)) { this.memoryData = DelimitedFileHelper.TextToDataTable(definition, connection, textReader.ReadToEnd()); base.connected = true; // Mark the provider as connected return(true); // Connected without any errors } } return(false); // Failed if we get to here }
/// <summary> /// Connect to the flat file source /// </summary> /// <param name="connectionString">The connection string to use</param> /// <returns>If the file exists when it is connected</returns> public override Boolean Connect(DataItemDefinition definition, DataConnection connection) { Boolean result = false; // Failed by default // Check if not an object if (connection == null) { return(false); } // Get the processed connection string (with any injected items) String connectionString = connection.ConnectionStringProcessed; // Does the file that we are trying to connect to exist? if (File.Exists(connectionString)) { // Connect to the file and read the data from it using (Stream fileStream = File.OpenRead(connectionString)) { result = Connect(definition, connection, fileStream); // Do a standard stream connect to reuse that code if (result) { this.Connection = connection; // Remember the connection string } } } this.MarkLastAction(); // Tell the provider base class that it did something return(result); // Return the result of the read }
/// <summary> /// Analyse the connection to get the definition back /// </summary> /// <param name="request">The analysis request paramaters</param> /// <returns>The data item definition derived from the connection and object</returns> public override DataItemDefinition Analyse(AnalyseRequest <object> request) { DataItemDefinition result = new DataItemDefinition(); // Empty Response By Default // Are we connected and have an object name? if ((this.ObjectName ?? String.Empty) != String.Empty && this.Connected) { // Set up the command to run and select all columns from the object using (SqlCommand command = new SqlCommand($"select top {request.SampleSize.ToString()} * from {this.ObjectName}", this.sqlConnection)) { // Run the command using (SqlDataReader dataReader = command.ExecuteReader()) { DataTable tempTable = new DataTable(); // Create a temporary table tempTable.Load(dataReader); // Run the data reader to load the results result.FromDataTable(tempTable); // Tell the definition to load itself from the data table provided // Clean up tempTable = null; } } } // Send the result back return(result); }
public IDataProvider Get( Package package, DataConnection connection, DataItemDefinition definition, Boolean addToCache) { IDataProvider result = null; // Create a fail state by default // Do we already have a provider set up for this data connection String uniqueKey = GenerateConnectionKey( new List <Guid>() { connection.Id, ((definition == null || definition.Id == null) ? Guid.Empty : definition.Id) }); Boolean existingProvider = providers.ContainsKey(uniqueKey); if (!existingProvider) { result = Get(connection.ProviderType); } else { result = providers[uniqueKey]; } // If the provider is stale (something configuration-wise has been changed) if (result?.LastAction <= connection.LastUpdated) { #warning "Need a simple way of reseting a connection rather than doing each change manually" } // Did we get a provider? if (result != null) { // Set the package if it is not assigned connection.ParentPackage = (connection.ParentPackage ?? package); // Set the provider to be connected if it is not already connected // or if the provider has an updated definition if (!result.Connected || (definition != null && result.LastAction <= definition.LastUpdated)) { result.Connect(definition, connection); // Connect attempt existingProvider = false; // Re-connected potentially so reset to make sure it's added to the cache } // If the provider was not in the pooled collection of providers then add it if (!existingProvider && addToCache) { providers[uniqueKey] = result; } } // Return the provider return(result); }
/// <summary> /// Look at the file and try and represent the file as a dataset without a definition /// </summary> /// <returns>A representation of the data</returns> public override DataItemDefinition Analyse(AnalyseRequest <Object> request) { // Create a blank result data table DataItemDefinition result = new DataItemDefinition() { }; String rawData = ""; // The data which ultimately will be read from // Check to see what can of analysis is being requested if ((request.Connection?.ConnectionString ?? String.Empty) != String.Empty) { // Get the stream of data from the raw file rawData = File.ReadAllText(request.Connection.ConnectionStringProcessed); } else { // No connection was provided so use the raw data provided instead switch (request.Data.GetType().ToShortName()) { case "string": rawData = (String)request.Data; break; default: ((Stream)request.Data).Position = 0; // Reset the stream position // Read the data from the stream StreamReader reader = new StreamReader((Stream)request.Data); rawData = reader.ReadToEnd(); // Reset the position again so that it can be re-used ((Stream)request.Data).Position = 0; break; } } // Pass down to the analyse text core function AnalyseRequest <String> analyseTextRequest = new AnalyseRequest <String>() { Data = rawData, Connection = request.Connection }; result = DelimitedFileHelper.AnalyseText(analyseTextRequest); base.MarkLastAction(); // Tell the provider base class that it did something // Send the analysis data table back return(result); }
/// <summary> /// Override the connection object to connect to the Sql Server /// </summary> /// <param name="definition">The definition of the data to connect to</param> /// <param name="connection">The connect properties to connect to the server</param> /// <returns>If the connection worked</returns> public override Boolean Connect(DataItemDefinition definition, DataConnection connection) { if (!this.Connected) { // Connect to the sql server this.sqlConnection = new SqlConnection(connection.ConnectionStringProcessed); try { this.sqlConnection.Open(); // Start the connection this.definition = definition; // Assign the definition this.ObjectName = connection.ObjectName; // Assign the object name being set to return(true); // Success! } catch (Exception ex) { return(false); } } else { return(true); // Already connected, why return false? } }
/// <summary> /// Connect to this connection /// </summary> /// <param name="definition">The definition of the data in the connection</param> /// <param name="connection">The connection</param> /// <param name="stream">The stream to connect to</param> /// <returns>If the system connected ok</returns> public virtual bool Connect(DataItemDefinition definition, DataConnection connection, Stream stream) => throw new NotImplementedException();