Example #1
0
        private Node Search(object[] d)
        {
            Node x = _root;

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

                int c = CompareRow(d, x.GetData());

                if (c == 0)
                {
                    return(x);
                }
                else if (c < 0)
                {
                    x = x.GetLeft();
                }
                else
                {
                    x = x.GetRight();
                }
            }

            return(null);
        }
Example #2
0
        public bool Next()
        {
            if (bOuterJoin && nCurrent == null)
            {
                return(false);
            }

            nCurrent = iIndex.Next(nCurrent);

            while (nCurrent != null)
            {
                oCurrentData = nCurrent.GetData();

                if (!Test(eEnd))
                {
                    break;
                }

                if (Test(eAnd))
                {
                    return(true);
                }

                nCurrent = iIndex.Next(nCurrent);
            }

            oCurrentData = oEmptyData;

            return(false);
        }
Example #3
0
        public void MoveData(Table from)
        {
            Index index = from.PrimaryIndex;
            Node  n     = index.First();

            while (n != null)
            {
                if (Trace.StopEnabled)
                {
                    Trace.Stop();
                }

                object[] o = n.GetData();

                InsertNoCheck(o, null);

                n = index.Next(n);
            }

            index = PrimaryIndex;
            n     = index.First();

            while (n != null)
            {
                if (Trace.StopEnabled)
                {
                    Trace.Stop();
                }

                object[] o = n.GetData();

                from.DeleteNoCheck(o, null);

                n = index.Next(n);
            }
        }
Example #4
0
        public bool FindFirst()
        {
            if (iIndex == null)
            {
                iIndex = tTable.PrimaryIndex;
            }

            if (eStart == null)
            {
                nCurrent = iIndex.First();
            }
            else
            {
                ColumnType type = eStart.Arg.ColumnType;
                object     o    = eStart.Arg2.GetValue(type);

                nCurrent = iIndex.FindFirst(o, eStart.Type);
            }

            while (nCurrent != null)
            {
                oCurrentData = nCurrent.GetData();

                if (!Test(eEnd))
                {
                    break;
                }

                if (Test(eAnd))
                {
                    return(true);
                }

                nCurrent = iIndex.Next(nCurrent);
            }

            oCurrentData = oEmptyData;

            if (bOuterJoin)
            {
                return(true);
            }

            return(false);
        }
Example #5
0
        public Node Find(object[] data)
        {
            Node x = _root, n;

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

                int i = CompareRowNonUnique(data, x.GetData());

                if (i == 0)
                {
                    return(x);
                }
                else if (i > 0)
                {
                    n = x.GetRight();
                }
                else
                {
                    n = x.GetLeft();
                }

                if (n == null)
                {
                    return(null);
                }

                x = n;
            }

            return(null);
        }
Example #6
0
        public Node FindFirst(object value, ExpressionType compare)
        {
            Trace.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 (Trace.StopEnabled)
                {
                    Trace.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 (Trace.StopEnabled)
                {
                    Trace.Stop();
                }

                x = Next(x);
            }

            return(x);
        }
Example #7
0
        public void Insert(Node i)
        {
            object[] data = i.GetData();
            Node     n = _root, x = n;
            bool     way     = true;
            int      compare = -1;

            while (true)
            {
                if (Trace.StopEnabled)
                {
                    Trace.Stop();
                }

                if (n == null)
                {
                    if (x == null)
                    {
                        _root = i;

                        return;
                    }

                    Set(x, way, i);

                    break;
                }

                x       = n;
                compare = CompareRow(data, x.GetData());

                Trace.Check(compare != 0, Trace.VIOLATION_OF_UNIQUE_INDEX);

                way = compare < 0;
                n   = Child(x, way);
            }

            while (true)
            {
                if (Trace.StopEnabled)
                {
                    Trace.Stop();
                }

                int sign = way ? 1 : -1;

                switch (x.GetBalance() * sign)
                {
                case 1:
                    x.SetBalance(0);

                    return;

                case 0:
                    x.SetBalance(-sign);

                    break;

                case -1:
                    Node l = Child(x, way);

                    if (l.GetBalance() == -sign)
                    {
                        Replace(x, l);
                        Set(x, way, Child(l, !way));
                        Set(l, !way, x);
                        x.SetBalance(0);
                        l.SetBalance(0);
                    }
                    else
                    {
                        Node r = Child(l, !way);

                        Replace(x, r);
                        Set(l, !way, Child(r, way));
                        Set(r, way, l);
                        Set(x, way, Child(r, !way));
                        Set(r, !way, x);

                        int rb = r.GetBalance();

                        x.SetBalance((rb == -sign) ? sign : 0);
                        l.SetBalance((rb == sign) ? -sign : 0);
                        r.SetBalance(0);
                    }

                    return;
                }

                if (x.Equals(_root))
                {
                    return;
                }

                way = From(x);
                x   = x.GetParent();
            }
        }
Example #8
0
        public void Insert(Node i)
        {
            object[] data = i.GetData();
            Node    n = _root, x = n;
            bool way = true;
            int     compare = -1;

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

                if (n == null)
                {
                    if (x == null)
                    {
                        _root = i;

                        return;
                    }

                    Set(x, way, i);

                    break;
                }

                x = n;
                compare = CompareRow(data, x.GetData());

                TracingHelper.Check(compare != 0, TracingHelper.VIOLATION_OF_UNIQUE_INDEX);

                way = compare < 0;
                n = Child(x, way);
            }

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

                int sign = way ? 1 : -1;

                switch (x.GetBalance() * sign)
                {

                    case 1:
                        x.SetBalance(0);

                        return;

                    case 0:
                        x.SetBalance(-sign);

                        break;

                    case -1:
                        Node l = Child(x, way);

                        if (l.GetBalance() == -sign)
                        {
                            Replace(x, l);
                            Set(x, way, Child(l, !way));
                            Set(l, !way, x);
                            x.SetBalance(0);
                            l.SetBalance(0);
                        }
                        else
                        {
                            Node r = Child(l, !way);

                            Replace(x, r);
                            Set(l, !way, Child(r, way));
                            Set(r, way, l);
                            Set(x, way, Child(r, !way));
                            Set(r, !way, x);

                            int rb = r.GetBalance();

                            x.SetBalance((rb == -sign) ? sign : 0);
                            l.SetBalance((rb == sign) ? -sign : 0);
                            r.SetBalance(0);
                        }

                        return;
                }

                if (x.Equals(_root))
                {
                    return;
                }

                way = From(x);
                x = x.GetParent();
            }
        }
Example #9
0
        public bool Next()
        {
            if (bOuterJoin && nCurrent == null)
            {
                return false;
            }

            nCurrent = iIndex.Next(nCurrent);

            while (nCurrent != null)
            {
                oCurrentData = nCurrent.GetData();

                if (!Test(eEnd))
                {
                    break;
                }

                if (Test(eAnd))
                {
                    return true;
                }

                nCurrent = iIndex.Next(nCurrent);
            }

            oCurrentData = oEmptyData;

            return false;
        }
Example #10
0
        public Result GetScript(bool bDrop, bool bInsert, bool bCached, Channel channel)
        {
            channel.CheckAdmin();

            Result r = new Result(1);

            r.Type[0]  = ColumnType.VarChar;
            r.Table[0] = "SYSTEM_SCRIPT";
            r.Label[0] = "COMMAND";
            r.Name[0]  = "COMMAND";

            StringBuilder a = new StringBuilder();

            for (int i = 0; i < tTable.Count; i++)
            {
                Table t = (Table)tTable[i];

                if (bDrop)
                {
                    AddRow(r, "DROP TABLE \"" + t.Name + "\"");
                }

                a.Remove(0, a.Length);
                a.Append("CREATE ");

                if (t.IsCached)
                {
                    a.Append("CACHED ");
                }

                a.Append("TABLE ");
                a.Append('"');
                a.Append(t.Name);
                a.Append('"');
                a.Append("(");

                int   columns = t.ColumnCount;
                Index pki     = t.GetIndex("SYSTEM_PK");
                int   pk      = (pki == null) ? -1 : pki.Columns[0];

                for (int j = 0; j < columns; j++)
                {
                    a.Append('"');
                    a.Append(t.GetColumnName(j));
                    a.Append('"');
                    a.Append(" ");
                    a.Append(Column.GetColumnTypeString(t.GetType(j)));

                    if (!t.GetColumnIsNullable(j))
                    {
                        a.Append(" NOT NULL");
                    }

                    if (j == t.IdentityColumn)
                    {
                        a.Append(" IDENTITY");
                    }

                    if (j == pk)
                    {
                        a.Append(" PRIMARY KEY");
                    }

                    if (j < columns - 1)
                    {
                        a.Append(",");
                    }
                }

                ArrayList v = t.Constraints;

                for (int j = 0; j < v.Count; j++)
                {
                    Constraint c = (Constraint)v[j];

                    if (c.ConstraintType == ConstraintType.ForeignKey)
                    {
                        a.Append(",FOREIGN KEY");

                        int[] col = c.RefTableColumns;

                        a.Append(GetColumnList(c.RefTable, col, col.Length));
                        a.Append("REFERENCES ");
                        a.Append(c.MainTable.Name);

                        col = c.MainTableColumns;

                        a.Append(GetColumnList(c.MainTable, col, col.Length));
                    }
                    else if (c.ConstraintType == ConstraintType.Unique)
                    {
                        a.Append(",UNIQUE");

                        int[] col = c.MainTableColumns;

                        a.Append(GetColumnList(c.MainTable, col, col.Length));
                    }
                }

                a.Append(")");
                AddRow(r, a.ToString());

                Index index = null;

                while (true)
                {
                    index = t.GetNextIndex(index);

                    if (index == null)
                    {
                        break;
                    }

                    string indexname = index.Name;

                    if (indexname.Equals("SYSTEM_PK"))
                    {
                        continue;
                    }
                    else if (indexname.StartsWith("SYSTEM_FOREIGN_KEY"))
                    {
                        // foreign keys where created in the 'create table'
                        continue;
                    }
                    else if (indexname.StartsWith("SYSTEM_CONSTRAINT"))
                    {
                        // constraints where created in the 'create table'
                        continue;
                    }

                    a.Remove(0, a.Length);
                    a.Append("CREATE ");

                    if (index.IsUnique)
                    {
                        a.Append("UNIQUE ");
                    }

                    a.Append("INDEX ");
                    a.Append(indexname);
                    a.Append(" ON ");
                    a.Append(t.Name);

                    int[] col = index.Columns;
                    int   len = col.Length;

                    if (!index.IsUnique)
                    {
                        len--;
                    }

                    a.Append(GetColumnList(t, col, len));
                    AddRow(r, a.ToString());
                }

                if (bInsert)
                {
                    Index primary   = t.PrimaryIndex;
                    Node  x         = primary.First();
                    bool  integrity = true;

                    if (x != null)
                    {
                        integrity = false;

                        AddRow(r, "SET REFERENTIAL_INTEGRITY FALSE");
                    }

                    while (x != null)
                    {
                        AddRow(r, t.GetInsertStatement(x.GetData()));

                        x = primary.Next(x);
                    }

                    if (!integrity)
                    {
                        AddRow(r, "SET REFERENTIAL_INTEGRITY TRUE");
                    }
                }

                if (bCached && t.IsCached)
                {
                    a.Remove(0, a.Length);
                    a.Append("SET TABLE ");

                    a.Append('"');
                    a.Append(t.Name);
                    a.Append('"');
                    a.Append(" INDEX '");
                    a.Append(t.IndexRoots);
                    a.Append("'");
                    AddRow(r, a.ToString());
                }
            }

            ArrayList uList = aAccess.GetUsers();

            for (int i = 0; i < uList.Count; i++)
            {
                User u = (User)uList[i];

                // todo: this is not a nice implementation
                if (u == null)
                {
                    continue;
                }

                string name = u.Name;

                if (!name.Equals("PUBLIC"))
                {
                    a.Remove(0, a.Length);
                    a.Append("CREATE USER ");

                    a.Append(name);
                    a.Append(" PASSWORD ");
                    a.Append("\"" + u.Password + "\"");

                    if (u.IsAdmin)
                    {
                        a.Append(" ADMIN");
                    }

                    AddRow(r, a.ToString());
                }

                Hashtable rights = u.Rights;

                if (rights == null)
                {
                    continue;
                }

                foreach (string dbObject in rights.Keys)
                {
                    AccessType right = (AccessType)rights[dbObject];

                    if (right == AccessType.None)
                    {
                        continue;
                    }

                    a.Remove(0, a.Length);
                    a.Append("GRANT ");

                    a.Append(Access.GetRight(right));
                    a.Append(" ON ");
                    a.Append(dbObject);
                    a.Append(" TO ");
                    a.Append(u.Name);
                    AddRow(r, a.ToString());
                }
            }

            if (dDatabase.IsIgnoreCase)
            {
                AddRow(r, "SET IGNORECASE TRUE");
            }

            Hashtable h = dDatabase.Alias;

            foreach (string alias in h.Keys)
            {
                string className = (string)h[alias];
                AddRow(r, "CREATE ALIAS " + alias + " FOR \"" + className + "\"");
            }

            return(r);
        }
Example #11
0
        /// <summary>
        /// Script the database objects to a file.
        /// </summary>
        /// <param name="db"></param>
        /// <param name="file"></param>
        /// <param name="full"></param>
        /// <param name="channel"></param>
        public static void ScriptToFile(Database db, string file, bool full, Channel channel)
        {
            if ((new FileInfo(file)).Exists)
            {
                // there must be no such file; overwriting not allowed for security
                throw TracingHelper.Error(TracingHelper.FILE_IO_ERROR, file);
            }

            try
            {
                DateTime time = DateTime.Now;

                // only ddl commands; needs not so much memory
                Result r;

                if (full)
                {
                    // no drop, no insert, and no positions for cached tables
                    r = db.GetScript(false, false, false, channel);
                }
                else
                {
                    // no drop, no insert, but positions for cached tables
                    r = db.GetScript(false, false, true, channel);
                }

                Record       n = r.Root;
                StreamWriter w = new StreamWriter(file);

                while (n != null)
                {
                    writeLine(w, (string)n.Data[0]);

                    n = n.Next;
                }

                // inserts are done separetely to save memory
                ArrayList tables = db.Tables;

                for (int i = 0; i < tables.Count; i++)
                {
                    Table t = (Table)tables[i];

                    // cached tables have the index roots set in the ddl script
                    if (full || !t.IsCached)
                    {
                        Index primary = t.PrimaryIndex;
                        Node  x       = primary.First();

                        while (x != null)
                        {
                            writeLine(w, t.GetInsertStatement(x.GetData()));

                            x = primary.Next(x);
                        }
                    }
                }

                w.Close();

                TimeSpan execution = DateTime.Now.Subtract(time);

                if (TracingHelper.TraceEnabled)
                {
                    TracingHelper.Write((Int64)execution.TotalMilliseconds);
                }
            }
            catch (IOException e)
            {
                TracingHelper.Error(TracingHelper.FILE_IO_ERROR, file + " " + e);
            }
        }