Exemple #1
0
        public static string GetOrderSql(ArrayList orderList, ClassMap cm)
        {
            AttributeMap am;
            OrderEntry   order;
            string       orderString = "";

            for (int i = 0; i < orderList.Count; i++)
            {
                order = (OrderEntry)orderList[i];
                am    = cm.GetAttributeMap(order.AttributeName);
                //如果是升序
                if (order.IsAscend)
                {
                    orderString += cm.GetFullyQualifiedName(am.Column.Name) + " ASC,";
                }
                else
                {
                    orderString += cm.GetFullyQualifiedName(am.Column.Name) + " DESC,";
                }
            }
            orderString = orderString.Substring(0, orderString.Length - 1);
            return(" ORDER BY " + orderString);
        }
Exemple #2
0
        //构造Sql语句前部分
        private void GetSqlSelect()
        {
            this.AddSqlClause("SELECT ");
            //Add clauses for all attributes. Don't add "," before the first attribute
            bool         isFirst = true;
            ClassMap     clsMap  = thisClassMap;
            AttributeMap attrMap;
            int          size;
            string       stemp = " AS " + clsMap.RelationalDatabase.QuotationMarksStart;

            do
            {
                //attrMap = clsMap.GetAttributeMap (0);
                size = clsMap.SelfClassAttributes.Count;
                for (int i = 0; i < size; i++)
                {
                    attrMap = (AttributeMap)clsMap.SelfClassAttributes[i];
                    this.AddSqlClause((isFirst?"":",") + this.clsMap.GetFullyQualifiedName(attrMap.Column.Name));
                    this.AddSqlClause(stemp + attrMap.Name + clsMap.RelationalDatabase.QuotationMarksEnd);
                    isFirst = false;
                }
                clsMap = clsMap.SuperClass;
            }while(clsMap != null);

            //Add the FROM clause to the statement
            this.AddSqlClause(" FROM " + ThisClassMap.RelationalDatabase.QuotationMarksStart + thisClassMap.Table.Name + ThisClassMap.RelationalDatabase.QuotationMarksEnd);

            if (ThisClassMap.RelationalDatabase.Vendor == DatabaseVendor.MsAccess || ThisClassMap.RelationalDatabase.Vendor == DatabaseVendor.MsSqlServer)
            {
                this.AddSqlClause(" With(nolock)");
            }

            isFirst = true;
            clsMap  = thisClassMap.SuperClass;
            while (clsMap != null)
            {
                this.AddSqlClause("," + clsMap.Table.Name);
                clsMap = clsMap.SuperClass;
            }
            while (clsMap != null)
            {
                ;
            }
            //此语句为SELECT的前部
            this._selecFromClause = this.sql;



            //Add WHERE Clause to the statement
            this.AddSqlClause(" WHERE 1=1 ");
            for (int i = 0; i < thisClassMap.GetKeySize(); i++)
            {
                attrMap = thisClassMap.GetKeyAttributeMap(i);
                //该列是否Primary Key
                if (attrMap.Column.KeyType == ColumnKeyTypes.PrimaryKey)
                {
                    this.AddSqlClause(" AND " + this.clsMap.GetFullyQualifiedName(attrMap.Column.Name) + "=" +
                                      base.clsMap.RelationalDatabase.GetStringParameter(attrMap.Name, i));
                }
            }

            //Add WHERE Clause table.column=superclass_table.column to the selectStatement
            clsMap  = this.clsMap;
            isFirst = true;
            do
            {
                size = clsMap.GetReferenceSize();
                for (int i = 0; i < size; i++)
                {
                    attrMap = clsMap.GetReferenceAttributeMap(i);
                    if (isFirst)
                    {
                        _stringForInherit = this.clsMap.GetFullyQualifiedName(attrMap.Column.Name) +
                                            "=" + this.clsMap.GetFullyQualifiedName(attrMap.Reference.Column.Name);
                        isFirst = false;
                    }
                    else
                    {
                        _stringForInherit += " AND " + this.clsMap.GetFullyQualifiedName(attrMap.Column.Name) +
                                             "=" + this.clsMap.GetFullyQualifiedName(attrMap.Reference.Column.Name);
                    }
                }
                if (this._stringForInherit != null)
                {
                    this.AddSqlClause(" AND " + this._stringForInherit);
                }
                clsMap = clsMap.SuperClass;
            }while (clsMap != null);
        }
Exemple #3
0
 //add by tintown
 //用于进行字段与字段的比较
 public SelectionCriteria(SelectionTypes selection, AttributeMap attribute, AttributeMap attribute2, ClassMap clsMap)
 {
     this.type            = selection;
     this._atrribute      = attribute;
     this._isFieldToField = true;
     this._attribute2     = attribute2;
     this.clsMap          = clsMap;
 }
Exemple #4
0
 public SelectCommander(ClassMap cm) : base(cm)
 {
     thisClassMap = cm;
     this.GetSqlSelect();
 }
Exemple #5
0
 //Constructor
 public SelectionCriteria(SelectionTypes criteriaType, string attributeName, object attributeValue, ClassMap clsMap)
 {
     this.type      = criteriaType;
     this.attrName  = attributeName;
     this.attrValue = attributeValue;
     this.clsMap    = clsMap;
 }
Exemple #6
0
 public SelectionCriteria(SelectionTypes selection, AttributeMap attribute, object attributeValue, ClassMap clsMap)
 {
     this.type       = selection;
     this._atrribute = attribute;
     this.attrValue  = attributeValue;
     this.clsMap     = clsMap;
 }
Exemple #7
0
 internal OrGroup(ClassMap clsMap)
 {
     this._classMap = clsMap;
 }
Exemple #8
0
        private string BuildForQuery(IDbCommand cmd)
        {
            string whereClause;

            fromClause = " FROM " + queryClass.RelationalDatabase.QuotationMarksStart + queryClass.Table.Name + queryClass.RelationalDatabase.QuotationMarksEnd;
            ClassMap cm = this.queryClass.SuperClass;

            while (cm != null)
            {
                fromClause += "," + queryClass.RelationalDatabase.QuotationMarksStart + cm.Table.Name + queryClass.RelationalDatabase.QuotationMarksEnd;
                cm          = cm.SuperClass;
            }

            whereClause = " WHERE " + GetSqlForConditions();
            if (this.queryClass.SuperClass != null)
            {
                whereClause += " AND " + this.queryClass.StringForInherit;
            }

            //orderClause = " ORDER BY ''"+orderClause;

            //生成Sql语句
            Query q;

            for (int j = 0; j < m_Querys.Count; j++)
            {
                q = (Query)m_Querys[j];
                //Select Clause
                selectClause += q.selectClause;
                //From Clause
                cm = q.queryClass;
                do
                {
                    fromClause += "," + queryClass.RelationalDatabase.QuotationMarksStart + cm.Table.Name + queryClass.RelationalDatabase.QuotationMarksEnd;
                    cm          = cm.SuperClass;
                }while (cm != null);
                //Where Clause
                whereClause += " AND " + q.GetSqlForConditions();
                if (q.queryClass.SuperClass != null)
                {
                    whereClause += " AND " + q.queryClass.StringForInherit;
                }
                //Order Clause
                orderClause += q.orderClause;
                //Group Clause
                groupClause += q.groupClause;
            }

            string selectC = "";

            if (this.m_top != 0)
            {
                selectString += " Top " + this.m_top.ToString() + " ";
            }

            if (selectClause != "")
            {
                selectC = this.selectString + selectClause.Remove(0, 1);
            }
            else
            {
                throw new PlException("构建Sql错误!", ErrorTypes.PesistentError);
            }
            if (groupClause != "")
            {
                groupClause = " GROUP BY " + groupClause.Remove(0, 1);
                orderClause = "";
            }

            //			Console.WriteLine(selectClause);
            //			Console.WriteLine(fromClause);
            //			Console.WriteLine(whereClause);
            //			Console.WriteLine(orderClause);
            //			Console.WriteLine(groupClause);
            if (this.orderClause != "")
            {
                orderClause = " ORDER BY " + orderClause.Remove(0, 1);
            }
            this._sqlString = selectC + fromClause + whereClause + orderClause + groupClause;
            return(this._sqlString);
        }
Exemple #9
0
        private AttributeMap GetAttributeMap(XmlReader reader, ClassMap clsMap, TableMap tblMap, int colIndex)
        {
            string attrName      = reader.GetAttribute("name");                         //类属性名
            string attrColumn    = reader.GetAttribute("column");                       //属性对应列名
            string attrKey       = reader.GetAttribute("key");                          //键类型
            string attrReference = reader.GetAttribute("reference");                    //引用父类属性
            string dataType      = reader.GetAttribute("type");                         //数据类型
            string autoIncrement = reader.GetAttribute("increment");                    //是否自动增加
            string timestamp     = reader.GetAttribute("timestamp");                    //时间戳
            string autoValue     = reader.GetAttribute("auto");                         //是否自动值

            AttributeMap attrMap = null;

            if (attrName != null)
            {
                ColumnMap colMap = null;
                //生成一个AttributeMap
                attrMap = new AttributeMap(attrName);
                if (attrColumn != null)
                {
                    colMap = new ColumnMap(attrColumn, tblMap);

                    if (attrKey != null)
                    {
                        //设置键类型
                        if (attrKey.ToUpper() == "PRIMARY")
                        {
                            colMap.KeyType = ColumnKeyTypes.PrimaryKey;
                        }
                        else if (attrKey.ToUpper() == "FOREIGN")
                        {
                            colMap.KeyType = ColumnKeyTypes.ForeignKey;
                        }
                    }
                    if (dataType == null)
                    {
                        string s = "不能确定列" + clsMap.GetFullyQualifiedName(colMap.Name) + "的数据类型!";
                        throw new PlException(s, ErrorTypes.XmlError);
                    }
                    //确定数据列的数据类型
                    //Xml中所有数据类型为OleDbType中的枚举类型
                    //将其映射到 DbType
                    switch (dataType.ToLower())
                    {
                    case "boolean":
                        colMap.Type = DbType.Boolean;
                        attrMap.SqlValueStringType = clsMap.RelationalDatabase.SqlValueType(DbType.Boolean);
                        break;

                    //Map to Int64
                    case "bigint":
                        colMap.Type = DbType.Int64;
                        break;

                    //Map to Binary
                    case "binary":
                        colMap.Type = DbType.Binary;
                        attrMap.SqlValueStringType = SqlValueTypes.NotSupport;
                        break;

                    //Map to Decimal
                    case "currency":
                        colMap.Type = DbType.Currency;
                        break;

                    //Map to DateTime
                    case "date":
                        //对Access的数据库做特殊处理
                        if (clsMap.RelationalDatabase.Vendor == MsAccess.VENDOR_NAME)
                        {
                            colMap.Type = DbType.Object;
                            attrMap.SqlValueStringType = SqlValueTypes.AccessDateString;
                        }
                        else if (clsMap.RelationalDatabase.Vendor == DatabaseVendor.Oracle)                              //如果是oracle的话,则会对日期使用to_date转换
                        {
                            colMap.Type = DbType.DateTime;
                            attrMap.SqlValueStringType = SqlValueTypes.OracleDate;
                        }
                        else
                        {
                            colMap.Type = DbType.DateTime;
                            attrMap.SqlValueStringType = SqlValueTypes.SimpleQuotesString;
                        }
                        break;

                    //Mapt to Date
                    case "dbdate":
                        colMap.Type = DbType.Date;
                        attrMap.SqlValueStringType = SqlValueTypes.SimpleQuotesString;
                        break;

                    //Map to Decimal
                    case "decimal":
                        colMap.Type = DbType.Decimal;
                        break;

                    //Map to Double
                    case "double":
                        colMap.Type = DbType.Double;
                        break;

                    //Map to Guid
                    case "guid":
                        colMap.Type = DbType.Guid;
                        attrMap.SqlValueStringType = SqlValueTypes.SimpleQuotesString;
                        break;

                    //Map to Object
                    case "object":
                        colMap.Type = DbType.Object;
                        attrMap.SqlValueStringType = SqlValueTypes.NotSupport;
                        break;

                    //Map to Single
                    case "single":
                        colMap.Type = DbType.Single;
                        break;

                    //Map to Int16
                    case "smallint":
                        colMap.Type = DbType.Int16;
                        break;

                    //Map to SBtype
                    case "tinyint":
                        colMap.Type = DbType.Byte;
                        break;

                    //Map to Int32
                    case "integer":
                        colMap.Type = DbType.Int32;
                        break;

                    //Map to String
                    case "varchar":
                    case "string":
                        colMap.Type = DbType.String;
                        attrMap.SqlValueStringType = SqlValueTypes.String;
                        break;


                    default:
                        throw new PlException("目前仍不支持的数据类型!", ErrorTypes.XmlError);
                    }
                    //该列不是自动增加
                    if (autoIncrement != null && autoIncrement.ToLower() == "true")
                    {
                        colMap.IsAutoValue           = true;
                        tblMap.AutoIdentityIndex     = colIndex;
                        clsMap.AutoIdentityAttribute = attrName;
                    }

                    //是不是timestamp
                    if (timestamp != null && timestamp.ToLower() == "true")
                    {
                        tblMap.TimestampColumn = attrColumn;
                        if (clsMap.RelationalDatabase.Vendor == DatabaseVendor.MySql)
                        {
                            tblMap.TimestampParameter = "?" + tblMap.TimestampColumn;
                        }
                        else
                        {
                            tblMap.TimestampParameter = "@" + tblMap.TimestampColumn;
                        }
                        //tblMap.TimestampParameter = "@" + tblMap.TimestampColumn ;
                        clsMap.TimestampAttribute = attrMap;
                        colMap.IsAutoValue        = true;
                    }

                    //是不是自动值
                    if (autoValue != null)
                    {
                        colMap.IsAutoValue = true;
                    }
                }                // end of if (column!=null)

                attrMap.Column = colMap;

                if ((attrReference != null) && (clsMap.SuperClass != null))
                {
                    //若该属性来自父类,则它的Reference属性指向父类的AttributeMap
                    AttributeMap r = clsMap.SuperClass.GetAttributeMap(attrReference, true);
                    if (r != null)
                    {
                        attrMap.Reference = r;
                    }
                }
            }
            return(attrMap);
        }
Exemple #10
0
 public DeleteCommander(ClassMap cm) : base(cm)
 {
     this.GetSqlDelete();
 }
Exemple #11
0
        //从Xml Node结点(Name=class)得到相应ClassMap
        private ClassMap GetClassMap(XmlNodeReader node)
        {
            string className      = node.GetAttribute("name");                                                                  //类名
            string tableName      = node.GetAttribute("table");                                                                 //表名
            string databaseName   = node.GetAttribute("database");                                                              //数据库名
            string superClassName = node.GetAttribute("superclass");                                                            //父类名
            //string timeStamp = node.GetAttribute ("timeStamp");							//时间戳

            string IsSaveToMemory = node.GetAttribute("IsSaveToMemory");

            if (IsSaveToMemory == null)
            {
                IsSaveToMemory = "false";
            }

            int    c = 0;
            string err;

            ClassMap clsMap = null;

            if ((className != null) && (databaseName != null) && (tableName != null))
            {
                RelationalDatabase r = (RelationalDatabase)this.DatabasePool[databaseName];
                if (r == null)
                {
                    err = "名为:" + databaseName + "的数据库映射信息未找到!";
                    throw new PlException(err, ErrorTypes.XmlError);
                }
                clsMap = new ClassMap(className, r);
                //从XML读取是否保存到内存值
                clsMap.IsSaveToMemory = IsSaveToMemory.ToLower() == "true"?true:false;

                if (superClassName != null)
                {
                    clsMap.SuperClass = (ClassMap)classMaps[superClassName];
                    if (clsMap.SuperClass == null)
                    {
                        err = "在superclass中引用 " + superClassName + "前,必须对该类映射!";
                        throw new PlException(err, ErrorTypes.XmlError);
                    }
                    clsMap.SelfClassAttributes = new ArrayList();
                }
                else
                {
                    //如果该类没有父类,则把SelfClassAttributes指向AttributesMapToTable
                    clsMap.SelfClassAttributes = clsMap.AttributesMapToTable;
                }
                //生成一个DatabaseMap对象,并加如databaseMaps
                //若它已存在则直接引用
                if (!databaseMaps.ContainsKey(databaseName))
                {
                    err = "在映射类:" + className + "时,名为:" + databaseName + "的数据库映射信息找不到!";
                    throw new PlException(err, ErrorTypes.XmlError);
                }
                //
                TableMap tblMap = new TableMap(tableName, (DatabaseMap)databaseMaps[databaseName]);

                int clsDep = node.Depth;
                while (node.Read() && node.Depth > clsDep)
                {
                    if ((node.NodeType == XmlNodeType.Element) && (node.Name == "attribute"))
                    {
                        //若该Node是Element类型能 且 它的名字是attribute
                        AttributeMap attrMap = GetAttributeMap(node, clsMap, tblMap, c);
                        if (attrMap != null)
                        {
                            clsMap.AddAttributeMap(attrMap);
                        }
                        c++;
                    }
                }
            }
            else
            {
                err = "ClassMap 缺少className,databaseName,tableName 这些必要属性!";
                throw new PlException(err, ErrorTypes.XmlError);
            }
            //缺乏主键
            if (clsMap.Table.PrimaryKeyIndex < 0)
            {
                err = "表 " + clsMap.Table.Name + " 未定义主键!";
                throw new PlException(err, ErrorTypes.XmlError);
            }
            return(clsMap);
        }
Exemple #12
0
 internal Condition(ClassMap clsMap)
 {
     this._classMap = clsMap;
 }
Exemple #13
0
 public UpdateCommander(ClassMap cm) : base(cm)
 {
     this.GetSqlUpdate();
 }
Exemple #14
0
 public InsertCommander(ClassMap cm) : base(cm)
 {
     this.GetSqlInsert();
 }
Exemple #15
0
        protected ClassMap clsMap;                                                              //保存ClassMap

        //Constructor
        public SqlCommander(ClassMap cm)
        {
            clsMap = cm;
        }