Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="JsonInsert{T}"/> class.
        /// </summary>
        /// <param name="databaseContext">
        /// The database context.
        /// </param>
        public JsonInsert([NotNull] IDatabaseContext databaseContext)
            : base(Singleton <JsonCommandFactory> .Instance, databaseContext)
        {
            if (databaseContext == null)
            {
                throw new ArgumentNullException(nameof(databaseContext));
            }

            this.nameQualifier = this.DatabaseContext.RepositoryAdapter.NameQualifier;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// The mock command provider.
        /// </summary>
        /// <param name="items">
        /// The merge items.
        /// </param>
        /// <param name="definitionProvider">
        /// The definition provider.
        /// </param>
        /// <param name="nameQualifier">
        /// The name qualifier.
        /// </param>
        /// <typeparam name="T">
        /// The type of item in the list to create an <see cref="IDbTableCommandFactory"/> mock for.
        /// </typeparam>
        /// <returns>
        /// The <see cref="Mock"/>.
        /// </returns>
        public static Mock <IDbTableCommandFactory> MockCommandFactory <T>(
            [NotNull] this IReadOnlyCollection <T> items,
            [NotNull] IEntityDefinitionProvider definitionProvider,
            [NotNull] INameQualifier nameQualifier)
        {
            if (items == null)
            {
                throw new ArgumentNullException(nameof(items));
            }

            if (definitionProvider == null)
            {
                throw new ArgumentNullException(nameof(definitionProvider));
            }

            if (nameQualifier == null)
            {
                throw new ArgumentNullException(nameof(nameQualifier));
            }

            var structureDefinition = definitionProvider.Resolve <T>();
            var orderedAttributes   = structureDefinition.ReturnableAttributes.OrderBy(definition => definition.Ordinal).ToList();

            var reader = items.MockDataReaderForList(orderedAttributes);

            var command = new Mock <IDbCommand>();

            command.Setup(dbCommand => dbCommand.ExecuteReader()).Returns(reader.Object);

            var commandProvider   = new Mock <IDbTableCommandFactory>();
            var databaseContext   = new Mock <IDatabaseContext>();
            var repositoryAdapter = new Mock <IRepositoryAdapter>();

            repositoryAdapter.Setup(adapter => adapter.NameQualifier).Returns(nameQualifier);
            repositoryAdapter.Setup(adapter => adapter.DefinitionProvider).Returns(definitionProvider);
            databaseContext.Setup(context => context.RepositoryAdapter).Returns(repositoryAdapter.Object);
            ////commandProvider.Setup(provider => provider.DatabaseContext).Returns(databaseContext.Object);
            ////commandProvider
            ////    .Setup(provider => provider.Create(It.IsAny<ITableCommand>(), It.IsAny<DataTable>(), It.IsAny<IDbTransaction>()))
            ////    .Returns(command.Object);

            commandProvider
            .Setup(provider => provider.Create(It.IsAny <IDatabaseContext>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <IEnumerable <T> >()))
            .Returns(command.Object);

            return(commandProvider);
        }
Ejemplo n.º 3
0
        public SqlAnalyzer(string connectionString, INameQualifier nameQualifier = null, TimeSpan cacheDuration = default(TimeSpan))
        {
            nameQualifier = nameQualifier ?? new SqlNameQualifier();

            if(cacheDuration == default(TimeSpan))
                cacheDuration = _defaultCacheDuration();

            _connectionString = connectionString;
            NameQualifier = nameQualifier;

            // We use function references instead of directly exposing the functions so
            // that we can build in caching without much work.
            // We'll do caching using Memoize - it'll cache the results of the function
            // for as long as cacheDuration.
            // Also, in some subclasses (ie, SqlMetaAnalyzer), we implement a different
            // caching scheme - using these function references lets us do that without
            // having to change anything here.

            // Why didn't we just do this through polymorphism, you ask?
            // Well, if we did, we wouldn't be able to compose our functions easily - we can't
            // override a function and just say "hey, use a memoized version of this function instead
            // of the base-class version" - we'd have to implement memoization from scratch in each
            // method. That's just a silly waste of time. Also, when we subclass, we'd have to implement
            // all of the memoization and cache invalidation we do in SqlMetaAnalyzer for each of these
            // methods again! Polymorphism doesn't allow us to do any manipulation of our functions - all you
            // can do is reimplement them, you can't get at the underlying binding and change it. That is to say,
            // there's no way using override to say "replace this method with a memoized version of it" - all you
            // can do is implement the guts of memoize inside your function, and repeat it for every function
            // you want to do the same thing to.
            var schemaEvents = Observable.FromEvent<string>(d => OnInvalidateSchemaCache += d, d => OnInvalidateSchemaCache -= d);
            var tableEvents = Observable.FromEvent<string>(d => OnInvalidateTableCache += d, d => OnInvalidateTableCache -= d);

            _getAutonumberKey = new Func<string, string>(_getAutonumberKeyFromDB).Memoize(cacheDuration, tableEvents);
            _getFieldTypes = new Func<string, Dictionary<string,SqlColumnType>>(_getFieldTypesFromDB).Memoize(cacheDuration, tableEvents);
            _getPrimaryKeys = new Func<string, ICollection<string>>(_getPrimaryKeysFromDB).Memoize(cacheDuration, tableEvents);
            _tableExists = new Func<string, bool>(_tableExistsFromDB).Memoize(cacheDuration, tableEvents);
            _schemaExists = new Func<string, bool>(_schemaExistsFromDb).Memoize(cacheDuration, schemaEvents);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="JoinClause"/> class.
 /// </summary>
 /// <param name="definitionProvider">
 /// The definition provider.
 /// </param>
 /// <param name="nameQualifier">
 /// The SQL name qualifier.
 /// </param>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="definitionProvider"/> or <paramref name="nameQualifier"/> is null.
 /// </exception>
 public JoinClause([NotNull] IEntityDefinitionProvider definitionProvider, [NotNull] INameQualifier nameQualifier)
 {
     this.definitionProvider = definitionProvider ?? throw new ArgumentNullException(nameof(definitionProvider));
     this.nameQualifier      = nameQualifier ?? throw new ArgumentNullException(nameof(nameQualifier));
 }