/// <summary> /// 将当前更改保存到数据库 /// </summary> /// <returns></returns> public T SaveChange() { DeExpression(); CheckNotNull.NotEmpty(setList, "Fields to be updated must be provided!"); CheckNotNull.NotEmpty(WhereConditions, "The update operation must specify where conditions!"); this.ToSQL(); this.CommandText += " RETURNING *;"; var properties = MyStagingUtils.GetDbFields(typeof(T)); using var reader = dbContext.ByMaster().Execute.ExecuteDataReader(CommandType.Text, CommandText, this.Parameters.ToArray()); try { reader.Read(); T obj = (T)Activator.CreateInstance(typeof(T)); foreach (var pi in properties) { var value = reader[pi.Name]; if (value != DBNull.Value) { pi.SetValue(obj, value); } } return(obj); } finally { Clear(); } }
/// <summary> /// 将当前更改保存到数据库 /// </summary> /// <returns></returns> public T SaveChange() { DeExpression(); CheckNotNull.NotEmpty(setList, "Fields to be updated must be provided!"); CheckNotNull.NotEmpty(WhereConditions, "The update operation must specify where conditions!"); try { this.ToSQL(); this.CommandText += " RETURNING *;"; using var reader = dbContext.ByMaster().Execute.ExecuteDataReader(CommandType.Text, CommandText, this.Parameters.ToArray()); T obj = default; if (reader.Read()) { obj = GetResult <T>(reader); } return(obj); } finally { setList.Clear(); Clear(); } }
/// <summary> /// 增加一组查询参数 /// </summary> /// <param name="parameters">输入参数</param> /// <returns></returns> public QueryContext <T> AddParameter(NpgsqlParameter[] parameters) { CheckNotNull.NotEmpty(parameters, nameof(parameters)); ParamList.AddRange(parameters); return(this); }
/// <summary> /// 将当前更改保存到数据库 /// </summary> /// <returns></returns> public int SaveChange() { if (WhereExpressionList.Count > 0) { foreach (var item in WhereExpressionList) { DbExpressionVisitor expression = new DbExpressionVisitor(); expression.Visit(item.Body); WhereList.Add(expression.SqlText.Builder.ToString().ToLower()); ParamList.AddRange(expression.SqlText.Parameters); } } CheckNotNull.NotEmpty(this.WhereList, "The delete operation must specify where conditions!"); this.ToString(); var affrows = 0; try { affrows = base.ExecuteNonQuery(this.CommandText); } finally { base.ParamList.Clear(); } return(affrows); }
/// <summary> /// 初始化数据库连接 /// </summary> /// <param name="options"></param> public static void InitConnection(StagingOptions options) { CheckNotNull.NotNull(options, nameof(options)); CheckNotNull.NotEmpty(options.ConnectionMaster, nameof(options.ConnectionMaster)); Options = options; // 初始化主库连接实例 ConnectionStringConfiguration conn = new ConnectionStringConfiguration() { ConnectionString = options.ConnectionMaster, DbConnection = new Npgsql.NpgsqlConnection(options.ConnectionMaster) }; InstanceMaster = new MasterExecute(options.Logger, conn); // 初始化从库连接实例 List <ConnectionStringConfiguration> connList = GetSlaves(options.ConnectionSlaves); if (connList != null) { InstanceSlave = new SlaveExecute(options.Logger, connList); } if (options.CacheOptions != null && options.CacheOptions.Cache != null) { CacheManager = new CacheManager(options.CacheOptions); } }
/// <summary> /// 将当前更改保存到数据库 /// </summary> /// <returns></returns> public int SaveChange() { DeExpression(); CheckNotNull.NotEmpty(WhereConditions, "The delete operation must specify where conditions!"); this.ToSQL(); var affrows = dbContext.Execute.ExecuteNonQuery(System.Data.CommandType.Text, this.CommandText, Parameters.ToArray()); return(affrows); }
public void Initialize(ProjectConfig config) { Tables = new List <TableInfo>(); StagingOptions options = new StagingOptions(config.ProjectName, config.ConnectionString) { Provider = ProviderType.MySql }; string schema = new MySqlConnection(options.Master).Database; dbContext = new MySqlDbContext(options); #region dir CheckNotNull.NotEmpty(config.ProjectName, nameof(config.ProjectName)); if (config.Mode == GeneralInfo.Db) { CheckNotNull.NotEmpty(config.OutputDir, nameof(config.OutputDir)); Config = new GeneralConfig { OutputDir = config.OutputDir, ProjectName = config.ProjectName, ModelPath = config.OutputDir }; if (!Directory.Exists(Config.ModelPath)) { Directory.CreateDirectory(Config.ModelPath); } } #endregion #region Tables string _sqltext = $@"SELECT TABLE_SCHEMA,TABLE_NAME,(CASE WHEN TABLE_TYPE = 'BASE TABLE' THEN 'TABLE'ELSE TABLE_TYPE END ) AS TABLE_TYPE FROM information_schema.`TABLES` WHERE TABLE_SCHEMA = '{schema}'"; dbContext.Execute.ExecuteDataReader(dr => { var table = new TableInfo() { Schema = dr["TABLE_SCHEMA"].ToString(), Name = dr["TABLE_NAME"].ToString(), Type = Enum.Parse <TableType>(dr["TABLE_TYPE"].ToString(), true) }; GetFields(table); Tables.Add(table); }, CommandType.Text, _sqltext); #endregion }
static ProjectConfig GetConfig(string[] args) { var config = new ProjectConfig(); string mode = "db"; for (int i = 0; i < args.Length; i++) { var item = args[i].ToLower(); switch (item) { case "-d": config.ConnectionString = args[i + 1]; break; case "-n": config.ContextName = args[i + 1]; break; case "-o": config.OutputDir = args[i + 1]; break; case "-t": config.Provider = args[i + 1]; break; case "-m": mode = args[i + 1].ToLower(); break; } i++; } CheckNotNull.NotEmpty(config.ConnectionString, "-d 参数必须提供"); CheckNotNull.NotEmpty(config.ContextName, "-n 参数必须提供"); CheckNotNull.NotEmpty(config.Provider, "-t 参数必须提供"); CheckNotNull.NotEmpty(mode, "-m 参数必须提供"); if (mode != "db" && mode != "code") { throw new ArgumentException("-m 参数错误,必须为 db 或者 code"); } config.Mode = mode == "db" ? GeneralInfo.Db : GeneralInfo.Code; if (config.Mode == GeneralInfo.Db && string.IsNullOrEmpty(config.OutputDir)) { config.OutputDir = Path.Combine(config.ContextName, "Model"); } var fileName = "MyStaging." + config.Provider; config.ProviderAssembly = AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(fileName)); return(config); }
/// <summary> /// 封装多个查询结果集,以管道的形式 /// </summary> /// <param name="master">是否在主库执行查询</param> /// <param name="contexts">查询上下文对象</param> /// <returns></returns> public static List <List <dynamic> > ExecutePipeLine(bool master, params IQueryPipe[] contexts) { CheckNotNull.NotEmpty(contexts, nameof(contexts)); StringBuilder sb = new StringBuilder(); List <DbParameter> parameters = new List <DbParameter>(); foreach (var ctx in contexts) { sb.AppendLine(ctx.CommandText); sb.Append(";"); parameters.AddRange(ctx.ParamList); } var cmdText = sb.ToString(); int pipeLine = contexts.Length; List <List <dynamic> > result = new List <List <dynamic> >(); if (master) { CheckNotNull.NotNull(InstanceMaster, nameof(InstanceMaster)); InstanceMaster.ExecuteDataReaderPipe(dr => { ExcutePipeResult(contexts, dr, pipeLine, result); }, CommandType.Text, cmdText, parameters.ToArray()); } else { CheckNotNull.NotNull(InstanceSlave, nameof(InstanceSlave)); InstanceSlave.ExecuteDataReaderPipe(dr => { ExcutePipeResult(contexts, dr, pipeLine, result); }, CommandType.Text, cmdText, parameters.ToArray()); } return(result); }
/// <summary> /// 将当前更改保存到数据库 /// </summary> /// <returns></returns> public int SaveChange() { if (WhereExpressionList.Count > 0) { foreach (var item in WhereExpressionList) { DbExpressionVisitor expression = new DbExpressionVisitor(); expression.Visit(item.Body); WhereList.Add(expression.SqlText.Builder.ToString().ToLower()); ParamList.AddRange(expression.SqlText.Parameters); } } CheckNotNull.NotEmpty(setList, "Fields to be updated must be provided!"); CheckNotNull.NotEmpty(WhereList, "The update operation must specify where conditions!"); this.ToSQL(); int affrows = 0; if (OnChanged != null) { this.CommandText += " RETURNING *;"; var objList = base.ByMaster().ExecuteReader <T>(this.CommandText); affrows = objList.Count; if (affrows > 0 && this.OnChanged != null) { OnChanged(objList[0]); } } else { affrows = base.ExecuteNonQuery(this.CommandText); } return(affrows); }
/// <summary> /// 增加一组查询参数 /// </summary> /// <param name="parameters">输入参数</param> /// <returns></returns> public virtual void AddParameter(params DbParameter[] parameters) { CheckNotNull.NotEmpty(parameters, nameof(parameters)); Parameters.AddRange(parameters); }
public void Initialize(ProjectConfig config) { this.config = config; Tables = new List <TableInfo>(); #region dir CheckNotNull.NotEmpty(config.ContextName, nameof(config.ContextName)); if (config.Mode == GeneralInfo.Db) { CheckNotNull.NotEmpty(config.OutputDir, nameof(config.OutputDir)); Config = new GeneralConfig { OutputDir = config.OutputDir, ProjectName = config.ContextName, ModelPath = config.OutputDir }; if (!Directory.Exists(Config.ModelPath)) { Directory.CreateDirectory(Config.ModelPath); } } #endregion #region Schemas string[] filters = new string[this.Filters.Count]; for (int i = 0; i < Filters.Count; i++) { filters[i] = $"'{Filters[i]}'"; } string sql = $@"SELECT schema_name FROM information_schema.schemata WHERE SCHEMA_NAME NOT IN({string.Join(",", filters)}) ORDER BY SCHEMA_NAME; "; List <string> schemas = new List <string>(); SQLContext.ExecuteDataReader(dr => { schemas.Add(dr[0].ToString()); }, CommandType.Text, sql); #endregion #region Tables foreach (var schema in schemas) { string _sqltext = $@"SELECT table_name,'table' as type FROM INFORMATION_SCHEMA.tables WHERE table_schema='{schema}' AND table_type='BASE TABLE' UNION ALL SELECT table_name,'view' as type FROM INFORMATION_SCHEMA.views WHERE table_schema = '{schema}'"; SQLContext.ExecuteDataReader(dr => { var table = new TableInfo() { Schema = schema, Name = dr["table_name"].ToString(), Type = dr["type"].ToString() == "table" ? TableType.Table : TableType.View }; GetFields(table); Tables.Add(table); }, CommandType.Text, _sqltext); } #endregion }