/// <summary> /// Execute an existing command, and translate the result set. This method supports auto-open. /// The Connection property of the command must be initialized before calling this method. /// </summary> /// <typeparam name="T">The type of result object to return. This must derive from Results.</typeparam> /// <param name="command">The command to execute.</param> /// <param name="returns">The reader to use to read the object from the stream.</param> /// <param name="commandBehavior">The behavior of the command when executed.</param> /// <param name="outputParameters">An object to write output parameters onto.</param> /// <returns>A data reader with the results.</returns> public static T Query <T>( this IDbCommand command, IQueryReader <T> returns, CommandBehavior commandBehavior = CommandBehavior.Default, object outputParameters = null) { if (command == null) { throw new ArgumentNullException("command"); } if (returns == null) { throw new ArgumentNullException("returns"); } return(command.Connection.ExecuteAndAutoClose( c => command, (cmd, r) => { var results = returns.Read(cmd, r); cmd.OutputParameters(outputParameters); return results; }, commandBehavior)); }
protected override void OnHydrate(ref FavoritesStreamReference instance, IQueryReader reader) { var id = new Identifier(reader["table_id"]); instance.id = id; instance.TableName = (string)reader["table"]; }
/// <summary> /// Lets us call QueryCore into a simple delegate for dynamic calls. /// </summary> /// <typeparam name="T">The type of object returned.</typeparam> /// <param name="command">The command to execute.</param> /// <param name="returns">The definition of the return structure.</param> /// <param name="commandBehavior">The commandBehavior to use.</param> /// <param name="outputParameters">Optional output parameters.</param> /// <returns>The result of the query.</returns> private static T QueryCoreUntyped <T>( this IDbCommand command, IQueryReader returns, CommandBehavior commandBehavior = CommandBehavior.Default, object outputParameters = null) { // this method lets us convert QueryCore to a delegate for dynamic calls return(command.Query <T>((IQueryReader <T>)returns, commandBehavior, outputParameters)); }
/// <summary> /// Invokes a cached delegate of Query. /// </summary> /// <param name="command">The command to execute.</param> /// <param name="queryReader">The reader to use to read the records.</param> /// <param name="outputParameters">An object to output parameters onto.</param> /// <returns>The result of the invocation.</returns> private static object CallQuery(IDbCommand command, IQueryReader queryReader, object outputParameters) { var method = _queryMethods.GetOrAdd( queryReader.ReturnType, t => (Func <IDbCommand, IQueryReader, CommandBehavior, object, object>)Delegate.CreateDelegate( typeof(Func <IDbCommand, IQueryReader, CommandBehavior, object, object>), typeof(DBCommandExtensions).GetMethod("QueryCoreUntyped", BindingFlags.NonPublic | BindingFlags.Static).MakeGenericMethod(t))); return(method(command, queryReader, CommandBehavior.Default, outputParameters)); }
/// <summary> /// Invokes a cached delegate of QueryAsync. /// </summary> /// <param name="command">The command to execute.</param> /// <param name="queryReader">The reader to use to read the records.</param> /// <param name="cancellationToken">The cancellation token to use for the operation.</param> /// <returns>The result of the invocation.</returns> private static object CallQueryAsync(IDbCommand command, IQueryReader queryReader, CancellationToken?cancellationToken) { var method = _queryAsyncMethods.GetOrAdd( queryReader.ReturnType, t => (Func <IDbCommand, IQueryReader, CommandBehavior, CancellationToken?, object, object>)Delegate.CreateDelegate( typeof(Func <IDbCommand, IQueryReader, CommandBehavior, CancellationToken?, object, object>), typeof(DBConnectionExtensions).GetMethod("QueryCoreAsyncUntyped", BindingFlags.NonPublic | BindingFlags.Static).MakeGenericMethod(t))); return(method(command, queryReader, CommandBehavior.Default, cancellationToken, null)); }
protected virtual void OnHydrate(ref T instance, IQueryReader reader) { var type = instance.GetType(); foreach (var field in Mapping.FieldMapping) { var prop = type.GetProperty(field.ObjectProperty); prop.SetValue(instance, reader[field.DbField], null); } }
public PerformanceTestState(IInterpreterRepository repository, ISpatialRepository spatialRepository, IQueryReader queryReader, ISymbolVisitor symbolVisitor, IExecutionVisitor executionVisitor, IFuncVisitor funcVisitor, ILogger logger, ISettings settings, string name) { Repository = repository; SpatialRepository = spatialRepository; this.queryReader = queryReader; this.symbolVisitor = symbolVisitor; this.executionVisitor = executionVisitor; this.funcVisitor = funcVisitor; this.logger = logger; this.settings = settings; this.Name = name; }
public ExecuteAllState(SymbolClearState clearState, IInterpreterRepository repository, ISpatialRepository spatialRepository, IQueryReader queryReader, ISymbolVisitor symbolVisitor, IExecutionVisitor executionVisitor, IFuncVisitor funcVisitor, string name, QueryShowState queryShowState, ResultShowState resultShowState) { Repository = repository; SpatialRepository = spatialRepository; this.clearState = clearState; this.queryReader = queryReader; this.symbolVisitor = symbolVisitor; this.executionVisitor = executionVisitor; this.funcVisitor = funcVisitor; this.queryShowState = queryShowState; this.resultShowState = resultShowState; Name = name; }
protected virtual void OnHydrate(ref T instance, IQueryReader reader) { var id = new Identifier(reader["id"]); instance.Key = id; var type = instance.GetType(); foreach (var field in Mapping.FieldMapping.Where(x => !x.PropertyFlags.HasFlag(FieldProperties.PrimaryKey))) { var prop = type.GetProperty(field.ObjectProperty); if (prop != null) { prop.SetValue(instance, reader[field.DbField], null); } } }
private static void ExecuteQuery(OleDbCommand ocmd, IQueryReader queryReader) { string connString = string.Format(ConnectionStringFormat, FileName); OleDbConnection mdbConn = new OleDbConnection(connString); try { mdbConn.Open(); ocmd.Connection = mdbConn; IDataReader reader = ocmd.ExecuteReader(); queryReader.ReadData(reader); } catch (Exception) { queryReader.SetFailure(); } finally { if (mdbConn.State != ConnectionState.Closed) { mdbConn.Close(); } } }
/// <summary> /// Compares the specified comparer. /// </summary> /// <param name="comparer">The comparer.</param> /// <param name="currentItem">The current item.</param> /// <returns></returns> private string Compare(DataComparer comparer, ValidationAction currentItem) { StringBuilder summary = new StringBuilder(); Console.WriteLine("Currently executing : " + currentItem.SourceQueryFileName); IQueryExecutor sqlQueryExecutor = QueryExecutorFactory.GetQueryExecutor(); IQueryReader qReader = QueryReaderFactory.GetQueryReader(); DataTable dt1 = new DataTable(); DataTable dt2 = new DataTable(); dt1 = sqlQueryExecutor.Execute(qReader.GetQuery(currentItem.SourceQueryFileName), DatabaseType.Source); dt2 = sqlQueryExecutor.Execute(qReader.GetQuery(currentItem.DestinationQueryFileName), DatabaseType.Destination); Stopwatch sw = new Stopwatch(); sw.Start(); comparer.Compare(dt1, dt2); sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds.ToString() + " ms"); Console.WriteLine("--------------------------------------------------------"); summary.AppendLine(currentItem.SourceQueryFileName); summary.Append("Number of records in Source table : ").Append(dt1.Rows.Count); summary.AppendLine(); summary.Append("Number of records in Destination table : ").Append(dt2.Rows.Count); summary.AppendLine(); summary.Append("Time taken : ").Append(sw.ElapsedMilliseconds); summary.AppendLine(); summary.AppendLine("-----------------------------------------------------------------"); return(summary.ToString()); }
public Context(IQueryReader queryReader, ISymbolVisitor symbolVisitor, IExecutionVisitor executionVisitor, IFuncVisitor funcVisitor, ISpatialRepository spatialRepository, IInterpreterRepository repository, ISettings settings, ILogger logger) { this.spatialRepository = spatialRepository; queryClearState = new QueryClearState() { Repository = repository, Name = "QueryClearState" }; queryAddState = new QueryAddState() { Repository = repository, Name = "QueryAddState" }; queryShowState = new QueryShowState(queryAddState) { Repository = repository, Name = "QueryShowState" }; queryAddState.ShowState = queryShowState; symbolShowState = new SymbolShowState(repository, "SymbolShowState"); symbolClearState = new SymbolClearState(repository, spatialRepository, symbolShowState, queryShowState, "SymbolClearState"); resultShowState = new ResultShowState(repository, queryShowState, "ResultShowState"); executeAllState = new ExecuteAllState(symbolClearState, repository, spatialRepository, queryReader, symbolVisitor, executionVisitor, funcVisitor, "ExecuteAllState", queryShowState, resultShowState); resultWriteState = new ResultWriteState(repository, "ResultWriteState", resultShowState); showSettingsState = new ShowSettingsState(settings, "ShowSettingsState"); showLicenceState = new ShowLicenceState(queryAddState, "ShowLicenceState"); performanceTestState = new PerformanceTestState(repository, spatialRepository, queryReader, symbolVisitor, executionVisitor, funcVisitor, logger, settings, "PerformanceTestState"); currentState = showLicenceState; currentState = currentState.Execute(""); }
/// <summary> /// Extends the reader by reading another set of records. /// </summary> /// <typeparam name="T1">The type of objects in the first set of results.</typeparam> /// <typeparam name="T2">The type of objects in the second set of results.</typeparam> /// <param name="previous">The previous reader.</param> /// <param name="recordReader">The mapping that defines the layout of the records in each row.</param> /// <returns>A reader that reads a Results object with multiple results.</returns> public static ResultsReader <T1, T2> Then <T1, T2>( this IQueryReader <Results <T1> > previous, IRecordReader <T2> recordReader) { return(new ResultsReader <T1, T2>(previous, recordReader)); }
public CategoryStorageProvider(IDatabaseConfiguration databaseConfiguration, IQueryReader queryReader, IQueryCommand queryCommand) : base(databaseConfiguration, queryReader, queryCommand) { }
protected internal object DoInvokeMember(InvokeMemberBinder binder, object[] args, Type returnType) { if (binder == null) { throw new ArgumentNullException("binder"); } if (args == null) { throw new ArgumentNullException("args"); } bool doAsync = false; IDbCommand cmd = null; int specialParameters = 0; int? timeout = null; IDbTransaction transaction = null; IQueryReader returns = null; object outputParameters = null; CancellationToken cancellationToken = CancellationToken.None; CallInfo callInfo = binder.CallInfo; int unnamedParameterCount = callInfo.ArgumentCount - callInfo.ArgumentNames.Count; // check the proc name - if it ends with Async, then call it asynchronously and return the results string procName = binder.Name; if (procName.EndsWith("async", StringComparison.OrdinalIgnoreCase)) { procName = procName.Substring(0, procName.Length - 5); doAsync = true; } // if there is a schema, use it if (!String.IsNullOrWhiteSpace(_schema)) { procName = _schema + "." + procName; } // go through the arguments and look for our special arguments // NOTE: this is intentionally case-sensitive so that you can use other cases if you need to pass a parameter by the same name. var argumentNames = callInfo.ArgumentNames; for (int i = 0; i < argumentNames.Count; i++) { switch (argumentNames[i]) { case "cancellationToken": cancellationToken = (CancellationToken)args[i + unnamedParameterCount]; specialParameters++; break; case "transaction": transaction = (IDbTransaction)args[i + unnamedParameterCount]; specialParameters++; break; case "commandTimeout": timeout = (int)args[i + unnamedParameterCount]; specialParameters++; break; case "returnType": returnType = (Type)args[i + unnamedParameterCount]; specialParameters++; break; case "returns": returns = (IQueryReader)args[i + unnamedParameterCount]; specialParameters++; break; case "outputParameters": outputParameters = args[i + unnamedParameterCount]; specialParameters++; break; #if !NOCOMPATIBILITY case "withGraph": { var withGraph = (Type)args[i + unnamedParameterCount]; dynamic graph = System.Activator.CreateInstance(withGraph); returns = graph.GetListReader(); specialParameters++; break; } case "withGraphs": { var types = (Type[])args[i + unnamedParameterCount]; returns = (IQueryReader)types[0].GetMethod("GetDefinitionFromGraphArray", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy).Invoke(null, new object[] { returnType, types }); specialParameters++; break; } #endif } } try { // if there is exactly one unnamed parameter, and the named parameters are all special parameters, and it's a reference type (and not a string) // then we will attempt to use the object's fields as the parameter values // this is so you can send an entire object to an insert method if (unnamedParameterCount == 1 && (callInfo.ArgumentNames.Count == specialParameters) && !args[0].GetType().IsValueType&& args[0].GetType() != typeof(String)) { cmd = _connection.CreateCommand(procName, args[0], CommandType.StoredProcedure, timeout, transaction); } else { // this isn't a single-object parameter, so we are going to map the parameters by position and by name // create a command cmd = _connection.CreateCommand(); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = procName; if (timeout.HasValue) { cmd.CommandTimeout = timeout.Value; } // unwrap the transaction because the transaction has to match the command and connection if (transaction != null) { cmd.Transaction = DBConnectionExtensions.UnwrapDbTransaction(transaction); } // fill in the parameters for the command object // we will do the values next DeriveParameters(cmd); // look at the unnamed parameters first. we will add them by position. var inputParameters = cmd.Parameters.OfType <IDataParameter>().Where(p => p.Direction.HasFlag(ParameterDirection.Input)).ToList(); for (int i = 0; i < unnamedParameterCount; i++) { inputParameters[i].Value = args[i]; } // go through all of the named arguments next. Note that they may overwrite indexed parameters. for (int i = unnamedParameterCount; i < callInfo.ArgumentNames.Count; i++) { string argumentName = callInfo.ArgumentNames[i]; // ignore our special parameters if (argumentName == "cancellationToken" || argumentName == "transaction" || argumentName == "commandTimeout" || argumentName == "returnType" || argumentName == "returns" || #if !NOCOMPATIBILITY argumentName == "withGraph" || argumentName == "withGraphs" || #endif argumentName == "outputParameters") { continue; } IDataParameter p = cmd.Parameters.OfType <IDataParameter>().First(parameter => String.Equals(parameter.ParameterName, argumentName, StringComparison.OrdinalIgnoreCase)); p.Value = args[i]; } // special handling for table parameters - replace them with list parameters // note that we may be modifying the parameters collection, so we copy the list here var provider = InsightDbProvider.For(cmd); foreach (var p in cmd.Parameters.OfType <IDataParameter>().Where(p => provider.IsTableValuedParameter(cmd, p)).ToList()) { // if any parameters are missing table parameters, then alert the developer if (p.Value == null) { throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "Table parameter {0} must be specified", p.ParameterName)); } // convert the value to an objectreader DbParameterGenerator.ListParameterHelper.ConvertListParameter(p, p.Value, cmd); } } // if we don't have a type, use FastExpando if (returnType == null) { returnType = typeof(FastExpando); } // if there was no named returns definition, check for an unnamed IQueryParameter if (returns == null) { returns = args.OfType <IQueryReader>().FirstOrDefault(); } // if there is no returns definition supplied, get one from the return type if (returns == null) { if (returnType.IsSubclassOf(typeof(Results))) { returns = (IQueryReader)returnType.GetMethod("GetReader", BindingFlags.Public | BindingFlags.Static).Invoke(null, Parameters.EmptyArray); } else { returns = (IQueryReader)typeof(ListReader <>).MakeGenericType(returnType).GetField("Default", BindingFlags.Static | BindingFlags.Public).GetValue(null); } } // get the proper query method to call based on whether we are doing this async and whether there is a single or multiple result set // the nice thing is that the generic expansion will automatically create the proper return type like IList<T> or Results<T>. if (doAsync) { return(CallQueryAsync(cmd, returns, cancellationToken)); } else { return(CallQuery(cmd, returns, outputParameters)); } } finally { if (cmd != null) { cmd.Dispose(); } } }
/// <summary> /// Extends the reader by reading another set of records. /// </summary> /// <typeparam name="T1">The type of objects in the first set of results.</typeparam> /// <typeparam name="T2">The type of objects in the second set of results.</typeparam> /// <typeparam name="T3">The type of objects in the third set of results.</typeparam> /// <typeparam name="T4">The type of objects in the fourth set of results.</typeparam> /// <typeparam name="T5">The type of objects in the fifth set of results.</typeparam> /// <typeparam name="T6">The type of objects in the sixth set of results.</typeparam> /// <typeparam name="T7">The type of objects in the seventh set of results.</typeparam> /// <typeparam name="T8">The type of objects in the eighth set of results.</typeparam> /// <typeparam name="T9">The type of objects in the nineth set of results.</typeparam> /// <typeparam name="T10">The type of objects in the tenth set of results.</typeparam> /// <typeparam name="T11">The type of objects in the eleventh set of results.</typeparam> /// <typeparam name="T12">The type of objects in the twelfth set of results.</typeparam> /// <typeparam name="T13">The type of objects in the thirteenth set of results.</typeparam> /// <typeparam name="T14">The type of objects in the fourteenth set of results.</typeparam> /// <typeparam name="T15">The type of objects in the fifteenth set of results.</typeparam> /// <typeparam name="T16">The type of objects in the sixteenth set of results.</typeparam> /// <param name="previous">The previous reader.</param> /// <param name="recordReader">The mapping that defines the layout of the records in each row.</param> /// <returns>A reader that reads a Results object with multiple results.</returns> public static ResultsReader <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16> Then <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16>( this IQueryReader <Results <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15> > previous, IRecordReader <T16> recordReader) { return(new ResultsReader <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16>(previous, recordReader)); }
/// <summary> /// Extends the reader by reading another set of records. /// </summary> /// <typeparam name="T1">The type of objects in the first set of results.</typeparam> /// <typeparam name="T2">The type of objects in the second set of results.</typeparam> /// <typeparam name="T3">The type of objects in the third set of results.</typeparam> /// <typeparam name="T4">The type of objects in the fourth set of results.</typeparam> /// <typeparam name="T5">The type of objects in the fifth set of results.</typeparam> /// <typeparam name="T6">The type of objects in the sixth set of results.</typeparam> /// <param name="previous">The previous reader.</param> /// <param name="recordReader">The mapping that defines the layout of the records in each row.</param> /// <returns>A reader that reads a Results object with multiple results.</returns> public static ResultsReader <T1, T2, T3, T4, T5, T6> Then <T1, T2, T3, T4, T5, T6>( this IQueryReader <Results <T1, T2, T3, T4, T5> > previous, IRecordReader <T6> recordReader) { return(new ResultsReader <T1, T2, T3, T4, T5, T6>(previous, recordReader)); }
public SQLiteProviderBase(IDatabaseConfiguration databaseConfiguration, IQueryReader queryReader, IQueryCommand queryCommand) { this.databaseConfiguration = databaseConfiguration; this.queryReader = queryReader; this.queryCommand = queryCommand; }
public CustomerController(IStories stories , IQueryReader <CustomerViewModel> customers) { _stories = stories; _customers = customers; }
public async Task <T> QueryAsync <T>(string sql, object parameters, IQueryReader <T> returns, CommandType commandType = CommandType.StoredProcedure, CommandBehavior commandBehavior = CommandBehavior.Default, int?commandTimeout = default(int?), IDbTransaction transaction = null, CancellationToken?cancellationToken = default(CancellationToken?), object outputParameters = null) { return(await _connection.QueryAsync(sql, parameters, returns, commandType, commandBehavior, commandTimeout, transaction, cancellationToken, outputParameters)); }
private void Hydrate(ref T instance, IQueryReader reader) { OnHydrate(ref instance, reader); }