コード例 #1
0
ファイル: CouchContext.cs プロジェクト: cole124/couchdb-net
        protected CouchContext(CouchOptions options)
        {
            Check.NotNull(options, nameof(options));

            var optionsBuilder  = new CouchOptionsBuilder(options);
            var databaseBuilder = new CouchDatabaseBuilder();

#pragma warning disable CA2214 // Do not call overridable methods in constructors
            OnConfiguring(optionsBuilder);
            OnDatabaseCreating(databaseBuilder);
#pragma warning restore CA2214 // Do not call overridable methods in constructors

            Client = new CouchClient(options);
            IEnumerable <PropertyInfo> databasePropertyInfos = GetDatabaseProperties();

            foreach (PropertyInfo dbProperty in databasePropertyInfos)
            {
                Type documentType = dbProperty.PropertyType.GetGenericArguments()[0];

                var initDatabasesTask = (Task)InitDatabasesGenericMethod.MakeGenericMethod(documentType)
                                        .Invoke(this, new object[] { dbProperty, options });
                initDatabasesTask.ConfigureAwait(false).GetAwaiter().GetResult();

                var applyDatabaseChangesTask = (Task)ApplyDatabaseChangesGenericMethod.MakeGenericMethod(documentType)
                                               .Invoke(this, new object[] { dbProperty, options, databaseBuilder });
                applyDatabaseChangesTask.ConfigureAwait(false).GetAwaiter().GetResult();
            }
        }
コード例 #2
0
ファイル: CouchClient.cs プロジェクト: panoukos41/couchdb-net
        /// <summary>
        /// Creates a new CouchDB client.
        /// </summary>
        /// <param name="endpoint">URI to the CouchDB endpoint.</param>
        /// <param name="couchSettingsFunc">A function to configure options.</param>
        public CouchClient(Uri endpoint, Action <CouchOptionsBuilder>?couchSettingsFunc = null)
        {
            var optionsBuilder = new CouchOptionsBuilder();

            couchSettingsFunc?.Invoke(optionsBuilder);
            _options     = optionsBuilder.Options;
            Endpoint     = endpoint;
            _flurlClient = GetConfiguredClient();
        }
コード例 #3
0
ファイル: CouchContext.cs プロジェクト: cole124/couchdb-net
        private async Task InitDatabasesAsync <TSource>(PropertyInfo propertyInfo, CouchOptions options)
            where TSource : CouchDocument
        {
            ICouchDatabase <TSource> database = options.CheckDatabaseExists
                ? await Client.GetOrCreateDatabaseAsync <TSource>().ConfigureAwait(false)
                : Client.GetDatabase <TSource>();

            propertyInfo.SetValue(this, database);
        }
コード例 #4
0
ファイル: CouchClient.cs プロジェクト: panoukos41/couchdb-net
        internal CouchClient(CouchOptions options)
        {
            if (options.Endpoint == null)
            {
                throw new InvalidOperationException("Database endpoint must be set.");
            }

            _options     = options;
            Endpoint     = _options.Endpoint;
            _flurlClient = GetConfiguredClient();
        }
コード例 #5
0
        private void InitializeDatabases(CouchOptions options, CouchDatabaseBuilder databaseBuilder)
        {
            foreach (PropertyInfo dbProperty in GetDatabaseProperties())
            {
                Type documentType = dbProperty.PropertyType.GetGenericArguments()[0];

                var initDatabasesTask = (Task)InitDatabasesGenericMethod.MakeGenericMethod(documentType)
                                        .Invoke(this, new object[] { dbProperty, options, databaseBuilder });
                initDatabasesTask.ConfigureAwait(false).GetAwaiter().GetResult();

                var applyDatabaseChangesTask = (Task)ApplyDatabaseChangesGenericMethod.MakeGenericMethod(documentType)
                                               .Invoke(this, new object[] { dbProperty, options, databaseBuilder });
                applyDatabaseChangesTask.ConfigureAwait(false).GetAwaiter().GetResult();
            }
        }
コード例 #6
0
ファイル: CouchClient.cs プロジェクト: panoukos41/couchdb-net
        /// <summary>
        /// Creates a new CouchDB client.
        /// </summary>
        /// <param name="couchSettingsFunc">A function to configure options.</param>
        public CouchClient(Action <CouchOptionsBuilder>?couchSettingsFunc = null)
        {
            var optionsBuilder = new CouchOptionsBuilder();

            couchSettingsFunc?.Invoke(optionsBuilder);

            if (optionsBuilder.Options.Endpoint == null)
            {
                throw new InvalidOperationException("Database endpoint must be set.");
            }

            _options     = optionsBuilder.Options;
            Endpoint     = _options.Endpoint;
            _flurlClient = GetConfiguredClient();
        }
コード例 #7
0
ファイル: CouchDatabase.cs プロジェクト: cole124/couchdb-net
        internal CouchDatabase(IFlurlClient flurlClient, CouchOptions options, QueryContext queryContext)
        {
            _flurlClient  = flurlClient;
            _options      = options;
            _queryContext = queryContext;

            var queryOptimizer  = new QueryOptimizer();
            var queryTranslator = new QueryTranslator(options);
            var querySender     = new QuerySender(flurlClient, queryContext);
            var queryCompiler   = new QueryCompiler(queryOptimizer, queryTranslator, querySender);

            _queryProvider = new CouchQueryProvider(queryCompiler);

            Security       = new CouchSecurity(NewRequest);
            LocalDocuments = new LocalDocuments(flurlClient, queryContext);
        }
コード例 #8
0
        protected CouchContext(CouchOptions options)
        {
            Check.NotNull(options, nameof(options));

            var optionsBuilder  = new CouchOptionsBuilder(options);
            var databaseBuilder = new CouchDatabaseBuilder();

#pragma warning disable CA2214 // Do not call overridable methods in constructors
            OnConfiguring(optionsBuilder);
            OnDatabaseCreating(databaseBuilder);
#pragma warning restore CA2214 // Do not call overridable methods in constructors

            Client = new CouchClient(options);

            SetupDiscriminators(databaseBuilder);
            InitializeDatabases(options, databaseBuilder);
        }
コード例 #9
0
        internal CouchDatabase(IFlurlClient flurlClient, CouchOptions options, QueryContext queryContext, string?discriminator)
        {
            _feedChangeLineStartPattern = new Regex(@"{""seq");
            _flurlClient   = flurlClient;
            _options       = options;
            _queryContext  = queryContext;
            _discriminator = discriminator;

            var queryOptimizer  = new QueryOptimizer();
            var queryTranslator = new QueryTranslator(options);
            var querySender     = new QuerySender(flurlClient, queryContext);
            var queryCompiler   = new QueryCompiler(queryOptimizer, queryTranslator, querySender, _discriminator);

            _queryProvider = new CouchQueryProvider(queryCompiler);

            Security       = new CouchSecurity(NewRequest);
            LocalDocuments = new LocalDocuments(flurlClient, queryContext);
        }
コード例 #10
0
        public static string GetName(this Type t, CouchOptions options)
        {
            var jsonObjectAttributes       = t.GetCustomAttributes(typeof(JsonObjectAttribute), true);
            JsonObjectAttribute?jsonObject = jsonObjectAttributes.Length > 0
                ? jsonObjectAttributes[0] as JsonObjectAttribute
                : null;

            if (jsonObject != null)
            {
                return(jsonObject.Id);
            }

            var typeName = t.Name;

            if (options.PluralizeEntities)
            {
                typeName = typeName.Pluralize();
            }
            return(options.DocumentsCaseType.Convert(typeName));
        }
コード例 #11
0
        public static string GetPropertyName(this MemberExpression m, CouchOptions options)
        {
            PropertyCaseType caseType = options.PropertiesCase;

            var members = new List <string> {
                m.Member.GetCouchPropertyName(caseType)
            };

            Expression currentExpression = m.Expression;

            while (currentExpression is MemberExpression cm)
            {
                members.Add(cm.Member.GetCouchPropertyName(caseType));
                currentExpression = cm.Expression;
            }

            members.Reverse();
            var propName = string.Join(".", members.ToArray());

            return(propName);
        }
コード例 #12
0
        private static async Task TryCreateOrUpdateIndexAsync <TSource>(
            CouchOptions options,
            IEnumerable <IndexInfo> indexes,
            IndexSetupDefinition <TSource> indexSetup,
            CouchDatabase <TSource> database)
            where TSource : CouchDocument
        {
            IndexInfo?currentIndex = TryFindIndex(
                indexes,
                indexSetup.Name,
                indexSetup.Options?.DesignDocument);

            if (currentIndex == null)
            {
                await database.CreateIndexAsync(
                    indexSetup.Name,
                    indexSetup.IndexBuilderAction,
                    indexSetup.Options)
                .ConfigureAwait(false);

                return;
            }

            if (!options.OverrideExistingIndexes)
            {
                return;
            }

            IndexDefinition indexDefinition = database.NewIndexBuilder(indexSetup.IndexBuilderAction).Build();

            if (!AreFieldsEqual(currentIndex.Fields, indexDefinition.Fields))
            {
                await database.DeleteIndexAsync(currentIndex)
                .ConfigureAwait(false);

                await database.CreateIndexAsync(indexSetup.Name, indexDefinition, indexSetup.Options)
                .ConfigureAwait(false);
            }
        }
コード例 #13
0
        private async Task InitDatabaseAsync <TSource>(PropertyInfo propertyInfo, CouchOptions options, CouchDatabaseBuilder databaseBuilder)
            where TSource : CouchDocument
        {
            ICouchDatabase <TSource> database;
            Type documentType = typeof(TSource);

            if (databaseBuilder.DocumentBuilders.ContainsKey(documentType))
            {
                var documentBuilder = (CouchDocumentBuilder <TSource>)databaseBuilder.DocumentBuilders[documentType];
                var databaseName    = documentBuilder.Database ?? Client.GetClassName(documentType);
                database = options.CheckDatabaseExists
                    ? await Client.GetOrCreateDatabaseAsync <TSource>(databaseName, documentBuilder.Shards, documentBuilder.Replicas, documentBuilder.Partitioned, documentBuilder.Discriminator).ConfigureAwait(false)
                    : Client.GetDatabase <TSource>(databaseName, documentBuilder.Discriminator);
            }
            else
            {
                database = options.CheckDatabaseExists
                    ? await Client.GetOrCreateDatabaseAsync <TSource>().ConfigureAwait(false)
                    : Client.GetDatabase <TSource>();
            }

            propertyInfo.SetValue(this, database);
        }
コード例 #14
0
        protected CouchContext(CouchOptions options)
        {
            Check.NotNull(options, nameof(options));

            var builder = new CouchOptionsBuilder(options);

#pragma warning disable CA2214 // Do not call overridable methods in constructors
            OnConfiguring(builder);
#pragma warning restore CA2214 // Do not call overridable methods in constructors

            Client = new CouchClient(options);

            PropertyInfo[] databasePropertyInfos = GetDatabaseProperties();

            foreach (PropertyInfo propertyInfo in databasePropertyInfos)
            {
                Type   documentType = propertyInfo.PropertyType.GetGenericArguments()[0];
                object?database;
                if (options.CheckDatabaseExists)
                {
                    MethodInfo getOrCreateDatabaseMethod = GetOrCreateDatabaseAsyncGenericMethod.MakeGenericMethod(documentType);
#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
                    var parameters = new[] { (object)null, null, default(CancellationToken) };
#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type.
                    var task = (Task)getOrCreateDatabaseMethod.Invoke(Client, parameters);
                    task.ConfigureAwait(false).GetAwaiter().GetResult();
                    PropertyInfo resultProperty = task.GetType().GetProperty(nameof(Task <object> .Result));
                    database = resultProperty.GetValue(task);
                }
                else
                {
                    MethodInfo getDatabaseMethod = GetDatabaseGenericMethod.MakeGenericMethod(documentType);
                    database = getDatabaseMethod.Invoke(Client, Array.Empty <object>());
                }
                propertyInfo.SetValue(this, database);
            }
        }
コード例 #15
0
        public static async Task <Stream> QueryContinuousWithFilterAsync <TSource>(this IFlurlRequest request, CouchOptions options, ChangesFeedFilter filter, CancellationToken cancellationToken)
            where TSource : CouchDocument
        {
            if (filter is DocumentIdsChangesFeedFilter documentIdsFilter)
            {
                return(await request
                       .SetQueryParam("filter", "_doc_ids")
                       .PostJsonStreamAsync(new ChangesFeedFilterDocuments(documentIdsFilter.Value), cancellationToken, HttpCompletionOption.ResponseHeadersRead)
                       .ConfigureAwait(false));
            }

            if (filter is SelectorChangesFeedFilter <TSource> selectorFilter)
            {
                MethodCallExpression whereExpression = Expression.Call(typeof(Queryable), nameof(Queryable.Where),
                                                                       new[] { typeof(TSource) }, Expression.Constant(Array.Empty <TSource>().AsQueryable()), selectorFilter.Value);

                var        optimizer      = new QueryOptimizer();
                Expression optimizedQuery = optimizer.Optimize(whereExpression);
                var        jsonSelector   = new QueryTranslator(options).Translate(optimizedQuery);
                return(await request
                       .WithHeader("Content-Type", "application/json")
                       .SetQueryParam("filter", "_selector")
                       .PostStringStreamAsync(jsonSelector, cancellationToken, HttpCompletionOption.ResponseHeadersRead)
                       .ConfigureAwait(false));
            }

            if (filter is DesignChangesFeedFilter)
            {
                return(await request
                       .SetQueryParam("filter", "_design")
                       .GetStreamAsync(cancellationToken, HttpCompletionOption.ResponseHeadersRead)
                       .ConfigureAwait(false));
            }

            if (filter is ViewChangesFeedFilter viewFilter)
            {
                return(await request
                       .SetQueryParam("filter", "_view")
                       .SetQueryParam("view", viewFilter.Value)
                       .GetStreamAsync(cancellationToken, HttpCompletionOption.ResponseHeadersRead)
                       .ConfigureAwait(false));
            }
            throw new InvalidOperationException($"Filter of type {filter.GetType().Name} not supported.");
        }
コード例 #16
0
 public DataContext(CouchOptions <DataContext> options) : base(options)
 {
 }
コード例 #17
0
 internal QueryTranslator(CouchOptions options)
 {
     _sb      = new StringBuilder();
     _options = options;
 }
コード例 #18
0
        private async Task ApplyDatabaseChangesAsync <TSource>(PropertyInfo propertyInfo, CouchOptions options, CouchDatabaseBuilder databaseBuilder)
            where TSource : CouchDocument
        {
            Type documentType = typeof(TSource);

            if (!databaseBuilder.DocumentBuilders.ContainsKey(documentType))
            {
                return;
            }

            var database        = (CouchDatabase <TSource>)propertyInfo.GetValue(this);
            var documentBuilder = (CouchDocumentBuilder <TSource>)databaseBuilder.DocumentBuilders[documentType];

            if (!documentBuilder.IndexDefinitions.Any())
            {
                return;
            }

            List <IndexInfo> indexes = await database.GetIndexesAsync().ConfigureAwait(false);

            foreach (IndexSetupDefinition <TSource> indexSetup in documentBuilder.IndexDefinitions)
            {
                await TryCreateOrUpdateIndexAsync(options, indexes, indexSetup, database)
                .ConfigureAwait(false);
            }
        }
コード例 #19
0
 public StellarPointerContext(CouchOptions <StellarPointerContext> couchOptions) : base(couchOptions)
 {
 }
コード例 #20
0
 public IndexBuilder(CouchOptions options, IAsyncQueryProvider queryProvider)
 {
     _options       = options;
     _queryProvider = queryProvider;
     _fields        = new List <string>();
 }
コード例 #21
0
 public MyDeathStarContext(CouchOptions<MyDeathStarContext> options)
     : base(options)
 {
     SeedDataAsync().GetAwaiter().GetResult();
 }