]> andersk Git - moira.git/blobdiff - regtape/students.dc
ifdef on GDSS (for sis)
[moira.git] / regtape / students.dc
index c015280f93569d1766475e246d0b006791124273..80462c781370628ca552c4ddd08fbcea566aad11 100644 (file)
@@ -77,6 +77,9 @@ struct entry {
 char *whoami;
 int newfinger = 0;
 
+#define SQL_DUPLICATE -40100
+#define SQL_DEADLOCK -49900
+#define sqlfail() (sqlca.sqlcode && sqlca.sqlcode != 100)
 
 main(argc, argv)
 int argc;
@@ -116,10 +119,24 @@ char **argv;
     setlinebuf(stderr);
 
     EXEC SQL CONNECT moira;
+    if (sqlca.sqlcode != 0) {
+       com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
+       exit(1);
+    }
 
     while (e = get_next_entry(in)) {
+    again:
        process_entry(e);
-       EXEC SQL COMMIT;
+       EXEC SQL COMMIT WORK;
+       if (sqlca.sqlcode != 0) {
+           if (sqlca.sqlcode == SQL_DEADLOCK) {
+               com_err(whoami, MR_DEADLOCK, "commiting work");
+               goto again;
+           } else {
+               com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
+               exit(1);
+           }
+       }
        if (wait) {
            printf("Next");
            fflush(stdout);
@@ -237,33 +254,56 @@ struct entry *e;
     int changed, nochange, encrypted;
     char buf[BUFSIZ], *from, *to;
     EXEC SQL BEGIN DECLARE SECTION;
-    char *first, *last, *eid, *title, *sid, *name, *rname, *rdept, *rtitle;
-    char *rophone, *rhphone;
+    char *first, *last, *middle, *eid, *title, *sid, *name, *rname, *rdept, *rtitle;
+    char *rophone, *rhphone, *prog;
     char class[9], haddr[128], hphone[33], ophone[33], dept[33], raddr[128];
-    int id, status;
+    char dfirst[17], dlast[17], dmiddle[17];
+    int id, status, who;
     EXEC SQL END DECLARE SECTION;
 
+    who = WHO;
+    prog = PROG;
     first = e->first;
     if (strlen(first) > 16)
       first[16] = 0;
     last = e->last;
     if (strlen(last) > 16)
       last[16] = 0;
+    middle = e->middle;
     eid = e->eid;
     sid = e->id;
     id = 0;
     encrypted = 0;
 
-/* Get user info */
-    EXEC SQL SELECT users_id, type, home_addr, home_phone, office_phone, status, department
-      INTO :id, :class, :haddr, :hphone, :ophone, :status, :dept
+    /* Get user info */
+    EXEC SQL REPEATED SELECT users_id, first, last, middle, type, home_addr, home_phone, office_phone, status, department
+      INTO :id, :dfirst, :dlast, :dmiddle, :class, :haddr, :hphone, :ophone, :status, :dept
       FROM users
-      WHERE last = :last and first = :first and clearid = :sid;
+      WHERE clearid = :sid;
+    if (sqlfail()) {
+       if (sqlca.sqlcode == SQL_DUPLICATE) {
+           com_err(whoami, 0, "duplicate ID number %s on user %s %s", sid, first, last);
+           return;
+       } else if (sqlca.sqlcode == SQL_DEADLOCK) {
+           com_err(whoami, MR_DEADLOCK, "looking up user %s", sid);
+           EXEC SQL ROLLBACK;
+           return process_entry(e);
+       } else
+         sqlexit();
+    }
     if (id == 0) {
-       EXEC SQL SELECT users_id, type, home_addr, home_phone, office_phone, status, department
-         INTO :id, :class, :haddr, :hphone, :ophone, :status, :dept
+       EXEC SQL REPEATED SELECT users_id, first, last, middle, type, home_addr, home_phone, office_phone, status, department
+         INTO :id, :dfirst, :dlast, :dmiddle, :class, :haddr, :hphone, :ophone, :status, :dept
          FROM users
          WHERE last = :last and first = :first and clearid = :eid;
+       if (sqlfail()) {
+           if (sqlca.sqlcode == SQL_DEADLOCK) {
+               com_err(whoami, MR_DEADLOCK, "looking up user %s", sid);
+               EXEC SQL ROLLBACK;
+               return process_entry(e);
+           } else if (sqlca.sqlcode != SQL_DUPLICATE)
+             sqlexit();
+       }
        encrypted++;
        if (id == 0) {
            newuser(e);
@@ -271,10 +311,10 @@ struct entry *e;
        }
     }
 
-/* See if class changed: if it's different, and the value in the database
- * is not STAFF or SIPB, then update the database.  Since they were on the
- * students tape, make the account usable.
- */
+    /* See if class changed: if it's different, and the value in the database
    * is not STAFF or SIPB, then update the database.  Since they were on the
    * students tape, make the account usable.
    */
     if (strcmp(e->class, strtrim(class)) &&
        strcmp(class, "STAFF") && strcmp(class, "SIPB")) {
        com_err(whoami, 0, "updating class for user %s %s from %s to %s",
@@ -282,15 +322,34 @@ struct entry *e;
        if (status == US_NOT_ALLOWED) status = US_NO_LOGIN_YET;
        if (status == US_ENROLL_NOT_ALLOWED) status = US_ENROLLED;
        strcpy(class, e->class);
-       EXEC SQL UPDATE users
+       EXEC SQL REPEATED UPDATE users
          SET type = :class, status = :status, modtime = 'now',
-           modby = WHO, modwith = PROG
+           modby = :who, modwith = :prog
          WHERE users_id = :id;   
+       if (sqlca.sqlcode != 0) {
+           com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
+           exit(1);
+       }
     }
 
-    /* Deal with updating the finger info if necessary */
+    /* Update name if necessary */
+    if (strcmp(first, strtrim(dfirst)) ||
+       strcmp(last, strtrim(dlast)) ||
+       strcmp(middle, strtrim(dmiddle))) {
+       com_err(whoami, 0, "updating real name for %s %s", first, last);
+       EXEC SQL UPDATE users
+         SET first = :first, last = :last, middle = :middle,
+               modby = :who, modwith = :prog, modtime = 'now'
+         WHERE users_id = :id;
+       if (sqlca.sqlcode != 0) {
+           com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
+           exit(1);
+       }
+    }
 
+    /* Deal with updating the finger info if necessary */
     changed = nochange = 0;
+    if (encrypted) changed++;
     strcpy(buf, e->address);
     if (*e->dorm) {
        strcat(buf, " ");
@@ -411,17 +470,37 @@ struct entry *e;
        EXEC SQL REPEATED UPDATE users
          SET home_addr = :haddr, home_phone = :hphone,
            office_phone = :ophone, department = :dept,
-           fmodtime = 'now', fmodby = WHO, fmodwith = PROG,
+           fmodtime = 'now', fmodby = :who, fmodwith = :prog,
            xname = :name, xdept = :rdept, xtitle = :rtitle,
            xaddress = :raddr, xphone1 = :rhphone, xphone2 = :rophone,
            xmodtime = date('now'), clearid = :sid
          WHERE users_id = :id;
+       if (sqlca.sqlcode != 0) {
+           if (sqlca.sqlcode == SQL_DEADLOCK) {
+               com_err(whoami, MR_DEADLOCK, "updating user %s", sid);
+               EXEC SQL ROLLBACK;
+               return process_entry(e);
+           } else {
+               com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
+               exit(1);
+           }
+       }
     }  else {
        EXEC SQL REPEATED UPDATE users
          SET xname = :name, xdept = :rdept, xtitle = :rtitle,
            xaddress = :raddr, xphone1 = :rhphone, xphone2 = :rophone,
            xmodtime = date('now'), clearid = :sid
          WHERE users_id = :id;
+       if (sqlca.sqlcode != 0) {
+           if (sqlca.sqlcode == SQL_DEADLOCK) {
+               com_err(whoami, MR_DEADLOCK, "updating user %s", sid);
+               EXEC SQL ROLLBACK;
+               return process_entry(e);
+           } else {
+               com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
+               exit(1);
+           }
+       }
     }
 }
 
@@ -472,8 +551,8 @@ struct entry *e;
     strncpy(ophone, buf, 12);
     strncpy(dept, e->course, 12);
     
-    id = set_next_object_id("users_id", 0);
-    uid = set_next_object_id("uid", 1);
+    id = set_next_users_id(0);
+    uid = set_next_uid(1);
     sprintf(login, "#%d", uid);
     last = e->last;
     first = e->first;
@@ -511,7 +590,7 @@ struct entry *e;
        FixCase(raddr);
        strcat(raddr, "MIT INTERDEPARTMENTAL MAIL");
     }
-    EXEC SQL INSERT INTO users
+    EXEC SQL REPEATED INSERT INTO users
       (login, users_id, uid, shell, last, first, middle, status,
        clearid, type, modtime, modby, modwith, fullname, home_addr,
        home_phone, office_phone, department, fmodtime, fmodby, fmodwith,
@@ -520,21 +599,31 @@ struct entry *e;
              :sid, :class, 'now', :who, :prog, :fullname, :haddr, :hphone,
              :ophone, :dept, 'now', :who, :prog, 'NONE', :name, :dept,
              :title, :raddr, :hphone, :ophone, date('now'));
-    com_err(whoami, 0, "adding user %s %s", e->first, e->last);
+    if (sqlca.sqlcode != 0) {
+       if (sqlca.sqlcode == SQL_DEADLOCK) {
+           com_err(whoami, MR_DEADLOCK, "adding user %s", sid);
+           EXEC SQL ROLLBACK;
+           return newuser(e);
+       } else {
+           com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
+           exit(1);
+       }
+    } else
+      com_err(whoami, 0, "adding user %s %s", e->first, e->last);
 }
 
 
-set_next_object_id(object, limit)
-    char *object;
+
+set_next_users_id(limit)
     int limit;
 {
     EXEC SQL BEGIN DECLARE SECTION;
-    char *name;
-    int rowcount, flag, value;
+    int rowcount, flag, value, retval;
     EXEC SQL END DECLARE SECTION;
 
-    name = object;
-    EXEC SQL SELECT value INTO :value FROM numvalues WHERE name = :name;
+    EXEC SQL REPEATED SELECT value INTO :value FROM numvalues 
+      WHERE name = 'users_id';
+    if (sqlfail()) sqlexit();
     if (sqlca.sqlerrd[2] != 1) {
        EXEC SQL ROLLBACK;
        com_err(whoami, MR_INTERNAL, "values table inconsistancy");
@@ -542,7 +631,9 @@ set_next_object_id(object, limit)
     }
 
     flag = 0;
-    EXEC SQL SELECT :name INTO :flag FROM users WHERE :name = :value;
+    EXEC SQL REPEATED SELECT users_id INTO :flag FROM users
+      WHERE users_id = :value;
+    if (sqlfail()) sqlexit();
     if (sqlca.sqlerrd[2] == 0)
       flag = 0;
     while (flag) {
@@ -550,14 +641,76 @@ set_next_object_id(object, limit)
        if (limit && value > MAX_ID_VALUE)
            value = MIN_ID_VALUE;
        flag = 0;
-       EXEC SQL SELECT :name INTO :flag FROM users WHERE :name = :value;
+       EXEC SQL REPEATED SELECT users_id INTO :flag FROM users 
+         WHERE users_id = :value;
+       if (sqlfail()) sqlexit();
        if (sqlca.sqlerrd[2] == 0)
          flag = 0;
     }
 
-    value++;
+    retval = value++;
     if (limit && value > MAX_ID_VALUE)
       value = MIN_ID_VALUE;
-    EXEC SQL REPEATED UPDATE numvalues SET value = :value WHERE name = :name;
-    return(value);
+    EXEC SQL REPEATED UPDATE numvalues SET value = :value
+      WHERE name = 'users_id';
+    if (sqlca.sqlcode != 0) {
+       com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
+       exit(1);
+    }
+    return(retval);
+}
+
+set_next_uid(limit)
+    int limit;
+{
+    EXEC SQL BEGIN DECLARE SECTION;
+    int rowcount, flag, value, retval;
+    EXEC SQL END DECLARE SECTION;
+
+    EXEC SQL REPEATED SELECT value INTO :value FROM numvalues 
+      WHERE name = 'uid';
+    if (sqlfail()) sqlexit();
+    if (sqlca.sqlerrd[2] != 1) {
+       EXEC SQL ROLLBACK;
+       com_err(whoami, MR_INTERNAL, "values table inconsistancy");
+       exit(1);
+    }
+
+    flag = 0;
+    EXEC SQL REPEATED SELECT uid INTO :flag FROM users WHERE uid = :value;
+    if (sqlfail()) sqlexit();
+    if (sqlca.sqlerrd[2] == 0)
+      flag = 0;
+    while (flag) {
+       value++;
+       if (limit && value > MAX_ID_VALUE)
+           value = MIN_ID_VALUE;
+       flag = 0;
+       EXEC SQL REPEATED SELECT uid INTO :flag FROM users WHERE uid = :value;
+       if (sqlfail()) sqlexit();
+       if (sqlca.sqlerrd[2] == 0)
+         flag = 0;
+    }
+
+    retval = value++;
+    if (limit && value > MAX_ID_VALUE)
+      value = MIN_ID_VALUE;
+    EXEC SQL REPEATED UPDATE numvalues SET value = :value WHERE name = 'uid';
+    if (sqlca.sqlcode != 0) {
+       com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
+       exit(1);
+    }
+    return(retval);
+}
+
+
+sqlexit()
+{
+    if (sqlca.sqlcode == SQL_DEADLOCK)
+      com_err(whoami, MR_DEADLOCK, "unrecoverable ingres error %d",
+             sqlca.sqlcode);
+    else
+      com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
+    EXEC SQL ROLLBACK WORK;
+    exit(1);
 }
This page took 0.047903 seconds and 4 git commands to generate.