public override DbParameter[] GetDbParamtersByObject(string sql, object obj) => Utils.GetDbParamtersByObject <DmParameter>(sql, obj, null, (name, type, value) => { var dbtype = (DmDbType)_orm.CodeFirst.GetDbInfo(type)?.type; switch (dbtype) { case DmDbType.Bit: if (value == null) { value = null; } else { value = (bool)value == true ? 1 : 0; } dbtype = DmDbType.Int32; break; case DmDbType.Char: case DmDbType.VarChar: case DmDbType.Text: value = string.Concat(value); break; } var ret = new DmParameter { ParameterName = $":{name}", DmSqlType = dbtype, Value = value }; return(ret); });
public override DbParameter AppendParamter(List <DbParameter> _params, string parameterName, ColumnInfo col, Type type, object value) { if (string.IsNullOrEmpty(parameterName)) { parameterName = $"p_{_params?.Count}"; } var dbtype = (DmDbType)_orm.CodeFirst.GetDbInfo(type)?.type; switch (dbtype) { case DmDbType.Bit: if (value == null) { value = null; } else { value = (bool)value == true ? 1 : 0; } dbtype = DmDbType.Int32; break; case DmDbType.Char: case DmDbType.VarChar: case DmDbType.Text: value = string.Concat(value); break; } var ret = new DmParameter { ParameterName = QuoteParamterName(parameterName), DmSqlType = dbtype, Value = value }; _params?.Add(ret); return(ret); }
/// <summary> /// if mysql return MySqlParameter[] pars /// if sqlerver return SqlParameter[] pars ... /// </summary> /// <param name="parameters"></param> /// <returns></returns> public override IDataParameter[] ToIDbDataParameter(params SugarParameter[] parameters) { if (parameters == null || parameters.Length == 0) { return(null); } DmParameter[] result = new DmParameter[parameters.Length]; int index = 0; var isVarchar = this.Context.IsVarchar(); foreach (var parameter in parameters) { if (parameter.Value == null) { parameter.Value = DBNull.Value; } var sqlParameter = new DmParameter(); sqlParameter.ParameterName = parameter.ParameterName; sqlParameter.Size = parameter.Size; sqlParameter.Value = parameter.Value; sqlParameter.DbType = parameter.DbType; if (sqlParameter.DbType == System.Data.DbType.Guid) { sqlParameter.DbType = System.Data.DbType.String; sqlParameter.Value = sqlParameter.Value.ToString(); } if (parameter.Direction == 0) { parameter.Direction = ParameterDirection.Input; } sqlParameter.Direction = parameter.Direction; result[index] = sqlParameter; if (sqlParameter.Direction.IsIn(ParameterDirection.Output, ParameterDirection.InputOutput, ParameterDirection.ReturnValue)) { if (this.OutputParameters == null) { this.OutputParameters = new List <IDataParameter>(); } this.OutputParameters.RemoveAll(it => it.ParameterName == sqlParameter.ParameterName); this.OutputParameters.Add(sqlParameter); } if (isVarchar && sqlParameter.DbType == System.Data.DbType.String) { sqlParameter.DbType = System.Data.DbType.AnsiString; } ++index; } return(result); }
public override string GetNativeDbTypeName(IDataParameter para) { DmParameter oraPara = (DmParameter)para; DmDbType oraType = oraPara.DmSqlType; if (oraType == DmDbType.Date) { return("Date"); } else if (oraType == DmDbType.Int32) { return("INT"); } else { return(oraType.ToString()); } }
/// <summary> /// 获取一个新参数对象 /// </summary> /// <param name="paraName">参数名</param> /// <param name="dbType">参数数据类型</param> /// <param name="size">参数大小</param> /// <returns>特定于数据源的参数对象</returns> public override IDataParameter GetParameter(string paraName, System.Data.DbType dbType, int size) { DmParameter para = new DmParameter(); para.ParameterName = paraName; if (size > 2000) { //长度大于2000,将引发clob类型错误的问题,详细请参考 http://blog.csdn.net/pojianbing/article/details/2789426 para.DmSqlType = DmDbType.Clob; para.Size = size; } else { para.DbType = dbType; para.Size = size; } return(para); }
/// <summary> /// 获取属性数据库参数 /// </summary> /// <param name="attribute">特性</param> /// <param name="property">属性</param> /// <param name="obj"></param> /// <returns></returns> private static DbParameter GetDbParameter(this DbParameterAttribute attribute, PropertyInfo property, object obj) { if (string.IsNullOrEmpty(attribute.Name)) { throw new FreeSqlException($"{property.Name} 属性的 DbParameterAttribute 特性 Name 不可为空"); } DbParameter dbParameter; switch (attribute.DataType) { case DataType.MySql: case DataType.OdbcMySql: dbParameter = new MySqlParameter { MySqlDbType = (MySqlDbType)attribute.DbType }; break; case DataType.PostgreSQL: case DataType.OdbcPostgreSQL: dbParameter = new NpgsqlParameter { NpgsqlDbType = (NpgsqlDbType)attribute.DbType }; break; case DataType.Oracle: case DataType.OdbcOracle: dbParameter = new OracleParameter { OracleDbType = (OracleDbType)attribute.DbType }; break; case DataType.Sqlite: dbParameter = new SQLiteParameter { DbType = (DbType)attribute.DbType }; break; case DataType.Dameng: dbParameter = new DmParameter { DmSqlType = (DmDbType)attribute.DbType }; break; case DataType.Odbc: case DataType.OdbcKingbaseES: case DataType.OdbcDameng: case DataType.MsAccess: case DataType.ShenTong: dbParameter = new OdbcParameter { OdbcType = (OdbcType)attribute.DbType }; break; case DataType.OdbcSqlServer: case DataType.SqlServer: default: dbParameter = new SqlParameter { SqlDbType = (SqlDbType)attribute.DbType }; break; } dbParameter.SetPropertyValue(nameof(dbParameter.ParameterName), attribute.Name) .SetPropertyValue(nameof(dbParameter.Direction), attribute.Direction) .SetPropertyValue(nameof(dbParameter.Value), property.GetValue(obj)) .SetPropertyValue(nameof(dbParameter.Size), attribute.Size) .SetPropertyValue(nameof(dbParameter.Scale), attribute.Scale) .SetPropertyValue(nameof(dbParameter.Precision), attribute.Precision) .SetPropertyValue(nameof(dbParameter.IsNullable), attribute.IsNullable); return(dbParameter); }
/// <summary> /// if mysql return MySqlParameter[] pars /// if sqlerver return SqlParameter[] pars ... /// </summary> /// <param name="parameters"></param> /// <returns></returns> public override IDataParameter[] ToIDbDataParameter(params SugarParameter[] parameters) { if (parameters == null || parameters.Length == 0) { return(null); } DmParameter[] result = new DmParameter[parameters.Length]; int index = 0; foreach (var parameter in parameters) { if (parameter.Value == null) { parameter.Value = DBNull.Value; } var sqlParameter = new DmParameter { Size = parameter.Size == -1 ? 0 : parameter.Size, ParameterName = parameter.ParameterName, DbType = parameter.DbType }; if (sqlParameter.ParameterName[0] == '@') { sqlParameter.ParameterName = ':' + sqlParameter.ParameterName.Substring(1, sqlParameter.ParameterName.Length - 1); } if (this.CommandType == CommandType.StoredProcedure) { sqlParameter.ParameterName = sqlParameter.ParameterName.TrimStart(':'); } //if (parameter.IsRefCursor) //{ // sqlParameter.DbType = DmDbType.Cursor; // OracleDbType.RefCursor; //} if (sqlParameter.DbType == System.Data.DbType.Guid) { sqlParameter.DbType = System.Data.DbType.String; sqlParameter.Value = sqlParameter.Value.ObjToString(); } else if (parameter.DbType == System.Data.DbType.Boolean) { sqlParameter.DbType = System.Data.DbType.Int16; if (parameter.Value == DBNull.Value) { parameter.Value = 0; } else { sqlParameter.Value = (bool)parameter.Value ? 1 : 0; } } else { if (parameter.Value != null && parameter.Value.GetType() == UtilConstants.GuidType) { parameter.Value = parameter.Value.ToString(); } sqlParameter.Value = parameter.Value; } if (parameter.Direction != 0) { sqlParameter.Direction = parameter.Direction; } result[index] = sqlParameter; if (sqlParameter.Direction.IsIn(ParameterDirection.Output, ParameterDirection.InputOutput, ParameterDirection.ReturnValue)) { if (this.OutputParameters == null) { this.OutputParameters = new List <IDataParameter>(); } this.OutputParameters.RemoveAll(it => it.ParameterName == sqlParameter.ParameterName); this.OutputParameters.Add(sqlParameter); } ++index; } return(result); }