Exemplo n.º 1
0
        public PrepareResult PrepareCommand(string input, out Statement statement)
        {
            statement = new Statement();
            if (input.Substring(0, 4).Equals("Save", StringComparison.OrdinalIgnoreCase))
            {
                statement.StatementType = StatementType_T.STATEMENT_SAVE;
            }
            else if (input.Substring(0, 6).Equals("insert", StringComparison.OrdinalIgnoreCase))
            {
                statement.StatementType = StatementType_T.STATEMENT_INSERT;
                input = input.Trim();
                var list = input.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                if (list.Length != 4)
                {
                    return(PrepareResult.PREPARE_SYNTAX_ERROR);
                }
                UserRow row = new UserRow();
                row.id        = int.Parse(list[1]);
                row.username  = list[2];
                row.email     = list[3];
                statement.Row = row;
            }
            else if (input.Substring(0, 6).Equals("select", StringComparison.OrdinalIgnoreCase))
            {
                statement.StatementType = StatementType_T.STATEMENT_SELECT;
            }

            return(PrepareResult.PREPARE_SUCCESS);
        }
Exemplo n.º 2
0
        public static UserRow DeSerialize(byte[] bytes)
        {
            UserRow user = new UserRow();

            user.id       = BitConverter.ToInt32(bytes, 0);
            user.username = Encoding.ASCII.GetString(bytes, 4, 32).Trim('\0');
            user.email    = Encoding.ASCII.GetString(bytes, 36, 255).Trim('\0');
            return(user);
        }
Exemplo n.º 3
0
        public void CursorValue(out byte[] page, out int offset)
        {
            UserRow userRow         = new UserRow();
            var     pageNumber      = this.RowNumber / userRow.RowPerPage;
            var     rowNumberInPage = this.RowNumber % userRow.RowPerPage;

            page   = this.Table.Pager.GetPage(pageNumber);
            offset = rowNumberInPage * userRow.RowSize;
        }
Exemplo n.º 4
0
        public static UserRow[] Select()
        {
            UserRow[] result = new UserRow[MaxRowNumber];
            Console.WriteLine("Count:" + (MaxRowNumber + 1));
            Console.WriteLine("id\tusername\temail");
            for (int i = 0; i < MaxRowNumber; i++)
            {
                var    pageNumber      = i / UserRow.RowPerPage;
                var    rowNumberInPage = i % UserRow.RowPerPage;
                var    offset          = rowNumberInPage * UserRow.RowSize;
                byte[] rowData         = new byte[UserRow.RowSize];
                Array.Copy(Pages[pageNumber], offset, rowData, 0, UserRow.RowSize);

                var row = UserRow.DeSerialize(rowData);

                Console.WriteLine($"{row.id}\t{row.username}\t{row.email}");
                result[i] = row;
            }
            return(result);
        }
Exemplo n.º 5
0
        public static void Insert(UserRow row)
        {
            if (MaxRowNumber >= TableMaxRows)
            {
                Console.WriteLine("Table Full");
                return;
            }

            var bytes           = row.Serialize();
            var pageNumber      = MaxRowNumber / UserRow.RowPerPage;
            var rowNumberInPage = MaxRowNumber % UserRow.RowPerPage;
            var offset          = rowNumberInPage * UserRow.RowSize;

            if (Pages[pageNumber] == null)
            {
                Pages[pageNumber] = new byte[PageSize];
                MaxPage           = pageNumber;
            }

            Array.Copy(bytes, 0, Pages[pageNumber], offset, UserRow.RowSize);
            MaxRowNumber++;
        }