Beispiel #1
0
 public BatchContext(string sql, NamedParameter[] parameters, IEnumerable<object[]> paramSets, Type entityType)
 {
     EntityType = entityType;
     CommandText = sql;
     Parameters = parameters;
     ParameterSets = paramSets;
 }
Beispiel #2
0
        protected override void InitializeParameter(IDbDataParameter p, NamedParameter parameter, object value)
        {
            p.ParameterName = parameter.Name;
            p.Value = value ?? DBNull.Value;
            var sqlType = parameter.SqlType;

            if (parameter.SqlType != null)
            {
                if (sqlType.Length > 0)
                    p.Size = sqlType.Length;
                if (sqlType.Precision > 0)
                    p.Precision = sqlType.Precision;
                if (sqlType.Scale > 0)
                    p.Scale = sqlType.Scale;
                if (sqlType.Required)
                    (p as DbParameter).IsNullable = false;

                switch (sqlType.DbType)
                {
                    case DBType.DateTime:
                        p.DbType = DbType.String;
                        if (p.Value != DBNull.Value && !object.Equals(null, p.Value))
                            p.Value = ((DateTime)p.Value).ToString("yyyy/MM/dd HH:mm:ss");
                        break;
                    case DBType.Int64:
                        p.DbType = DbType.Int32;
                        if (p.Value != DBNull.Value && !object.Equals(null, p.Value))
                            p.Value = System.Convert.ToInt32((long)p.Value);
                        break;
                }
            }
            //p.DbType = SqlType.ToDbType(parameter.sqlType.DbType);
            ConvertDBTypeToNativeType(p, sqlType.DbType);
        }
Beispiel #3
0
        public static string ExtendMessage(string message, string sql, object entity = null, object[] parameterValues = null, NamedParameter[] parameters = null)
        {
            var sb = new StringBuilder(512).AppendLine();
            if (entity != null)
            {
                try
                {
                    sb.Append("[Entity Name:").Append(entity.GetType().Name).AppendLine("]");
                    sb.AppendLine( NLite.Serialization.SimpleJson.SerializeObject(entity));
                }
                catch
                {
                }
            }

            sb.Append(message).Append(Environment.NewLine).Append("[ ").Append(sql ?? SQLNotAvailable).Append(" ]");

            if (parameters != null && parameters.Length > 0)
            {
                sb.AppendLine();
                for (int i = 0, n = parameters.Length; i < n; i++)
                {
                    object value = parameterValues[i] ?? "null";
                    sb.Append("  ").Append("Name:").Append(parameters[i].Name).Append(" - Value:").Append(value);
                    sb.AppendLine();
                }
            }

            sb.Append(Environment.NewLine);
            return sb.ToString();
        }
Beispiel #4
0
 public CommandContext(string sql, NamedParameter[] parameters, object[] parameterValues, Type entityType, OperationType op, bool supportsVersionCheck, object instance)
 {
     EntityType = entityType;
     Instance = instance;
     CommandText = sql;
     Parameters = parameters;
     ParameterValues = parameterValues;
     OperationType = op;
     SupportsVersionCheck = supportsVersionCheck;
 }
Beispiel #5
0
        public static Exception Convert(ISqlLog log, ISQLExceptionConverter converter,DbExceptionContextInfo exceptionContextInfo,
                                           object[] parameterValues, NamedParameter[] namedParameters)
        {
            var sql = TryGetActualSqlQuery(exceptionContextInfo.SqlException, exceptionContextInfo.Sql);
            string extendMessage = ExtendMessage(
                exceptionContextInfo.Message,
                sql != null ? sql.ToString() : null,
                exceptionContextInfo.Entity,
                parameterValues,
                namedParameters);
            log.Error(exceptionContextInfo.SqlException, extendMessage);

            return converter.Convert(exceptionContextInfo);
        }
Beispiel #6
0
        public void LogParameters(NamedParameter[] parameters, object[] paramValues)
        {
            if (paramValues != null)
            {
                for (int i = 0, n = parameters.Length; i < n; i++)
                {
                    var p = parameters[i];
                    var v = paramValues[i];

                    if (v == null || v == DBNull.Value)
                        Trace.WriteLine(string.Format("-- {0}:(DbType = {1}, Length = {2}, Value = NULL)", p.Name, p.sqlType.DbType, p.sqlType.Length),"ELinq");
                    else
                        Trace.WriteLine(string.Format("-- {0}:(DbType = {1}, Length = {2}, Value = {3})", p.Name, p.sqlType.DbType, p.sqlType.Length, v),"ELinq");
                }
            }
        }
Beispiel #7
0
        public void LogCommand(string commandText, NamedParameter[] parameters, object[] paramValues)
        {
            Trace.WriteLine(commandText, "ELinq");
            if (paramValues != null)
                this.LogParameters(parameters, paramValues);
            Trace.WriteLine("", "ELinq");
            Trace.WriteLine(Version, "ELinq");

            var dbContext = ExecuteContext.DbContext;
            if (dbContext != null)
                Trace.WriteLine("-- DbProviderName:" + dbContext.dbConfiguration.DbProviderName, "ELinq");

            var dialect = ExecuteContext.Dialect;
            if (dialect != null)
                Trace.WriteLine("-- Dialect:" + dialect.GetType().Name, "ELinq");
        }
Beispiel #8
0
        protected override void InitializeParameter(IDbDataParameter p, NamedParameter parameter, object value)
        {
            if (parameter.SqlType != null)
            {

                switch (parameter.sqlType.DbType)
                {
                    case DBType.Guid:
                        if (value != null)
                            value = value.ToString();
                        break;
                    case DBType.DateTime:
                        if (value != null)
                        {
                            DateTime d = value.GetType().IsNullable() ? (value as DateTime?).Value : (DateTime)value;
                            value = d.ToString("s");
                        }
                        break;
                }
            }

            base.InitializeParameter(p, parameter, value);
        }
Beispiel #9
0
        protected override void InitializeParameter(System.Data.IDbDataParameter p, NamedParameter parameter, object value)
        {
            p.ParameterName = parameter.Name;
            p.Value = value ?? DBNull.Value;
            var sqlType = parameter.SqlType;

            if (parameter.SqlType != null)
            {
                if (sqlType.Length > 0)
                    p.Size = sqlType.Length;
                if (sqlType.Precision > 0)
                    p.Precision = sqlType.Precision;
                if (sqlType.Scale > 0)
                    p.Scale = sqlType.Scale;
                if (sqlType.Required)
                    (p as DbParameter).IsNullable = false;

                switch (sqlType.DbType)
                {
                    case DBType.NChar:
                    case DBType.NVarChar:
                        {
                            var str = value as string;
                            if (string.IsNullOrEmpty(str))
                                p.Size = 2;
                            else
                                p.Size = str.Length * 2;
                            break;
                        }
                    case DBType.Char:
                    case DBType.VarChar:
                        {
                            var str = value as string;
                            if (string.IsNullOrEmpty(str))
                                p.Size = 1;
                            else
                                p.Size = str.Length * 1;
                            break;
                        }
                    case DBType.Guid:
                        parameter.sqlType = SqlType.Get(DBType.Binary, 16);
                        if (value is Guid)
                            p.Value = ((Guid)value).ToByteArray();
                        else if (value != null)
                            p.Value = (((Guid?)value).Value).ToByteArray();
                        break;
                    case DBType.Binary:
                    case DBType.Image:
                        if (value is Guid)
                        {
                            p.Value = ((Guid)value).ToByteArray();
                            parameter.sqlType = SqlType.Get(DBType.Binary, 16);
                        }
                        else if (value is Guid?)
                        {
                            p.Value = ((Guid?)value).Value.ToByteArray();
                            parameter.sqlType = SqlType.Get(DBType.Binary, 16);
                        }
                        break;
                }
            }
            ConvertDBTypeToNativeType(p, parameter.sqlType.DbType);
        }
Beispiel #10
0
 public virtual void AddParameter(DbCommand command, NamedParameter parameter, object value)
 {
     IDbDataParameter p = command.CreateParameter();
     InitializeParameter(p, parameter, value);
     command.Parameters.Add(p);
 }
Beispiel #11
0
        protected virtual void InitializeParameter(IDbDataParameter p, NamedParameter parameter, object value)
        {
            p.ParameterName = parameter.Name;
            p.Value = value ?? DBNull.Value;
            var sqlType = parameter.SqlType;

            if (parameter.SqlType != null)
            {

                if (sqlType.Precision > 0)
                    p.Precision = sqlType.Precision;
                if (sqlType.Scale > 0)
                    p.Scale = sqlType.Scale;
                if (sqlType.Required)
                    (p as DbParameter).IsNullable = false;
                if (sqlType.Length > 0)
                    p.Size = sqlType.Length;
                else
                    InitializeParameterLengthWhenZero(p, value, sqlType);
            }
            ConvertDBTypeToNativeType(p, parameter.sqlType.DbType);
        }
Beispiel #12
0
 public void LogParameters(NamedParameter[] parameters, object[] paramValues)
 {
 }
Beispiel #13
0
 public void LogCommand(string commandText, NamedParameter[] parameters, object[] paramValues)
 {
 }