Esempio n. 1
0
        /// <summary>
        /// Edit the selected query.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mnuQueryEdit_Click(object sender, EventArgs e)
        {
            if (selectedQuery != null && selectedEntity != null)
            {
                using (AddQuery addQuery = new AddQuery(selectedQuery, dataModel.GetConnectionString(), selectedEntity.Queries))
                {
                    addQuery.Width  = dataModel.DialogWidth;
                    addQuery.Height = dataModel.DialogHeight;
                    addQuery.Text   = String.Format("Edit query \"{0}\" of entity \"{1}\"", selectedQuery.Name, selectedEntity.Name);
                    if (addQuery.ShowDialog(this) == DialogResult.OK)
                    {
                        selectedQuery.Name          = addQuery.QueryName;
                        selectedQuery.CommandText   = addQuery.CommandText;
                        selectedQuery.CommandType   = addQuery.CommandType;
                        selectedQuery.ExecuteMethod = addQuery.ExecuteMethod;
                        selectedQuery.ReturnType    = "object";

                        RefreshQuery(selectedQuery, selectedEntity);
                        OnModifiedWithRefresh();
                        okQueryTimer.Start();
                    }
                    dataModel.DialogWidth  = addQuery.Width;
                    dataModel.DialogHeight = addQuery.Height;
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Show the dialog for adding a new query to an entity.
        /// </summary>
        /// <param name="entity">the entity to which a query should be added</param>
        private void AddNewQuery(Entity entity)
        {
            string connectionString = dataModel.GetConnectionString();

            if (String.IsNullOrEmpty(connectionString))
            {
                using (Connection frm = new Connection(connectionStringService))
                {
                    if (frm.ShowDialog(this) == DialogResult.OK)
                    {
                        connectionString = frm.ConnectionString;
                        dataModel.EditorProperties.ConnectionString = frm.ConnectionStringName;
                    }
                }
            }

            if (!String.IsNullOrEmpty(connectionString))
            {
                using (AddQuery addQuery = new AddQuery(connectionString, entity.Queries))
                {
                    addQuery.Width  = dataModel.DialogWidth;
                    addQuery.Height = dataModel.DialogHeight;
                    addQuery.Text   = "Add a new query to entity \"" + entity.Name + "\"";
                    if (addQuery.ShowDialog(this) == DialogResult.OK)
                    {
                        try
                        {
                            // get query metadata from DB
                            DB.Analyzer analyzer = new DB.Analyzer(dataModel.GetConnectionString());
                            DB.Metadata md       = analyzer.GetQueryMetadata(addQuery.CommandText, addQuery.CommandType);

                            bool addOk           = true;
                            bool incompatibility = false;
                            if (addQuery.ExecuteMethod == ExecuteMethod.Reader)
                            {
                                if (entity.Properties.Count == 0)
                                {
                                    entity.SetColumns(md);
                                }
                                else
                                {
                                    List <string> errorDetails = CheckCompatibility(entity, md);
                                    if (errorDetails.Count > 0)
                                    {
                                        using (CompatibilityError errorDlg = new CompatibilityError(errorDetails, addQuery.QueryName))
                                        {
                                            if (errorDlg.ShowDialog(this) == DialogResult.OK)
                                            {
                                                entity.SetColumns(md);
                                                incompatibility = true;
                                            }
                                            else
                                            {
                                                addOk = false;
                                            }
                                        }
                                    }
                                }
                            }

                            if (addOk)
                            {
                                Query query = new Query(this, entity)
                                {
                                    Name          = addQuery.QueryName,
                                    CommandText   = addQuery.CommandText,
                                    CommandType   = addQuery.CommandType,
                                    ExecuteMethod = addQuery.ExecuteMethod,
                                    ReturnType    = "object"
                                };
                                query.Parameters.AddRange(md.Parameters);
                                entity.Queries.Add(query);

                                if (incompatibility)
                                {
                                    RefreshQuery(query, entity, true, true);
                                    okQueryTimer.Start();
                                }
                            }

                            OnModifiedWithRefresh();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(this, "An error occured while adding the new query:\r\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                            Query query = new Query(this, entity)
                            {
                                Name          = addQuery.QueryName,
                                CommandText   = addQuery.CommandText,
                                CommandType   = addQuery.CommandType,
                                ExecuteMethod = addQuery.ExecuteMethod,
                                ReturnType    = "object",
                                ShowError     = true
                            };
                            entity.Queries.Add(query);
                            OnModifiedWithRefresh();
                        }
                    }
                    dataModel.DialogWidth  = addQuery.Width;
                    dataModel.DialogHeight = addQuery.Height;
                }
            }
        }