Exemple #1
0
 private string GetTableName(object treeNode)
 {
     TableNode node = (TableNode)treeNode;
     StringBuilder sb = new StringBuilder();
     DataProviderHelper helper = new DataProviderHelper();
     if (!node.Connection.Default)
     {
         sb.Append(node.Connection.Prefix);
         sb.Append(":");
     }
     if (!String.IsNullOrEmpty(node.SchemaName))
     {
         sb.Append(helper.NativeFormatIdentifier(node.SchemaName));
         sb.Append(".");
     }
     sb.Append(helper.NativeFormatIdentifier(node.TableName));
     return sb.ToString();
 }
Exemple #2
0
        private TableType GetDataProviderTableType(DataSourceInfo dsi, String identifier)
        {           
            DataProviderHelper helper = new DataProviderHelper(dsi.ProviderInvariantName, dsi.ConnectionString);                       
            using (DbConnection connection = dsi.CreateConnection())
            {                
                DbCommand command = connection.CreateCommand();                
                
                StringBuilder sb = new StringBuilder();
                sb.Append("SELECT * FROM ");
                sb.Append(identifier);
                if (helper.OrderByColumnsInSelect)
                    sb.Append(" ORDER BY 1");
                command.CommandText = sb.ToString();                
                
                try
                {
                    DbDataReader reader;                    
                    connection.Open();
                    bool hasKeyInfo = false;                    
                    try
                    {
                        reader = command.ExecuteReader(CommandBehavior.SchemaOnly | CommandBehavior.KeyInfo);
                        hasKeyInfo = true;
                    }
                    catch (Exception ex)
                    {
                        Trace.TraceWarning("GetDataProviderTableType.ExecuteReader: {0}", ex.Message);                        
                        reader = command.ExecuteReader(CommandBehavior.SchemaOnly);                        
                    }

                    DataTable dt_src = reader.GetSchemaTable();
                    DataTable dt = RowType.CreateSchemaTable();
                    int n = 0;
                    foreach (DataRow r in dt_src.Rows)
                    {
                        DataRow r1 = dt.NewRow();
                        foreach (DataColumn col in dt_src.Columns)
                        {
                            DataColumn dest = dt.Columns[col.ColumnName];
                            if (dt.Columns.IndexOf(col.ColumnName) != -1 && col.ColumnName != "ColumnName")
                                r1[dest] = r[col];
                        }
                        r1["ColumnOrdinal"] = n++;
                        string columnName = (string)r["ColumnName"];
                        r1["ColumnName"] = helper.NativeFormatIdentifier(columnName);
                        r1["ProviderColumnName"] = columnName;
                        r1["IsCaseSensitive"] = 
                            helper.IdentifierCase == IdentifierCase.Sensitive;
                        dt.Rows.Add(r1);
                    }

                    reader.Close();
                    
                    RowType rtype = new RowType(dt);
                    if (hasKeyInfo && rtype.Fields[0].BaseTableName != null)
                        return new TableType(identifier, rtype.Fields[0].BaseCatalogName,
                            rtype.Fields[0].BaseSchemaName, rtype.Fields[0].BaseTableName, dsi, rtype, helper.Smart);
                    else
                    {
                        string schema = null;
                        string catalog = null;
                        string tableName = null;
                        
                        string[] identifierPart = helper.SplitIdentifier(identifier);                        
                        int length = identifierPart.Length;

                        if (length == 3)
                            catalog = identifierPart[identifierPart.Length - length--];

                        if (length == 2)
                            schema = identifierPart[identifierPart.Length - length--];

                        if (length == 1)
                            tableName = identifierPart[identifierPart.Length - length];
                        
                        return new TableType(identifier, catalog, schema, tableName, dsi, rtype, helper.Smart);
                    }
                }
                catch (Exception ex)
                {
                    Trace.TraceWarning("GetDataProviderTableType: {0}", ex.Message);
                    return null;
                }
            }
        }
Exemple #3
0
        public static Resultset QueryLdap([Implict] Executive engine, String domainName, String filter, String properties)
        {
            QueryNode.LispProcessingContext owner = (QueryNode.LispProcessingContext)engine.Owner;
            if (String.IsNullOrEmpty(domainName))
                domainName = String.Format("LDAP://{0}", Environment.UserDomainName);
            DirectoryEntry entry = new DirectoryEntry(domainName);
            DirectorySearcher searcher = new DirectorySearcher(entry);
            if (owner.QueryContext.LdapClientTimeout != TimeSpan.Zero)
                searcher.ClientTimeout = owner.QueryContext.LdapClientTimeout;
            searcher.SizeLimit = owner.QueryContext.LdapSearchLimit;
            searcher.CacheResults = true;
            searcher.Filter = filter;
            string[] properties_array = properties.Split(new char[] { ',' });
            foreach (string prop in properties_array)
                searcher.PropertiesToLoad.Add(prop);
            Type[] dataType = new Type[properties_array.Length];
            bool[] is_array = new bool[properties_array.Length];
            SearchResultCollection result = searcher.FindAll();
            for (int p = 0; p < dataType.Length; p++)
            {
                dataType[p] = null;
                foreach (SearchResult sr in result)
                {
                    ResultPropertyValueCollection value_col = sr.Properties[properties_array[p]];
                    if (value_col.Count > 0)
                    {
                        Type curr;
                        is_array[p] |= value_col.Count > 1;
                        if (value_col[0] != null)
                        {
                            curr = value_col[0].GetType();
                            if (curr != typeof(System.Object) && Type.GetTypeCode(curr) == TypeCode.Object)
                                curr = typeof(System.Object);
                            if (dataType[p] == null || curr == typeof(System.Object))
                                dataType[p] = curr;
                            else
                                if (dataType[p] != curr)
                                    dataType[p] = typeof(System.String);
                        }
                        if (dataType[p] == typeof(System.Object))
                            break;
                    }
                }
                if (dataType[p] == null)
                    dataType[p] = typeof(System.String);
            }

            DataTable dt = RowType.CreateSchemaTable();
            DataProviderHelper helper = new DataProviderHelper();
            for (int k = 0; k < dataType.Length; k++)
            {
                DataRow dr = dt.NewRow();
                dr["ColumnName"] = helper.NativeFormatIdentifier(properties_array[k]);
                dr["IsCaseSensitive"] = true;
                dr["ColumnOrdinal"] = k;
                if (is_array[k])
                    dr["DataType"] = typeof(System.Object);
                else
                    dr["DataType"] = dataType[k];
                dt.Rows.Add(dr);
            }
            Resultset rs = new Resultset(new RowType(dt), null);
            foreach (SearchResult sr in result)
            {
                Row row = rs.NewRow();
                for (int p = 0; p < properties_array.Length; p++)
                {
                    ResultPropertyValueCollection value_col = sr.Properties[properties_array[p]];
                    if (value_col.Count > 0)
                    {
                        if (value_col.Count == 1)
                        {
                            if (dataType[p] == typeof(System.String))
                                row.SetString(p, value_col[0].ToString());
                            else
                                row.SetValue(p, value_col[0]);
                        }
                        else
                        {
                            object[] values = (object[])Array.CreateInstance(dataType[p], value_col.Count);
                            for (int k = 0; k < values.Length; k++)
                                values[k] = value_col[k];
                            row.SetValue(p, values);
                        }
                    }
                }
                rs.Enqueue(row);
            }
            searcher.Dispose();
            entry.Dispose();
            return rs;
        }
Exemple #4
0
 protected override RowType CreateRowType()
 {
     if (_rowType == null)
     {
         DataProviderHelper helper = new DataProviderHelper(_providerInvariantName, _connectionString);
         DbConnection connection = DataProviderHelper.CreateDbConnection(_providerInvariantName);
         connection.ConnectionString = _connectionString;
         connection.Open();
         DbCommand command = connection.CreateCommand();
         command.CommandText = _commandText;
         for (int k = 0; k < _parameterBindings.Length; k++)
         {
             DbParameter parameter = command.CreateParameter();
             parameter.ParameterName = _helper.FormatParameter(
                 String.Format("p{0}", command.Parameters.Count));
             parameter.Direction = ParameterDirection.Input;
             command.Parameters.Add(parameter);
         }
         DbDataReader reader = command.ExecuteReader(CommandBehavior.SchemaOnly | CommandBehavior.CloseConnection);
         try
         {
             DataTable dt_src = reader.GetSchemaTable();
             DataTable dt = RowType.CreateSchemaTable();
             int n = 0;
             foreach (DataRow r in dt_src.Rows)
             {
                 DataRow r1 = dt.NewRow();
                 foreach (DataColumn col in dt_src.Columns)
                 {
                     DataColumn dest = dt.Columns[col.ColumnName];
                     if (dt.Columns.IndexOf(col.ColumnName) != -1 && col.ColumnName != "ColumnName")
                         r1[dest] = r[col];
                 }
                 r1["ColumnOrdinal"] = n++;
                 string columnName = (string)r["ColumnName"];
                 r1["ColumnName"] = helper.NativeFormatIdentifier(columnName);
                 r1["ProviderColumnName"] = columnName;
                 r1["IsCaseSensitive"] =
                     helper.IdentifierCase == IdentifierCase.Sensitive;
                 dt.Rows.Add(r1);
             }
             _rowType = new RowType(dt);
         }
         finally
         {
             reader.Close();
         }
     }
     return _rowType;
 }
Exemple #5
0
 private DataTable CreateRowType(StreamReader reader, char delimiter, bool columnNameHeader)
 {
     string[] values = new string[MaxTextColumns];
     StringBuilder sb = new StringBuilder();
     string[] header = null;
     int maxcols = 0;
     CsvParser parser = new CsvParser(delimiter);
     while (!reader.EndOfStream)
     {
         maxcols = parser.Get(reader, values);
         if (maxcols > 0 && columnNameHeader)
         {
             header = new string[maxcols];
             Array.Copy(values, header, maxcols);
             break;
         }
     }
     List<string> names = new List<string>();
     DataProviderHelper helper = new DataProviderHelper();
     DataTable dt = RowType.CreateSchemaTable();
     for (int k = 0; k < maxcols; k++)
     {
         DataRow dr = dt.NewRow();
         dr["ColumnOrdinal"] = k;
         if (columnNameHeader && header != null && k < header.Length)
         {
             String name = helper.NativeFormatIdentifier(header[k]);
             if (String.IsNullOrEmpty(name))
                 name = "Col";
             dr["ColumnName"] = Util.CreateUniqueName(names, name);
         }
         else
             dr["ColumnName"] = String.Format("Col{0}", k + 1);
         dr["DataType"] = typeof(System.String);
         dt.Rows.Add(dr);
     }
     return dt;
 }
Exemple #6
0
        private DataTable CreateRowType(IniFile ini, string section, ref TextFileDataFormat df)
        {            
            ParseIniTextDataFormat(ini, section, ref df);
            ParseIniCharacterSet(ini, section, ref df);            
            
            df.DateTimeFormat = GetIniString(ini, section, "DateTimeFormat", df.DateTimeFormat);
            df.DecimalSymbol = GetIniString(ini, section, "DecimalSymbol", df.DecimalSymbol);
            df.CurrencySymbol = GetIniString(ini, section, "CurrencySymbol", df.CurrencySymbol);
            df.CurrencyThousandSymbol = GetIniString(ini, section, "CurrencyThousandSymbol", df.CurrencyThousandSymbol);
            df.CurrencyDecimalSymbol = GetIniString(ini, section, "CurrencyDecimalSymbol", df.CurrencyDecimalSymbol);

            df.NumberDigits = GetIniNumber(ini, section, "NumberDigits", df.NumberDigits);
            df.CurrencyDigits = GetIniNumber(ini, section, "CurrencyDigits", df.CurrencyDigits);

            df.ColumnNameHeader = GetIniBoolean(ini, section, "ColNameHeader", df.ColumnNameHeader);
            df.NumberLeadingZeros = GetIniBoolean(ini, section, "NumberLeadingZeros", df.NumberLeadingZeros);
            df.SequentialProcessing = GetIniBoolean(ini, section, "SequentialProcessing", df.SequentialProcessing);

            ParseCurrencyNegFormat(ini, section, ref df);
            ParseCurrencyPosFormat(ini, section, ref df);

            df.NullValue = GetIniString(ini, section, "NullValue", null);

            List<string> names = new List<string>();
            List<int> length = new List<int>();
            DataProviderHelper helper = new DataProviderHelper();
            DataTable dt = RowType.CreateSchemaTable();

            string[] keys = ini.GetEntryNames(section);
            Regex regex = new Regex("^Col[0-9]+$");
            StringBuilder sb = new StringBuilder();
            string[] values = new string[4];
            int ordinal = 0;
            for (int k = 0; k < keys.Length; k++)
                if (regex.IsMatch(keys[k]))
                {
                    CsvParser parser = new CsvParser(' ');
                    if (parser.Get((string)ini.GetValue(section, keys[k]), values) > 0)
                    {
                        DataRow dr = dt.NewRow();
                        dr["ColumnOrdinal"] = ordinal++;
                        dr["ColumnName"] = Util.CreateUniqueName(names, helper.NativeFormatIdentifier(values[0]));
                        dr["DataType"] = DecodeDataType(values[1]);
                        if (values[2] == "Width" && values[3] != null)
                        {
                            int size = Int32.Parse(values[3]);
                            if ((System.Type)dr["DataType"] == typeof(System.String))
                                dr["ColumnSize"] = size;
                            length.Add(size);
                        }
                        else
                            if (df.TextFormat == TextDataFormat.FixedLength)
                                throw new ESQLException(Properties.Resources.ExpectedWidthInSchemaIni, values[0]);
                        dt.Rows.Add(dr);                        
                    }
                }
            if (df.TextFormat == TextDataFormat.FixedLength)
            {
                if (dt.Rows.Count == 0)
                    throw new ESQLException(Properties.Resources.ColumnDataTypeRequred);
                df.Width = length.ToArray();
            }
            return dt;
        }