Ejemplo n.º 1
0
        /// <summary>
        /// Finalizes a nested transaction.
        /// </summary>
        /// <param name="rollback"></param>
        internal void EndNestedTransaction(bool rollback)
        {
            TracingHelper.Check(!_closed, TracingHelper.CONNECTION_IS_CLOSED);

            TracingHelper.Assert(_nestedTransaction, "EndNestedTransaction");

            int i = _transaction.Count - 1;

            if (rollback)
            {
                while (i >= _nestedOldTransIndex)
                {
                    Transaction t = (Transaction)_transaction[i];

                    t.Rollback();

                    i--;
                }
            }

            _nestedTransaction = false;
            _autoCommit        = _nestedOldAutoCommit;

            if (_autoCommit == true)
            {
                _transaction.RemoveRange(_nestedOldTransIndex, (_transaction.Count - _nestedOldTransIndex));
            }
        }
Ejemplo n.º 2
0
        public bool CanRemove()
        {
            Node n = nFirstIndex;

            while (n != null)
            {
                if (TracingHelper.AssertEnabled)
                {
                    TracingHelper.Assert(n.iBalance != -2);
                }

                if (TracingHelper.StopEnabled)
                {
                    TracingHelper.Stop();
                }

                if (n.iParent == 0 && n.nParent == null)
                {
                    return(true);
                }

                n = n.nNext;
            }

            return(false);
        }
Ejemplo n.º 3
0
        public void CreatePrimaryKey()
        {
            TracingHelper.Assert(iPrimaryKey == -1, "Table.createPrimaryKey");
            AddColumn("SYSTEM_ID", ColumnType.Integer, true, true);
            CreatePrimaryKey(iColumnCount - 1);

            iVisibleColumns = iColumnCount - 1;
        }
Ejemplo n.º 4
0
        public object[] GetData()
        {
            if (TracingHelper.AssertEnabled)
            {
                TracingHelper.Assert(iBalance != -2);
            }

            return(rData.GetData());
        }
Ejemplo n.º 5
0
        public int GetBalance()
        {
            if (TracingHelper.AssertEnabled)
            {
                TracingHelper.Assert(iBalance != -2);

                // rData.iLastAccess=Row.iCurrentAccess++;
            }

            return(iBalance);
        }
Ejemplo n.º 6
0
        public void CreatePrimaryKey(int column)
        {
            TracingHelper.Assert(iPrimaryKey == -1, "Table.createPrimaryKey(column)");

            iVisibleColumns = iColumnCount;
            iPrimaryKey     = column;

            int[] col = new int[1];
            col[0] = column;

            CreateIndex(col, "SYSTEM_PK", true);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Begins a new nested transaction.
        /// </summary>
        internal void BeginNestedTransaction()
        {
            TracingHelper.Check(!_closed, TracingHelper.CONNECTION_IS_CLOSED);

            TracingHelper.Assert(!_nestedTransaction, "beginNestedTransaction");

            _nestedOldAutoCommit = _autoCommit;

            // now all transactions are logged
            _autoCommit          = false;
            _nestedOldTransIndex = _transaction.Count;
            _nestedTransaction   = true;
        }
Ejemplo n.º 8
0
        public void SetBalance(int b)
        {
            if (TracingHelper.AssertEnabled)
            {
                TracingHelper.Assert(iBalance != -2);
            }

            if (iBalance != b)
            {
                rData.RowChanged();

                iBalance = b;
            }
        }
Ejemplo n.º 9
0
        private int iId;                     // id of index this table

        public Node(Row r, BinaryReader din, int id)
        {
            iId      = id;
            rData    = r;
            iBalance = din.ReadInt32();
            iLeft    = din.ReadInt32();
            iRight   = din.ReadInt32();
            iParent  = din.ReadInt32();

            if (TracingHelper.AssertEnabled)
            {
                TracingHelper.Assert(iBalance != -2);
            }
        }
Ejemplo n.º 10
0
        private bool From(Node x)
        {
            if (x.Equals(_root))
            {
                return(true);
            }

            if (TracingHelper.AssertEnabled)
            {
                TracingHelper.Assert(x.GetParent() != null);
            }

            return(x.Equals(x.GetParent().GetLeft()));
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Remove method declaration.
        /// This method is used to remove Rows from the Cache. It is called
        /// by the CleanUp method.
        /// </summary>
        /// <param name="row">Row to be removed</param>
        private void Remove(Row row)
        {
            if (TracingHelper.AssertEnabled)
            {
                TracingHelper.Assert(!row.Changed);
            }

            // make sure _rowLastChecked does not point to r
            if (row == _rowLastChecked)
            {
                _rowLastChecked = _rowLastChecked.Next;

                if (_rowLastChecked == row)
                {
                    _rowLastChecked = null;
                }
            }

            // make sure _rowData[k] does not point here
            int k = row.Pos & MASK;

            if (_rowData[k] == row)
            {
                Row n = row.Next;

                _rowFirst = n;

                if (n == row || (n.Pos & MASK) != k)
                {
                    n = null;
                }

                _rowData[k] = n;
            }

            // make sure _rowFirst does not point here
            if (row == _rowFirst)
            {
                _rowFirst = _rowFirst.Next;

                if (row == _rowFirst)
                {
                    _rowFirst = null;
                }
            }

            row.Free();

            _cacheSize--;
        }
Ejemplo n.º 12
0
        public Node GetLeft()
        {
            if (TracingHelper.AssertEnabled)
            {
                TracingHelper.Assert(iBalance != -2);
            }

            if (iLeft == 0)
            {
                return(nLeft);
            }

            // rData.iLastAccess=Row.iCurrentAccess++;
            return(rData.GetNode(iLeft, iId));
        }
Ejemplo n.º 13
0
 /// <summary>
 /// Replaces the main or reference table with a new one.
 /// </summary>
 /// <param name="oldTable"></param>
 /// <param name="newTable"></param>
 internal void ReplaceTable(Table oldTable, Table newTable)
 {
     if (oldTable == _mainTable)
     {
         _mainTable = newTable;
     }
     else if (oldTable == _refTable)
     {
         _refTable = newTable;
     }
     else
     {
         TracingHelper.Assert(false, "could not replace");
     }
 }
Ejemplo n.º 14
0
        public void Write(BinaryWriter writer)
        {
            if (TracingHelper.AssertEnabled)
            {
                TracingHelper.Assert(iBalance != -2);
            }

            writer.Write(iBalance);
            writer.Write(iLeft);
            writer.Write(iRight);
            writer.Write(iParent);

            if (nNext != null)
            {
                nNext.Write(writer);
            }
        }
Ejemplo n.º 15
0
        public void CreateIndex(int[] column, string name, bool unique)
        {
            TracingHelper.Assert(iPrimaryKey != -1, "createIndex");

            for (int i = 0; i < iIndexCount; i++)
            {
                Index index = GetIndex(i);

                if (name.Equals(index.Name))
                {
                    throw TracingHelper.Error(TracingHelper.INDEX_ALREADY_EXISTS);
                }
            }

            int s = column.Length;

            // The primary key field is added for non-unique indexes
            // making all indexes unique
            int[]        col  = new int[unique ? s : s + 1];
            ColumnType[] type = new ColumnType[unique ? s : s + 1];

            for (int j = 0; j < s; j++)
            {
                col[j]  = column[j];
                type[j] = GetColumn(col[j]).ColumnType;
            }

            if (!unique)
            {
                col[s]  = iPrimaryKey;
                type[s] = GetColumn(iPrimaryKey).ColumnType;
            }

            Index newindex = new Index(name, col, type, unique);

            if (iIndexCount != 0)
            {
                TracingHelper.Assert(IsEmpty, "createIndex");
            }

            vIndex.Add(newindex);

            iIndexCount++;
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Constructor using Main and Reference Table.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="main"></param>
        /// <param name="child"></param>
        /// <param name="columnMain"></param>
        /// <param name="columnRef"></param>
        public Constraint(ConstraintType type, Table main, Table child, int[] columnMain, int[] columnRef)
        {
            _type        = type;
            _mainTable   = main;
            _refTable    = child;
            _mainColumns = columnMain;
            _refColumns  = columnRef;
            _len         = columnMain.Length;

            if (TracingHelper.AssertEnabled)
            {
                TracingHelper.Assert(columnMain.Length == columnRef.Length);
            }

            _mainData  = _mainTable.NewRow;
            _refData   = _refTable.NewRow;
            _mainIndex = _mainTable.GetIndexForColumns(columnMain);
            _refIndex  = _refTable.GetIndexForColumns(columnRef);
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Swaps the conditions for this expression.
        /// </summary>
        public void SwapCondition()
        {
            ExpressionType i = ExpressionType.Equal;

            switch (_type)
            {
            case ExpressionType.BiggerEqual:
                i = ExpressionType.SmallerEqual;

                break;

            case ExpressionType.SmallerEqual:
                i = ExpressionType.BiggerEqual;

                break;

            case ExpressionType.Smaller:
                i = ExpressionType.Bigger;

                break;

            case ExpressionType.Bigger:
                i = ExpressionType.Smaller;

                break;

            case ExpressionType.Equal:
                break;

            default:
                TracingHelper.Assert(false, "Expression.swapCondition");
                break;
            }

            _type = i;

            Expression e = eArg;

            eArg  = eArg2;
            eArg2 = e;
        }
Ejemplo n.º 18
0
        public void AddColumn(string name, ColumnType type, bool nullable, bool identity)
        {
            if (identity)
            {
                TracingHelper.Check(type == ColumnType.Integer, TracingHelper.WRONG_DATA_TYPE, name);
                TracingHelper.Check(iIdentityColumn == -1, TracingHelper.SECOND_PRIMARY_KEY,
                                    name);

                iIdentityColumn = iColumnCount;
            }

            if (type == ColumnType.Timestamp)
            {
                iTimestampColumn = iColumnCount;
            }

            TracingHelper.Assert(iPrimaryKey == -1, "Table.addColumn");
            vColumn.Add(new Column(name, nullable, type, identity));

            iColumnCount++;
        }
Ejemplo n.º 19
0
        public void SetParent(Node n)
        {
            if (TracingHelper.AssertEnabled)
            {
                TracingHelper.Assert(iBalance != -2);
            }

            rData.RowChanged();

            if (n == null)
            {
                iParent = 0;
                nParent = null;
            }
            else if (n.rData.Pos != 0)
            {
                iParent = n.rData.Pos;
            }
            else
            {
                nParent = n;
            }
        }
Ejemplo n.º 20
0
        public bool Equals(Node node)
        {
            if (TracingHelper.AssertEnabled)
            {
                TracingHelper.Assert(iBalance != -2);

                // rData.iLastAccess=Row.iCurrentAccess++;
            }

            if (TracingHelper.AssertEnabled)
            {
                if (node != this)
                {
                    TracingHelper.Assert(rData.Pos == 0 || node == null ||
                                         node.rData.Pos != rData.Pos);
                }
                else
                {
                    TracingHelper.Assert(node.rData.Pos == rData.Pos);
                }
            }

            return(node == this);
        }
Ejemplo n.º 21
0
        /// <summary>
        /// Executes an SQL statement and return the results.
        /// </summary>
        /// <param name="statement">The SQL statement to execute.</param>
        /// <param name="channel">The channel to use.</param>
        /// <returns>The Result object.</returns>
        public Result Execute(string statement, Channel channel)
        {
            if (TracingHelper.TraceEnabled)
            {
                TracingHelper.Write(statement);
            }

            Tokenizer c            = new Tokenizer(statement);
            Parser    p            = new Parser(this, c, channel);
            Result    rResult      = new Result();
            string    newStatement = string.Empty;
            int       updateCount  = 0;

            try
            {
                if (_log != null && _log.cCache != null)
                {
                    _log.cCache.CleanUp();
                }

                if (TracingHelper.AssertEnabled)
                {
                    TracingHelper.Assert(!channel.IsNestedTransaction);
                }

                TracingHelper.Check(channel != null, TracingHelper.ACCESS_IS_DENIED);
                TracingHelper.Check(!_shutDown, TracingHelper.DATABASE_IS_SHUTDOWN);

                while (true)
                {
                    int    begin  = c.Position;
                    bool   script = false;
                    string sToken = c.GetString();

                    if (sToken.Equals(""))
                    {
                        break;
                    }

                    switch (sToken)
                    {
                    case "SELECT":
                        rResult = p.ProcessSelect();
                        break;

                    case "INSERT":
                        rResult = p.ProcessInsert();
                        break;

                    case "UPDATE":
                        rResult = p.ProcessUpdate();
                        break;

                    case "DELETE":
                        rResult = p.ProcessDelete();
                        break;

                    case "ALTER":
                        rResult = p.ProcessAlter();
                        break;

                    case "CREATE":
                        rResult = ProcessCreate(c, channel);
                        script  = true;
                        break;

                    case "DROP":
                        rResult = ProcessDrop(c, channel);
                        script  = true;
                        break;

                    case "GRANT":
                        rResult = ProcessGrantOrRevoke(c, channel, true);
                        script  = true;
                        break;

                    case "REVOKE":
                        rResult = ProcessGrantOrRevoke(c, channel, false);
                        script  = true;
                        break;

                    case "CONNECT":
                        rResult = ProcessConnect(c, channel);
                        break;

                    case "DISCONNECT":
                        rResult = ProcessDisconnect(c, channel);
                        break;

                    case "SET":
                        rResult = ProcessSet(c, channel);
                        script  = true;
                        break;

                    case "SCRIPT":
                        rResult = ProcessScript(c, channel);
                        break;

                    case "COMMIT":
                        rResult = ProcessCommit(c, channel);
                        script  = true;
                        break;

                    case "ROLLBACK":
                        rResult = ProcessRollback(c, channel);
                        script  = true;
                        break;

                    case "SHUTDOWN":
                        rResult = ProcessShutdown(c, channel);
                        break;

                    case "CHECKPOINT":
                        rResult = ProcessCheckpoint(channel);
                        break;

                    case "CALL":
                        rResult = p.ProcessCall();
                        break;

                    case "SHOW":
                        rResult = ProcessShow(c, channel);
                        break;

                    case "DECLARE":
                        rResult = p.ProcessDeclare();
                        script  = true;
                        break;

                    case ";":
                        continue;

                    default:
                        throw TracingHelper.Error(TracingHelper.UnexpectedToken, sToken);
                    }

                    if (rResult != null && rResult.UpdateCount > updateCount)
                    {
                        updateCount = rResult.UpdateCount;
                    }

                    if (script && _log != null)
                    {
                        int end = c.Position;

                        _log.Write(channel, c.GetPart(begin, end));
                    }
                }
            }
            catch (Exception e)
            {
                rResult = new Result(TracingHelper.GetMessage(e) + " in statement [" + statement + "]");
            }

            if (rResult != null && rResult.UpdateCount < updateCount)
            {
                rResult.SetUpdateCount(updateCount);
            }

            return(rResult);
        }
Ejemplo n.º 22
0
        /// <summary>
        /// Test condition for True or False value.
        /// </summary>
        /// <returns></returns>
        public bool Test()
        {
            switch (_type)
            {
            case ExpressionType.True:
                return(true);

            case ExpressionType.Not:
                TracingHelper.Assert(eArg2 == null, "Expression.test");

                return(!eArg.Test());

            case ExpressionType.And:
                return(eArg.Test() && eArg2.Test());

            case ExpressionType.Or:
                return(eArg.Test() || eArg2.Test());

            case ExpressionType.Like:

                // todo: now for all tests a new 'like' object required!
                string     s    = (string)eArg2.GetValue(ColumnType.VarChar);
                ColumnType type = eArg._columnType;
                Like       l    = new Like(s, cLikeEscape, type == ColumnType.VarCharIgnoreCase);
                string     c    = (string)eArg.GetValue(ColumnType.VarChar);

                return(l.Compare(c));

            case ExpressionType.In:
                return(eArg2.TestValueList(eArg.GetValue(), eArg._columnType));

            case ExpressionType.Exists:
                Result r = eArg.sSelect.GetResult(1, null);                            // 1 is already enough

                return(r.Root != null);
            }

            TracingHelper.Check(eArg != null, TracingHelper.GENERAL_ERROR);

            object     o     = eArg.GetValue();
            ColumnType dtype = eArg._columnType;

            TracingHelper.Check(eArg2 != null, TracingHelper.GENERAL_ERROR);

            object o2     = eArg2.GetValue(dtype);
            int    result = Column.Compare(o, o2, dtype);

            switch (_type)
            {
            case ExpressionType.Equal:
                return(result == 0);

            case ExpressionType.Bigger:
                return(result > 0);

            case ExpressionType.BiggerEqual:
                return(result >= 0);

            case ExpressionType.SmallerEqual:
                return(result <= 0);

            case ExpressionType.Smaller:
                return(result < 0);

            case ExpressionType.NotEqual:
                return(result != 0);
            }

            TracingHelper.Assert(false, "Expression.test2");

            return(false);
        }
Ejemplo n.º 23
0
        public void SetCondition(Expression e)
        {
            ExpressionType type = e.Type;
            Expression     e1   = e.Arg;
            Expression     e2   = e.Arg2;

            if (type == ExpressionType.And)
            {
                SetCondition(e1);
                SetCondition(e2);

                return;
            }

            int candidate;

            switch (type)
            {
            case ExpressionType.NotEqual:

            case ExpressionType.Like:                        // todo: maybe use index

            case ExpressionType.In:
                candidate = 0;

                break;

            case ExpressionType.Equal:
                candidate = 1;

                break;

            case ExpressionType.Bigger:

            case ExpressionType.BiggerEqual:
                candidate = 2;

                break;

            case ExpressionType.Smaller:

            case ExpressionType.SmallerEqual:
                candidate = 3;

                break;

            default:

                // not a condition so forget it
                return;
            }

            if (e1.Filter == this)
            {
                // ok include this
            }
            else if (e2.Filter == this && candidate != 0)
            {
                // swap and try again to allow index usage
                e.SwapCondition();
                SetCondition(e);

                return;
            }
            else
            {
                // unrelated: don't include
                return;
            }

            TracingHelper.Assert(e1.Filter == this, "setCondition");

            if (!e2.IsResolved)
            {
                return;
            }

            if (candidate == 0)
            {
                AddAndCondition(e);

                return;
            }

            int   i     = e1.ColumnNumber;
            Index index = tTable.GetIndexForColumn(i);

            if (index == null || (iIndex != index && iIndex != null))
            {
                // no index or already another index is used
                AddAndCondition(e);

                return;
            }

            iIndex = index;

            if (candidate == 1)
            {
                // candidate for both start & end
                if (eStart != null || eEnd != null)
                {
                    AddAndCondition(e);

                    return;
                }

                eStart = new Expression(e);
                eEnd   = eStart;
            }
            else if (candidate == 2)
            {
                // candidate for start
                if (eStart != null)
                {
                    AddAndCondition(e);

                    return;
                }

                eStart = new Expression(e);
            }
            else if (candidate == 3)
            {
                // candidate for end
                if (eEnd != null)
                {
                    AddAndCondition(e);

                    return;
                }

                eEnd = new Expression(e);
            }

            e.SetTrue();
        }
Ejemplo n.º 24
0
        public void Back()
        {
            TracingHelper.Assert(!bWait, "back");

            bWait = true;
        }
Ejemplo n.º 25
0
        public Node FindFirst(object value, ExpressionType compare)
        {
            TracingHelper.Assert(compare == ExpressionType.Bigger ||
                                 compare == ExpressionType.Equal ||
                                 compare == ExpressionType.BiggerEqual,
                                 "Index.findFirst");

            Node x     = _root;
            int  iTest = 1;

            if (compare == ExpressionType.Bigger)
            {
                iTest = 0;
            }

            while (x != null)
            {
                if (TracingHelper.StopEnabled)
                {
                    TracingHelper.Stop();
                }

                bool t = CompareValue(value, x.GetData()[_column_0]) >= iTest;

                if (t)
                {
                    Node r = x.GetRight();

                    if (r == null)
                    {
                        break;
                    }

                    x = r;
                }
                else
                {
                    Node l = x.GetLeft();

                    if (l == null)
                    {
                        break;
                    }

                    x = l;
                }
            }

            while (x != null &&
                   CompareValue(value, x.GetData()[_column_0]) >= iTest)
            {
                if (TracingHelper.StopEnabled)
                {
                    TracingHelper.Stop();
                }

                x = Next(x);
            }

            return(x);
        }