예제 #1
0
        public SqlServerProvider(IDistributedCache cache, ILogger log, string masterConnectionString, string[] slaveConnectionString)
        {
            if (log == null)
            {
                log = new LoggerFactory(new[] { new Microsoft.Extensions.Logging.Debug.DebugLoggerProvider() }).CreateLogger("FreeSql.SqlServer");
            }

            this.InternalCommonUtils      = new SqlServerUtils(this);
            this.InternalCommonExpression = new SqlServerExpression(this.InternalCommonUtils);

            this.Cache = new CacheProvider(cache, log);
            this.Ado   = new SqlServerAdo(this.InternalCommonUtils, this.Cache, log, masterConnectionString, slaveConnectionString);
            this.Aop   = new AopProvider();

            this.DbFirst   = new SqlServerDbFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
            this.CodeFirst = new SqlServerCodeFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);

            if (this.Ado.MasterPool != null)
            {
                using (var conn = this.Ado.MasterPool.Get()) {
                    try {
                        (this.InternalCommonUtils as SqlServerUtils).IsSelectRowNumber = int.Parse(conn.Value.ServerVersion.Split('.')[0]) <= 10;
                    } catch {
                    }
                }
            }
        }
예제 #2
0
        public SqlServerProvider(string masterConnectionString, string[] slaveConnectionString)
        {
            this.InternalCommonUtils      = new SqlServerUtils(this);
            this.InternalCommonExpression = new SqlServerExpression(this.InternalCommonUtils);

            this.Ado = new SqlServerAdo(this.InternalCommonUtils, masterConnectionString, slaveConnectionString);
            this.Aop = new AopProvider();

            this.DbFirst   = new SqlServerDbFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
            this.CodeFirst = new SqlServerCodeFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);

            if (this.Ado.MasterPool != null)
            {
                using (var conn = this.Ado.MasterPool.Get())
                {
                    try
                    {
                        (this.InternalCommonUtils as SqlServerUtils).IsSelectRowNumber = int.Parse(conn.Value.ServerVersion.Split('.')[0]) <= 10;
                    }
                    catch
                    {
                    }
                }
            }
        }
예제 #3
0
        public void Build(IDbFirst dbfirst, string templateDirectory, string outputDirectory, params string[] database)
        {
            if (dbfirst == null)
            {
                throw new ArgumentException("dbfirst 参数不能为 null");
            }
            if (string.IsNullOrEmpty(templateDirectory) || Directory.Exists(templateDirectory) == false)
            {
                throw new ArgumentException("templateDirectory 目录不存在");
            }
            if (string.IsNullOrEmpty(templateDirectory))
            {
                throw new ArgumentException("outputDirectory 不能为 null");
            }
            if (database == null || database.Any() == false)
            {
                throw new ArgumentException("database 参数不能为空");
            }
            if (Directory.Exists(outputDirectory) == false)
            {
                Directory.CreateDirectory(outputDirectory);
            }
            templateDirectory = new DirectoryInfo(templateDirectory).FullName;
            outputDirectory   = new DirectoryInfo(outputDirectory).FullName;
            if (templateDirectory.IndexOf(outputDirectory, StringComparison.CurrentCultureIgnoreCase) != -1)
            {
                throw new ArgumentException("outputDirectory 目录不能设置在 templateDirectory 目录内");
            }
            var tables = dbfirst.GetTablesByDatabase(database);
            var tpl    = new TemplateEngin(templateDirectory, "FreeSql", "FreeSql.DatabaseModel");

            BuildEachDirectory(templateDirectory, outputDirectory, tpl, dbfirst, tables);
            tpl.Dispose();
        }
예제 #4
0
        public OdbcSqlServerProvider(string masterConnectionString, string[] slaveConnectionString, Func <DbConnection> connectionFactory = null)
        {
            this.InternalCommonUtils      = new OdbcSqlServerUtils(this);
            this.InternalCommonExpression = new OdbcSqlServerExpression(this.InternalCommonUtils);

            this.Ado = new OdbcSqlServerAdo(this.InternalCommonUtils, masterConnectionString, slaveConnectionString, connectionFactory);
            this.Aop = new AopProvider();

            this.DbFirst   = new OdbcSqlServerDbFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
            this.CodeFirst = new OdbcSqlServerCodeFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);

            if (this.Ado.MasterPool != null)
            {
                try
                {
                    using (var conn = this.Ado.MasterPool.Get())
                    {
                        (this.InternalCommonUtils as OdbcSqlServerUtils).ServerVersion = int.Parse(conn.Value.ServerVersion.Split('.')[0]);
                    }
                }
                catch
                {
                }
            }
        }
예제 #5
0
        public OdbcPostgreSQLProvider(string masterConnectionString, string[] slaveConnectionString)
        {
            this.InternalCommonUtils      = new OdbcPostgreSQLUtils(this);
            this.InternalCommonExpression = new OdbcPostgreSQLExpression(this.InternalCommonUtils);

            this.Ado = new OdbcPostgreSQLAdo(this.InternalCommonUtils, masterConnectionString, slaveConnectionString);
            this.Aop = new AopProvider();

            this.DbFirst   = new OdbcPostgreSQLDbFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
            this.CodeFirst = new OdbcPostgreSQLCodeFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
        }
예제 #6
0
        public KingbaseESProvider(string masterConnectionString, string[] slaveConnectionString, Func <DbConnection> connectionFactory = null)
        {
            this.InternalCommonUtils      = new KingbaseESUtils(this);
            this.InternalCommonExpression = new KingbaseESExpression(this.InternalCommonUtils);

            this.Ado = new KingbaseESAdo(this.InternalCommonUtils, masterConnectionString, slaveConnectionString, connectionFactory);
            this.Aop = new AopProvider();

            this.DbFirst   = new KingbaseESDbFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
            this.CodeFirst = new KingbaseESCodeFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
        }
예제 #7
0
        public OracleProvider(string masterConnectionString, string[] slaveConnectionString)
        {
            this.InternalCommonUtils      = new OracleUtils(this);
            this.InternalCommonExpression = new OracleExpression(this.InternalCommonUtils);

            this.Ado = new OracleAdo(this.InternalCommonUtils, masterConnectionString, slaveConnectionString);
            this.Aop = new AopProvider();

            this.DbFirst   = new OracleDbFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
            this.CodeFirst = new OracleCodeFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
        }
예제 #8
0
        public OdbcDamengProvider(string masterConnectionString, string[] slaveConnectionString, Func <DbConnection> connectionFactory = null)
        {
            this.InternalCommonUtils      = new OdbcDamengUtils(this);
            this.InternalCommonExpression = new OdbcDamengExpression(this.InternalCommonUtils);

            this.Ado = new OdbcDamengAdo(this.InternalCommonUtils, masterConnectionString, slaveConnectionString, connectionFactory);
            this.Aop = new AopProvider();

            this.DbFirst   = new OdbcDamengDbFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
            this.CodeFirst = new OdbcDamengCodeFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
        }
예제 #9
0
        public OdbcMySqlProvider(string masterConnectionString, string[] slaveConnectionString)
        {
            this.InternalCommonUtils      = new OdbcMySqlUtils(this);
            this.InternalCommonExpression = new OdbcMySqlExpression(this.InternalCommonUtils);

            this.Ado = new OdbcMySqlAdo(this.InternalCommonUtils, masterConnectionString, slaveConnectionString);
            this.Aop = new AopProvider();

            this.DbFirst   = new OdbcMySqlDbFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
            this.CodeFirst = new OdbcMySqlCodeFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
        }
예제 #10
0
        public FirebirdProvider(string masterConnectionString, string[] slaveConnectionString, Func <DbConnection> connectionFactory = null)
        {
            this.InternalCommonUtils      = new FirebirdUtils(this);
            this.InternalCommonExpression = new FirebirdExpression(this.InternalCommonUtils);

            this.Ado = new FirebirdAdo(this.InternalCommonUtils, masterConnectionString, slaveConnectionString, connectionFactory);
            this.Aop = new AopProvider();

            this.DbFirst   = new FirebirdDbFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
            this.CodeFirst = new FirebirdCodeFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
        }
예제 #11
0
        }                               // => throw new NotImplementedException("FreeSql.Provider.Sqlite 未实现该功能");
        public SqliteProvider(string masterConnectionString, string[] slaveConnectionString, Func <DbConnection> connectionFactory = null)
        {
            this.InternalCommonUtils      = new SqliteUtils(this);
            this.InternalCommonExpression = new SqliteExpression(this.InternalCommonUtils);

            this.Ado = new SqliteAdo(this.InternalCommonUtils, masterConnectionString, slaveConnectionString, connectionFactory);
            this.Aop = new AopProvider();

            this.CodeFirst = new SqliteCodeFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
            this.DbFirst   = new SqliteDbFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
            if (connectionFactory != null)
            {
                this.CodeFirst.IsNoneCommandParameter = true;
            }
        }
예제 #12
0
        public MySqlProvider(IDistributedCache cache, ILogger log, string masterConnectionString, string[] slaveConnectionString)
        {
            if (log == null)
            {
                log = new LoggerFactory(new[] { new Microsoft.Extensions.Logging.Debug.DebugLoggerProvider() }).CreateLogger("FreeSql.MySql");
            }

            this.InternalCommonUtils      = new MySqlUtils(this);
            this.InternalCommonExpression = new MySqlExpression(this.InternalCommonUtils);

            this.Cache = new CacheProvider(cache, log);
            this.Ado   = new MySqlAdo(this.InternalCommonUtils, this.Cache, log, masterConnectionString, slaveConnectionString);

            this.DbFirst   = new MySqlDbFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
            this.CodeFirst = new MySqlCodeFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
        }
예제 #13
0
        public OdbcDamengProvider(string masterConnectionString, string[] slaveConnectionString, Func <DbConnection> connectionFactory = null)
        {
            this.InternalCommonUtils      = new OdbcDamengUtils(this);
            this.InternalCommonExpression = new OdbcDamengExpression(this.InternalCommonUtils);

            this.Ado = new OdbcDamengAdo(this.InternalCommonUtils, masterConnectionString, slaveConnectionString, connectionFactory);
            this.Aop = new AopProvider();

            this.DbFirst   = new OdbcDamengDbFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
            this.CodeFirst = new OdbcDamengCodeFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);

            //this.Aop.AuditValue += new EventHandler<Aop.AuditValueEventArgs>((_, e) =>
            //{
            //    if (e.Value == null && e.Column.Attribute.IsPrimary == false && e.Column.Attribute.IsIdentity == false)
            //        e.Value = Utils.GetDataReaderValue(e.Property.PropertyType.NullableTypeOrThis(), e.Column.Attribute.DbDefautValue);
            //});
        }
예제 #14
0
        /// <summary>
        /// 公告方法:筛选需要创建的实体类
        /// </summary>
        /// <param name="IDbFirst"></param>
        /// <param name="path"></param>
        /// <param name="lstTableNames"></param>
        /// <returns></returns>
        public IDbFirst GetDbFirst(IDbFirst IDbFirst, string path, string[] lstTableNames)
        {
            var files = Directory.GetFiles(path);
            var nms   = new List <string>();

            foreach (var m in files)
            {
                var f = Path.GetFileName(m).Replace(".cs", "");
                nms.Add(f);
            }
            if (lstTableNames != null && lstTableNames.Length > 0)
            {
                IDbFirst = IDbFirst.Where(lstTableNames);
            }
            else if (nms.Count > 0)
            {
                IDbFirst = IDbFirst.Where(p => !nms.Any(l => l.ToLower().StartsWith(p.ToLower())));
            }
            return(IDbFirst);
        }
예제 #15
0
        public FirebirdProvider(string masterConnectionString, string[] slaveConnectionString, Func <DbConnection> connectionFactory = null)
        {
            this.InternalCommonUtils      = new FirebirdUtils(this);
            this.InternalCommonExpression = new FirebirdExpression(this.InternalCommonUtils);

            this.Ado = new FirebirdAdo(this.InternalCommonUtils, masterConnectionString, slaveConnectionString, connectionFactory);
            this.Aop = new AopProvider();

            this.DbFirst   = new FirebirdDbFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);
            this.CodeFirst = new FirebirdCodeFirst(this, this.InternalCommonUtils, this.InternalCommonExpression);

            if ((this.Ado as FirebirdAdo).IsFirebird2_5)
            {
                this.Aop.ConfigEntityProperty += (_, e) =>
                {
                    if (e.Property.PropertyType.NullableTypeOrThis() == typeof(bool))
                    {
                        e.ModifyResult.MapType = typeof(short);
                    }
                }
            }
            ;
        }
예제 #16
0
        void BuildEachDirectory(string templateDirectory, string outputDirectory, TemplateEngin tpl, IDbFirst dbfirst, List <DbTableInfo> tables)
        {
            if (Directory.Exists(outputDirectory) == false)
            {
                Directory.CreateDirectory(outputDirectory);
            }
            var files = Directory.GetFiles(templateDirectory);

            foreach (var file in files)
            {
                var fi = new FileInfo(file);
                if (string.Compare(fi.Extension, ".FreeSql", true) == 0)
                {
                    var outputExtension = "." + fi.Name.Split('.')[1];
                    if (fi.Name.StartsWith("for-table."))
                    {
                        foreach (var table in tables)
                        {
                            var result = tpl.RenderFile(file, new Dictionary <string, object>()
                            {
                                { "table", table }, { "dbfirst", dbfirst }
                            });
                            if (result.EndsWith("return;"))
                            {
                                continue;
                            }
                            var outputName = table.Name + outputExtension;
                            var mcls       = Regex.Match(result, @"\s+class\s+(\w+)");
                            if (mcls.Success)
                            {
                                outputName = mcls.Groups[1].Value + outputExtension;
                            }
                            var outputStream = Encoding.UTF8.GetBytes(result);
                            var fullname     = outputDirectory + "/" + outputName;
                            if (File.Exists(fullname))
                            {
                                File.Delete(fullname);
                            }
                            using (var outfs = File.Open(fullname, FileMode.OpenOrCreate, FileAccess.Write)) {
                                outfs.Write(outputStream, 0, outputStream.Length);
                                outfs.Close();
                            }
                        }
                        continue;
                    }
                    else
                    {
                        var result = tpl.RenderFile(file, new Dictionary <string, object>()
                        {
                            { "tables", tables }, { "dbfirst", dbfirst }
                        });
                        var outputName = fi.Name;
                        var mcls       = Regex.Match(result, @"\s+class\s+(\w+)");
                        if (mcls.Success)
                        {
                            outputName = mcls.Groups[1].Value + outputExtension;
                        }
                        var outputStream = Encoding.UTF8.GetBytes(result);
                        var fullname     = outputDirectory + "/" + outputName;
                        if (File.Exists(fullname))
                        {
                            File.Delete(fullname);
                        }
                        using (var outfs = File.Open(fullname, FileMode.OpenOrCreate, FileAccess.Write)) {
                            outfs.Write(outputStream, 0, outputStream.Length);
                            outfs.Close();
                        }
                    }
                }
                File.Copy(file, outputDirectory + file.Replace(templateDirectory, ""), true);
            }
            var dirs = Directory.GetDirectories(templateDirectory);

            foreach (var dir in dirs)
            {
                BuildEachDirectory(dir, outputDirectory + dir.Replace(templateDirectory, ""), tpl, dbfirst, tables);
            }
        }
        public static IDbFirst GetDbFirst(ConnectionConfig currentConnectionConfig)
        {
            IDbFirst result = CreateInstance <IDbFirst>(GetClassName(currentConnectionConfig.DbType.ToString(), "DbFirst"));

            return(result);
        }