Exemple #1
0
        public bool Any(Expression <Func <TModel, object> > specifierExpression)
        {
            var filterExpressions = new List <DbQueryWhereClause <TModel> >()
            {
                new DbQueryWhereClause <TModel>()
                {
                    Clause   = specifierExpression,
                    Operator = DbQueryConditionOperators.And
                }
            };

            var strategy = _queryStrategyProvider.GetSelectAggregationStrategy(
                _queryTable.ModelStrategy,
                DbQueryAggregateMode.Any,
                filterExpressions);
            var sql      = strategy.GetDbQueryScript();
            var dbparams = strategy.GetDbParameters().ToList();

            _serviceProvider.Open();

            IDataReader reader = null;
            bool        isAny;

            try
            {
                reader = _serviceProvider.QueryGetReader(sql, dbparams);
                isAny  = reader.Read();
            }
            catch (Exception exception)
            {
                throw new DbOperationException("ERROR_SQL_EXECUTION_FAILED", exception, sql, dbparams);
            }
            finally
            {
                reader?.Close();

                _serviceProvider.Close();
            }

            return(isAny);
        }
Exemple #2
0
        public IEnumerable <dynamic> ExecuteQuery(string sql, object queryParams = null)
        {
            var autoClose = !(_provider.IsConnectionOpen());

            if (autoClose)
            {
                _provider.Open();
            }

            var dbParams = GetQueryDataParameters(queryParams);
            var reader   = _provider.QueryGetReader(sql, dbParams);
            var records  = DbModelBindingHelper.GetDataRecords(reader);
            var result   = FormatDataRecordToDynamicModel(records);

            reader.Close();

            if (autoClose)
            {
                _provider.Close();
            }

            return(result);
        }
Exemple #3
0
        public IEnumerable <DbTableDescriptor> EnumerateDbTableDescriptors()
        {
            var strategy = _container.Resolve <IDbTableInspectorStrategy>();

            if (strategy == null)
            {
                throw new ArgumentNullException("EnumerateTableDescriptorStrategyNotFound");
            }

            var reader       = _provider.QueryGetReader(strategy.GetDbQueryScript(), strategy.GetDbParameters());
            var dbTypeFinder = _container.Resolve <IDbTypeFinder>();
            List <DbTableDescriptor> tables = new List <DbTableDescriptor>();
            DbTableDescriptor        table  = null;

            while (reader.Read())
            {
                string tableName = reader.GetString(0);
                string tableDesc = reader.GetString(1);

                // detect table property.
                if (table == null)
                {
                    table                 = new DbTableDescriptor();
                    table.TableName       = tableName;
                    table.LegalEntityName = tableName.Replace(" ", "_");
                    table.Columns         = new List <DbColumnDescriptor>();
                    table.Description     = tableDesc;
                }
                else if (string.Compare(table.TableName, tableName, StringComparison.InvariantCultureIgnoreCase) != 0)
                {
                    // add table into tables.
                    tables.Add(table);
                    // create new instance for next table.
                    table                 = new DbTableDescriptor();
                    table.Columns         = new List <DbColumnDescriptor>();
                    table.TableName       = tableName;
                    table.LegalEntityName = tableName.Replace(" ", "_");
                    table.Description     = tableDesc;
                }

                // load column information.
                string columnName   = reader.GetString(2);
                int    dbTypeNumber = reader.GetInt32(3);
                int    length       = Convert.ToInt32(reader.GetValue(5));
                bool   isNullable   = reader.GetBoolean(6);
                bool   isIdentity   = reader.GetBoolean(7);
                bool   isComputed   = reader.GetBoolean(8);
                bool   isPrimaryKey = Convert.ToBoolean(reader.GetValue(10));
                string columnDesc   = reader.GetString(11);
                DbType dbType;

                if (!dbTypeFinder.FindByTypeNumber(dbTypeNumber, false, out dbType))
                {
                    throw new InvalidOperationException("InvalidDbTypeNumber:" + dbTypeNumber.ToString());
                }

                // workaround: ignore column had been added.
                if (!table.Columns.Where(c => c.ColumnName == columnName).Any())
                {
                    table.Columns.Add(new DbColumnDescriptor()
                    {
                        ColumnDbType    = dbType,
                        ColumnName      = columnName,
                        Description     = columnDesc,
                        LegalColumnName = columnName.Replace(" ", "_"),
                        IsComputed      = isComputed,
                        IsIdentity      = isIdentity,
                        IsPrimaryKey    = isPrimaryKey,
                        IsNullable      = isNullable,
                        Length          = length
                    });
                }
            }

            reader.Close();

            return(tables);
        }