コード例 #1
0
        internal SqlGeneratorQueryModelVisitor(ExcelQueryArgs args)
        {
            _args              = args;
            SqlStatement       = new SqlParts();
            SqlStatement.Table = (String.IsNullOrEmpty(_args.StartRange)) ?
                                 string.Format("[{0}$]",
                                               _args.WorksheetName) :
                                 string.Format("[{0}${1}:{2}]",
                                               _args.WorksheetName, _args.StartRange, _args.EndRange);

            if (_args.WorksheetName.ToLower().EndsWith(".csv"))
            {
                SqlStatement.Table = SqlStatement.Table.Replace("$]", "]");
            }
        }
コード例 #2
0
        private bool CheckIfInvalidColumnNameUsed(SqlParts sql)
        {
            var usedColumns  = sql.ColumnNamesUsed;
            var tableColumns = ExcelUtilities.GetColumnNames(_args.WorksheetName, _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);
        }
コード例 #3
0
        private void LogSqlStatement(SqlParts sqlParts)
        {
            if (_log.IsDebugEnabled)
            {
                var logMessage = new StringBuilder();
                logMessage.AppendFormat("{0};", sqlParts.ToString());
                for (var i = 0; i < sqlParts.Parameters.Count(); i++)
                {
                    var paramValue   = sqlParts.Parameters.ElementAt(i).Value.ToString();
                    var paramMessage = string.Format(" p{0} = '{1}';",
                                                     i, sqlParts.Parameters.ElementAt(i).Value.ToString());

                    if (paramValue.IsNumber())
                    {
                        paramMessage = paramMessage.Replace("'", "");
                    }
                    logMessage.Append(paramMessage);
                }

                var sqlLog = LogManager.GetLogger("LinqToExcel.SQL");
                sqlLog.Debug(logMessage.ToString());
            }
        }
コード例 #4
0
        /// <summary>
        /// Executes the sql query and returns the data results
        /// </summary>
        /// <typeparam name="T">Data type in the main from clause (queryModel.MainFromClause.ItemType)</typeparam>
        /// <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. Valid worksheet names are: '{1}'",
                                                _args.WorksheetName, string.Join("', '", ExcelUtilities.GetWorksheetNames(_args.FileName).ToArray())));
                    }
                    if (!CheckIfInvalidColumnNameUsed(sql))
                    {
                        throw e;
                    }
                }

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

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

            return(results);
        }