예제 #1
0
        public RETTYPE ExecuteScale <T, RETTYPE>(IDbConnection conn, string strExpression, string strSql, params object[] parameters) where T : class
        {
            RETTYPE result;

            try
            {
                Type     typeFromHandle = typeof(T);
                TableMap tableMap       = TableMapFactory.Instance[typeFromHandle];
                if (null == tableMap)
                {
                    throw new ORMException("构造" + typeFromHandle.FullName + "的映射产生错误");
                }
                if (strSql == null || strSql.Trim().Length < 1)
                {
                    strSql = "select " + strExpression + " from " + tableMap.TableName;
                }
                else if (strSql.Trim().ToLower().StartsWith("order"))
                {
                    strSql = string.Concat(new string[]
                    {
                        "select ",
                        strExpression,
                        " from ",
                        tableMap.TableName,
                        " ",
                        strSql
                    });
                }
                else if (!strSql.Trim().ToLower().StartsWith("select"))
                {
                    if (!strSql.Trim().ToLower().StartsWith("where "))
                    {
                        strSql = "where " + strSql;
                    }
                    strSql = string.Concat(new string[]
                    {
                        "select ",
                        strExpression,
                        " from ",
                        tableMap.TableName,
                        " ",
                        strSql
                    });
                }
                IDbCommand dbCommand = this.PrepareCommand(conn, strSql, parameters);
                object     obj       = dbCommand.ExecuteScalar();
                if (obj.Equals(DBNull.Value))
                {
                    obj = null;
                }
                if (this.Verbose)
                {
                    this.VerboseInfo(dbCommand);
                }
                result = (RETTYPE)((object)this.m_type2DbType.GetNullValue(typeof(RETTYPE), obj));
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(result);
        }
예제 #2
0
        public string GetTableNameFromType(Type t)
        {
            TableMap tableMap = this[t];

            return((tableMap == null) ? t.Name : tableMap.TableName);
        }
예제 #3
0
        public int Update(IDbConnection conn, object obj, string[] fieldUpdate, string[] fieldWhere)
        {
            if (obj == null || conn == null)
            {
                throw new ORMException((obj == null) ? "输入的对象为空!" : "数据库连接为空!");
            }
            TableMap tableMap = TableMapFactory.Instance[obj];

            if (tableMap == null || null == tableMap.MappedFields)
            {
                throw new ORMException("构造" + obj.GetType().FullName + "的映射产生错误");
            }
            if (fieldWhere == null || fieldWhere.Length < 1)
            {
                List <FieldAttribute> primaryKeys = tableMap.PrimaryKeys;
                if (null != primaryKeys)
                {
                    fieldWhere = new string[primaryKeys.Count];
                    for (int i = 0; i < primaryKeys.Count; i++)
                    {
                        fieldWhere[i] = primaryKeys[i].TableFiled;
                    }
                }
            }
            string text = "";

            if (fieldWhere != null)
            {
                string[] array = fieldWhere;
                for (int j = 0; j < array.Length; j++)
                {
                    string         text2          = array[j];
                    FieldAttribute fieldAttribute = tableMap[text2];
                    if (null == fieldAttribute)
                    {
                        throw new ORMException(obj.GetType().FullName + "的映射产生错误:无法找到" + text2 + "的字段映射");
                    }
                    text = text + "[" + fieldAttribute.TableFiled.Trim().ToUpper() + "]";
                }
            }
            string                text3     = "";
            string                text4     = "";
            IDbCommand            dbCommand = this.PrepareCommand(conn, null, null);
            List <FieldAttribute> list      = tableMap.MappedFields;

            if (fieldUpdate != null && fieldUpdate.Length > 0)
            {
                list = new List <FieldAttribute>();
                for (int j = 0; j < fieldUpdate.Length; j++)
                {
                    string         strFieldName    = fieldUpdate[j];
                    FieldAttribute fieldAttribute2 = tableMap[strFieldName];
                    if (null != fieldAttribute2)
                    {
                        list.Add(fieldAttribute2);
                    }
                }
            }
            foreach (FieldAttribute fieldAttribute in list)
            {
                //FieldAttribute fieldAttribute;
                if (text.IndexOf("[" + fieldAttribute.TableFiled.ToUpper() + "]") < 0)
                {
                    if (text4.Length > 0)
                    {
                        text4 += ",";
                    }
                    if (!this.m_type2DbType.IsDbNullValue(fieldAttribute.GetValue(obj)))
                    {
                        string parameterName = this.GetParameterName(fieldAttribute.TableFiled);
                        text4 = text4 + fieldAttribute.TableFiled + "=" + parameterName;
                        IDbDataParameter value = this.CreateParameter(dbCommand, fieldAttribute, parameterName, obj);
                        dbCommand.Parameters.Add(value);
                    }
                    else
                    {
                        text4 = text4 + fieldAttribute.TableFiled + "=null";
                    }
                }
            }
            foreach (FieldAttribute fieldAttribute in list)
            {
                //FieldAttribute fieldAttribute;
                if (text.IndexOf("[" + fieldAttribute.TableFiled.ToUpper() + "]") >= 0)
                {
                    string           parameterName = this.GetParameterName(fieldAttribute.TableFiled);
                    IDbDataParameter value         = this.CreateParameter(dbCommand, fieldAttribute, parameterName, obj);
                    if (text3.Length > 0)
                    {
                        text3 += " and ";
                    }
                    text3 = text3 + fieldAttribute.TableFiled + "=" + parameterName;
                    dbCommand.Parameters.Add(value);
                }
            }
            if (text4 == null || text4.Length < 1)
            {
                throw new ORMException(obj.GetType().FullName + "的映射产生错误:无法找到可更新的字段");
            }
            if (text3 == null || text3.Trim().Length < 1)
            {
                throw new ORMException(obj.GetType().FullName + "的映射产生错误:找不到更新数据库表格的可参考字段");
            }
            text3 = " where " + text3;
            string format = "update {0} set {1} {2}";

            dbCommand.CommandText = string.Format(format, tableMap.TableName, text4, text3);
            if (this.Verbose)
            {
                this.VerboseInfo(dbCommand);
            }
            return(dbCommand.ExecuteNonQuery());
        }