Exemple #1
0
        public virtual DbFields GetQuerySchema(string querySQL)
        {
            var result = new DbFields();

            var dbParameters = new DbParameters(
                GetQueryParameters(querySQL)
                .ToDictionary(x => x, x => (object)DBNull.Value));

            var reader      = this.Select(querySQL, dbParameters).ToDataReader();
            var schemaTable = reader.GetSchemaTable();

            foreach (DataRow row in schemaTable.Rows)
            {
                string columnName = row.ItemArray[0].ToString().Length > 0
                    ? row.ItemArray[0].ToString()
                    : $"Column{schemaTable.Rows.IndexOf(row) + 1}";

                if (string.IsNullOrEmpty(columnName))
                {
                    int columnIndex = 0;
                    do
                    {
                        columnName = $"Column{++columnIndex}";
                    }while (result.ContainsKey(columnName));
                }

                if (!result.ContainsKey(columnName))
                {
                    result.Add(columnName, string.Empty);
                }
            }

            return(result);
        }
        /// <summary>
        /// Attempt to locate a field in this definition's field list. If the field
        /// is not found, it will be added to the field list. Either way, the field's
        /// index in the view will be returned.
        /// </summary>
        /// <param name="dBField">The field to find or add.</param>
        /// <returns>The index of the field in the view (0-based).</returns>
        internal int FindOrAddField(DBField dBField)
        {
            int fieldIndex = DbFields.IndexOf(dBField);

            if (fieldIndex == -1)
            {
                fieldIndex = DbFields.Count;
                DbFields.Add(dBField);
            }
            return(fieldIndex);
        }