Пример #1
0
 /**
  * Constructor declaration
  *
  *
  * @param c
  * @param id
  */
 public Channel(Channel c, int id)
 {
     iId = id;
     dDatabase = c.dDatabase;
     uUser = c.uUser;
     tTransaction = new ArrayList();
     bAutoCommit = true;
     bReadOnly = c.bReadOnly;
 }
        /**
         * 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;
        }
        /**
         * Method declaration
         *
         *
         * @param name
         * @param channel
         *
         * @return
         *
         * @throws Exception
         */
        public Table getSystemTable(string name, Channel channel)
        {
            if (name.Equals("SYSTEM_PROCEDURES"))
            {
                Table t = createTable(name);

                t.addColumn("PROCEDURE_" + META_CAT, Column.VARCHAR);
                t.addColumn("PROCEDURE_" + META_SCHEM, Column.VARCHAR);
                t.addColumn("PROCEDURE_NAME", Column.VARCHAR);
                t.addColumn("NUM_INPUT_PARAMS", Column.INTEGER);
                t.addColumn("NUM_OUTPUT_PARAMS", Column.INTEGER);
                t.addColumn("NUM_RESULT_SETS", Column.INTEGER);
                t.addColumn("REMARKS", Column.VARCHAR);
                t.addColumn("PROCEDURE_TYPE", Column.SMALLINT);
                t.createPrimaryKey();

                return t;
            }
            else if (name.Equals("SYSTEM_PROCEDURECOLUMNS"))
            {
                Table t = createTable(name);

                t.addColumn("PROCEDURE_" + META_CAT, Column.VARCHAR);
                t.addColumn("PROCEDURE_" + META_SCHEM, Column.VARCHAR);
                t.addColumn("PROCEDURE_NAME", Column.VARCHAR);
                t.addColumn("COLUMN_NAME", Column.VARCHAR);
                t.addColumn("COLUMN_TYPE", Column.SMALLINT);
                t.addColumn("DATA_TYPE", Column.SMALLINT);
                t.addColumn("TYPE_NAME", Column.VARCHAR);
                t.addColumn("PRECISION", Column.INTEGER);
                t.addColumn("LENGTH", Column.INTEGER);
                t.addColumn("SCALE", Column.SMALLINT);
                t.addColumn("RADIX", Column.SMALLINT);
                t.addColumn("NULLABLE", Column.SMALLINT);
                t.addColumn("REMARKS", Column.VARCHAR);
                t.createPrimaryKey();

                return t;
            }
            else if (name.Equals("SYSTEM_TABLES"))
            {
                Table t = createTable(name);

                t.addColumn("TABLE_" + META_CAT, Column.VARCHAR);
                t.addColumn("TABLE_" + META_SCHEM, Column.VARCHAR);
                t.addColumn("TABLE_NAME", Column.VARCHAR);
                t.addColumn("TABLE_TYPE", Column.VARCHAR);
                t.addColumn("REMARKS", Column.VARCHAR);
                t.createPrimaryKey();

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

                    o[2] = table.getName();
                    o[3] = "TABLE";

                    t.insert(o, null);
                }

                return t;
            }
            else if (name.Equals("SYSTEM_SCHEMAS"))
            {
                Table t = createTable(name);

                t.addColumn("TABLE_" + META_SCHEM, Column.VARCHAR);
                t.createPrimaryKey();

                return t;
            }
            else if (name.Equals("SYSTEM_CATALOGS"))
            {
                Table t = createTable(name);

                t.addColumn("TABLE_" + META_CAT, Column.VARCHAR);
                t.createPrimaryKey();

                return t;
            }
            else if (name.Equals("SYSTEM_TABLETYPES"))
            {
                Table t = createTable(name);

                t.addColumn("TABLE_TYPE", Column.VARCHAR);
                t.createPrimaryKey();

                object[] o = t.getNewRow();

                o[0] = "TABLE";

                t.insert(o, null);

                return t;
            }
            else if (name.Equals("SYSTEM_COLUMNS"))
            {
                Table t = createTable(name);

                t.addColumn("TABLE_" + META_CAT, Column.VARCHAR);
                t.addColumn("TABLE_" + META_SCHEM, Column.VARCHAR);
                t.addColumn("TABLE_NAME", Column.VARCHAR);
                t.addColumn("COLUMN_NAME", Column.VARCHAR);
                t.addColumn("DATA_TYPE", Column.SMALLINT);
                t.addColumn("TYPE_NAME", Column.VARCHAR);
                t.addColumn(META_COLUMN_SIZE, Column.INTEGER);
                t.addColumn(META_BUFFER_LENGTH, Column.INTEGER);
                t.addColumn(META_DECIMAL_DIGITS, Column.INTEGER);
                t.addColumn(META_NUM_PREC_RADIX, Column.INTEGER);
                t.addColumn("NULLABLE", Column.INTEGER);
                t.addColumn("REMARKS", Column.VARCHAR);

                // Access and Intersolv do not return this fields
                t.addColumn("COLUMN_DEF", Column.VARCHAR);
                t.addColumn("SQL_DATA_TYPE", Column.VARCHAR);
                t.addColumn("SQL_DATETIME_SUB", Column.INTEGER);
                t.addColumn("CHAR_OCTET_LENGTH", Column.INTEGER);
                t.addColumn("ORDINAL_POSITION", Column.VARCHAR);
                t.addColumn("IS_NULLABLE", Column.VARCHAR);
                t.createPrimaryKey();

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

                    for (int j = 0; j < columns; j++)
                    {
                        object[] o = t.getNewRow();

                        o[2] = table.getName();
                        o[3] = table.getColumnName(j);
                        o[4] = table.getColumnType(j);
                        o[5] = Column.getType(table.getColumnType(j));

                        int nullable;

                        if (table.getColumnIsNullable(j))
                        {
                            nullable = true.ToInt32();
                        }
                        else
                        {
                            nullable = false.ToInt32();
                        }

                        o[10] = nullable;

                        if (table.getIdentityColumn() == j)
                        {
                            o[11] = "IDENTITY";
                        }

                        t.insert(o, null);
                    }
                }

                return t;
            }
            else if (name.Equals("SYSTEM_COLUMNPRIVILEGES"))
            {
                Table t = createTable(name);

                t.addColumn("TABLE_" + META_CAT, Column.VARCHAR);
                t.addColumn("TABLE_" + META_SCHEM, Column.VARCHAR);
                t.addColumn("TABLE_NAME", Column.VARCHAR);
                t.addColumn("COLUMN_NAME", Column.VARCHAR);
                t.addColumn("GRANTOR", Column.VARCHAR);
                t.addColumn("GRANTEE", Column.VARCHAR);
                t.addColumn("PRIVILEGE", Column.VARCHAR);
                t.addColumn("IS_GRANTABLE", Column.VARCHAR);
                t.createPrimaryKey();

                /*
                 * // todo: get correct info
                 * for(int i=0;i<tTable.size();i++) {
                 * Table table=(Table)tTable.elementAt(i);
                 * int columns=table.getColumnCount();
                 * for(int j=0;j<columns;j++) {
                 * object o[]=t.getNewRow();
                 * o[2]=table.getName();
                 * o[3]=table.getColumnName(j);
                 * o[4]="sa";
                 * o[6]="FULL";
                 * o[7]="NO";
                 * t.insert(o,null);
                 * }
                 * }
                 */
                return t;
            }
            else if (name.Equals("SYSTEM_TABLEPRIVILEGES"))
            {
                Table t = createTable(name);

                t.addColumn("TABLE_" + META_CAT, Column.VARCHAR);
                t.addColumn("TABLE_" + META_SCHEM, Column.VARCHAR);
                t.addColumn("TABLE_NAME", Column.VARCHAR);
                t.addColumn("GRANTOR", Column.VARCHAR);
                t.addColumn("GRANTEE", Column.VARCHAR);
                t.addColumn("PRIVILEGE", Column.VARCHAR);
                t.addColumn("IS_GRANTABLE", Column.VARCHAR);
                t.createPrimaryKey();

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

                    o[2] = table.getName();
                    o[3] = "sa";
                    o[5] = "FULL";

                    t.insert(o, null);
                }

                return t;
            }
            else if (name.Equals("SYSTEM_BESTROWIDENTIFIER"))
            {
                Table t = createTable(name);

                t.addColumn("SCOPE", Column.SMALLINT);
                t.addColumn("COLUMN_NAME", Column.VARCHAR);
                t.addColumn("DATA_TYPE", Column.SMALLINT);
                t.addColumn("TYPE_NAME", Column.VARCHAR);
                t.addColumn(META_COLUMN_SIZE, Column.INTEGER);
                t.addColumn(META_BUFFER_LENGTH, Column.INTEGER);
                t.addColumn(META_DECIMAL_DIGITS, Column.SMALLINT);
                t.addColumn("PSEUDO_COLUMN", Column.SMALLINT);
                t.createPrimaryKey();

                return t;
            }
            else if (name.Equals("SYSTEM_VERSIONCOLUMNS"))
            {
                Table t = createTable(name);

                t.addColumn("SCOPE", Column.INTEGER);
                t.addColumn("COLUMN_NAME", Column.VARCHAR);
                t.addColumn("DATA_TYPE", Column.SMALLINT);
                t.addColumn("TYPE_NAME", Column.VARCHAR);
                t.addColumn(META_COLUMN_SIZE, Column.SMALLINT);
                t.addColumn(META_BUFFER_LENGTH, Column.INTEGER);
                t.addColumn(META_DECIMAL_DIGITS, Column.SMALLINT);
                t.addColumn("PSEUDO_COLUMN", Column.SMALLINT);
                t.createPrimaryKey();

                return t;
            }
            else if (name.Equals("SYSTEM_PRIMARYKEYS"))
            {
                Table t = createTable(name);

                t.addColumn("TABLE_" + META_CAT, Column.VARCHAR);
                t.addColumn("TABLE_" + META_SCHEM, Column.VARCHAR);
                t.addColumn("TABLE_NAME", Column.VARCHAR);
                t.addColumn("COLUMN_NAME", Column.VARCHAR);
                t.addColumn("KEY_SEQ", Column.SMALLINT);
                t.addColumn("PK_NAME", Column.VARCHAR);
                t.createPrimaryKey();

                for (int i = 0; i < tTable.Count; i++)
                {
                    Table table = (Table) tTable[i];
                    Index index = table.getIndex("SYSTEM_PK");
                    int[]   cols = index.getColumns();
                    int   len = cols.Length;

                    for (int j = 0; j < len; j++)
                    {
                        object[] o = t.getNewRow();

                        o[2] = table.getName();
                        o[3] = table.getColumnName(cols[j]);
                        o[4] = j + 1;
                        o[5] = "SYSTEM_PK";

                        t.insert(o, null);
                    }
                }

                return t;
            }
            else if (name.Equals("SYSTEM_IMPORTEDKEYS"))
            {
                Table t = createTable(name);

                t.addColumn("PKTABLE_" + META_CAT, Column.VARCHAR);
                t.addColumn("PKTABLE_" + META_SCHEM, Column.VARCHAR);
                t.addColumn("PKTABLE_NAME", Column.VARCHAR);
                t.addColumn("PKCOLUMN_NAME", Column.VARCHAR);
                t.addColumn("FKTABLE_" + META_CAT, Column.VARCHAR);
                t.addColumn("FKTABLE_" + META_SCHEM, Column.VARCHAR);
                t.addColumn("FKTABLE_NAME", Column.VARCHAR);
                t.addColumn("FKCOLUMN_NAME", Column.VARCHAR);
                t.addColumn("KEY_SEQ", Column.SMALLINT);
                t.addColumn("UPDATE_RULE", Column.SMALLINT);
                t.addColumn("DELETE_RULE", Column.SMALLINT);
                t.addColumn("FK_NAME", Column.VARCHAR);
                t.addColumn("PK_NAME", Column.VARCHAR);
                t.addColumn("DEFERRABILITY", Column.SMALLINT);
                t.createPrimaryKey();

                return t;
            }
            else if (name.Equals("SYSTEM_EXPORTEDKEYS"))
            {
                Table t = createTable(name);

                t.addColumn("PKTABLE_" + META_CAT, Column.VARCHAR);
                t.addColumn("PKTABLE_" + META_SCHEM, Column.VARCHAR);
                t.addColumn("PKTABLE_NAME", Column.VARCHAR);
                t.addColumn("PKCOLUMN_NAME", Column.VARCHAR);
                t.addColumn("FKTABLE_" + META_CAT, Column.VARCHAR);
                t.addColumn("FKTABLE_" + META_SCHEM, Column.VARCHAR);
                t.addColumn("FKTABLE_NAME", Column.VARCHAR);
                t.addColumn("FKCOLUMN_NAME", Column.VARCHAR);
                t.addColumn("KEY_SEQ", Column.SMALLINT);
                t.addColumn("UPDATE_RULE", Column.SMALLINT);
                t.addColumn("DELETE_RULE", Column.SMALLINT);
                t.addColumn("FK_NAME", Column.VARCHAR);
                t.addColumn("PK_NAME", Column.VARCHAR);
                t.addColumn("DEFERRABILITY", Column.SMALLINT);
                t.createPrimaryKey();

                return t;
            }
            else if (name.Equals("SYSTEM_CROSSREFERENCE"))
            {
                Table t = createTable(name);

                t.addColumn("PKTABLE_" + META_CAT, Column.VARCHAR);
                t.addColumn("PKTABLE_" + META_SCHEM, Column.VARCHAR);
                t.addColumn("PKTABLE_NAME", Column.VARCHAR);
                t.addColumn("PKCOLUMN_NAME", Column.VARCHAR);
                t.addColumn("FKTABLE_" + META_CAT, Column.VARCHAR);
                t.addColumn("FKTABLE_" + META_SCHEM, Column.VARCHAR);
                t.addColumn("FKTABLE_NAME", Column.VARCHAR);
                t.addColumn("FKCOLUMN_NAME", Column.VARCHAR);
                t.addColumn("KEY_SEQ", Column.INTEGER);
                t.addColumn("UPDATE_RULE", Column.SMALLINT);
                t.addColumn("DELETE_RULE", Column.SMALLINT);
                t.addColumn("FK_NAME", Column.VARCHAR);
                t.addColumn("PK_NAME", Column.VARCHAR);
                t.addColumn("DEFERRABILITY", Column.SMALLINT);
                t.createPrimaryKey();

                return t;
            }
            else if (name.Equals("SYSTEM_TYPEINFO"))
            {
                Table t = createTable(name);

                t.addColumn("TYPE_NAME", Column.VARCHAR);
                t.addColumn("DATA_TYPE", Column.SMALLINT);
                t.addColumn("PRECISION", Column.INTEGER);
                t.addColumn("LITERAL_PREFIX", Column.VARCHAR);
                t.addColumn("LITERAL_SUFFIX", Column.VARCHAR);
                t.addColumn("CREATE_PARAMS", Column.VARCHAR);
                t.addColumn("NULLABLE", Column.SMALLINT);
                t.addColumn("CASE_SENSITIVE", Column.VARCHAR);
                t.addColumn("SEARCHABLE", Column.SMALLINT);
                t.addColumn("UNSIGNED_ATTRIBUTE", Column.BIT);
                t.addColumn(META_FIXED_PREC_SCALE, Column.BIT);
                t.addColumn("AUTO_INCREMENT", Column.BIT);
                t.addColumn("LOCAL_TYPE_NAME", Column.VARCHAR);
                t.addColumn("MINIMUM_SCALE", Column.SMALLINT);
                t.addColumn("MAXIMUM_SCALE", Column.SMALLINT);

                // this columns are not supported by Access and Intersolv
                t.addColumn("SQL_DATE_TYPE", Column.INTEGER);
                t.addColumn("SQL_DATETIME_SUB", Column.INTEGER);
                t.addColumn("NUM_PREC_RADIX", Column.INTEGER);
                t.createPrimaryKey();

                for (int i = 0; i < Column.TYPES.Length; i++)
                {
                    object[] o = t.getNewRow();
                    int    type = Column.TYPES[i];

                    o[0] = Column.getType(type);
                    o[1] = type;
                    o[2] = 0;		 // precision
                    o[6] = true; // need Column to track nullable for this
                    o[7] = true;	 // case sensitive
                    o[8] = true;;
                    o[9] = false;       // unsigned
                    o[10] = (type == Column.NUMERIC	|| type == Column.DECIMAL);
                    o[11] = (type == Column.INTEGER);
                    o[12] = o[0];
                    o[13] = 0;
                    o[14] = 0;    // maximum scale
                    o[15] = 0;
                    o[16] = o[15];
                    o[17] = 10;

                    t.insert(o, null);
                }

                return t;
            }
            else if (name.Equals("SYSTEM_INDEXINFO"))
            {
                Table t = createTable(name);

                t.addColumn("TABLE_" + META_CAT, Column.VARCHAR);
                t.addColumn("TABLE_" + META_SCHEM, Column.VARCHAR);
                t.addColumn("TABLE_NAME", Column.VARCHAR);
                t.addColumn("NON_UNIQUE", Column.BIT);
                t.addColumn("INDEX_QUALIFIER", Column.VARCHAR);
                t.addColumn("INDEX_NAME", Column.VARCHAR);
                t.addColumn("TYPE", Column.SMALLINT);
                t.addColumn(META_ORDINAL_POSITON, Column.SMALLINT);
                t.addColumn("COLUMN_NAME", Column.VARCHAR);
                t.addColumn(META_ASC_OR_DESC, Column.VARCHAR);
                t.addColumn("CARDINALITY", Column.INTEGER);
                t.addColumn("PAGES", Column.INTEGER);
                t.addColumn("FILTER_CONDITION", Column.VARCHAR);
                t.createPrimaryKey();

                for (int i = 0; i < tTable.Count; i++)
                {
                    Table table = (Table) tTable[i];
                    Index index = null;

                    while (true)
                    {
                        index = table.getNextIndex(index);

                        if (index == null)
                        {
                            break;
                        }

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

                        // this removes the column that makes every index unique
                        if (!index.isUnique())
                        {
                            len--;
                        }

                        for (int j = 0; j < len; j++)
                        {
                            object[] o = t.getNewRow();

                            o[2] = table.getName();
                            o[3] = !index.isUnique();
                            o[5] = index.getName();
                            o[6] = 1;
                            o[7] = (j + 1);
                            o[8] = table.getColumnName(cols[j]);
                            o[9] = "A";

                            t.insert(o, null);
                        }
                    }
                }

                return t;
            }
            else if (name.Equals("SYSTEM_UDTS"))
            {
                Table t = createTable(name);

                t.addColumn("TYPE_" + META_CAT, Column.VARCHAR);
                t.addColumn("TYPE_" + META_SCHEM, Column.VARCHAR);
                t.addColumn("TYPE_NAME", Column.VARCHAR);
                t.addColumn("CLASS_NAME", Column.BIT);
                t.addColumn("DATA_TYPE", Column.VARCHAR);
                t.addColumn("REMARKS", Column.VARCHAR);
                t.createPrimaryKey();

                return t;
            }
            else if (name.Equals("SYSTEM_CONNECTIONINFO"))
            {
                Table t = createTable(name);

                t.addColumn("KEY", Column.VARCHAR);
                t.addColumn("VALUE", Column.VARCHAR);
                t.createPrimaryKey();

                object[] o = t.getNewRow();

                o[0] = "USER";
                o[1] = channel.getUsername();

                t.insert(o, null);

                o = t.getNewRow();
                o[0] = "READONLY";
                o[1] = channel.isReadOnly() ? "TRUE" : "FALSE";

                t.insert(o, null);

                o = t.getNewRow();
                o[0] = "MAXROWS";
                o[1] = "" + channel.getMaxRows();

                t.insert(o, null);

                o = t.getNewRow();
                o[0] = "DATABASE";
                o[1] = "" + channel.getDatabase().getName();

                t.insert(o, null);

                o = t.getNewRow();
                o[0] = "IDENTITY";
                o[1] = "" + channel.getLastIdentity();

                t.insert(o, null);

                return t;
            }
            else if (name.Equals("SYSTEM_USERS"))
            {
                Table t = createTable(name);

                t.addColumn("USER", Column.VARCHAR);
                t.addColumn("ADMIN", Column.BIT);
                t.createPrimaryKey();

                ArrayList v = aAccess.getUsers();

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

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

                    string user = u.getName();

                    if (!user.Equals("PUBLIC"))
                    {
                        object[] o = t.getNewRow();

                        o[0] = user;
                        o[1] = u.isAdmin();

                        t.insert(o, null);
                    }
                }

                return t;
            }

            return null;
        }
Пример #4
0
        /**
         * Method declaration
         *
         *
         * @param c
         * @param s
         *
         * @throws Exception
         */
        public void write(Channel c, string s)
        {
            if (bRestoring || s == null || s.Equals(""))
            {
                return;
            }

            if (!bReadOnly)
            {
                int id = 0;

                if (c != null)
                {
                    id = c.getId();
                }

                if (id != mLastId)
                {
                    s = "/*C" + id + "*/" + s;
                    mLastId = id;
                }

                try
                {
                    writeLine(wScript, s);

                    if (bWriteDelay)
                    {
                        bNeedFlush = true;
                    }
                    else
                    {
                        wScript.Flush();
                    }
                }
                catch (IOException e)
                {
                    Trace.error(Trace.FILE_IO_ERROR, sFileScript);
                }

                if (iLogSize > 0 && iLogCount++ > 100)
                {
                    iLogCount = 0;

                    if ((new File(sFileScript)).Length > iLogSize * 1024 * 1024)
                    {
                        checkpoint();
                    }
                }
            }
        }
Пример #5
0
        /**
         * Method declaration
         *
         *
         * @throws Exception
         */
        private void runScript()
        {
            if (Trace.TRACE)
            {
                Trace.trace();
            }

            if (!(new File(sFileScript)).Exists)
            {
                return;
            }

            bRestoring = true;

            dDatabase.setReferentialIntegrity(false);

            ArrayList channel = new ArrayList();

            channel.Add(cSystem);

            Channel current = cSystem;
            int     size = 1;

            try
            {
                DateTime	     time = DateTime.Now;
                StreamReader r = new StreamReader(sFileScript);

                while (true)
                {
                    string s = r.ReadLine();

                    if (s == null)
                    {
                        break;
                    }

                    if (s.StartsWith("/*C"))
                    {
                        int id = Int32.FromString(s.Substring(3,(s.IndexOf('*', 4)-3)));

                        if (id > channel.Count)
                        {
                            current = new Channel(cSystem, id);

                            channel.Insert(id, current);
                            dDatabase.registerChannel(current);
                        }
                        else
                        {
                            current = (Channel) channel[id - 1];
                        }

                        s = s.Substring(s.IndexOf('/', 1) + 1);
                    }

                    if (!s.Equals(""))
                    {
                        dDatabase.execute(s, current);
                    }

                    if (s.Equals("DISCONNECT"))
                    {
                        int id = current.getId();

                        current = new Channel(cSystem, id);

                        channel.RemoveAt(id);
                        channel.Insert(id, current);
                    }
                }

                r.Close();

                for (int i = 0; i < size; i++)
                {
                    current = (Channel) channel[i];

                    if (current != null)
                    {
                        current.rollback();
                    }
                }

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

                if (Trace.TRACE)
                {
                    Trace.trace(execution.TotalMilliseconds.ToInt64());
                }
            }
            catch (IOException e)
            {
                throw Trace.error(Trace.FILE_IO_ERROR, sFileScript + " " + e);
            }

            dDatabase.setReferentialIntegrity(true);

            bRestoring = false;
        }
Пример #6
0
 /**
  * Constructor declaration
  *
  *
  * @param db
  * @param system
  * @param name
  */
 public Log(Database db, Channel system, string name)
 {
     dDatabase = db;
     cSystem = system;
     sName = name;
     sFileProperties = sName + ".cfg";
     sFileScript = sName + ".log";
     sFileCache = sName + ".data";
     sFileBackup = sName + ".backup";
 }
Пример #7
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);
            }
        }
Пример #8
0
 /**
  * Method declaration
  *
  *
  * @param row
  * @param c
  *
  * @throws Exception
  */
 public void insertNoCheck(object[] row, Channel c)
 {
     insertNoCheck(row, c, true);
 }
Пример #9
0
        /**
         * Method declaration
         *
         *
         * @param row
         * @param c
         * @param log
         *
         * @throws Exception
         */
        public void insertNoCheck(object[] row, Channel c,
			bool log)
        {
            int i;

            if (iIdentityColumn != -1)
            {
                if (row[iIdentityColumn] == null)
                {
                    if (c != null)
                    {
                        c.setLastIdentity(iIdentityId);
                    }

                    row[iIdentityColumn] = iIdentityId++;
                }
                else
                {
                    i = (int) row[iIdentityColumn];

                    if (iIdentityId <= i)
                    {
                        if (c != null)
                        {
                            c.setLastIdentity(i);
                        }

                        iIdentityId = i + 1;
                    }
                }
            }

            for (i = 0; i < iColumnCount; i++)
            {
                if (row[i] == null &&!getColumn(i).isNullable())
                {
                    throw Trace.error(Trace.TRY_TO_INSERT_NULL);
                }
            }

            try
            {
                Row r = new Row(this, row);

                for (i = 0; i < iIndexCount; i++)
                {
                    Node n = r.getNode(i);

                    getIndex(i).insert(n);
                }
            }
            catch (Exception e)
            {    // rollback insert
                for (--i; i >= 0; i--)
                {
                    getIndex(i).delete(row, i == 0);
                }

                throw e;		      // and throw error again
            }

            if (c != null)
            {
                c.addTransactionInsert(this, row);
            }

            if (lLog != null)
            {
                lLog.write(c, getInsertStatement(row));
            }
        }
Пример #10
0
        /**
         * Method declaration
         *
         *
         * @param row
         * @param c
         *
         * @throws Exception
         */
        public void insert(object[] row, Channel c)
        {
            if (dDatabase.isReferentialIntegrity())
            {
                for (int i = 0; i < iConstraintCount; i++)
                {
                    ((Constraint) vConstraint[i]).checkInsert(row);
                }
            }

            insertNoCheck(row, c);
        }
Пример #11
0
        /**
         * Method declaration
         *
         *
         * @param result
         * @param c
         *
         * @throws Exception
         */
        public void insert(Result result, Channel c)
        {
            // if violation of constraints can occur, insert must be rolled back
            // outside of this function!
            Record r = result.rRoot;
            int    len = result.getColumnCount();

            while (r != null)
            {
                object[] row = getNewRow();

                for (int i = 0; i < len; i++)
                {
                    row[i] = r.data[i];
                }

                insert(row, c);

                r = r.next;
            }
        }
Пример #12
0
        /**
         * Method declaration
         *
         *
         * @param row
         * @param c
         * @param log
         *
         * @throws Exception
         */
        public void deleteNoCheck(object[] row, Channel c,
			bool log)
        {
            for (int i = 1; i < iIndexCount; i++)
            {
                getIndex(i).delete(row, false);
            }

            // must delete data last
            getIndex(0).delete(row, true);

            if (c != null)
            {
                c.addTransactionDelete(this, row);
            }

            if (lLog != null)
            {
                lLog.write(c, getDeleteStatement(row));
            }
        }
Пример #13
0
 /**
  * Method declaration
  *
  *
  * @param row
  * @param c
  *
  * @throws Exception
  */
 public void deleteNoCheck(object[] row, Channel c)
 {
     deleteNoCheck(row, c, true);
 }
Пример #14
0
 /**
  * Constructor declaration
  *
  *
  * @param db
  * @param t
  * @param channel
  */
 public Parser(Database db, Tokenizer t, Channel channel)
 {
     dDatabase = db;
     tTokenizer = t;
     cChannel = channel;
 }