Beispiel #1
0
        private void Init(MDataTable mTable, string conn)
        {
            if (mTable.Columns == null || mTable.Columns.Count == 0)
            {
                Error.Throw("MDataTable's columns can't be null or columns'length can't be zero");
            }
            if (string.IsNullOrEmpty(mTable.TableName))
            {
                Error.Throw("MDataTable's tablename can't  null or empty");
            }
            mdt = sourceTable = mTable;

            if (mdt.TableName.IndexOfAny(new char[] { '(', ')' }) > -1)
            {
                mdt.TableName = mdt.TableName.Substring(mdt.TableName.LastIndexOf(')') + 1).Trim();
            }

            _Conn = !string.IsNullOrEmpty(conn) ? conn : mTable.Conn;
            if (!DBTool.ExistsTable(mdt.TableName, _Conn, out dalTypeTo, out database))
            {
                DBTool.ErrorMsg = null;
                if (!DBTool.CreateTable(mdt.TableName, mdt.Columns, _Conn))
                {
                    Error.Throw("Create Table Error:" + mdt.TableName + DBTool.ErrorMsg);
                }
            }
            MDataColumn column = DBTool.GetColumns(mdt.TableName, _Conn);

            FixTable(column);//
            if (mdt.Columns.Count == 0)
            {
                Error.Throw("After fix table columns, length can't be zero");
            }
            SetDbBaseForTransaction();
        }
Beispiel #2
0
        private void Init(MDataTable mTable, string conn)
        {
            if (mTable.Columns == null || mTable.Columns.Count == 0)
            {
                Error.Throw("MDataTable's columns can't be null or columns'length can't be zero");
            }
            if (string.IsNullOrEmpty(mTable.TableName))
            {
                Error.Throw("MDataTable's tablename can't  null or empty");
            }
            mdt = sourceTable = mTable;

            if (mdt.TableName.IndexOfAny(new char[] { '(', ')' }) > -1)
            {
                mdt.TableName = mdt.TableName.Substring(mdt.TableName.LastIndexOf(')') + 1).Trim();
            }
            if (!string.IsNullOrEmpty(conn))
            {
                _Conn = conn;
            }
            else
            {
                if (mTable.DynamicData != null && mTable.DynamicData is MAction)//尝试多动态中获取链接
                {
                    _Conn = ((MAction)mTable.DynamicData).ConnString;
                }
                else if (mTable.DynamicData != null && mTable.DynamicData is MProc)
                {
                    _Conn = ((MProc)mTable.DynamicData).ConnString;
                }
                else
                {
                    _Conn = mTable.Conn;
                }
            }

            if (!DBTool.Exists(mdt.TableName, "U", _Conn))
            {
                DBTool.ErrorMsg = null;
                if (!DBTool.CreateTable(mdt.TableName, mdt.Columns, _Conn))
                {
                    Error.Throw("Create Table Error:" + mdt.TableName + DBTool.ErrorMsg);
                }
            }
            MDataColumn column = DBTool.GetColumns(mdt.TableName, _Conn);

            FixTable(column);//
            if (mdt.Columns.Count == 0)
            {
                Error.Throw("After fix table columns, length can't be zero");
            }
            dalTypeTo = column.DataBaseType;
            SetDalBaseForTransaction();
        }
Beispiel #3
0
        public MDataColumn Clone()
        {
            MDataColumn mcs = new MDataColumn();

            mcs.dalType        = dalType;
            mcs.CheckDuplicate = false;
            mcs.isViewOwner    = isViewOwner;
            foreach (string item in relationTables)
            {
                mcs.AddRelateionTableName(item);
            }
            for (int i = 0; i < base.Count; i++)
            {
                mcs.Add(base[i].Clone());
            }
            return(mcs);
        }
 /// <summary>
 /// 进行列修正(只有移除 和 修正类型,若无主键列,则新增主键列)
 /// </summary>
 private void FixTable(MDataColumn column)
 {
     if (column.Count > 0)
     {
         bool tableIsChange = false;
         for (int i = mdt.Columns.Count - 1; i >= 0; i--)
         {
             if (!column.Contains(mdt.Columns[i].ColumnName))//没有此列
             {
                 if (!tableIsChange)
                 {
                     mdt           = mdt.Clone();//列需要变化时,克隆一份,不变更原有数据。
                     tableIsChange = true;
                 }
                 mdt.Columns.RemoveAt(i);
             }
             else
             {
                 MCellStruct ms           = column[mdt.Columns[i].ColumnName]; //新表的字段
                 Type        valueType    = mdt.Columns[i].ValueType;          //存档的字段的值的原始类型。
                 bool        isChangeType = mdt.Columns[i].SqlType != ms.SqlType;
                 mdt.Columns[i].Load(ms);
                 if (isChangeType)
                 {
                     //修正数据的数据类型。
                     foreach (MDataRow row in mdt.Rows)
                     {
                         row[i].FixValue();//重新自我赋值修正数据类型。
                     }
                 }
             }
         }
         //主键检测,若没有,则补充主键
         if (column.JointPrimary != null && column.JointPrimary.Count > 0)
         {
             if (!mdt.Columns.Contains(column[0].ColumnName) && (column[0].IsPrimaryKey || column[0].IsAutoIncrement))
             {
                 MCellStruct ms = column[0].Clone();
                 mdt            = mdt.Clone();//列需要变化时,克隆一份,不变更原有数据。
                 ms.MDataColumn = null;
                 mdt.Columns.Insert(0, ms);
             }
         }
     }
 }
Beispiel #5
0
        /// <summary>
        /// 单个条件
        /// </summary>
        private static TFilter GetSingleTFilter(string where, MDataColumn mdc)
        {
            //id like 'a>b=c'
            //id>'a like b'
            where = where.TrimStart('(').TrimEnd(')').Trim();
            int quoteIndex = where.IndexOf('\'');

            quoteIndex = quoteIndex == -1 ? 0 : quoteIndex;
            TFilter tFilter = null;

            foreach (KeyValuePair <string, Op> opItem in Ops)
            {
                if (GetTFilterOk(where, quoteIndex, opItem.Key, opItem.Value, mdc, out tFilter))
                {
                    break;
                }
            }
            return(tFilter);
        }
Beispiel #6
0
        public MDataColumn Clone()
        {
            MDataColumn mcs = new MDataColumn();

            mcs.DataBaseType   = DataBaseType;
            mcs.CheckDuplicate = false;
            mcs.isViewOwner    = isViewOwner;
            mcs.TableName      = TableName;
            mcs.Description    = Description;
            foreach (string item in relationTables)
            {
                mcs.AddRelateionTableName(item);
            }
            for (int i = 0; i < this.Count; i++)
            {
                mcs.Add(this[i].Clone());
            }
            return(mcs);
        }
Beispiel #7
0
 public MDataRow(MDataColumn mdc)
 {
     Table.Columns.AddRange(mdc);
     base.Capacity = mdc.Count;
 }
Beispiel #8
0
        /// <summary>
        /// 条件比较
        /// </summary>
        private static bool GetTFilterOk(string where, int quoteIndex, string sign, Op op, MDataColumn mdc, out TFilter tFilter)
        {
            bool result = false;

            tFilter = null;
            int index = where.ToLower().IndexOf(sign, 0, quoteIndex > 0 ? quoteIndex : where.Length);

            if (index > 0)
            {
                string columnAName  = where.Substring(0, index).Trim();
                int    columnAIndex = mdc.GetIndex(columnAName);
                string valueB       = where.Substring(index + sign.Length).Trim(' ', '\'').Replace("''", "'");
                if (op == Op.In || op == Op.NotIn)
                {
                    valueB = ',' + valueB.TrimStart('(', ')').Replace("'", "") + ",";//去除单引号。
                }
                int columnBIndex = -1;
                if (quoteIndex == 0 && mdc.Contains(valueB)) //判断右侧的是否列名。
                {
                    columnBIndex = mdc.GetIndex(valueB);
                }
                tFilter = new TFilter(Ao.None, columnAName, columnAIndex, op, valueB, columnBIndex);
                if (columnBIndex == -1 && !string.IsNullOrEmpty(Convert.ToString(valueB)))      //右侧是值类型,转换值的类型。
                {
                    if (columnAIndex > -1 && DataType.GetGroup(mdc[columnAIndex].SqlType) == 3) //bool型
                    {
                        switch (Convert.ToString(tFilter._valueB).ToLower())
                        {
                        case "true":
                        case "1":
                        case "on":
                            tFilter._valueB = true;
                            break;

                        case "false":
                        case "0":
                        case "":
                            tFilter._valueB = false;
                            break;

                        default:
                            tFilter._valueB = null;
                            break;
                        }
                    }
                    else
                    {
                        try
                        {
                            tFilter._valueB = ConvertTool.ChangeType(tFilter._valueB, columnAIndex > -1 ? typeof(string) : mdc[columnAIndex].ValueType);
                        }
                        catch
                        {
                        }
                    }
                }
                result = true;
            }
            return(result);
        }
Beispiel #9
0
        /// <summary>
        /// 多个条件
        /// </summary>
        private static List <TFilter> GetTFilter(object whereObj, MDataColumn mdc, out List <TFilter> group2)
        {
            group2 = new List <TFilter>();
            List <TFilter> tFilterList = new List <TFilter>();
            string         whereStr    = SqlFormat.GetIFieldSql(whereObj);

            whereStr = SqlCreate.FormatWhere(whereStr, mdc, DataBaseType.None, null);
            string lowerWhere = whereStr.ToLower();
            string andSign    = " and ";
            string orSign     = " or ";
            int    andIndex   = IndexOf(lowerWhere, andSign, 0);// lowerWhere.IndexOf(andSign);
            int    orIndex    = IndexOf(lowerWhere, orSign, 0);

            TFilter filter = null;

            if (andIndex == -1 && orIndex == -1)//仅有一个条件
            {
                filter = GetSingleTFilter(whereStr, mdc);
                if (filter != null)
                {
                    tFilterList.Add(filter);
                }
            }
            else if (orIndex == -1) // 只有and条件
            {
                int andStartIndex = 0;
                while (andIndex > -1)
                {
                    filter = GetSingleTFilter(whereStr.Substring(andStartIndex, andIndex - andStartIndex), mdc);
                    if (filter != null)
                    {
                        if (andStartIndex > 0)
                        {
                            filter._Ao = Ao.And;
                        }
                        tFilterList.Add(filter);
                    }
                    andStartIndex = andIndex + andSign.Length;
                    andIndex      = IndexOf(lowerWhere, andSign, andStartIndex + 1);
                }
                filter = GetSingleTFilter(whereStr.Substring(andStartIndex), mdc);
                if (filter != null)
                {
                    filter._Ao = Ao.And;
                    tFilterList.Add(filter);
                }
            }
            else if (andIndex == -1) //只有or 条件
            {
                int orStartIndex = 0;
                while (orIndex > -1)
                {
                    filter = GetSingleTFilter(whereStr.Substring(orStartIndex, orIndex - orStartIndex), mdc);
                    if (filter != null)
                    {
                        if (orStartIndex > 0)
                        {
                            filter._Ao = Ao.Or;
                        }
                        tFilterList.Add(filter);
                    }
                    orStartIndex = orIndex + orSign.Length;
                    orIndex      = IndexOf(lowerWhere, orSign, orStartIndex + 1);
                }
                filter = GetSingleTFilter(whereStr.Substring(orStartIndex), mdc);
                if (filter != null)
                {
                    filter._Ao = Ao.Or;
                    tFilterList.Add(filter);
                }
            }
            else //有and 又有 or
            {
                bool isAnd      = andIndex < orIndex;
                bool lastAnd    = isAnd;
                int  andOrIndex = isAnd ? andIndex : orIndex;//最小的,前面的先处理

                int  andOrStartIndex = 0;
                bool needGroup2      = isAnd;//如果是and开头,则分成两组
                while (andOrIndex > -1)
                {
                    filter = GetSingleTFilter(whereStr.Substring(andOrStartIndex, andOrIndex - andOrStartIndex), mdc);
                    if (filter != null)
                    {
                        if (andOrStartIndex > 0)
                        {
                            filter._Ao = lastAnd ? Ao.And : Ao.Or;
                        }
                        tFilterList.Add(filter);
                    }
                    andOrStartIndex = andOrIndex + (isAnd ? andSign.Length : orSign.Length);
                    if (isAnd)
                    {
                        andIndex = IndexOf(lowerWhere, andSign, andOrStartIndex + 1);
                    }
                    else
                    {
                        orIndex = IndexOf(lowerWhere, orSign, andOrStartIndex + 1);
                    }
                    lastAnd = isAnd;
                    if (andIndex == -1)
                    {
                        isAnd      = false;
                        andOrIndex = orIndex;
                    }
                    else if (orIndex == -1)
                    {
                        isAnd      = true;
                        andOrIndex = andIndex;
                    }
                    else
                    {
                        isAnd      = andIndex < orIndex;
                        andOrIndex = isAnd ? andIndex : orIndex;//最小的,前面的先处理
                    }
                }
                filter = GetSingleTFilter(whereStr.Substring(andOrStartIndex), mdc);
                if (filter != null)
                {
                    filter._Ao = lastAnd ? Ao.And : Ao.Or;
                    tFilterList.Add(filter);
                }
                if (tFilterList.Count > 2 && needGroup2)
                {
                    int okflag = -1;
                    for (int i = 0; i < tFilterList.Count; i++)
                    {
                        if (okflag == -1 && tFilterList[i]._Ao == Ao.Or)
                        {
                            i--;//返回上一个索引1,2,3
                            okflag = i;
                        }
                        if (okflag != -1)
                        {
                            group2.Add(tFilterList[i]);
                        }
                    }
                    tFilterList.RemoveRange(okflag, tFilterList.Count - okflag);
                }
            }
            // string firstFilter=whereStr.su


            return(tFilterList);
        }
Beispiel #10
0
        /// <summary>
        /// 多个条件
        /// </summary>
        private static TFilter[] GetTFilter(object whereObj, MDataColumn mdc)
        {
            List <TFilter> tFilterList = new List <TFilter>();
            string         whereStr    = SqlFormat.GetIFieldSql(whereObj);

            whereStr = SqlCreate.FormatWhere(whereStr, mdc, DalType.None, null);
            string lowerWhere = whereStr.ToLower();
            string andSign    = " and ";
            string orSign     = " or ";
            int    andIndex   = IndexOf(lowerWhere, andSign, 0);// lowerWhere.IndexOf(andSign);
            int    orIndex    = IndexOf(lowerWhere, orSign, 0);

            TFilter filter = null;

            if (andIndex == -1 && orIndex == -1)//仅有一个条件
            {
                filter = GetSingleTFilter(whereStr, mdc);
                if (filter != null)
                {
                    tFilterList.Add(filter);
                }
            }
            else if (orIndex == -1) // 只有and条件
            {
                int andStartIndex = 0;
                while (andIndex > -1)
                {
                    filter = GetSingleTFilter(whereStr.Substring(andStartIndex, andIndex - andStartIndex), mdc);
                    if (filter != null)
                    {
                        if (andStartIndex > 0)
                        {
                            filter._Ao = Ao.And;
                        }
                        tFilterList.Add(filter);
                    }
                    andStartIndex = andIndex + andSign.Length;
                    andIndex      = IndexOf(lowerWhere, andSign, andStartIndex + 1);
                }
                filter = GetSingleTFilter(whereStr.Substring(andStartIndex), mdc);
                if (filter != null)
                {
                    filter._Ao = Ao.And;
                    tFilterList.Add(filter);
                }
            }
            else if (andIndex == -1) //只有or 条件
            {
                int orStartIndex = 0;
                while (orIndex > -1)
                {
                    filter = GetSingleTFilter(whereStr.Substring(orStartIndex, orIndex - orStartIndex), mdc);
                    if (filter != null)
                    {
                        if (orStartIndex > 0)
                        {
                            filter._Ao = Ao.Or;
                        }
                        tFilterList.Add(filter);
                    }
                    orStartIndex = orIndex + orSign.Length;
                    orIndex      = IndexOf(lowerWhere, orSign, orStartIndex + 1);
                }
                filter = GetSingleTFilter(whereStr.Substring(orStartIndex), mdc);
                if (filter != null)
                {
                    filter._Ao = Ao.Or;
                    tFilterList.Add(filter);
                }
            }
            else //有and 又有 or
            {
                bool isAnd      = andIndex < orIndex;
                bool lastAnd    = isAnd;
                int  andOrIndex = isAnd ? andIndex : orIndex;//最小的,前面的先处理

                int andOrStartIndex = 0;

                while (andOrIndex > -1)
                {
                    filter = GetSingleTFilter(whereStr.Substring(andOrStartIndex, andOrIndex - andOrStartIndex), mdc);
                    if (filter != null)
                    {
                        if (andOrStartIndex > 0)
                        {
                            filter._Ao = lastAnd ? Ao.And : Ao.Or;
                        }
                        tFilterList.Add(filter);
                    }
                    andOrStartIndex = andOrIndex + (isAnd ? andSign.Length : orSign.Length);
                    if (isAnd)
                    {
                        andIndex = IndexOf(lowerWhere, andSign, andOrStartIndex + 1);
                    }
                    else
                    {
                        orIndex = IndexOf(lowerWhere, orSign, andOrStartIndex + 1);
                    }
                    lastAnd = isAnd;
                    if (andIndex == -1)
                    {
                        isAnd      = false;
                        andOrIndex = orIndex;
                    }
                    else if (orIndex == -1)
                    {
                        isAnd      = true;
                        andOrIndex = andIndex;
                    }
                    else
                    {
                        isAnd      = andIndex < orIndex;
                        andOrIndex = isAnd ? andIndex : orIndex;//最小的,前面的先处理
                    }
                }
                filter = GetSingleTFilter(whereStr.Substring(andOrStartIndex), mdc);
                if (filter != null)
                {
                    filter._Ao = lastAnd ? Ao.And : Ao.Or;
                    tFilterList.Add(filter);
                }
            }
            // string firstFilter=whereStr.su


            return(tFilterList.ToArray());
        }
Beispiel #11
0
 public MDataRow(MDataColumn mdc)
 {
     CellList = new List <MDataCell>(mdc.Count);
     Table.Columns.AddRange(mdc);
 }