Example #1
0
        /// <summary>
        /// Sets the column.
        /// </summary>
        /// <param name="columnid">The columnid.</param>
        /// <param name="coltyp">The coltyp.</param>
        /// <param name="isAscii">Whether a textual column is Ascii.</param>
        /// <param name="index">The index.</param>
        /// <param name="obj">The object.</param>
        /// <exception cref="System.InvalidOperationException">You may only update fields through Cursor.EditRecord.</exception>
        /// <remarks>
        /// Itags start at 1, so we add 1 to the index
        /// </remarks>
        private void SetColumn(JET_COLUMNID columnid, JET_coltyp coltyp, bool isAscii, int index, object obj)
        {
            lock (this.isamSession)
            {
                this.cursor.CheckDisposed();

                if ((this.grbit & RetrieveColumnGrbit.RetrieveCopy) == 0)
                {
                    this.cursor.CheckRecord();
                    throw new InvalidOperationException("You may only update fields through Cursor.EditRecord.");
                }

                // if this is a Sort or PreSortTemporary TT and we are setting
                // an LV column then always attempt to use intrinsic LVs
                SetColumnGrbit grbitSet = SetColumnGrbit.None;
                if ((this.cursor.TableDefinition.Type == TableType.Sort ||
                     this.cursor.TableDefinition.Type == TableType.PreSortTemporary) &&
                    (coltyp == JET_coltyp.LongText || coltyp == JET_coltyp.LongBinary))
                {
                    grbitSet = grbitSet | SetColumnGrbit.IntrinsicLV;
                }

                JET_SETINFO setinfo = new JET_SETINFO();
                setinfo.ibLongValue  = 0;
                setinfo.itagSequence = index + 1;

                byte[] bytes       = Converter.BytesFromObject(coltyp, isAscii, obj);
                int    bytesLength = bytes == null ? 0 : bytes.Length;

                Api.JetSetColumn(this.isamSession.Sesid, this.tableid, columnid, bytes, bytesLength, grbitSet, setinfo);

                this.updateID++;
            }
        }
Example #2
0
        /// <summary>
        /// Creates a single column with the specified definition in the table
        /// underlying this table definition
        /// </summary>
        /// <param name="columnDefinition">The column definition.</param>
        /// <returns>The <see cref="Columnid"/> object corresponding to the
        /// newly-added column.</returns>
        /// <remarks>
        /// It is currently not possible to add an AutoIncrement column to a
        /// table that is being used by a Cursor.  All such Cursors must be
        /// disposed before the column can be successfully added.
        /// </remarks>
        public Columnid AddColumn(ColumnDefinition columnDefinition)
        {
            lock (this.database.IsamSession)
            {
                using (IsamTransaction trx = new IsamTransaction(this.database.IsamSession))
                {
                    OpenTableGrbit grbit = OpenTableGrbit.None;

                    // if we are trying to add an auto-inc column then we must
                    // be able to open the table for exclusive access.  if we can't
                    // then we will not be able to add the column
                    if ((columnDefinition.Flags & ColumnFlags.AutoIncrement) != 0)
                    {
                        grbit = grbit | OpenTableGrbit.DenyRead;
                    }

                    // open the table with the appropriate access
                    JET_TABLEID tableid;
                    Api.JetOpenTable(this.database.IsamSession.Sesid, this.database.Dbid, this.name, null, 0, grbit, out tableid);

                    // add the new column to the table
                    JET_COLUMNDEF columndef = new JET_COLUMNDEF();
                    columndef.coltyp = DatabaseCommon.ColtypFromColumnDefinition(columnDefinition);
                    columndef.cp     = JET_CP.Unicode;
                    columndef.cbMax  = columnDefinition.MaxLength;
                    columndef.grbit  = Converter.ColumndefGrbitFromColumnFlags(columnDefinition.Flags);
                    byte[] defaultValueBytes = Converter.BytesFromObject(
                        columndef.coltyp,
                        false /* ASCII */,
                        columnDefinition.DefaultValue);
                    int          defaultValueBytesLength = (defaultValueBytes == null) ? 0 : defaultValueBytes.Length;
                    JET_COLUMNID jetColumnid;
                    Api.JetAddColumn(
                        this.database.IsamSession.Sesid,
                        tableid,
                        columnDefinition.Name,
                        columndef,
                        defaultValueBytes,
                        defaultValueBytesLength,
                        out jetColumnid);

                    // commit our change
                    Api.JetCloseTable(this.database.IsamSession.Sesid, tableid);
                    trx.Commit();
                    DatabaseCommon.SchemaUpdateID++;

                    // return the columnid for the new column
                    return(new Columnid(
                               columnDefinition.Name,
                               jetColumnid,
                               columndef.coltyp,
                               columndef.cp == JET_CP.ASCII));
                }
            }
        }
Example #3
0
        /// <summary>
        /// Creates a single table with the specified definition in the database
        /// </summary>
        /// <param name="tableDefinition">The table definition.</param>
        public override void CreateTable(TableDefinition tableDefinition)
        {
            lock (this.IsamSession)
            {
                this.CheckDisposed();

                using (IsamTransaction trx = new IsamTransaction(this.IsamSession))
                {
                    // FUTURE-2013/11/15-martinc: Consider using JetCreateTableColumnIndex(). It would be
                    // a bit faster because it's only a single managed/native transition.

                    // Hard-code the initial space and density.
                    JET_TABLEID tableid;
                    Api.JetCreateTable(this.IsamSession.Sesid, this.dbid, tableDefinition.Name, 16, 90, out tableid);

                    foreach (ColumnDefinition columnDefinition in tableDefinition.Columns)
                    {
                        JET_COLUMNDEF columndef = new JET_COLUMNDEF();
                        columndef.coltyp = IsamDatabase.ColtypFromColumnDefinition(columnDefinition);
                        columndef.cp     = JET_CP.Unicode;
                        columndef.cbMax  = columnDefinition.MaxLength;

                        columndef.grbit = Converter.ColumndefGrbitFromColumnFlags(columnDefinition.Flags);
                        byte[] defaultValueBytes = Converter.BytesFromObject(
                            columndef.coltyp,
                            false /*ASCII */,
                            columnDefinition.DefaultValue);

                        JET_COLUMNID columnid;
                        int          defaultValueLength = (defaultValueBytes == null) ? 0 : defaultValueBytes.Length;
                        Api.JetAddColumn(
                            this.IsamSession.Sesid,
                            tableid,
                            columnDefinition.Name,
                            columndef,
                            defaultValueBytes,
                            defaultValueLength,
                            out columnid);
                    }

                    foreach (IndexDefinition indexDefinition in tableDefinition.Indices)
                    {
                        JET_INDEXCREATE[] indexcreates = new JET_INDEXCREATE[1];
                        indexcreates[0] = new JET_INDEXCREATE();

                        indexcreates[0].szIndexName            = indexDefinition.Name;
                        indexcreates[0].szKey                  = IsamDatabase.IndexKeyFromIndexDefinition(indexDefinition);
                        indexcreates[0].cbKey                  = indexcreates[0].szKey.Length;
                        indexcreates[0].grbit                  = IsamDatabase.GrbitFromIndexDefinition(indexDefinition);
                        indexcreates[0].ulDensity              = indexDefinition.Density;
                        indexcreates[0].pidxUnicode            = new JET_UNICODEINDEX();
                        indexcreates[0].pidxUnicode.lcid       = indexDefinition.CultureInfo.LCID;
                        indexcreates[0].pidxUnicode.dwMapFlags = (uint)Converter.UnicodeFlagsFromCompareOptions(indexDefinition.CompareOptions);
                        indexcreates[0].rgconditionalcolumn    = IsamDatabase.ConditionalColumnsFromIndexDefinition(indexDefinition);
                        indexcreates[0].cConditionalColumn     = indexcreates[0].rgconditionalcolumn.Length;
                        indexcreates[0].cbKeyMost              = indexDefinition.MaxKeyLength;
                        Api.JetCreateIndex2(this.IsamSession.Sesid, tableid, indexcreates, indexcreates.Length);
                    }

                    // The initially-created tableid is opened exclusively.
                    Api.JetCloseTable(this.IsamSession.Sesid, tableid);
                    trx.Commit();
                    DatabaseCommon.SchemaUpdateID++;
                }
            }
        }
Example #4
0
        /// <summary>
        /// Creates a <see cref="ColumnDefinition"/> object representing the column passed in by <paramref name="columnBase"/>.
        /// </summary>
        /// <param name="database">The database.</param>
        /// <param name="tableName">Name of the table.</param>
        /// <param name="columnBase">The <see cref="JET_COLUMNBASE"/> object that represents this particular column.</param>
        /// <returns>
        /// A <see cref="ColumnDefinition"/> object based on the <paramref name="columnBase"/> object.
        /// </returns>
        internal static ColumnDefinition Load(IsamDatabase database, string tableName, JET_COLUMNBASE columnBase)
        {
            lock (database.IsamSession)
            {
                using (IsamTransaction trx = new IsamTransaction(database.IsamSession))
                {
                    JET_SESID sesid = database.IsamSession.Sesid;

                    // load info for the column
                    ColumnDefinition columnDefinition = new ColumnDefinition();

                    columnDefinition.columnid = new Columnid(columnBase);
                    columnDefinition.name     = columnDefinition.columnid.Name;
                    columnDefinition.type     = columnDefinition.columnid.Type;

                    columnDefinition.flags = ColumnFlagsFromGrbits(columnBase.grbit);

                    columnDefinition.maxLength = (int)columnBase.cbMax;
                    columnDefinition.IsAscii   = columnBase.cp == JET_CP.ASCII;

                    // there is currently no efficient means to retrieve the
                    // default value of a specific column from JET.  so, we are
                    // going to reach into the catalog and fetch it directly
                    JET_TABLEID tableidCatalog;
                    Api.JetOpenTable(
                        sesid,
                        database.Dbid,
                        "MSysObjects",
                        null,
                        0,
                        OpenTableGrbit.ReadOnly,
                        out tableidCatalog);

                    Api.JetSetCurrentIndex(sesid, tableidCatalog, "RootObjects");
                    Api.MakeKey(sesid, tableidCatalog, true, MakeKeyGrbit.NewKey);
                    Api.MakeKey(
                        sesid,
                        tableidCatalog,
                        Converter.BytesFromObject(JET_coltyp.Text, true, columnBase.szBaseTableName),
                        MakeKeyGrbit.None);
                    Api.JetSeek(sesid, tableidCatalog, SeekGrbit.SeekEQ);
                    JET_COLUMNBASE columnbaseCatalog;
                    Api.JetGetTableColumnInfo(sesid, tableidCatalog, "ObjidTable", out columnbaseCatalog);
                    uint objidTable = Api.RetrieveColumnAsUInt32(sesid, tableidCatalog, columnbaseCatalog.columnid).GetValueOrDefault();

                    Api.JetSetCurrentIndex(sesid, tableidCatalog, "Name");
                    Api.MakeKey(sesid, tableidCatalog, objidTable, MakeKeyGrbit.NewKey);
                    Api.MakeKey(sesid, tableidCatalog, (short)2, MakeKeyGrbit.None);
                    Api.MakeKey(sesid, tableidCatalog, columnBase.szBaseColumnName, Encoding.ASCII, MakeKeyGrbit.None);
                    Api.JetSeek(sesid, tableidCatalog, SeekGrbit.SeekEQ);

                    Api.JetGetTableColumnInfo(sesid, tableidCatalog, "DefaultValue", out columnbaseCatalog);
                    byte[] defaultValueBytes = Api.RetrieveColumn(sesid, tableidCatalog, columnbaseCatalog.columnid);

                    Columnid isamColumnid = columnDefinition.columnid;
                    columnDefinition.defaultValue = Converter.ObjectFromBytes(
                        isamColumnid.Coltyp,
                        columnDefinition.IsAscii,
                        defaultValueBytes);

                    columnDefinition.ReadOnly = true;

                    return(columnDefinition);
                }
            }
        }