Beispiel #1
0
        public static bool ExecuteNonQuery(this ICURDProperties cURDProperties, string sql, params object[] parameterrs)
        {
            bool result = false;

            try
            {
                if (cURDProperties == null)
                {
                    return(false);
                }
                using (IDbConnection dbConnection = cURDProperties.GetDBConnection())
                {
                    dbConnection.Open();
                    var transation = dbConnection.BeginTransaction();
                    if (parameterrs != null && parameterrs.Length > 0)
                    {
                        result = dbConnection.Execute(sql, parameterrs, transation) > 0;
                        transation.Commit();
                    }
                    else
                    {
                        result = dbConnection.Execute(sql) > 0;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(result);
        }
Beispiel #2
0
        public static string GetTableName(this ICURDProperties updateProperties, string tableIndex = null, int id = 0)
        {
            var tableAttribute = updateProperties.GetCustomerAttributes <MatchedTableAttribute>().FirstOrDefault(w => w.ID == id);
            var tableName      = tableAttribute?.Name;

            if (string.IsNullOrEmpty(tableName))
            {
                tableName = updateProperties.GetType().Name;
            }
            //tableName = tableName.Trim();//Support Use Sencond Type TableIndex For Table FullName
            var    ITableSharding  = updateProperties as ITableSharding;
            string tableNameFormat = string.Empty;

            if (ITableSharding != null)
            {
                tableNameFormat = string.Format(tableName, ITableSharding.__TableIndex__);
                if (tableIndex == null)
                {
                    return(tableNameFormat);
                }
            }
            if (tableName.StartsWith("{") && tableName.EndsWith("}"))
            {
                tableName = AppSetting.GetConfig(tableName.TrimStart('{').TrimEnd('}'));
            }
            if (tableName.Contains("{") && tableName.Contains("}"))
            {
                tableNameFormat = string.Format(tableName, tableIndex);
                return(tableNameFormat);
            }
            return(tableName);
        }
Beispiel #3
0
        public static DBServerType GetDBServerType(this ICURDProperties cURDProperties, int id = 0)
        {
            var tableAttribute = cURDProperties.GetMatchedTableAttribute();

            if (tableAttribute == null)
            {
                throw new Exception("TableAttribute Not Found");
            }
            DBServerType dBServerType = tableAttribute.DBServerType;

            return(dBServerType);
        }
Beispiel #4
0
        public static List <string> GetMatchedKeyNameAndValues(this ICURDProperties updateProperties, int id = 0)
        {
            List <string> result = new List <string>();

            if (updateProperties == null)
            {
                return(result);
            }
            var properties = updateProperties.GetType().GetProperties();

            result.Add(" Where 1=0 ");
            if (properties != null)
            {
                foreach (var property in properties)
                {
                    if (property.IgnoreProperty(updateProperties, id))
                    {
                        continue;
                    }
                    var matchedKeyAttribute = property.GetCustomAttributes(typeof(MatchedKeysAttribute), true).Select(w => w as MatchedKeysAttribute).FirstOrDefault(w => w.ID == id);
                    if (matchedKeyAttribute != null)
                    {
                        var key = property.Name;
                        result.Add(string.Format("and {0}=@{0}", key));
                    }
                    var matchedColumnAttribute = property.GetCustomAttributes(typeof(MatchedColumnAttribute), true).Select(w => w as MatchedColumnAttribute).FirstOrDefault(w => w.ID == id);
                    if (matchedColumnAttribute != null)
                    {
                        var columnName = matchedColumnAttribute.AliasName;
                        if (string.IsNullOrEmpty(columnName))
                        {
                            columnName = property.Name;
                        }
                        var contactNotation = matchedColumnAttribute.ContactNotation;
                        var columnValue     = "@" + property.Name;
                        var filterRelation  = matchedColumnAttribute.FilterRelation;
                        if (string.Compare(contactNotation, "like", true) == 0 && property.PropertyType == typeof(string))
                        {
                            property.SetValue(updateProperties, "%" + property.GetValue(updateProperties, null) + "%");
                        }
                        result.Add(string.Format(" {0} {1} {2} {3} ", filterRelation, columnName, contactNotation, columnValue));
                    }
                }
            }
            if (result.Count > 1 && !string.IsNullOrEmpty(result[1]))
            {
                result[1] = result[1].Replace("And", "Or ( ");
                result[result.Count - 1] += " ) ";
            }
            return(result);
        }
Beispiel #5
0
        public static string GetConnectionKey(this ICURDProperties updateProperties, int id = 0)
        {
            var tableAttribute = updateProperties.GetCustomerAttributes <MatchedTableAttribute>().FirstOrDefault(w => w.ID == id);
            var connectionKey  = tableAttribute?.ConnectionKey;

            if (string.IsNullOrEmpty(connectionKey))
            {
                return(connectionKey);
            }
            connectionKey = connectionKey.Trim();
            if (connectionKey.StartsWith('{') && connectionKey.EndsWith('}'))
            {
                connectionKey = AppSetting.GetConfig(connectionKey.TrimStart('{').TrimEnd('}'));
            }
            return(connectionKey);
        }
Beispiel #6
0
        public static IDbConnection GetDBConnection(this ICURDProperties cURDProperties, int id = 0)
        {
            var tableAttribute = cURDProperties.GetMatchedTableAttribute();

            if (tableAttribute == null)
            {
                throw new Exception("TableAttribute Not Found");
            }
            DBServerType dBServerType           = tableAttribute.DBServerType;
            string       connectionKey          = tableAttribute.ConnectionKey;
            string       connectionString       = AppSetting.GetConfig(connectionKey);
            string       connectionStringFormat = connectionString;
            var          IDataBaseSharding      = cURDProperties as IDataBaseSharding;

            if (IDataBaseSharding != null)
            {
                connectionStringFormat = string.Format(connectionString, IDataBaseSharding.__DataBaseIndex__);
            }
            return(DBConnectionFactory.GetDbConnection(connectionStringFormat, dBServerType));
        }
Beispiel #7
0
        public static List <string> GetInMatchedKeyNameAndValues(this ICURDProperties updateProperties, int id = 0, bool ignoreMatched = false, params List <object>[] listsIn)
        {
            List <string> result = new List <string>();

            if (updateProperties == null)
            {
                return(result);
            }
            var properties = updateProperties.GetType().GetProperties();

            result.Add(" Where 1=0 ");
            bool hasMatchedColumn = false;
            int  i = 0;

            if (properties != null)
            {
                foreach (var property in properties)
                {
                    if (!hasMatchedColumn && property.MatchedProperty(updateProperties, id) != null)
                    {
                        hasMatchedColumn = true;
                    }
                    if (!ignoreMatched && property.IgnoreProperty(updateProperties, id))
                    {
                        continue;
                    }
                    if (ignoreMatched && property.IgnoreMatchedProperty(updateProperties, id))
                    {
                        continue;
                    }
                    var matchedKeyAttribute = property.GetCustomAttributes(typeof(MatchedKeysAttribute), true).Select(w => w as MatchedKeysAttribute).FirstOrDefault(w => w.ID == id);
                    if (matchedKeyAttribute != null)
                    {
                        var key = property.Name;
                        result.Add(string.Format("And {0}=@{0}", key));
                    }
                    var matchedColumnAttribute = property.GetCustomAttributes(typeof(MatchedColumnAttribute), true).Select(w => w as MatchedColumnAttribute).FirstOrDefault(w => w.ID == id);
                    if (matchedColumnAttribute != null)
                    {
                        var columnName = matchedColumnAttribute.AliasName;
                        if (string.IsNullOrEmpty(columnName))
                        {
                            columnName = property.Name;
                        }
                        var contactNotation = matchedColumnAttribute.ContactNotation;
                        var columnValue     = "@" + property.Name;
                        var filterRelation  = matchedColumnAttribute.FilterRelation;
                        if (string.Compare(contactNotation, "like", true) == 0 && property.PropertyType == typeof(string))
                        {
                            property.SetValue(updateProperties, "%" + property.GetValue(updateProperties, null) + "%");
                        }
                        if (!string.IsNullOrEmpty(contactNotation) && contactNotation.ToUpper().Contains("IN") && listsIn != null && listsIn.Length > i)
                        {
                            columnValue = property.GetColumnInValue(i, listsIn);
                            result.Add(string.Format(" {0} {1} {2} {3} ", filterRelation, columnName, contactNotation, columnValue));
                            i++;
                        }
                        else
                        {
                            if (property.GetValue(updateProperties, null) == null && contactNotation == "=")
                            {
                                contactNotation = "is"; columnValue = "null";
                            }
                            result.Add(string.Format(" {0} {1} {2} {3} ", filterRelation, columnName, contactNotation, columnValue));
                        }
                    }
                }
            }
            if (result.Count == 1 && hasMatchedColumn)
            {
                return(new List <string>());
            }
            if (result.Count > 1 && !string.IsNullOrEmpty(result[1]))
            {
                result[1] = result[1].Replace("And", "Or ( ");
                result[result.Count - 1] += " ) ";
            }
            return(result);
        }
Beispiel #8
0
        public static MatchedTableAttribute GetMatchedTableAttribute(this ICURDProperties updateProperties, int id = 0)
        {
            var tableAttribute = updateProperties.GetCustomerAttributes <MatchedTableAttribute>().FirstOrDefault(w => w.ID == id);

            return(tableAttribute);
        }