Esempio n. 1
0
        /// <summary>
        /// 构造函数。
        /// </summary>
        public BaseSqlGenerator()
        {
            //默认的数据库类型为Oracle 数据库.
            _DatabaseType = MB.Orm.Persistence.DatabaseConfigurationScope.CreateDatabaseType();

            _ParameterPrefix = MB.Orm.DbSql.SqlShareHelper.SQL_XML_CFG_PARAM_PREFIX;
        }
        /// <summary>
        /// 根据数据类型检查指定的值在数据库中是否已经存在
        /// </summary>
        /// <param name="dataInDocType">需要进行检查的数据类型</param>
        /// <param name="entity">需要检查的实体对象</param>
        /// <param name="checkPropertys">需要检查的属性名称</param>
        /// <returns>true 或 false</returns>
        public bool CheckValueIsExists(object dataInDocType, object entity, string[] checkPropertys)
        {
            ObjectDataMappingAttribute mappingAtt = AttributeConfigHelper.Instance.GetObjectDataMappingAttribute(dataInDocType);

            if (mappingAtt == null)
            {
                throw new MB.RuleBase.Exceptions.RequireConfigDataMappingException(dataInDocType);
            }

            string tableName = mappingAtt.MappingTableName;
            object keyValue  = MB.Util.MyReflection.Instance.InvokePropertyForGet(entity, mappingAtt.KeyName);

            if (keyValue == null)
            {
                keyValue = string.Empty;
            }

            string sql = string.Format("SELECT 1 FROM {0} WHERE ROWNUM <=1 AND {1}<>'{2}' AND ", tableName, mappingAtt.KeyName, keyValue.ToString().Replace("'", "''"));

            string where = string.Empty;
            MB.Orm.Enums.DatabaseType dbaseType = MB.Orm.Persistence.DatabaseHelper.CreateDatabaseType();
            foreach (string name in checkPropertys)
            {
                if (!string.IsNullOrEmpty(where))
                {
                    where += " AND ";
                }
                object checkVal = MB.Util.MyReflection.Instance.InvokePropertyForGet(entity, name);
                Type   type     = MB.Util.MyReflection.Instance.GetPropertyType(entity, name);

                if (type == typeof(string)) //如果是string
                {
                    string checkValStr = checkVal as string;
                    if (string.IsNullOrEmpty(checkValStr))
                    {
                        where += string.Format(" ({0} = '' OR {0} IS NULL) ", name);
                    }
                    else
                    {
                        where += string.Format("{0} = '{1}'", name, checkVal.ToString().Replace("'", "''"));
                    }
                }
                else if (type == typeof(DateTime))
                {
                    //如果是日期类型,日期类型只检查到
                    DateTime checkValueUnBoxed = (DateTime)checkVal;
                    if (dbaseType == DatabaseType.Oracle)
                    {
                        where += string.Format("TO_CHAR({0}, 'YYYY-MM-DD') = '{1}'", name, checkValueUnBoxed.ToString("yyyy-MM-dd").Replace("'", "''"));
                    }
                    else
                    {
                        where += string.Format("{0} = '{1}'", name, checkValueUnBoxed.ToString("yyyy-MM-dd").Replace("'", "''"));
                    }
                }
                else if (type == typeof(DateTime?))
                {
                    if (checkVal == null)
                    {
                        where += string.Format(" {0} IS NULL ", name);
                    }
                    else
                    {
                        DateTime checkValueUnBoxed = (DateTime)checkVal;
                        if (dbaseType == DatabaseType.Oracle)
                        {
                            //如果是日期类型,日期类型只检查到
                            where += string.Format("TO_CHAR({0}, 'YYYY-MM-DD') = '{1}'", name, checkValueUnBoxed.ToString("yyyy-MM-dd").Replace("'", "''"));
                        }
                        else
                        {
                            where += string.Format("{0} = '{1}'", name, checkValueUnBoxed.ToString("yyyy-MM-dd").Replace("'", "''"));
                        }
                    }
                }
                else
                {
                    if (checkVal == null)
                    {
                        where += string.Format(" {0} IS NULL ", name);
                    }
                    else
                    {
                        where += string.Format("{0} = '{1}'", name, checkVal.ToString().Replace("'", "''"));
                    }
                }
            }
            Database db = MB.Orm.Persistence.DatabaseHelper.CreateDatabase();

            sql += where;

            DbCommand dbCmd = db.GetSqlStringCommand(sql);
            object    val   = DatabaseExecuteHelper.NewInstance.ExecuteScalar(db, dbCmd);

            if (val != null)
            {
                return(MB.Util.MyConvert.Instance.ToBool(val));
            }
            else
            {
                return(false);
            }
        }
Esempio n. 3
0
 /// <summary>
 /// 设置对应需要操作的数据库类型。
 /// 可以从应用程序的Config 文件中获取。
 /// </summary>
 /// <param name="databaseType"></param>
 public void SetDatabaseType(MB.Orm.Enums.DatabaseType databaseType)
 {
     _DatabaseType    = databaseType;
     _ParameterPrefix = MB.Orm.DbSql.SqlShareHelper.SQL_XML_CFG_PARAM_PREFIX;// MB.Orm.Common.DbShare.Instance.GetPramPrefixByDatabaseType(databaseType);
 }