sábado, 18 de julho de 2015

QueryBuilder

Não fique acorrentado às carreiras convencionais ou às formas tradicionais de competição. Dê a si mesmo a permissão para ser você mesmo. Abonde as tentativas de ser a pessoa que você sempre pensou que devia ser – o estudante modelo ou o executivo organizado, por exemplo – e permita a si mesmo ser o que você é.


using System;
using System.IO;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Data;
using System.Data.OleDb;
using System.Diagnostics;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.ComponentModel.Design.Serialization;
using System.Reflection;
using System.Globalization;
using AttributeCollection = System.ComponentModel.AttributeCollection;

namespace Lapuinka.Query
{
       public class Log
       {
             public static void Write(string Apath,string Amsg)
             {
                    using (StreamWriter sw =  File.AppendText(Apath))
                           sw.WriteLine(DateTime.Now.ToLongDateString()+"@ ["+Amsg+"]");
             }

       }
       internal class connectionTypeConvert : DataSourceConverter
       {
             public override System.Boolean CanConvertFrom (System.ComponentModel.ITypeDescriptorContext context , System.Type sourceType )
             {
                    System.Boolean ret = false;
                    if (sourceType.Equals(typeof(string)))
                           ret = true;
                    else
                           ret = base.CanConvertFrom(context , sourceType );
                    return ret;
             }
             #region Display Control IDs In List
             public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
             {
                    if ((context == null) || (context.Container == null))
                    {
                           return null;
                    }
                    Object[] serverControls = this.GetConnections(context.Container);
                    if (serverControls != null)
                    {
                           return new StandardValuesCollection(serverControls);
                    }
                    return null;
             }
             private object[] GetConnections(IContainer container)
             {
                    ArrayList availableControls = new ArrayList();
                    foreach( IComponent component in container.Components )
                    {
                           IDbConnection objConn = component as IDbConnection;
                           if ( (objConn != null)     )
                           {
                                  availableControls.Add(((Component)objConn).Site.Name);
                           }
                    }
                    availableControls.Sort(Comparer.Default);
                    return  availableControls.ToArray();
             }
             private object[] GetControls(IContainer container)
             {
                    ArrayList availableControls = new ArrayList();
                    foreach( IComponent component in container.Components )
                    {
                           Control serverControl = component as Control;
                           if ( serverControl != null &&
                                  !(serverControl is Page) &&
                                  serverControl.ID != null &&
                                  serverControl.ID.Length != 0  &&
                                  IncludeControl(serverControl)
                                  )
                           {
                                  availableControls.Add(serverControl.ID);
                           }
                    }
                    availableControls.Sort(Comparer.Default);
                    return availableControls.ToArray();
             }
             protected virtual Boolean IncludeControl( Control serverControl )
             {
                    return true;
             }

             #endregion

             /*public override System.Boolean GetStandardValuesExclusive ( System.ComponentModel.ITypeDescriptorContext context )
             {
                    System.Boolean ret;
                    ret = GetStandardValuesExclusive (  context );
                    return ret;
             }*/
             public override System.Boolean GetStandardValuesSupported ( System.ComponentModel.ITypeDescriptorContext context )
             {
                   
                    System.Boolean ret;
                    ret = base.GetStandardValuesSupported (context );
                    return ret;
             }

             public override  System.Boolean CanConvertTo ( System.ComponentModel.ITypeDescriptorContext context , System.Type destinationType )
             {
                    System.Boolean ret = false;
                    if (destinationType.Equals(typeof(string)))
                           ret = true;
                    else
                           ret = base.CanConvertTo(context , destinationType );
                    return ret;

             }
             public override System.Object ConvertFrom ( System.ComponentModel.ITypeDescriptorContext context , System.Globalization.CultureInfo culture , System.Object value )
             {
                    System.Object ret = new object();//value já vem dataSet1
                    ret = base.ConvertFrom (context ,culture,value );
                    return ret;
             }
            
             public override  System.Object ConvertTo ( System.ComponentModel.ITypeDescriptorContext context , System.Globalization.CultureInfo culture , System.Object value , System.Type destinationType )
             {
                    System.Object ret = new object();
                    if (destinationType.Equals(typeof(string)))
                           ret = value.ToString();
                    else
                           ret = base.ConvertTo (context , culture ,  value , destinationType );
                    return ret;
             }
             public override System.Object CreateInstance ( System.ComponentModel.ITypeDescriptorContext context , System.Collections.IDictionary propertyValues )
             {
                    System.Object ret = new object();
                    ret = base.CreateInstance (context , propertyValues );
                    return ret;
             }
            

       }

       public class SQLBuilderDesigner : ControlDesigner, IDataSourceProvider
       {
            
             private DataTable _dummyDataTable;
             private DataTable _designTimeDataTable;

             #region Proxies of the properties that are involved in DataBinding
             ///
             /// This is a proxy for the DataMember field that is required to attach the DataMemberConverter to.
             ///
             public string DataMember
             {
                    get
                    {
                           return ((SQLBuilder)this.Component).DataMember;
                    }
                    set
                    {
                           ((SQLBuilder)this.Component).DataMember = value;
                    }
             }


             ///
             /// This is a proxy for the DataSource field that is required to attach the DataSourceConverter to.
             /// This is especially required as it allows us to represent the DataSource property as a string
             /// rather then as an object.
             ///
             public string DataSource
             {
                    get
                    {
                           DataBinding binding = DataBindings["DataSource"];
                           if (binding != null)
                                  return binding.Expression;
                           return string.Empty;
                    }
                    set
                    {                         
                           if ((value == null) || (value.Length == 0))
                                  base.DataBindings.Remove("DataSource");
                           else
                           {
                                  DataBinding binding = DataBindings["DataSource"];
                                  if (binding == null)
                                        binding = new DataBinding("DataSource",
                                               typeof(IEnumerable), value);
                                  else
                                        binding.Expression = value;
                                  DataBindings.Add(binding);
                           }

                           OnBindingsCollectionChanged("DataSource");
                    }
             }

             ///
             /// This is a proxy for the Connection field that is required to attach the ComponentConverter to.
             /// This is especially required as it allows us to represent the Connection property as a string
             /// rather then as an object.
             ///
             public string Connection
             {
                    get
                    {
                           DataBinding binding = DataBindings["Connection"];
                           if (binding != null)
                                  return binding.Expression;
                           return string.Empty;
                    }
                    set
                    {                         
                           if ((value == null) || (value.Length == 0))
                                  base.DataBindings.Remove("Connection");
                           else
                           {
                                  DataBinding binding = DataBindings["Connection"];
                                  if (binding == null)
                                        binding = new DataBinding("Connection",
                                               typeof(IEnumerable), value);
                                  else
                                        binding.Expression = value;
                                  DataBindings.Add(binding);
                           }

                           OnBindingsCollectionChanged("Connection");
                    }
             }
             #endregion

             #region Overrides
             ///
             /// Set to True so that the control can be resized on the form.
             ///
             public override bool AllowResize
             {
                    get
                    {
                           return true;
                    }
             }

            


             ///
             /// Used to modify the Attributes of the 'Data' related fields such that
             /// the correct TypeConverters are added to the Attributes. For some reason
             /// adding the attributes directly doesn't work.
             ///
             /// The dictionary
             protected override void PreFilterProperties(IDictionary properties)
             {
                    base.PreFilterProperties(properties);
                    PropertyDescriptor prop;

                    prop = (PropertyDescriptor)properties["Connection"];
                    if(prop!=null)
                    {
                           AttributeCollection runtimeAttributes = prop.Attributes;
                           // make a copy of the original attributes but make room for one extra attribute ie the TypeConverter attribute
                           Attribute[] attrs = new Attribute[runtimeAttributes.Count + 1];
                           runtimeAttributes.CopyTo(attrs, 0);
                           attrs[runtimeAttributes.Count] = new TypeConverterAttribute(typeof(connectionTypeConvert));
                           prop = TypeDescriptor.CreateProperty(this.GetType(), "Connection", typeof(string),attrs);
                           properties["Connection"] = prop;
                    }
                   
                    #region DataSource
                    prop = (PropertyDescriptor)properties["DataSource"];
                    if(prop!=null)
                    {
                           AttributeCollection runtimeAttributes = prop.Attributes;
                           // make a copy of the original attributes but make room for one extra attribute ie the TypeConverter attribute
                           Attribute[] attrs = new Attribute[runtimeAttributes.Count + 1];
                           runtimeAttributes.CopyTo(attrs, 0);
                           attrs[runtimeAttributes.Count] = new TypeConverterAttribute(typeof(DataSourceConverter));
                           prop = TypeDescriptor.CreateProperty(this.GetType(), "DataSource", typeof(string),attrs);
                           properties["DataSource"] = prop;
                    }                  

                    prop = (PropertyDescriptor)properties["DataMember"];
                    if(prop!=null)
                    {
                           AttributeCollection runtimeAttributes = prop.Attributes;
                           Attribute[] attrs = new Attribute[runtimeAttributes.Count + 1];
                           // make a copy of the original attributes but make room for one extra attribute ie the TypeConverter attribute
                           runtimeAttributes.CopyTo(attrs, 0);
                           attrs[runtimeAttributes.Count] = new TypeConverterAttribute(typeof(DataMemberConverter));
                           prop = TypeDescriptor.CreateProperty(this.GetType(), "DataMember", typeof(string),attrs);
                           properties["DataMember"] = prop;
                    }                  

                    prop = (PropertyDescriptor)properties["DataValueField"];
                    if(prop!=null)
                    {
                           AttributeCollection runtimeAttributes = prop.Attributes;
                           Attribute[] attrs = new Attribute[runtimeAttributes.Count + 1];
                           // make a copy of the original attributes but make room for one extra attribute ie the TypeConverter attribute
                           runtimeAttributes.CopyTo(attrs, 0);
                           attrs[runtimeAttributes.Count] = new TypeConverterAttribute(typeof(DataFieldConverter));
                           prop = TypeDescriptor.CreateProperty(this.GetType(), "DataValueField", typeof(string),attrs);
                           properties["DataValueField"] = prop;
                    }                  
                   
                    prop = (PropertyDescriptor)properties["DataTextField"];
                    if(prop!=null)
                    {
                           AttributeCollection runtimeAttributes = prop.Attributes;
                           Attribute[] attrs = new Attribute[runtimeAttributes.Count + 1];
                           // make a copy of the original attributes but make room for one extra attribute ie the TypeConverter attribute
                           runtimeAttributes.CopyTo(attrs, 0);
                           attrs[runtimeAttributes.Count] = new TypeConverterAttribute(typeof(DataFieldConverter));
                           prop = TypeDescriptor.CreateProperty(this.GetType(), "DataTextField", typeof(string),attrs);
                           properties["DataTextField"] = prop;
                    }                  
                    #endregion
             }
             ///
             /// Modifies the control such that it will display Databound/Unbound as appropriate.
             ///
             /// The HTML representing our object at runtime.
             public override string GetDesignTimeHtml()
             {
                    SQLBuilder dbt = (SQLBuilder)Component;
                    string designTimeHTML = null;

                    IEnumerable designTimeDataSource = GetDesignTimeDataSource(5);

                    try
                    {
                           //dbt.DataSource = designTimeDataSource;
                           //dbt.Connection = this.Connection;
                           //dbt.DataBind();
                           designTimeHTML = base.GetDesignTimeHtml();
                    }
                    catch(Exception e)
                    {
                           designTimeHTML = GetErrorDesignTimeHtml(e);
                    }
                    finally
                    {
                           //dbt.DataSource = null;
                    }
                    return designTimeHTML;
             }

             protected override string GetErrorDesignTimeHtml(Exception e)
             {
                    return   " SQLBuilder: [" + ((SQLBuilder)Component).ID + "{"+e.Message+"} ]
";
             }

             #endregion
                          
             #region IDataSourceProvider methods
             ///
             /// Used by the DataFieldConverter to resolve the DataSource and DataMember combination
             /// so that it can populate a dropdown with a list of available fields.
             ///
             IEnumerable IDataSourceProvider.GetResolvedSelectedDataSource()
             {
                    DataBinding binding;
                    binding = this.DataBindings["DataSource"];
                    if (binding != null)
                           return DesignTimeData.GetSelectedDataSource(this.Component, binding.Expression, this.DataMember);
                    return null;
             }

             ///
             /// Used by the DataMemberConverter to resolve the DataSource which it can then use
             /// to populate a drop down box containing a list of available tables.
             ///
             /// The object that is our DataSource
             object IDataSourceProvider.GetSelectedDataSource()
             {
                    DataBinding binding;

                    binding = this.DataBindings["DataSource"];
                    if (binding != null)
                           return DesignTimeData.GetSelectedDataSource(this.Component, binding.Expression);
                    return null;
             }
             #endregion

             #region IEnumerable
             public IEnumerable GetDesignTimeDataSource(int minimumRows)
             {
                    IEnumerable selectedDataSource = ((IDataSourceProvider)this).GetResolvedSelectedDataSource();

                    DataTable dataTable = _designTimeDataTable;
                   

                    //if possible, use the data table corresponding to the
                    // selected data source

                    if (dataTable == null)
                    {
                           if (selectedDataSource != null)
                           {
                                  _designTimeDataTable = DesignTimeData.CreateSampleDataTable(selectedDataSource);
                                  dataTable = _designTimeDataTable;

                                 
                           }

                           if (dataTable == null)
                           {
                                  //fall back on a dummy data source if it isn't
                                  // possible to create a sample data table
                                  if (_dummyDataTable == null)
                                  {
                                        _dummyDataTable = DesignTimeData.CreateDummyDataTable();
                                  }

                                  dataTable = _dummyDataTable;
                           }
                    }

                    IEnumerable liveDataSource = DesignTimeData.GetDesignTimeDataSource(dataTable, minimumRows);
                    return liveDataSource;
             }

             #endregion

            



       }
      
}



Postar um comentário