public void GetAndCreateTest()
        {
            GeneratorSettings generatorSettings = new GeneratorSettings();
            UniqueNamer uniqueNamer = new UniqueNamer();
            TemplateContent templateContent = new TemplateContent(generatorSettings, uniqueNamer);

            AbpDbContentTemplate abpDbContentTemplate = new AbpDbContentTemplate(templateContent);
            MySQLSchemaProvider mySqlSchemaProvider = new MySQLSchemaProvider();
            const string mysqlConn = "Server=127.0.0.1;Database=esms;Uid=root;Pwd=123qwe!@#;";
            SchemaSelector schemaSelector = new SchemaSelector(mySqlSchemaProvider, mysqlConn);

            foreach (TableSchema tableSchema in schemaSelector.Tables)
            {
                foreach (DataObjectBase columnSchema in tableSchema.Columns)
                {
                    Console.WriteLine(columnSchema.FullName);
                }
            }

            //EntityContext<AbpEntity, AbpEntityProperty> entityContext = abpDbContentTemplate.GetAndCreate(schemaSelector);
            //Entity<AbpEntityProperty> entity = entityContext.Entities.First(t => t.ClassName == "SysUser");

            //foreach (Relationship relationship in entity.Relationships.Where(t=>t.ThisEntity.Contains("SysUser")&&t.ThisPropertyName=="LastRole"))
            //{
            //    Console.WriteLine(relationship.JoinTable);
            //}
        }
Esempio n. 2
0
        public CommandSchema[] GetCommands(string connectionString, DatabaseSchema database)
        {
            string commandText        = string.Format("SELECT ROUTINE_NAME, '' OWNER, CREATED FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_SCHEMA = '{0}' AND ROUTINE_TYPE = 'PROCEDURE' ORDER BY 1", database.Name);
            List <CommandSchema> list = new List <CommandSchema>();

            using (DbConnection dbConnection = MySQLSchemaProvider.CreateConnection(connectionString))
            {
                dbConnection.Open();
                DbCommand dbCommand = dbConnection.CreateCommand();
                dbCommand.CommandText = commandText;
                dbCommand.Connection  = dbConnection;
                using (IDataReader dataReader = dbCommand.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    while (dataReader.Read())
                    {
                        list.Add(new CommandSchema(database, dataReader.GetString(0), dataReader.GetString(1), dataReader.GetDateTime(2)));
                    }
                    if (!dataReader.IsClosed)
                    {
                        dataReader.Close();
                    }
                }
                if (dbConnection.State != ConnectionState.Closed)
                {
                    dbConnection.Close();
                }
            }
            return(list.ToArray());
        }
Esempio n. 3
0
        public void GenerateTest()
        {
            GeneratorSettings generatorSettings = new GeneratorSettings();
            MySQLSchemaProvider mySqlSchemaProvider = new MySQLSchemaProvider();
            const string mysqlConn = "Server=127.0.0.1;Database=yt_qd_ytsf;Uid=root;Pwd=123qwe!@#;";
            SchemaSelector schemaSelector = new SchemaSelector(mySqlSchemaProvider, mysqlConn);
            Generator generator = new Generator();
            generator.Settings.TableNaming = generatorSettings.TableNaming;
            generator.Settings.EntityNaming = generatorSettings.EntityNaming;
            generator.Settings.RelationshipNaming = generatorSettings.RelationshipNaming;
            generator.Settings.ContextNaming = generatorSettings.ContextNaming;
            EntityContext entityContext = generator.Generate(schemaSelector);
            foreach (Entity entity in entityContext.Entities)
            {
                Property property =
                    entity.Properties.FirstOrDefault(
                        t => (t.IsPrimaryKey.HasValue && t.IsPrimaryKey.Value) && (t.IsIdentity.HasValue && t.IsIdentity.Value));
                Console.WriteLine(entity.ClassName + " --- " + (property != null ? property.PropertyName : ""));

            }
        }
Esempio n. 4
0
        public ColumnSchema[] GetTableColumns(string connectionString, TableSchema table)
        {
            string commandText       = string.Format("SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_OCTET_LENGTH, NUMERIC_PRECISION, NUMERIC_SCALE, CASE IS_NULLABLE WHEN 'NO' THEN 0 ELSE 1 END IS_NULLABLE, COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '{0}' AND TABLE_NAME = '{1}' ORDER BY ORDINAL_POSITION", table.Database.Name, table.Name);
            List <ColumnSchema> list = new List <ColumnSchema>();

            using (DbConnection dbConnection = MySQLSchemaProvider.CreateConnection(connectionString))
            {
                dbConnection.Open();
                DbCommand dbCommand = dbConnection.CreateCommand();
                dbCommand.CommandText = commandText;
                dbCommand.Connection  = dbConnection;
                using (IDataReader dataReader = dbCommand.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    while (dataReader.Read())
                    {
                        string @string    = dataReader.GetString(0);
                        string string2    = dataReader.GetString(1);
                        long   num        = (!dataReader.IsDBNull(2)) ? dataReader.GetInt64(2) : 0L;
                        byte   b          = (byte)((!dataReader.IsDBNull(3)) ? dataReader.GetInt32(3) : 0);
                        int    num2       = (!dataReader.IsDBNull(4)) ? dataReader.GetInt32(4) : 0;
                        bool   flag       = !dataReader.IsDBNull(5) && dataReader.GetBoolean(5);
                        string string3    = dataReader.GetString(6);
                        int    num3       = (num < 2147483647L) ? ((int)num) : 2147483647;
                        bool   isUnsigned = string3.IndexOf("unsigned") > -1;
                        DbType dbType     = string2.ToDbType(isUnsigned);
                        list.Add(new ColumnSchema(table, @string, dbType, string2, num3, b, num2, flag));
                    }
                    if (!dataReader.IsClosed)
                    {
                        dataReader.Close();
                    }
                }
                if (dbConnection.State != ConnectionState.Closed)
                {
                    dbConnection.Close();
                }
            }
            return(list.ToArray());
        }
Esempio n. 5
0
        public ExtendedProperty[] GetExtendedProperties(string connectionString, SchemaObjectBase schemaObject)
        {
            List <ExtendedProperty> list = new List <ExtendedProperty>();

            if (schemaObject is ColumnSchema)
            {
                ColumnSchema columnSchema = schemaObject as ColumnSchema;
                string       commandText  = string.Format(
                    @"SELECT EXTRA, COLUMN_DEFAULT, COLUMN_TYPE,COLUMN_COMMENT
                     FROM INFORMATION_SCHEMA.COLUMNS
                     WHERE TABLE_SCHEMA = '{0}' AND TABLE_NAME = '{1}' AND COLUMN_NAME = '{2}'", columnSchema.Table.Database.Name, columnSchema.Table.Name, columnSchema.Name);
                using (DbConnection dbConnection = MySQLSchemaProvider.CreateConnection(connectionString))
                {
                    dbConnection.Open();
                    DbCommand dbCommand = dbConnection.CreateCommand();
                    dbCommand.CommandText = commandText;
                    dbCommand.Connection  = dbConnection;
                    using (IDataReader dataReader = dbCommand.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        while (dataReader.Read())
                        {
                            string text  = dataReader.GetString(0).ToLower();
                            bool   flag  = dataReader.IsDBNull(1);
                            string text2 = "";
                            if (!flag)
                            {
                                text2 = dataReader.GetString(1).ToUpper();
                            }
                            string text3       = dataReader.GetString(2).ToUpper();
                            string textCOMMENT = dataReader.GetString(3).ToUpper();
                            bool   flag2       = text.IndexOf("auto_increment") > -1;
                            list.Add(new ExtendedProperty("CS_IsIdentity", flag2, columnSchema.DataType));
                            if (flag2)
                            {
                                list.Add(new ExtendedProperty("CS_IdentitySeed", 1, columnSchema.DataType));
                                list.Add(new ExtendedProperty("CS_IdentityIncrement", 1, columnSchema.DataType));
                            }
                            list.Add(new ExtendedProperty("CS_ColumnDefaultIsNull", flag, DbType.Boolean));
                            list.Add(new ExtendedProperty("CS_Default", text2, DbType.String));
                            list.Add(new ExtendedProperty("CS_ColumnDefault", text2, DbType.String));
                            list.Add(new ExtendedProperty("CS_SystemType", text3, DbType.String));
                            list.Add(new ExtendedProperty("CS_ColumnType", text3, DbType.String));
                            list.Add(new ExtendedProperty("CS_Description", textCOMMENT, DbType.String));
                            list.Add(new ExtendedProperty("CS_ColumnExtra", text.ToUpper(), DbType.String));
                        }
                        if (!dataReader.IsClosed)
                        {
                            dataReader.Close();
                        }
                    }
                    if (dbConnection.State != ConnectionState.Closed)
                    {
                        dbConnection.Close();
                    }
                }
            }
            if (schemaObject is TableSchema)
            {
                TableSchema tableSchema  = schemaObject as TableSchema;
                string      commandText2 = string.Format("SHOW CREATE TABLE `{0}`.`{1}`", tableSchema.Database.Name, tableSchema.Name);
                using (DbConnection dbConnection2 = MySQLSchemaProvider.CreateConnection(connectionString))
                {
                    dbConnection2.Open();
                    DbCommand dbCommand2 = dbConnection2.CreateCommand();
                    dbCommand2.CommandText = commandText2;
                    dbCommand2.Connection  = dbConnection2;
                    using (IDataReader dataReader2 = dbCommand2.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        while (dataReader2.Read())
                        {
                            string @string = dataReader2.GetString(1);
                            list.Add(new ExtendedProperty("CS_CreateTableScript", @string, DbType.String));
                            list.Add(new ExtendedProperty("CS_CreateTableScript", @string, DbType.String));
                        }
                        if (!dataReader2.IsClosed)
                        {
                            dataReader2.Close();
                        }
                    }
                    if (dbConnection2.State != ConnectionState.Closed)
                    {
                        dbConnection2.Close();
                    }
                }
                string commandTextTABLES = string.Format(
                    @"SELECT TABLE_COMMENT
                     FROM INFORMATION_SCHEMA.TABLES
                     WHERE TABLE_SCHEMA = '{0}' AND TABLE_NAME = '{1}'", tableSchema.Database.Name, tableSchema.Name);
                using (DbConnection dbConnection3 = MySQLSchemaProvider.CreateConnection(connectionString))
                {
                    dbConnection3.Open();
                    DbCommand dbCommand2 = dbConnection3.CreateCommand();
                    dbCommand2.CommandText = commandTextTABLES;
                    dbCommand2.Connection  = dbConnection3;
                    using (IDataReader dataReader2 = dbCommand2.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        while (dataReader2.Read())
                        {
                            string textCOMMENT = dataReader2.GetString(0);
                            list.Add(new ExtendedProperty("CS_Description", textCOMMENT, DbType.String));
                        }
                        if (!dataReader2.IsClosed)
                        {
                            dataReader2.Close();
                        }
                    }
                    if (dbConnection3.State != ConnectionState.Closed)
                    {
                        dbConnection3.Close();
                    }
                }
            }
            return(list.ToArray());
        }