Exemplo n.º 1
0
        private DataTable ToDdateTable(List <T> datas)
        {
            DataTable tempDataTable = ReflectionInoCore <DataTable> .GetInstance().GetOrCreate("BulkCopyAsync" + typeof(T).FullName,
                                                                                               () =>
            {
                if (AsName == null)
                {
                    return(queryable.Where(it => false).Select("*").ToDataTable());
                }
                else
                {
                    return(queryable.AS(AsName).Where(it => false).Select("*").ToDataTable());
                }
            }
                                                                                               );

            var dt = new DataTable();

            foreach (DataColumn item in tempDataTable.Columns)
            {
                dt.Columns.Add(item.ColumnName, item.DataType);
            }
            dt.TableName = GetTableName();
            var columns = entityInfo.Columns;
            var isMySql = this.context.CurrentConnectionConfig.DbType.IsIn(DbType.MySql, DbType.MySqlConnector);

            foreach (var item in datas)
            {
                var dr = dt.NewRow();
                foreach (var column in columns)
                {
                    if (column.IsIgnore || column.IsOnlyIgnoreInsert)
                    {
                        continue;
                    }
                    var name = column.DbColumnName;
                    if (name == null)
                    {
                        name = column.PropertyName;
                    }
                    var value = ValueConverter(column, PropertyCallAdapterProvider <T> .GetInstance(column.PropertyName).InvokeGet(item));
                    if (isMySql && column.UnderType == UtilConstants.BoolType)
                    {
                        if (value.ObjToBool() == false)
                        {
                            value = DBNull.Value;
                        }
                    }
                    else if (column.UnderType == UtilConstants.DateTimeOffsetType && value != null)
                    {
                        value = UtilMethods.ConvertFromDateTimeOffset((DateTimeOffset)value);
                    }
                    dr[name] = value;
                }
                dt.Rows.Add(dr);
            }

            return(dt);
        }
Exemplo n.º 2
0
 public virtual object FormatValue(object value)
 {
     if (value == null)
     {
         return("NULL");
     }
     else
     {
         var type = UtilMethods.GetUnderType(value.GetType());
         if (type == UtilConstants.DateType)
         {
             var date = value.ObjToDate();
             if (date < Convert.ToDateTime("1900-1-1"))
             {
                 date = Convert.ToDateTime("1900-1-1");
             }
             return("'" + date.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'");
         }
         else if (type == UtilConstants.ByteArrayType)
         {
             string bytesString = "0x" + BitConverter.ToString((byte[])value).Replace("-", "");
             return(bytesString);
         }
         else if (type.IsEnum())
         {
             if (this.Context.CurrentConnectionConfig.MoreSettings?.TableEnumIsString == true)
             {
                 return(value.ToSqlValue());
             }
             else
             {
                 return(Convert.ToInt64(value));
             }
         }
         else if (type == UtilConstants.BoolType)
         {
             return(value.ObjToBool() ? "1" : "0");
         }
         else if (type == UtilConstants.StringType || type == UtilConstants.ObjType)
         {
             return("N'" + value.ToString().ToSqlFilter() + "'");
         }
         else if (type == UtilConstants.DateTimeOffsetType)
         {
             var date = UtilMethods.ConvertFromDateTimeOffset((DateTimeOffset)value);
             return("'" + date.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'");
         }
         else if (type == UtilConstants.FloatType)
         {
             return("N'" + Convert.ToDouble(value).ToString() + "'");
         }
         else
         {
             return("N'" + value.ToString() + "'");
         }
     }
 }
Exemplo n.º 3
0
        /// <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);
            }
            MySqlParameter[] result = new MySqlParameter[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 MySqlParameter();
                sqlParameter.ParameterName = parameter.ParameterName;
                sqlParameter.Size          = parameter.Size;
                sqlParameter.Value         = parameter.Value;
                sqlParameter.DbType        = parameter.DbType;
                if (parameter.Direction == 0)
                {
                    parameter.Direction = ParameterDirection.Input;
                }
                sqlParameter.Direction = parameter.Direction;
                //if (sqlParameter.Direction == 0)
                //{
                //    sqlParameter.Direction = ParameterDirection.Input;
                //}
                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;
                }
                else if (parameter.DbType == System.Data.DbType.DateTimeOffset)
                {
                    if (sqlParameter.Value != DBNull.Value)
                    {
                        sqlParameter.Value = UtilMethods.ConvertFromDateTimeOffset((DateTimeOffset)sqlParameter.Value);
                    }
                    sqlParameter.DbType = System.Data.DbType.DateTime;
                }
                ++index;
            }
            return(result);
        }
Exemplo n.º 4
0
 public override object FormatValue(object value)
 {
     if (value == null)
     {
         return("NULL");
     }
     else
     {
         string N    = this.Context.GetN();
         var    type = UtilMethods.GetUnderType(value.GetType());
         if (type == UtilConstants.DateType)
         {
             var date = value.ObjToDate();
             if (date < Convert.ToDateTime("1900-1-1"))
             {
                 date = Convert.ToDateTime("1900-1-1");
             }
             return("to_date('" + date.ToString("yyyy-MM-dd HH:mm:ss") + "', 'YYYY-MM-DD HH24:MI:SS') ");
         }
         else if (type.IsEnum())
         {
             return(Convert.ToInt64(value));
         }
         else if (type.IsIn(UtilConstants.IntType, UtilConstants.LongType, UtilConstants.ShortType))
         {
             return(value);
         }
         else if (type == UtilConstants.GuidType)
         {
             return("'" + value.ToString() + "'");
         }
         else if (type == UtilConstants.ByteArrayType)
         {
             string bytesString = "0x" + BitConverter.ToString((byte[])value).Replace("-", "");
             return(bytesString);
         }
         else if (type == UtilConstants.BoolType)
         {
             return(value.ObjToBool() ? "1" : "0");
         }
         else if (type == UtilConstants.DateTimeOffsetType)
         {
             var date = UtilMethods.ConvertFromDateTimeOffset((DateTimeOffset)value);
             return("to_date('" + date.ToString("yyyy-MM-dd HH:mm:ss") + "', 'YYYY-MM-DD HH24:MI:SS') ");
         }
         else if (type == UtilConstants.StringType || type == UtilConstants.ObjType)
         {
             return(N + "'" + value.ToString().ToSqlFilter() + "'");
         }
         else
         {
             return(N + "'" + value.ToString() + "'");
         }
     }
 }
Exemplo n.º 5
0
 public object FormatValue(object value, string name)
 {
     if (value == null)
     {
         return("NULL");
     }
     else
     {
         string N    = this.Context.GetN();
         var    type = UtilMethods.GetUnderType(value.GetType());
         if (type == UtilConstants.StringType && value.ToString().Contains("{SugarSeq:=}"))
         {
             return(value.ToString().Replace("{SugarSeq:=}", ""));
         }
         if (type == UtilConstants.DateType)
         {
             var date = value.ObjToDate();
             if (date < Convert.ToDateTime("1900-1-1"))
             {
                 date = Convert.ToDateTime("1900-1-1");
             }
             return("to_timestamp('" + date.ToString("yyyy-MM-dd HH:mm:ss.ffffff") + "', 'YYYY-MM-DD HH24:MI:SS.FF') ");
         }
         else if (type.IsEnum())
         {
             return(Convert.ToInt64(value));
         }
         else if (type == UtilConstants.ByteArrayType)
         {
             string bytesString = "0x" + BitConverter.ToString((byte[])value).Replace("-", "");
             return(bytesString);
         }
         else if (type == UtilConstants.BoolType)
         {
             return(value.ObjToBool() ? "1" : "0");
         }
         else if (type == UtilConstants.DateTimeOffsetType)
         {
             var date = UtilMethods.ConvertFromDateTimeOffset((DateTimeOffset)value);
             return("to_timestamp('" + date.ToString("yyyy-MM-dd HH:mm:ss.ffffff") + "', 'YYYY-MM-DD HH24:MI:SS.FF') ");
         }
         else if (type == UtilConstants.StringType || type == UtilConstants.ObjType)
         {
             if (value.ToString().Length > 2000)
             {
                 ++i;
                 var parameterName = this.Builder.SqlParameterKeyWord + name + i;
                 this.Parameters.Add(new SugarParameter(parameterName, value));
                 return(parameterName);
             }
             else
             {
                 return(N + "'" + value.ToString().ToSqlFilter() + "'");
             }
         }
         else
         {
             return(N + "'" + value.ToString() + "'");
         }
     }
 }
Exemplo n.º 6
0
        /// <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);
            }
            OracleParameter[] result = new OracleParameter[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 OracleParameter();
                sqlParameter.Size          = parameter.Size == -1 ? 0 : parameter.Size;
                sqlParameter.ParameterName = parameter.ParameterName;
                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.OracleDbType = OracleDbType.RefCursor;
                }
                if (parameter.IsClob)
                {
                    sqlParameter.OracleDbType = OracleDbType.Clob;
                    sqlParameter.Value        = parameter.Value;
                }
                if (parameter.IsArray)
                {
                    sqlParameter.OracleDbType   = OracleDbType.Varchar2;
                    sqlParameter.CollectionType = OracleCollectionType.PLSQLAssociativeArray;
                }
                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.DateTimeOffset)
                {
                    if (parameter.Value != DBNull.Value)
                    {
                        sqlParameter.Value = UtilMethods.ConvertFromDateTimeOffset((DateTimeOffset)parameter.Value);
                    }
                    sqlParameter.DbType = System.Data.DbType.DateTime;
                }
                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.DbType == System.Data.DbType.DateTime)
                {
                    sqlParameter.Value  = parameter.Value;
                    sqlParameter.DbType = System.Data.DbType.DateTime;
                }
                else if (parameter.DbType == System.Data.DbType.AnsiStringFixedLength)
                {
                    sqlParameter.DbType = System.Data.DbType.AnsiStringFixedLength;
                    sqlParameter.Value  = parameter.Value;
                }
                else if (parameter.DbType == System.Data.DbType.AnsiString)
                {
                    sqlParameter.DbType = System.Data.DbType.AnsiString;
                    sqlParameter.Value  = parameter.Value;
                }
                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);
                }
                if (isVarchar && sqlParameter.DbType == System.Data.DbType.String)
                {
                    sqlParameter.DbType = System.Data.DbType.AnsiString;
                }
                ++index;
            }
            return(result);
        }