Esempio n. 1
0
        public static void PopulateEntityProperty(Control control, PropertyInfo p, object entity, bool treatZeroAsNull)
        {
            bool isNullable = p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>);

            Type   controlType      = control.GetType();
            string controlTypeName  = controlType.FullName;
            string propertyTypeName = p.PropertyType.FullName;

            if (controlTypeName.Equals(typeof(TextBox).FullName))
            {
                TextBox textBox = (TextBox)control;
                p.SetValue(entity, textBox.Text, null);
            }
            else if (controlTypeName.Equals(typeof(NumericTextBoxWindows).FullName))
            {
                NumericTextBoxWindows numericTextBox = (NumericTextBoxWindows)control;
                object value = DataHelperWindows.ChangeType(numericTextBox.Value, p.PropertyType);
                if (treatZeroAsNull && value != null && value.ToString() == "0")
                {
                    value = null;
                }
                p.SetValue(entity, value, null);
            }
            else if (controlTypeName.Equals(typeof(CheckBox).FullName))
            {
                CheckBox checkBox = (CheckBox)control;
                p.SetValue(entity, checkBox.Checked, null);
            }
            else if (controlTypeName.Equals(typeof(NumericUpDown).FullName))
            {
                NumericUpDown numericUpDown = (NumericUpDown)control;
                object        value         = DataHelperWindows.ChangeType(numericUpDown.Value, p.PropertyType);
                if (treatZeroAsNull && value != null && value.ToString() == "0")
                {
                    value = null;
                }
                p.SetValue(entity, value, null);
            }
            else if (controlTypeName.Equals(typeof(DateTimePicker).FullName))
            {
                DateTimePicker dateTimePicker = (DateTimePicker)control;
                p.SetValue(entity, dateTimePicker.Value, null);
            }
            else if (controlTypeName.Equals(typeof(ComboBox).FullName))
            {
                ComboBox comboBox   = (ComboBox)control;
                Array    enumValues = EnumHelperWindows.GetEnumValues(p.PropertyType);
                p.SetValue(entity, comboBox.SelectedItem, null);
            }
            else
            {
                throw new Exception(string.Format(
                                        "Unexpected controltype {0} to be used to populate property {1} on {2}.",
                                        controlTypeName,
                                        p.Name,
                                        entity.GetType().FullName));
            }
        }
Esempio n. 2
0
        public static object GetControlValue(Control control, PropertyInfo p, bool treatZeroAsNull)
        {
            bool isNullable = p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>);

            Type   controlType      = control.GetType();
            string controlTypeName  = controlType.FullName;
            string propertyTypeName = p.PropertyType.FullName;

            if (controlTypeName.Equals(typeof(TextBox).FullName))
            {
                TextBox textBox = (TextBox)control;
                return(textBox.Text);
            }
            else if (controlTypeName.Equals(typeof(NumericTextBoxWindows).FullName))
            {
                NumericTextBoxWindows numericTextBox = (NumericTextBoxWindows)control;
                object value = DataHelperWindows.ChangeType(numericTextBox.Value, p.PropertyType);
                if (treatZeroAsNull && value != null && value.ToString() == "0")
                {
                    value = null;
                }
                return(value);
            }
            else if (controlTypeName.Equals(typeof(CheckBox).FullName))
            {
                CheckBox checkBox = (CheckBox)control;
                return(checkBox.Checked);
            }
            else if (controlTypeName.Equals(typeof(NumericUpDown).FullName))
            {
                NumericUpDown numericUpDown = (NumericUpDown)control;
                object        value         = DataHelperWindows.ChangeType(numericUpDown.Value, p.PropertyType);
                if (treatZeroAsNull && value != null && value.ToString() == "0")
                {
                    value = null;
                }
                return(value);
            }
            else if (controlTypeName.Equals(typeof(DateTimePicker).FullName))
            {
                DateTimePicker dateTimePicker = (DateTimePicker)control;
                return(dateTimePicker.Value);
            }
            else if (controlTypeName.Equals(typeof(ComboBox).FullName))
            {
                ComboBox comboBox   = (ComboBox)control;
                Array    enumValues = EnumHelperWindows.GetEnumValues(p.PropertyType);
                return(comboBox.SelectedItem);
            }
            else
            {
                throw new Exception(string.Format(
                                        "Unexpected controltype {0} to be used to retrieve control value using property type {1}.",
                                        controlTypeName,
                                        p.Name));
            }
        }
        public override List <object> Query(
            QueryWindows query,
            string propertyNameFilter,
            Type entityType,
            bool disposeConnectionAfterExecute,
            DbConnection connection,
            DbTransaction transaction)
        {
            List <object> result = null;

            try
            {
                List <DatabaseTableWindows> tablesMentioned = GetTablesMentionedInQuery(query);
                if (connection == null)
                {
                    connection = new SQLiteConnection(_connectionString);
                }
                if (connection.State != ConnectionState.Open)
                {
                    connection.Open();
                }
                using (SQLiteCommand command = new SQLiteCommand(query.SqlQueryString, (SQLiteConnection)connection))
                {
                    if (transaction != null)
                    {
                        command.Transaction = (SQLiteTransaction)transaction;
                    }
                    query.SqlParameters.ForEach(p => command.Parameters.Add(p));
                    command.CommandType = System.Data.CommandType.Text;
                    using (SQLiteDataReader reader = command.ExecuteReader())
                    {
                        result = DataHelperWindows.ParseReaderToEntities(reader, entityType, propertyNameFilter);
                    }
                }
            }
            finally
            {
                if (disposeConnectionAfterExecute &&
                    connection != null &&
                    connection.State != ConnectionState.Closed)
                {
                    connection.Dispose();
                }
            }
            return(result);
        }
        public override List <object> Query(QueryWindows query, string propertyNameFilter, Type entityType)
        {
            List <DatabaseTableWindows> tablesMentioned = GetTablesMentionedInQuery(query);
            List <object> result = null;

            using (SQLiteConnection connection = new SQLiteConnection(_connectionString))
            {
                connection.Open();
                using (SQLiteCommand command = new SQLiteCommand(query.SqlQueryString, connection))
                {
                    query.SqlParameters.ForEach(p => command.Parameters.Add(p));
                    command.CommandType = System.Data.CommandType.Text;
                    using (SQLiteDataReader reader = command.ExecuteReader())
                    {
                        result = DataHelperWindows.ParseReaderToEntities(reader, entityType, propertyNameFilter);
                    }
                }
            }
            return(result);
        }
        public override List <object> Query(
            string sqlQueryString,
            OrmAssemblySqlWindows ormCollectibleAssembly,
            string typeName,
            string propertyNameFilter,
            out OrmTypeWindows ormCollecibleType)
        {
            if (ormCollectibleAssembly == null)
            {
                throw new NullReferenceException(string.Format("ormCollectibleAssembly may not be null."));
            }
            if (ormCollectibleAssembly.AssemblyBuilderAccess != AssemblyBuilderAccess.RunAndCollect)
            {
                throw new ArgumentException(string.Format(
                                                "Querying the database with a raw SQL query string requires an {0} with the {1} property set to {2}.",
                                                typeof(OrmAssemblyWindows).FullName,
                                                EntityReaderGeneric <OrmAssemblyWindows> .GetPropertyName(p => p.AssemblyBuilderAccess, false),
                                                AssemblyBuilderAccess.RunAndCollect));
            }
            List <object> result = null;

            using (SQLiteConnection connection = new SQLiteConnection(_connectionString))
            {
                connection.Open();
                using (SQLiteCommand command = new SQLiteCommand(sqlQueryString, connection))
                {
                    command.CommandType = System.Data.CommandType.Text;
                    using (SQLiteDataReader reader = command.ExecuteReader())
                    {
                        ormCollecibleType = ormCollectibleAssembly.CreateOrmTypeFromSqlDataReader(typeName, reader, true);
                        result            = DataHelperWindows.ParseReaderToEntities(reader, ormCollecibleType.DotNetType, propertyNameFilter);
                    }
                }
            }
            return(result);
        }
Esempio n. 6
0
        public static Dictionary <string, Control> GetControlsForEntity(
            Type entityType,
            bool includeLabels,
            DockStyle dockStyle,
            Color backColor,
            List <string> hiddenProperties,
            List <string> unmanagedProperties,
            bool shapeColumnNames,
            List <LinkLabel> resetLinks,
            Color resetLinksBackColor)
        {
            if (resetLinks != null)
            {
                resetLinks.Clear();
            }
            Dictionary <string, Control> controls        = new Dictionary <string, Control>();
            Dictionary <string, Type>    controlMappings = GetTypeNameToControlTypeMappings();

            PropertyInfo[] entityProperties = entityType.GetProperties();
            for (int i = 0; i < entityProperties.Length; i++)
            {
                PropertyInfo p = entityProperties[i];
                string       propertyNameMatch = shapeColumnNames ? DataShaperWindows.ShapeCamelCaseString(p.Name) : p.Name;
                if (hiddenProperties.Contains(propertyNameMatch) || unmanagedProperties.Contains(propertyNameMatch))
                {
                    continue;
                }
                Type propertyType = null;
                if (p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    propertyType = p.PropertyType.GetGenericArguments()[0];
                }
                else
                {
                    propertyType = p.PropertyType;
                }
                if (!controlMappings.ContainsKey(propertyType.FullName))
                {
                    continue;
                }
                Type    controlType = controlMappings[propertyType.FullName];
                Control control     = (Control)Activator.CreateInstance(controlType);
                control.Name    = string.Format("{0}", p.Name);
                control.Tag     = p.PropertyType;
                control.Dock    = DockStyle.Fill;
                control.TabStop = true;
                //control.TabIndex = i;
                //control.Height = 40;

                Panel pnlInputControl = new Panel()
                {
                    Name = string.Format("pnl{0}", p.Name), Dock = dockStyle, Height = control.Height
                };
                pnlInputControl.Controls.Add(control);
                if (resetLinks != null)
                {
                    LinkLabel lnkReset = new LinkLabel()
                    {
                        Name = string.Format("lnk{0}", p.Name), Text = "Reset", Tag = control, Dock = DockStyle.Right, Width = 60, BackColor = resetLinksBackColor
                    };
                    pnlInputControl.Controls.Add(lnkReset);
                    resetLinks.Add(lnkReset);
                }
                if (!(control is TextBox) && !(control is NumericTextBoxWindows) && !(control is NumericUpDown) && !(control is DateTimePicker) && !(control is ComboBox))
                {
                    control.BackColor = backColor;
                }
                if (control is NumericUpDown)
                {
                    ((NumericUpDown)control).Maximum = int.MaxValue;
                }
                if (control is NumericTextBoxWindows && (propertyType == typeof(Single)) || propertyType == typeof(Double) || propertyType == typeof(Decimal))
                {
                    NumericTextBoxWindows numericTextBox = (NumericTextBoxWindows)control;
                    numericTextBox.AllowDecimal = true;
                }
                if (includeLabels)
                {
                    Label label = new Label()
                    {
                        Name      = string.Format("lbl{0}", p.Name),
                        Text      = string.Format("{0}:", DataShaperWindows.ShapeCamelCaseString(p.Name)),
                        Dock      = dockStyle,
                        BackColor = backColor,
                        //Height = 40,
                    };
                    controls.Add(label.Name, label);
                }
                controls.Add(pnlInputControl.Name, pnlInputControl);
            }
            return(DataHelperWindows.ReverseDictionaryOrder <string, Control>(controls));
        }
        public override List <object> Query(
            string columnName,
            object columnValue,
            string propertyNameFilter,
            Type entityType,
            bool disposeConnectionAfterExecute,
            DbConnection connection,
            DbTransaction transaction)
        {
            List <object>          result     = null;
            List <SQLiteParameter> parameters = new List <SQLiteParameter>();

            PropertyInfo[] entityProperties = entityType.GetProperties();

            StringBuilder selectCommand = new StringBuilder();

            selectCommand.AppendLine(string.Format("SELECT * FROM [{0}]", entityType.Name));

            if (!string.IsNullOrEmpty(columnName))
            {
                StringBuilder whereClause = new StringBuilder();
                whereClause.AppendLine("WHERE ");
                whereClause.Append(string.Format("[{0}] = @{0}", columnName));
                parameters.Add(new SQLiteParameter(string.Format("@{0}", columnName), columnValue));
                selectCommand.AppendLine(whereClause.ToString());
            }
            string selectCommandText = selectCommand.ToString();

            try
            {
                if (connection == null)
                {
                    connection = new SQLiteConnection(_connectionString);
                }
                if (connection.State != ConnectionState.Open)
                {
                    connection.Open();
                }
                using (SQLiteCommand command = new SQLiteCommand(selectCommandText, (SQLiteConnection)connection))
                {
                    if (transaction != null)
                    {
                        command.Transaction = (SQLiteTransaction)transaction;
                    }
                    parameters.ForEach(p => command.Parameters.Add(p));
                    command.CommandType = System.Data.CommandType.Text;
                    using (SQLiteDataReader reader = command.ExecuteReader())
                    {
                        result = DataHelperWindows.ParseReaderToEntities(reader, entityType, propertyNameFilter);
                    }
                }
            }
            finally
            {
                if (disposeConnectionAfterExecute &&
                    connection != null &&
                    connection.State != ConnectionState.Closed)
                {
                    connection.Dispose();
                }
            }
            return(result);
        }