/// <summary>
        /// Attempts to create a new schema using a connection related to the parameters in the dialog.
        /// </summary>
        /// <param name="schemaName">The name of the new schema.</param>
        /// <returns><c>true</c> if the schema is created successfully, <c>false</c> otherwise.</returns>
        private bool CreateSchema(string schemaName)
        {
            bool success = true;

            try
            {
                using (var conn = new MySqlConnectionSupport())
                {
                    var connectionStringBuilder = new MySqlConnectionStringBuilder(ConnectionString)
                    {
                        Database = string.Empty
                    };
                    conn.Initialize(null);
                    conn.ConnectionString = connectionStringBuilder.ConnectionString;
                    conn.Open(false);
                    conn.ExecuteWithoutResults(string.Format("CREATE DATABASE `{0}`", schemaName), 1, null, 0);
                }
            }
            catch (Exception ex)
            {
                success = false;
                MySqlSourceTrace.WriteAppErrorToLog(ex, Resources.ErrorTitle, string.Format(Resources.ErrorAttemptingToCreateDB, schemaName), true);
            }

            return(success);
        }
Exemple #2
0
        protected override DataTable ReadTable(DataConnectionWrapper connection,
                                               object[] restrictions, string sort)
        {
            DataTable dt = base.ReadTable(connection, restrictions, sort);

            return(MySqlConnectionSupport.ConvertAllBinaryColumns(dt));
        }
 private void dbList_DropDown(object sender, EventArgs e)
 {
     if (!this.dbListPopulated)
     {
         DbConnectionStringBuilder builder = ((base.ConnectionProperties) as MySqlConnectionProperties).ConnectionStringBuilder;
         try
         {
             using (MySqlConnectionSupport support = new MySqlConnectionSupport())
             {
                 support.Initialize(null);
                 support.ConnectionString = builder.ConnectionString;
                 support.Open(false);
                 this.dbList.Items.Clear();
                 using (DataReader reader = support.Execute("SHOW DATABASES", 1, null, 0))
                 {
                     while (reader.Read())
                     {
                         string str = reader.GetItem(0).ToString().ToLowerInvariant();
                         if (!(str == "information_schema") && !(str == "mysql"))
                         {
                             this.dbList.Items.Add(reader.GetItem(0));
                         }
                     }
                     this.dbListPopulated = true;
                 }
             }
         }
         catch (Exception)
         {
             MessageBox.Show("Unable to retrieve the list of databases");
         }
     }
 }
        private bool AttemptToCreateDatabase()
        {
            bool flag;
            MySqlConnectionProperties connectionProperties    = (MySqlConnectionProperties)(base.ConnectionProperties);
            DbConnectionStringBuilder connectionStringBuilder = connectionProperties.ConnectionStringBuilder;
            string str = (string)connectionStringBuilder["Database"];

            connectionStringBuilder["Database"] = "";
            try
            {
                using (MySqlConnectionSupport support = new MySqlConnectionSupport())
                {
                    support.Initialize(null);
                    support.ConnectionString = connectionStringBuilder.ConnectionString;
                    support.Open(false);
                    support.ExecuteWithoutResults("CREATE DATABASE `" + this.dbList.Text + "`", 1, null, 0);
                }
                flag = true;
            }
            catch (Exception)
            {
                MessageBox.Show(string.Format("There was an error attempting to create the database '{0}'", this.dbList.Text));
                flag = false;
            }
            finally
            {
                connectionStringBuilder["Database"] = str;
            }
            return(flag);
        }
Exemple #5
0
        internal static List <string> GetSchemas(MySqlConnectionStringBuilder csb)
        {
            var schemas = new List <string>();

            try
            {
                using (MySqlConnectionSupport conn = new MySqlConnectionSupport())
                {
                    conn.Initialize(null);
                    conn.ConnectionString = csb.ConnectionString;
                    conn.Open(false);
                    using (DataReader reader = conn.Execute("SHOW DATABASES", 1, null, 0))
                    {
                        while (reader.Read())
                        {
                            string dbName = reader.GetItem(0).ToString().ToLowerInvariant();
                            if (dbName == "performance_schema")
                            {
                                continue;
                            }
                            if (dbName == "information_schema")
                            {
                                continue;
                            }
                            if (dbName == "mysql")
                            {
                                continue;
                            }
                            schemas.Add(dbName);
                        }
                    }
                }
                return(schemas);
            }
            catch
            {
                return(null);
            }
        }
        private bool DatabaseExists()
        {
            DbConnectionStringBuilder builder = ((base.ConnectionProperties as MySqlConnectionProperties).ConnectionStringBuilder);

            try
            {
                using (MySqlConnectionSupport support = new MySqlConnectionSupport())
                {
                    support.Initialize(null);
                    support.ConnectionString = (builder.ConnectionString);
                    support.Open(false);
                }
                return(true);
            }
            catch (DbException exception)
            {
                if (!exception.Message.ToLowerInvariant().StartsWith("unknown database", StringComparison.OrdinalIgnoreCase))
                {
                    throw;
                }
                return(false);
            }
        }
        /// <summary>
        /// Reads table with Database Objects which satisfy given restriction. Applies filter for aditional restriction
        /// if MySQL version is less then 5.0.
        /// </summary>
        /// <param name="connection">The DataConnectionWrapper to be used for enumeration.</param>
        /// <param name="restrictions">The restrictions to be putted on the retrieved objects set.</param>
        /// <param name="sort">Sort expresion to append after ORDER BY clause.</param>
        /// <returns>Returns table with Database Objects which satisfy given restriction.</returns>
        protected override DataTable ReadTable(DataConnectionWrapper connection, object[] restrictions, string sort)
        {
            if (connection == null)
            {
                throw new ArgumentNullException("connection");
            }

            // Use base method to read table
            DataTable first_result = base.ReadTable(connection, restrictions, sort);

            // fixup collation names
            DataTable result = MySqlConnectionSupport.ConvertAllBinaryColumns(first_result);

            // If there is now result from bas, return immediately
            if (result == null)
            {
                return(result);
            }

            // Extract server version
            Version serverVersion = connection.ServerVersion;

            // For latest version just call base
            if (serverVersion == null || serverVersion.Major >= 5)
            {
                return(result);
            }

            // For legacy version apply restrictions to result manualy (first slot - character set name, third slot - flag is default)

            // At first check if there is any restrictions
            if (restrictions == null || restrictions.Length <= 0)
            {
                return(result);
            }
            if (String.IsNullOrEmpty(restrictions[0] as String) &&
                (restrictions.Length < 3 || String.IsNullOrEmpty(restrictions[2] as String)))
            {
                return(result);
            }

            // Iterates through rows and filter them
            foreach (DataRow collation in result.Select())
            {
                // Apply character set name filter
                if (!String.IsNullOrEmpty(restrictions[0] as String) &&
                    !DataInterpreter.CompareInvariant(
                        restrictions[0] as String,
                        DataInterpreter.GetStringNotNull(collation, Attributes.CharacterSetName)))
                {
                    collation.Delete();
                    continue;
                }

                // Apply is_default constraint
                if (restrictions.Length >= 3 && !String.IsNullOrEmpty(restrictions[2] as String) &&
                    !DataInterpreter.CompareInvariant(
                        restrictions[2] as String,
                        DataInterpreter.GetStringNotNull(collation, Attributes.IsDefault)))
                {
                    collation.Delete();
                    continue;
                }
            }

            // Accept changes and return results
            result.AcceptChanges();
            return(result);
        }