Пример #1
0
        /**
         * Method declaration
         *
         *
         * @param from
         *
         * @throws Exception
         */
        public void moveData(Table from)
        {
            Index index = from.getPrimaryIndex();
            Node  n     = index.first();

            while (n != null)
            {
                if (Trace.STOP)
                {
                    Trace.stop();
                }

                object[] o = n.getData();

                insertNoCheck(o, null);

                n = index.next(n);
            }

            index = getPrimaryIndex();
            n     = index.first();

            while (n != null)
            {
                if (Trace.STOP)
                {
                    Trace.stop();
                }

                object[] o = n.getData();

                from.deleteNoCheck(o, null);

                n = index.next(n);
            }
        }
Пример #2
0
        /**
         * Method declaration
         *
         *
         * @return
         *
         * @throws Exception
         */
        public bool findFirst()
        {
            if (iIndex == null)
            {
                iIndex = tTable.getPrimaryIndex();
            }

            if (eStart == null)
            {
                nCurrent = iIndex.first();
            }
            else
            {
                int    type = eStart.getArg().getDataType();
                object o    = eStart.getArg2().getValue(type);

                nCurrent = iIndex.findFirst(o, eStart.getType());
            }

            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);
        }
Пример #3
0
        /**
         * Method declaration
         *
         *
         * @param db
         * @param file
         * @param full
         * @param channel
         *
         * @throws Exception
         */
        public static void scriptToFile(Database db, string file, bool full,
                                        Channel channel)
        {
            if ((new File(file)).Exists)
            {
                // there must be no such file; overwriting not allowed for security
                throw Trace.error(Trace.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.rRoot;
                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.getTables();

                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.getPrimaryIndex();
                        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 (Trace.TRACE)
                {
                    Trace.trace(execution.TotalMilliseconds.ToInt64());
                }
            }
            catch (IOException e)
            {
                Trace.error(Trace.FILE_IO_ERROR, file + " " + e);
            }
        }
Пример #4
0
        /**
         * Method declaration
         *
         *
         * @param bDrop
         * @param bInsert
         * @param bCached
         * @param channel
         *
         * @return
         *
         * @throws Exception
         */
        public Result getScript(bool bDrop, bool bInsert, bool bCached,
                                Channel channel)
        {
            channel.checkAdmin();

            Result r = new Result(1);

            r.iType[0]  = Column.VARCHAR;
            r.sTable[0] = "SYSTEM_SCRIPT";
            r.sLabel[0] = "COMMAND";
            r.sName[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.getName());
                }

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

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

                a.Append("TABLE ");
                a.Append(t.getName());
                a.Append("(");

                int   columns = t.getColumnCount();
                Index pki     = t.getIndex("SYSTEM_PK");
                int   pk      = (pki == null) ? -1 : pki.getColumns()[0];

                for (int j = 0; j < columns; j++)
                {
                    a.Append(t.getColumnName(j));
                    a.Append(" ");
                    a.Append(Column.getType(t.getType(j)));

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

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

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

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

                ArrayList v = t.getConstraints();

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

                    if (c.getType() == Constraint.FOREIGN_KEY)
                    {
                        a.Append(",FOREIGN KEY");

                        int[] col = c.getRefColumns();

                        a.Append(getColumnList(c.getRef(), col, col.Length));
                        a.Append("REFERENCES ");
                        a.Append(c.getMain().getName());

                        col = c.getMainColumns();

                        a.Append(getColumnList(c.getMain(), col, col.Length));
                    }
                    else if (c.getType() == Constraint.UNIQUE)
                    {
                        a.Append(",UNIQUE");

                        int[] col = c.getMainColumns();

                        a.Append(getColumnList(c.getMain(), 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.getName();

                    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.getName());

                    int[] col = index.getColumns();
                    int   len = col.Length;

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

                    a.Append(getColumnList(t, col, len));
                    addRow(r, a.ToString());
                }

                if (bInsert)
                {
                    Index primary   = t.getPrimaryIndex();
                    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(t.getName());
                    a.Append(" INDEX '");
                    a.Append(t.getIndexRoots());
                    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.getName();

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

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

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

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

                Hashtable rights = u.getRights();

                if (rights == null)
                {
                    continue;
                }

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

                    if (right == 0)
                    {
                        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.getName());
                    addRow(r, a.ToString());
                }
            }

            if (dDatabase.isIgnoreCase())
            {
                addRow(r, "SET IGNORECASE TRUE");
            }

            Hashtable h = dDatabase.getAlias();

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

            return(r);
        }