Ejemplo n.º 1
0
        /// <summary>
        /// 获取表名
        /// </summary>
        /// <param name="data"></param>
        /// <param name="manager"></param>
        /// <returns></returns>
        public string GetTableName(PocoData data, TableNameManager manager = null)
        {
            var ti           = data.TableInfo;
            var databaseName = ti.DatabaseName;
            var schemaName   = ti.SchemaName;
            var tableName    = ti.TableName;

            if (manager != null && manager.IsUsed)
            {
                var setting = manager.TryGetSetting(ti.SettingName);
                if (string.IsNullOrEmpty(databaseName))
                {
                    if (string.IsNullOrEmpty(setting.DatabaseNameNullText) == false)
                    {
                        databaseName = setting.DatabaseNameNullText;
                    }
                }
                else
                {
                    if (string.IsNullOrEmpty(setting.DatabaseNamePrefixText) == false)
                    {
                        databaseName = setting.DatabaseNamePrefixText + databaseName;
                    }
                    if (string.IsNullOrEmpty(setting.DatabaseNameSuffixText) == false)
                    {
                        databaseName = databaseName + setting.DatabaseNameNullText;
                    }
                }

                if (string.IsNullOrEmpty(schemaName))
                {
                    if (string.IsNullOrEmpty(setting.SchemaNameNullText) == false)
                    {
                        schemaName = setting.SchemaNameNullText;
                    }
                }
                else
                {
                    if (string.IsNullOrEmpty(setting.SchemaNamePrefixText) == false)
                    {
                        schemaName = setting.SchemaNamePrefixText + schemaName;
                    }
                    if (string.IsNullOrEmpty(setting.SchemaNameSuffixText) == false)
                    {
                        schemaName = schemaName + setting.SchemaNameSuffixText;
                    }
                }

                if (string.IsNullOrEmpty(setting.TableNamePrefixText) == false)
                {
                    tableName = setting.TableNamePrefixText + tableName;
                }
                if (string.IsNullOrEmpty(setting.TableNameSuffixText) == false)
                {
                    tableName = tableName + setting.TableNameSuffixText;
                }
            }
            return(GetTableName(databaseName, schemaName, tableName));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 获取表名 ,不带databaseName , schemaName
        /// </summary>
        /// <param name="data"></param>
        /// <param name="manager"></param>
        /// <returns></returns>
        public string GetMiniTableName(PocoData data, TableNameManager manager = null)
        {
            var ti        = data.TableInfo;
            var tableName = ti.TableName;

            if (manager != null && manager.IsUsed)
            {
                var setting = manager.TryGetSetting(ti.SettingName);
                if (string.IsNullOrEmpty(setting.TableNamePrefixText) == false)
                {
                    tableName = setting.TableNamePrefixText + tableName;
                }
                if (string.IsNullOrEmpty(setting.TableNameSuffixText) == false)
                {
                    tableName = tableName + setting.TableNameSuffixText;
                }
            }
            return(GetTableName(null, null, tableName));
        }
Ejemplo n.º 3
0
        public void Execute()
        {
            try
            {
                lock (_lock)
                {
                    var connection = _dbContext.Database.GetDbConnection();

                    bool isConnectionClosed = connection.State == ConnectionState.Closed;

                    if (isConnectionClosed)
                    {
                        connection.Open();
                    }

                    var existingTableNames = new List <string>();
                    using (var command = connection.CreateCommand())
                    {
                        command.CommandText = "SELECT table_name from INFORMATION_SCHEMA.TABLES WHERE table_type = 'base table'";

                        using (var reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                existingTableNames.Add(reader.GetString(0).ToLowerInvariant());
                            }
                        }
                    }

                    var tableNames = new string[] { TableNameManager.GetCurrentDayTableName(), TableNameManager.GetTableNameByDay(DateTime.UtcNow.AddDays(1)), TableNameManager.GetTableNameByDay(DateTime.UtcNow.AddDays(2)) };

                    foreach (var newTableName in tableNames)
                    {
                        if (!existingTableNames.Contains(newTableName.ToLower()))
                        {
                            using (var createCommand = connection.CreateCommand())
                            {
                                createCommand.CommandText = $@"CREATE TABLE [dbo].[{newTableName}](
	[Id] [uniqueidentifier] NOT NULL,
	[Time] [datetime2](7) NOT NULL,
	[Timeout] [int] NOT NULL,
	[EventName] [nvarchar](max) NULL,
	[IsError] [bit] NOT NULL,
 CONSTRAINT [PK_{newTableName}] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY];
ALTER TABLE [dbo].[{newTableName}] ADD  DEFAULT (CONVERT([bit],(0))) FOR [IsError];";
                                createCommand.ExecuteNonQuery();
                            }
                        }
                    }



                    if (isConnectionClosed)
                    {
                        connection.Close();
                    }
                }
            }
            finally
            {
            }
        }