aboutsummaryrefslogtreecommitdiff
path: root/pkg/tbtables/tbrdel.x
blob: 23796f15692dff142897126bd4fe5164c5c51429 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
include <tbset.h>
include "tbtables.h"
include "tblerr.h"

# tbrdel -- delete rows
# This routine deletes a range of rows.
# NOTE:  The number of rows TB_NROWS(tp) will be modified by this routine
# if the range of rows to be deleted is within the table.  The physical
# disk space occupied by these rows will not be deallocated; the file
# will still be just as large as before.
#
# Phil Hodge, 23-Mar-1988  Subroutine created.
# Phil Hodge,  3-Apr-1995  Set TB_MODIFIED to true.
# Phil Hodge,  6-Mar-1998  Modify to allow a row selector, if the rows
#			to be deleted are at the end of the table.

procedure tbrdel (tp, firstrow, lastrow)

pointer tp		# i: pointer to table descriptor
int	firstrow	# i: first row to be deleted
int	lastrow		# i: last row to be deleted
#--
int	row1		# actual first row to be deleted
int	nrows		# number of rows written to table
int	ndel		# number of rows to be deleted
int	rst_rownum()
errchk	tbxnll, tbynll, tbznll, tbfchp, tbrsft

begin
	if (firstrow < 1 || lastrow < 1)
	    call error (1, "tbrdel:  Row number less than one is invalid.")

	if (firstrow > lastrow)
	    return

	nrows = TB_NROWS(tp)

	if (TB_ROW_SELECT(tp) == YES) {

	    if (lastrow < TB_NSEL_ROWS(tp))
		call error (1,
	"Can't delete rows in the middle of a table that uses a row selector.")

	    if (firstrow > TB_NSEL_ROWS(tp))
		return				# nothing to do

	    row1 = rst_rownum (TB_ROWSET(tp), firstrow)
	    # and row2 = nrows

	    if (nrows - row1 > TB_NSEL_ROWS(tp) - firstrow)
		call error (1,
	"tbrdel:  Range of rows to delete includes non-selected rows.")

	    ndel = nrows - row1 + 1

	    if (TB_TYPE(tp) == TBL_TYPE_S_ROW)
		call tbxnll (tp, max (1, nrows-ndel+1), nrows)
	    else if (TB_TYPE(tp) == TBL_TYPE_S_COL)
		call tbynll (tp, max (1, nrows-ndel+1), nrows)
	    else if (TB_TYPE(tp) == TBL_TYPE_TEXT)
		call tbznll (tp, max (1, nrows-ndel+1), nrows)
	    else if (TB_TYPE(tp) == TBL_TYPE_FITS)
		call tbfchp (tp, ndel)
	    else
		call error (ER_TBCORRUPTED, "tbrdel:  invalid table type")

	    TB_NROWS(tp) = max (0, nrows - ndel)

	} else {

	    ndel = min (nrows, lastrow) - firstrow + 1
	    # Shift the rows.
	    call tbrsft (tp, firstrow, -ndel)
	}

	TB_MODIFIED(tp) = true
end