delcon.c
上传用户:dgyhgb
上传日期:2007-01-07
资源大小:676k
文件大小:10k
- /*
- * delcon.c - mass deletion of rows satisfyed specific condition
- * on basis scanning of specific table
- * by itself, by specific index, by specific filter
- * Kernel of GNU SQL-server
- *
- * This file is a part of GNU SQL Server
- *
- * Copyright (c) 1996, 1997, Free Software Foundation, Inc
- * Developed at the Institute of System Programming
- * This file is written by Vera Ponomarenko
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- *
- * Contacts: gss@ispras.ru
- *
- */
- /* $Id: delcon.c,v 1.247 1997/04/28 12:15:01 vera Exp $ */
- #include "destrn.h"
- #include "strml.h"
- #include "fdcltrn.h"
- #include "xmem.h"
- extern struct des_nseg desnseg;
- extern char *pbuflj;
- extern struct ADBL adlj;
- extern i4_t ljmsize;
- static void
- mdel (struct d_r_t *dr, char *cort, u2_t corsize,
- struct des_tid *tid, struct des_tid *ref_tid)
- {
- u2_t sn;
- struct id_rel idr;
- i4_t rn;
- modmes ();
- idr.urn.segnum = sn = dr->segnr;
- idr.urn.obnum = rn = dr->desrbd.relnum;
- idr.pagenum = dr->pn_r;
- idr.index = dr->ind_r;
- wmlj (DELLJ, ljmsize + corsize, &adlj, &idr, tid, 0);
- orddel (sn, rn, tid, ref_tid, corsize);
- proind (ordindd, dr, dr->desrbd.indnum, cort, tid);
- }
- CPNM
- delcrl (struct id_rel *pidrl, u2_t slsz, char *sc)
- {
- u2_t fn, fdf, *ali, *ai, corsize;
- char *asp = NULL;
- struct des_field *df;
- u2_t sn, pn;
- struct A pg;
- char *arrpnt[BD_PAGESIZE];
- u2_t arrsz[BD_PAGESIZE];
- sn = pidrl->urn.segnum;
- if (sn != NRSNUM)
- {
- struct d_r_t *dr;
- struct d_sc_i *scind;
- struct ldesscan *disc;
- char *cort;
- struct des_tid tid, ref_tid;
- u2_t ind, sz, pnr, indr;
- i4_t rn, ans;
- i2_t n;
- CPNM cpn;
-
- if ((cpn = cont_fir (pidrl, &dr)) != OK)
- return (cpn);
- pnr = pidrl->pagenum;
- indr = pidrl->index;
- fn = dr->desrbd.fieldnum;
- fdf = dr->desrbd.fdfnum;
- df = (struct des_field *) (dr + 1);
- if (testcond (df, fn, fdf, 0, NULL, &slsz, sc, 0, NULL) != OK)
- return (NCF);
- if ((cpn = synlsc (WSC, pidrl, sc, slsz, fn, (u2_t *) NULL)) != OK)
- return (cpn);
- rn = pidrl->urn.obnum;
- cort = pbuflj + ljmsize;
- scind = rel_scan (sn, rn, (char *) dr, &n, 0, NULL, NULL, 0, 0, NULL);
- disc = &scind->dessc;
- ans = fgetnext (disc, &pn, &sz, SLOWSCAN);
- while (ans != EOI)
- {
- ind = 0;
- m1:
- while ((asp = getpg (&pg, sn, pn, 's')) == NULL);
- ai = (u2_t *) (asp + phsize);
- ali = ai + ((struct page_head *) asp)->lastin;
- for (ai += ind; ai <= ali; ai++, ind++)
- {
- if (*ai != 0 && CHECK_PG_ENTRY(ai) &&
- (corsize = fnd_slc (dr, asp, ai, sc, slsz, cort, &ref_tid)) != 0)
- {
- tid.tpn = pn;
- tid.tindex = ind;
- putpg (&pg, 'n');
- mdel (dr, cort, corsize, &tid, &ref_tid);
- BUF_endop ();
- ind++;
- goto m1;
- }
- }
- putpg (&pg, 'n');
- ans = getnext (disc, &pn, &sz, SLOWSCAN);
- }
- delscan (n);
- }
- else
- {
- struct des_trel *dtr;
-
- dtr = (struct des_trel *) * (desnseg.tobtab + pidrl->urn.obnum);
- if (dtr->tobtr.prdt.prob != TREL)
- return (NDR);
- fn = dtr->fieldn;
- fdf = dtr->fdftr;
- df = (struct des_field *) (dtr + 1);
- if (testcond (df, fn, fdf, 0, NULL, &slsz, sc, 0, NULL) != OK)
- return (NCF);
- for (pn = dtr->tobtr.firstpn; pn != (u2_t) ~ 0;)
- {
- asp = getwl (&pg, NRSNUM, pn);
- ai = (u2_t *) (asp + phtrsize);
- ali = ai + ((struct p_h_tr *) asp)->linptr;
- pn = ((struct listtob *) asp)->nextpn;
- for (; ai <= ali; ai++)
- if (*ai != 0 && (corsize = tstcsel (df, fn, fdf, slsz, sc,
- asp + *ai, arrpnt, arrsz)) != 0)
- {
- comptr (asp, ai, corsize);
- *ai = 0;
- }
- if (frptr (asp) == 1)
- frptob ((struct des_tob *) dtr, asp, pn);
- else
- putwul (&pg, 'n');
- }
- }
- return (OK);
- }
- int
- delcin (struct id_ind *pidind, u2_t slsz, char *sc, u2_t diasz, char *diasc)
- {
- u2_t fn, fdf, *ai, sn, pn, oldpn;
- char *asp = NULL, *cort;
- struct des_field *df;
- struct ldesscan *disc;
- struct d_sc_i *scind;
- struct ldesind *di;
- i4_t pr, rep;
- struct d_r_t *dr;
- struct id_rel *pidrl;
- u2_t corsize, kn, dscsz, pnr, indr;
- i4_t rn;
- i2_t n;
- i4_t ans;
- struct des_tid tid, ref_tid;
- struct A pg;
- pidrl = &pidind->irii;
- sn = pidrl->urn.segnum;
- if ((ans = cont_id (pidind, &dr, &di)) != OK)
- return (ans);
- fn = dr->desrbd.fieldnum;
- fdf = dr->desrbd.fdfnum;
- df = (struct des_field *) (dr + 1);
- if (testcond (df, fn, fdf, 0, NULL, &slsz, sc, 0, NULL) != OK)
- return (NCF);
- ai = (u2_t *) (di + 1);
- if ((ans = testdsc (dr, &diasz, diasc, ai, &dscsz)) != OK)
- return (ans);
- if ((ans = synlsc (RSC, pidrl, sc, slsz, fn, (u2_t *) NULL)) != OK)
- return (ans);
- kn = di->ldi.kifn & ~UNIQ & MSK21B;
- if ((ans = synlsc (RSC, pidrl, diasc, diasz, kn, ai)) != OK)
- return (ans);
- rn = pidrl->urn.obnum;
- pnr = pidrl->pagenum;
- indr = pidrl->index;
- cort = pbuflj + ljmsize;
- scind = (struct d_sc_i *) lusc (&n, scisize, (char *) di,
- SCI, DSC, 0, NULL, sc, slsz,
- 0, NULL, diasz + size2b);
- disc = &scind->dessc;
- disc->curlpn = (u2_t) ~ 0;
- asp = (char *) scind + scisize + slsz + size2b;
- if (diasz == 0)
- disc->dpnsc = NULL;
- else
- disc->dpnsc = asp;
- t2bpack (diasz, asp);
- disc->dpnsval = asp + size2b + dscsz;
- asp += size2b;
- bcopy (diasc, asp, diasz);
- oldpn = (u2_t) ~0;
- if ((rep = ind_ftid (disc, &tid, SLOWSCAN)) != EOI)
- {
- oldpn = tid.tpn;
- while ((asp = getpg (&pg, sn, oldpn, 's')) == NULL);
- }
- pr = 0;
- for (; rep != EOI; rep = ind_tid (disc, &tid, SLOWSCAN))
- {
- pn = tid.tpn;
- if (pr == 1)
- {
- while ((asp = getpg (&pg, sn, pn, 's')) == NULL);
- oldpn = pn;
- pr = 0;
- }
- else if (oldpn != pn)
- {
- putpg (&pg, 'n');
- while ((asp = getpg (&pg, sn, pn, 's')) == NULL);
- oldpn = pn;
- }
- ai = (u2_t *) (asp + phsize) + tid.tindex;
- if (*ai != 0 &&
- (corsize = fnd_slc (dr, asp, ai, sc, slsz, cort, &ref_tid)) != 0)
- {
- putpg (&pg, 'n');
- mdel (dr, cort, corsize, &tid, &ref_tid);
- pr = 1;
- }
- }
- delscan (n);
- return (OK);
- }
- CPNM
- delcfl (i4_t idfl, u2_t slsz, char *sc)
- {
- u2_t fn, fdf, *afi, *ai, sn, pn, off, oldpn, flpn;
- char *asp = NULL, *cort, *aspfl;
- struct d_r_t *dr;
- struct des_field *df;
- struct des_tid *tid, *tidb, ref_tid;
- struct des_fltr *desfl;
- struct id_rel idrl;
- u2_t corsize, pnr, indr;
- i4_t rn;
- CPNM cpn;
- struct A pg, inflpg;
- if ((u2_t) idfl > desnseg.mtobnum)
- return (NIOB);
- desfl = (struct des_fltr *) * (desnseg.tobtab + idfl);
- if (desfl == NULL)
- return (NIOB);
- if (((struct prtob *) desfl)->prob != FLTR)
- return (NIOB);
- dr = desfl->pdrtf;
- fn = dr->desrbd.fieldnum;
- fdf = dr->desrbd.fdfnum;
- df = (struct des_field *) (dr + 1);
- if (testcond (df, fn, fdf, 0, NULL, &slsz, sc, 0, NULL) != OK)
- return (NCF);
- sn = dr->segnr;
- rn = dr->desrbd.relnum;
- pnr = dr->pn_r;
- indr = dr->ind_r;
- idrl.urn.segnum = sn;
- idrl.urn.obnum = rn;
- idrl.pagenum = pnr;
- idrl.index = indr;
- if ((cpn = synlsc (WSC, &idrl, sc, slsz, fn, (u2_t *) NULL)) != OK)
- return (cpn);
- cort = pbuflj + ljmsize;
- for (flpn = desfl->tobfl.firstpn; flpn != (u2_t) ~ 0;)
- {
- aspfl = getwl (&inflpg, NRSNUM, flpn);
- off = ((struct p_h_f *) aspfl)->freeoff;
- tid = (struct des_tid *) (aspfl + phfsize);
- oldpn = tid->tpn;
- while ((asp = getpg (&pg, sn, oldpn, 's')) == NULL);
- afi = (u2_t *) (asp + phsize);
- tidb = (struct des_tid *) (aspfl + off);
- for (; tid < tidb; tid++)
- {
- pn = tid->tpn;
- if (oldpn != pn)
- {
- putpg (&pg, 'n');
- while ((asp = getpg (&pg, sn, pn, 's')) == NULL);
- afi = (u2_t *) (asp + phsize);
- oldpn = pn;
- }
- ai = afi + tid->tindex;
- if (*ai != 0 &&
- (corsize = fnd_slc (dr, asp, ai, sc, slsz, cort, &ref_tid)) != 0)
- {
- putpg (&pg, 'n');
- mdel (dr, cort, corsize, tid, &ref_tid);
- }
- }
- flpn = ((struct p_h_f *) aspfl)->listfl.nextpn;
- putwul (&inflpg, 'n');
- }
- putpg (&pg, 'n');
- return (OK);
- }
- int
- fnd_slc (struct d_r_t *dr, char *asp, u2_t * ai, char *selcon,
- u2_t slsz, char *cort, struct des_tid *ref_tid)
- {
- char *tuple;
- unsigned char t;
- struct des_field *df;
- struct A inpage;
- u2_t fn, fdf;
- char *arrpnt[BD_PAGESIZE];
- u2_t arrsz[BD_PAGESIZE];
- int tuple_size;
- tuple = asp + *ai;
- t = *tuple & MSKCORT;
- if (t == CREM || t == IDTR)
- return (NCR);
- if (t == IND)
- {
- u2_t pn2, ind2;
- ref_tid->tindex = ind2 = t2bunpack (tuple + 1);
- ref_tid->tpn = pn2 = t2bunpack (tuple + 1 + size2b);
- while ((asp = getpg (&inpage, dr->segnr, pn2, 's')) == NULL);
- ai = (u2_t *) (asp + phsize) + ind2;
- tuple = asp + *ai;
- }
- else
- ref_tid->tpn = (u2_t) ~ 0;
- fn = dr->desrbd.fieldnum;
- fdf = dr->desrbd.fdfnum;
- df = (struct des_field *) (dr + 1);
- tuple_size = tstcsel (df, fn, fdf, slsz, selcon, tuple, arrpnt, arrsz);
- if (tuple_size != 0 && cort != NULL)
- bcopy (tuple, cort, tuple_size);
- if (t == IND)
- putpg (&inpage, 'n');
- return (tuple_size);
- }