protected override void ThrowSqlException(Type tableType, Exception ex)
        {
            if (!(ex is NpgsqlException))
            {
                throw ex;
            }
            PostgresException nerror = ex as PostgresException;

            if (nerror.SqlState != "23505" || nerror.Detail.Contains(" already exists") == false)
            {
                throw ex;
            }

            StringBuilder output = new StringBuilder();

            string[] captions = null;
            string[] keys     = null;
            try
            {
                string msg = nerror.Detail;


                try
                {
                    var match = Regex.Match(msg, @"Key \((?<c>(\w| |,)+)\)");
                    if (match != null && match.Length > 0)
                    {
                        string indexname = match.Groups["c"].Value;
                        keys = indexname.Split(',');
                        keys = (from m in keys
                                select m.Trim()).ToArray();

                        captions = new string[keys.Length];

                        for (int i = 0; i < keys.Length; i++)
                        {
                            var pinfo = tableType.GetTypeInfo().GetProperty(keys[i]);

                            WayDBColumnAttribute columnAtt = pinfo.GetCustomAttribute(typeof(WayDBColumnAttribute)) as WayDBColumnAttribute;
                            captions[i] = columnAtt.Caption;
                            if (output.Length > 0)
                            {
                                output.Append(',');
                            }

                            output.Append(columnAtt.Caption.IsNullOrEmpty() ? keys[i] : columnAtt.Caption);
                        }
                    }
                }
                catch (Exception ee)
                {
                    throw ex;
                }
            }
            catch
            {
                throw ex;
            }
            throw new RepeatValueException(keys, captions, "此" + output + "已存在");
        }
Esempio n. 2
0
        protected override void ThrowSqlException(Type tableType, Exception ex)
        {
            if (!(ex is Microsoft.Data.SqlClient.SqlException))
            {
                throw ex;
            }
            if (((Microsoft.Data.SqlClient.SqlException)ex).Number != 2601)
            {
                throw ex;
            }

            string[]      keys     = null;
            string[]      captions = null;
            StringBuilder output   = new StringBuilder();

            try
            {
                string msg       = ex.Message;
                var    matches   = Regex.Matches(msg, @"\'(\w|\.)+\'");
                string idxName   = matches[0].Value.Substring(1, matches[0].Value.Length - 2);
                string tableName = matches[1].Value.Replace("dbo.", "");
                tableName = tableName.Substring(1, tableName.Length - 2);

                using (var sp_helpResult = this.SelectDataSet("sp_help [" + tableName + "]"))
                {
                    foreach (var dtable in sp_helpResult.Tables)
                    {
                        if (dtable.Columns.Any(m => m.ColumnName == "index_keys"))
                        {
                            var row = dtable.Rows.SingleOrDefault(m => m["index_name"].Equals(idxName));
                            keys = row["index_keys"].ToString().Split(',');
                            break;
                        }
                    }
                }
                for (int i = 0; i < keys.Length; i++)
                {
                    keys[i] = keys[i].Trim();
                }
                captions = new string[keys.Length];

                for (int i = 0; i < keys.Length; i++)
                {
                    var pinfo = tableType.GetTypeInfo().GetProperty(keys[i]);
                    WayDBColumnAttribute columnAtt = pinfo.GetCustomAttribute(typeof(WayDBColumnAttribute)) as WayDBColumnAttribute;
                    captions[i] = columnAtt.Caption;
                    if (output.Length > 0)
                    {
                        output.Append(',');
                    }

                    output.Append(columnAtt.Caption.IsNullOrEmpty() ? keys[i] : columnAtt.Caption);
                }
            }
            catch
            {
                throw ex;
            }
            throw new RepeatValueException(keys, captions, "此" + output + "已存在");
        }
Esempio n. 3
0
        protected virtual void ThrowSqlException(Type tableType, Exception ex)
        {
            if (!(ex is SqliteException))
            {
                throw ex;
            }
            if (((SqliteException)ex).SqliteErrorCode != 19)
            {
                throw ex;
            }

            List <string> keys = new List <string>();

            string[]      captions = null;
            StringBuilder output   = new StringBuilder();

            try
            {
                string msg = ex.Message.Substring(ex.Message.IndexOf("UNIQUE constraint failed:") + "UNIQUE constraint failed:".Length);

                try
                {
                    var    matches   = Regex.Matches(msg, @"(\w|\.)+");
                    string tableName = null;
                    foreach (Match columninfo in matches)
                    {
                        string[] arr = columninfo.Value.Split('.');
                        if (tableName == null)
                        {
                            tableName = arr[0];
                        }
                        string columnName = arr[1];
                        keys.Add(columnName);
                    }
                    captions = new string[keys.Count];

                    for (int i = 0; i < keys.Count; i++)
                    {
                        var pinfo = tableType.GetProperty(keys[i]);
                        WayDBColumnAttribute columnAtt = pinfo.GetCustomAttribute(typeof(WayDBColumnAttribute)) as WayDBColumnAttribute;
                        captions[i] = columnAtt.Caption;
                        if (output.Length > 0)
                        {
                            output.Append(',');
                        }

                        output.Append(columnAtt.Caption.IsNullOrEmpty() ? keys[i] : columnAtt.Caption);
                    }
                }
                catch
                {
                    throw ex;
                }
            }
            catch
            {
                throw ex;
            }
            throw new RepeatValueException(keys.ToArray(), captions, "此" + output + "已存在");
        }
Esempio n. 4
0
        protected override void ThrowSqlException(Type tableType, Exception ex)
        {
            if (!(ex is MySql.Data.MySqlClient.MySqlException))
            {
                throw ex;
            }
            if (((MySql.Data.MySqlClient.MySqlException)ex).Number != 1062)
            {
                throw ex;
            }

            StringBuilder output = new StringBuilder();

            string[] captions = null;
            string[] keys     = null;
            try
            {
                string msg = ex.Message;


                try
                {
                    var matches = Regex.Matches(msg, @"for key \'(?<n>(\w|\.|_)+)\'");
                    if (matches.Count > 0)
                    {
                        string indexname = matches[matches.Count - 1].Groups["n"].Value;
                        keys = indexname.Substring(indexname.IndexOf("_ej_") + 4).Split('_');

                        captions = new string[keys.Length];

                        for (int i = 0; i < keys.Length; i++)
                        {
                            var pinfo = tableType.GetTypeInfo().GetProperty(keys[i]);

                            WayDBColumnAttribute columnAtt = pinfo.GetCustomAttribute(typeof(WayDBColumnAttribute)) as WayDBColumnAttribute;
                            captions[i] = columnAtt.Caption;
                            if (output.Length > 0)
                            {
                                output.Append(',');
                            }

                            output.Append(columnAtt.Caption.IsNullOrEmpty() ? keys[i] : columnAtt.Caption);
                        }
                    }
                }
                catch (Exception ee)
                {
                    throw ex;
                }
            }
            catch
            {
                throw ex;
            }
            throw new RepeatValueException(keys, captions, "此" + output + "已存在");
        }