Exemplo n.º 1
0
        public ExcelQueryExecutor(ExcelQueryArgs args)
        {
            ValidateArgs(args);
            _args = args;

            if (_log.IsDebugEnabled)
            {
                _log.DebugFormat("Connection String: {0}", ExcelUtilities.GetConnection(args).ConnectionString);
            }

            GetWorksheetName();
        }
Exemplo n.º 2
0
        private bool CheckIfInvalidColumnNameUsed(SqlParts sql)
        {
            var usedColumns  = sql.ColumnNamesUsed;
            var tableColumns = ExcelUtilities.GetColumnNames(_args.WorksheetName, _args.NamedRangeName, _args.FileName);

            foreach (var column in usedColumns)
            {
                if (!tableColumns.Contains(column))
                {
                    throw new DataException(string.Format(
                                                "'{0}' is not a valid column name. " +
                                                "Valid column names are: '{1}'",
                                                column,
                                                string.Join("', '", tableColumns.ToArray())));
                }
            }
            return(false);
        }
Exemplo n.º 3
0
 private void GetWorksheetName()
 {
     if (_args.FileName.ToLower().EndsWith("csv"))
     {
         _args.WorksheetName = Path.GetFileName(_args.FileName);
     }
     else if (_args.WorksheetIndex.HasValue)
     {
         var worksheetNames = ExcelUtilities.GetWorksheetNames(_args);
         if (_args.WorksheetIndex.Value < worksheetNames.Count())
         {
             _args.WorksheetName = worksheetNames.ElementAt(_args.WorksheetIndex.Value);
         }
         else
         {
             throw new DataException("Worksheet Index Out of Range");
         }
     }
     else if (String.IsNullOrEmpty(_args.WorksheetName) && String.IsNullOrEmpty(_args.NamedRangeName))
     {
         _args.WorksheetName = "Sheet1";
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// Executes the sql query and returns the data results
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="queryModel">Linq query model</param>
        protected IEnumerable <object> GetDataResults(SqlParts sql, QueryModel queryModel)
        {
            IEnumerable <object> results;
            OleDbDataReader      data = null;

            var conn    = ExcelUtilities.GetConnection(_args);
            var command = conn.CreateCommand();

            try
            {
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }

                command.CommandText = sql.ToString();
                command.Parameters.AddRange(sql.Parameters.ToArray());
                try { data = command.ExecuteReader(); }
                catch (OleDbException e)
                {
                    if (e.Message.Contains(_args.WorksheetName))
                    {
                        throw new DataException(
                                  string.Format(
                                      "'{0}' is not a valid worksheet name in file {3}. Valid worksheet names are: '{1}'. Error received: {2}",
                                      _args.WorksheetName,
                                      string.Join("', '", ExcelUtilities.GetWorksheetNames(_args.FileName).ToArray()),
                                      e.Message, _args.FileName), e);
                    }
                    if (!CheckIfInvalidColumnNameUsed(sql))
                    {
                        throw;
                    }
                }

                var columns = ExcelUtilities.GetColumnNames(data);
                LogColumnMappingWarnings(columns);
                if (columns.Count() == 1 && columns.First() == "Expr1000")
                {
                    results = GetScalarResults(data);
                }
                else if (queryModel.MainFromClause.ItemType == typeof(ExcelRow))
                {
                    results = GetRowResults(data, columns);
                }
                else if (queryModel.MainFromClause.ItemType == typeof(ExcelRowNoHeader))
                {
                    results = GetRowNoHeaderResults(data);
                }
                else
                {
                    results = GetTypeResults(data, columns, queryModel);
                }
            }
            finally
            {
                command.Dispose();

                if (!_args.UsePersistentConnection)
                {
                    conn.Dispose();
                    _args.PersistentConnection = null;
                }
            }

            return(results);
        }