コード例 #1
0
        public static MySQLTable GetTable(MySQL sql, string table)
        {
            MySQLTable t = new MySQLTable();

            t.Name = table;

            foreach (DataRow row in sql.RetrieveDataTable($"SHOW COLUMNS FROM {table}").Rows)
            {
                t.Columns.Add(new ColumnProperty(MySQLHelper.Instance, (string)row["Field"], (string)row["Type"]));
            }
            return(t);
        }
コード例 #2
0
        public bool SyncObjectToTable(string collection, Type type)
        {
            List <ColumnProperty> columns = ColumnProperty.GetCollumns(MySQLHelper.Instance, type, true).Values.ToList();

            if (!MySQLTable.GetTables(this).Contains(collection))
            {
                MySQLTable.CreateTable(this, collection, columns);
                return(true);
            }
            else
            {
                MySQLTable            table = MySQLTable.GetTable(this, collection);
                List <ColumnProperty> todo  = table.Columns.ToList();
                foreach (ColumnProperty col in columns)
                {
                    ColumnProperty existing = table.Columns.FirstOrDefault(X => X.Name == col.Name);
                    if (existing == null)
                    {
                        System.Console.WriteLine($"SQL missing Column {col.Name}... Adding");
                        if (!MySQLTable.AddColumn(this, collection, col.Name, col.SqlType))
                        {
                            throw new Exception($"Failed to add collumn {col.Name}");
                        }
                    }
                    else if (!existing.SqlType.StartsWith(col.SqlType))
                    {
                        System.Console.WriteLine($"SQL incorrect Column Type for {col.Name}... Converting");
                        if (!MySQLTable.ConvertColumn(this, collection, col.Name, col.SqlType))
                        {
                            throw new Exception($"Failed to convert collumn {col.Name}");
                        }
                    }
                    if (existing != null)
                    {
                        todo.Remove(existing);
                    }
                }
                foreach (ColumnProperty prop in todo)
                {
                    System.Console.WriteLine($"Excess collumn {prop.Name}... Removing");
                    if (!MySQLTable.RemoveColumn(this, collection, prop.Name))
                    {
                        throw new Exception($"Failed to remove collumn {prop.Name}");
                    }
                }
            }
            return(true);
        }
コード例 #3
0
        public bool LoadCollection <C>() where C : UnifiedIMObject <C>
        {
            string collection = UnifiedCollectionAttribute.GetCollection <C>();

            if (string.IsNullOrEmpty(collection))
            {
                throw new Exception($"Missing UnifiedCollectionAttribute on type {typeof(C).Name}");
            }
            TableCache.Add(typeof(C), collection);
            ColumnCache.Add(typeof(C), ColumnProperty.GetCollumns <C>(MySQLHelper.Instance, true, "ObjectID").Values.ToList());
            ColumnProperty objidcol = GetColumns <C>().FirstOrDefault(X => X.Name == "ObjectID");

            foreach (ColumnProperty prop in GetColumns <C>())
            {
                if (prop.Name == "ObjectID")
                {
                    prop.OverrideSqlType("char(32)");
                }
                else
                {
                    prop.OverrideSqlType(MySQLHelper.Instance.GetSqlType(prop.Type, prop.Column));
                }
            }

            if (!MySQLTable.GetTables(SQL).Contains(collection))
            {
                try
                {
                    MySQLTable.CreateTable(SQL, collection, GetColumns <C>());
                }
                catch (Exception ex)
                {
                    throw new Exception($"Failed to create table {collection}");
                }
            }
            else
            {
                MySQLTable            table = MySQLTable.GetTable(SQL, collection);
                List <ColumnProperty> todo  = table.Columns.ToList();
                foreach (ColumnProperty col in GetColumns <C>())
                {
                    ColumnProperty existing = table.Columns.FirstOrDefault(X => X.Name == col.Name);
                    if (existing == null)
                    {
                        System.Console.WriteLine($"SQL missing Column {col.Name}... Adding");
                        try
                        {
                            MySQLTable.AddColumn(SQL, collection, col.Name, col.SqlType);
                        }
                        catch (Exception ex)
                        {
                            throw new Exception($"Failed to add collumn {col.Name}\n{ex.Message}");
                        }
                    }
                    else if (!existing.SqlType.StartsWith(col.SqlType))
                    {
                        System.Console.WriteLine($"SQL incorrect Column Type for {col.Name}... Converting");
                        try
                        {
                            MySQLTable.ConvertColumn(SQL, collection, col.Name, col.SqlType);
                        }
                        catch (Exception ex)
                        {
                            throw new Exception($"Failed to convert collumn {col.Name}\n{ex.Message}");
                        }
                    }
                    if (existing != null)
                    {
                        todo.Remove(existing);
                    }
                }
                foreach (ColumnProperty prop in todo)
                {
                    System.Console.WriteLine($"Excess collumn {prop.Name}... Removing");
                    try
                    {
                        MySQLTable.RemoveColumn(SQL, collection, prop.Name);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception($"Failed to remove collumn {prop.Name}\n{ex.Message}");
                    }
                }
            }

            return(true);
        }