Esempio n. 1
0
        private void viewBtn_Click(object sender, EventArgs e)
        {
            ClearDisplay();

            // open database and read records back - the database is transactional
            Env env = new Env(EnvCreateFlags.None);

            try {
                DbBTree       btree;
                Env.OpenFlags envFlags =
                    Env.OpenFlags.Create |
                    Env.OpenFlags.InitLock |
                    Env.OpenFlags.InitLog |
                    Env.OpenFlags.InitMPool |
                    Env.OpenFlags.InitTxn |
                    Env.OpenFlags.Recover;
                env.Open(homeDir, envFlags, 0);
                Db     db    = env.CreateDatabase(DbCreateFlags.None);
                Txn    txn   = env.TxnBegin(null, Txn.BeginFlags.None);
                DbFile tmpDb =
                    db.Open(txn, Path.Combine(homeDir, myDb), null, DbType.Unknown, Db.OpenFlags.None, 0);
                txn.Commit(Txn.CommitMode.None);
                if (tmpDb.DbType == DbType.BTree)
                {
                    btree = (DbBTree)tmpDb;
                }
                else
                {
                    throw new ApplicationException("Unexpected database type.");
                }

                List <Customer> custList = new List <Customer>();
                // DbBTreeCursor implements IDisposable - will be closed through "using"
                using (DbBTreeCursor cursor = btree.OpenCursor(null, DbFileCursor.CreateFlags.None)) {
                    Customer cust = null;
                    while (GetNextRecord(cursor, ref cust))
                    {
                        custList.Add(cust);
                    }
                }
                db.Close();

                DataGridViewColumn nameCol = new DataGridViewTextBoxColumn();
                nameCol.Name             = "NameCol";
                nameCol.HeaderText       = "Name";
                nameCol.DataPropertyName = "Name";
                dataGridView.Columns.Add(nameCol);
                dataGridView.DataSource = custList;
                tabControl.SelectTab("dataPage");
            }
            catch (Exception ex) {
                errBox.Text = ex.ToString();
                tabControl.SelectTab("errorPage");
            }
            finally {
                env.Close();
            }
        }
Esempio n. 2
0
        private List <StockItem> ReadStockItems(Vendor vendor)
        {
            Databases        dbs    = Databases.Instance;
            List <StockItem> items  = new List <StockItem>();
            DbBTreeCursor    cursor = dbs.VendorNameIdx.OpenCursor(null, DbFileCursor.CreateFlags.None);

            try {
                DbEntry secKeyEntry = dbs.Fmt.ToDbEntry <string>(vendor.Name);

#if false
                // same as below, but using standard cursor methods
                DbEntry    keyEntry  = DbEntry.Out(keyBuffer);
                DbEntry    dataEntry = DbEntry.Out(dataBuffer);
                ReadStatus status;
                status = cursor.PGetAt(ref secKeyEntry, ref keyEntry, ref dataEntry,
                                       DbcFile.GetAtMode.Set, DbcFile.ReadFlags.None);
                // we are not checking for ReadStatus.BufferSmall, but maybe we should
                while (status == ReadStatus.Success)
                {
                    StockItem item = null;
                    index = dataEntry.Start;
                    dbs.Formatter.Deserialize <StockItem>(ref item, dataEntry.Buffer, ref index);
                    items.Add(item);
                    status = cursor.PGet(ref secKeyEntry, ref keyEntry, ref dataEntry,
                                         DbcFile.GetMode.NextDup, DbcFile.ReadFlags.None);
                }
#endif
#if true
                // same as above, but using a foreach loop (which closes the cursor automatically)
                foreach (KeyDataPair entry in cursor.ItemsAt(ref secKeyEntry, DbFileCursor.ReadFlags.None))
                {
                    StockItem item = null;
                    dbs.Fmt.FromDbEntry <StockItem>(ref item, ref entry.Data);
                    items.Add(item);
                }
#endif
            }
            finally {
                cursor.Close();
            }

            inventoryView.DataSource = null;
            DataGridViewColumn col = new DataGridViewTextBoxColumn();
            col.Name             = "NameCol";
            col.HeaderText       = "Name";
            col.DataPropertyName = "Name";
            inventoryView.Columns.Add(col);
            inventoryView.DataSource = items;
            return(items);
        }
Esempio n. 3
0
        private bool GetNextRecord(DbBTreeCursor cursor, ref Customer cust)
        {
            ReadStatus status;

            // If we need to call SetLength further down, and that causes its value to
            // increase, then it will clear all buffer content between the old and the
            // new length, overwriting some of our data. So we set it to the maximum here.
            keyStream.SetLength(keyStream.Capacity);
            dataStream.SetLength(dataStream.Capacity);
            // we base our DbEntry instances on the serialization stream buffers
            DbEntry key  = DbEntry.Out(keyStream.GetBuffer());
            DbEntry data = DbEntry.Out(dataStream.GetBuffer());

            do
            {
                status = cursor.Get(ref key, ref data, DbFileCursor.GetMode.Next, DbFileCursor.ReadFlags.None);
                switch (status)
                {
                case ReadStatus.NotFound: return(false);

                case ReadStatus.KeyEmpty: continue; // skip deleted records

                case ReadStatus.BufferSmall:
                    if (key.Buffer.Length < key.Size)
                    {
                        keyStream.SetLength(key.Size);
                        key = DbEntry.Out(keyStream.GetBuffer());
                    }
                    if (data.Buffer.Length < data.Size)
                    {
                        dataStream.SetLength(data.Size);
                        data = DbEntry.Out(dataStream.GetBuffer());
                    }
                    continue;

                case ReadStatus.Success:
                    dataStream.Position = 0;
                    dataStream.SetLength(data.Size);
                    cust = (Customer)formatter.Deserialize(dataStream);
                    return(true);

                default:
                    return(false);
                }
            } while (true);
        }
Esempio n. 4
0
        public IList <T> Lookup <T>(ICondition condition) where T : class
        {
            dbName = typeof(T).FullName;
            initDatabase();
            List <T> result = new List <T>( );

            Txn trans = env.TxnBegin(null, Txn.BeginFlags.None);

            switch (condition.Name)
            {
            case "SELECT_ALL":
                using (DbBTreeCursor cursor = dbInstance.OpenCursor(null, DbFileCursor.CreateFlags.None)) {
                    T cust = null;

                    IEnumerable <KeyDataPair> lst = cursor.ItemsForward(false, DbFileCursor.ReadFlags.None);

                    foreach (KeyDataPair pair in lst)
                    {
                        using (MemoryStream mem = new MemoryStream(pair.Data.Buffer)) {
                            T t = (T)formatter.Deserialize(mem);

                            result.Add(t);
                        }
                    }
                }
                break;

            case "SELECT_BY_CODE":

                SelectByCodeCondition cond = condition as SelectByCodeCondition;

                if (cond == null)
                {
                    throw new InvalidDataException(String.Format("Invalid {0} condition", condition.Name));
                }
                foreach (int i in cond.HashCodes)
                {
                    T            t     = null;
                    byte[]       buf   = new byte[1024];
                    MemoryStream key   = new MemoryStream( );
                    MemoryStream value = new MemoryStream();
                    formatter.Serialize(key, i);

                    DbEntry dbKey = DbEntry.InOut(key.ToArray());
                    DbEntry dbVal = DbEntry.Out(buf);
                    while (true)
                    {
                        ReadStatus status =
                            dbInstance.GetExact(trans, ref dbKey, ref dbVal, DbFile.ReadFlags.None);


                        switch (status)
                        {
                        case ReadStatus.BufferSmall:
                            if (dbVal.Buffer.Length < dbVal.Size)
                            {
                                value.SetLength(dbVal.Size);
                                dbVal = DbEntry.Out(value.GetBuffer());
                            }
                            continue;

                        case ReadStatus.KeyEmpty:
                            goto brk;

                        case ReadStatus.NotFound:
                            goto brk;

                        case ReadStatus.Success:
                            value.Position = 0;
                            value.SetLength(dbVal.Size);
                            value.Write(dbVal.Buffer, 0, dbVal.Size);
                            value.Flush();
                            value.Seek(0, SeekOrigin.Begin);
                            t = (T)formatter.Deserialize(value);

                            result.Add(t);

                            key.Dispose();
                            value.Dispose();
                            break;
                        }
                    }
                    brk :;
                }

                break;

            default:
                throw new NotImplementedException(String.Format("Condition {0} is not implemented", condition.Name));
            }
            trans.Commit(Txn.CommitMode.Sync);
            env.Close();
            return(result);
        }