예제 #1
0
 public List <T> ReadModelsFromReader <T>(IDataReader Reader, int limit = -1)
 {
     if (TypeHelper != null && TypeHelper.GetSQLTypeIndexed(typeof(T)) != null)
     {
         return(ReadSQLBaseTypesUnsafe <T>(Reader, limit));
     }
     else
     {
         return(ReadClasses <T>(Reader, limit));
     }
 }
예제 #2
0
        public List <T> ReadSQLBaseTypes <T>(MySqlDataReader Reader, SQLTypeHelper Helper, int limit = -1)
        {
            SQLType Type = Helper.GetSQLTypeIndexed(typeof(T));

            if (Type == null)
            {
                throw new SQLIncompatableTypeException();
            }
            return(ReadSQLBaseTypesUnsafe <T>(Reader, limit));
        }
예제 #3
0
        public MySqlCommand BuildCreateTableCommand <T>(string TableName, MySqlConnection Connection = null)
        {
            List <SQLBuildField> fields = new List <SQLBuildField>();
            string dbEngine             = "InnoDB";

            if (Attribute.IsDefined(typeof(T), typeof(SQLDatabaseEngine)))
            {
                Attribute attrib = Attribute.GetCustomAttribute(typeof(T), typeof(SQLDatabaseEngine));
                dbEngine = ((SQLDatabaseEngine)attrib).DatabaseEngine;
            }
            foreach (FieldInfo field in typeof(T).GetFields())
            {
                SQLBuildField buildField = new SQLBuildField()
                {
                    Name = field.Name
                };
                bool include = true;
                foreach (Attribute attrib in Attribute.GetCustomAttributes(field))
                {
                    switch (attrib)
                    {
                    case SQLIgnore _:
                        include = false;
                        break;

                    case SQLAutoIncrement _:
                        buildField.AutoIncrement = true;
                        break;

                    case SQLDefault DefaultVal:
                        buildField.Default = DefaultVal.DefaultValue;
                        break;

                    case SQLPrimaryKey _:
                        buildField.PrimaryKey = true;
                        break;

                    case SQLPropertyName name:
                        buildField.Name = name.Name;
                        break;

                    case SQLNull _:
                        buildField.Null = true;
                        break;

                    case SQLUnique _:
                        buildField.Unique = true;
                        break;

                    case SQLIndex _:
                        buildField.Indexed = true;
                        break;

                    default:
                        if (attrib.GetType().BaseType == typeof(SQLType))
                        {
                            buildField.Type = ((SQLType)attrib);
                        }
                        break;
                    }
                }
                if (include && fields.Where(x => string.Equals(x.Name, buildField.Name, StringComparison.InvariantCultureIgnoreCase)).Count() == 0)
                {
                    if (buildField.Type == null)
                    {
                        buildField.Type = m_TypeHelper.GetSQLTypeIndexed(field.FieldType);
                    }
                    if (buildField.Type == null)
                    {
                        throw new SQLIncompatableTypeException(field.Name);
                    }
                    fields.Add(buildField);
                }
            }

            StringBuilder commandBuilder = new StringBuilder();

            commandBuilder.AppendLine($"CREATE TABLE `{MySqlHelper.EscapeString(TableName)}` (");
            List <string> bodyParams = new List <string>();
            List <object> defaults   = new List <object>();

            foreach (SQLBuildField field in fields)
            {
                bodyParams.Add($"    `{field.Name}` {field.SQLRepresentation} {(field.Null ? "NULL" : "NOT NULL")}{(field.Default != null ? $" DEFAULT @DEF{defaults.Count}" : "")}{(field.AutoIncrement ? " AUTO_INCREMENT" : "")}");

                if (field.Default != null)
                {
                    defaults.Add(field.Default);
                }
                if (field.PrimaryKey)
                {
                    bodyParams.Add($"    PRIMARY KEY (`{field.Name}`)");
                }
                if (field.Unique)
                {
                    bodyParams.Add($"    UNIQUE `{field.Name}_Unique` (`{field.Name}`)");
                }
                if (field.Indexed)
                {
                    bodyParams.Add($"    INDEX `{field.Name}_INDEX` (`{field.Name}`)");
                }
            }
            commandBuilder.AppendLine(string.Join(",\n", bodyParams));
            commandBuilder.Append($") ENGINE = {dbEngine};");
            MySqlCommand sqlCommand = (Connection != null ? new MySqlCommand(commandBuilder.ToString(), Connection) : new MySqlCommand(commandBuilder.ToString()));

            foreach (object def in defaults)
            {
                sqlCommand.Parameters.AddWithValue($"@DEF{defaults.IndexOf(def)}", def);
            }
            return(sqlCommand);
        }