userTransaction.c
上传用户:romrleung
上传日期:2022-05-23
资源大小:18897k
文件大小:18k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /* Copyright (C) 2003 MySQL AB
  2.    This program is free software; you can redistribute it and/or modify
  3.    it under the terms of the GNU General Public License as published by
  4.    the Free Software Foundation; either version 2 of the License, or
  5.    (at your option) any later version.
  6.    This program is distributed in the hope that it will be useful,
  7.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  8.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  9.    GNU General Public License for more details.
  10.    You should have received a copy of the GNU General Public License
  11.    along with this program; if not, write to the Free Software
  12.    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
  13. /***************************************************************
  14. * I N C L U D E D   F I L E S                                  *
  15. ***************************************************************/
  16. #include <ndb_global.h>
  17. #include <time.h>
  18. #include "sql.h"
  19. #include "sqlext.h"
  20. #include "userInterface.h"
  21. #include "userHandle.h"
  22. /***************************************************************
  23. * L O C A L   C O N S T A N T S                                *
  24. ***************************************************************/
  25. /***************************************************************
  26. * L O C A L   D A T A   S T R U C T U R E S                    *
  27. ***************************************************************/
  28. /***************************************************************
  29. * L O C A L   F U N C T I O N S                                *
  30. ***************************************************************/
  31. static int readSubscriberSessions(UserHandle        *uh,
  32.                                   SubscriberNumber number,
  33.                                   char            *transactionType);
  34. /***************************************************************
  35. * L O C A L   D A T A                                          *
  36. ***************************************************************/
  37. extern void handle_error(SQLHDBC  hdbc,
  38.                   SQLHENV  henv, 
  39.                   SQLHSTMT hstmt, 
  40.                   SQLRETURN rc,
  41.                   char *filename,
  42.                   int lineno);
  43. /***************************************************************
  44. * P U B L I C   D A T A                                        *
  45. ***************************************************************/
  46. /***************************************************************
  47. ****************************************************************
  48. * L O C A L   F U N C T I O N S   C O D E   S E C T I O N      *
  49. ****************************************************************
  50. ***************************************************************/
  51. static int readSubscriberSessions(UserHandle      *uh,
  52.                                   SubscriberNumber number,
  53.                                   char            *transactionType)
  54. {
  55.    SQLRETURN rc;
  56.    /*-----------------------------------------------------*/
  57.    /* SELECT activeSessions,groupId,changedBy,changedTime */
  58.    /* FROM SUBSCRIBER                                     */
  59.    /* WHERE subscriberNumber=x;                           */
  60.    /*-----------------------------------------------------*/
  61.    strcpy(uh->readSubscriberSession.values.number,number);
  62.    rc = SQLExecute(uh->readSubscriberSession.stmt);
  63.    if( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  64.       printf("%s %sn",
  65.              transactionType, 
  66.              "Unable to execute read subscriber session");
  67.       return(-1);
  68.    }
  69.    rc = SQLFetch(uh->readSubscriberSession.stmt);
  70.    if( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  71.       printf("%s %sn",
  72.              transactionType, 
  73.              "Unable to fetch read subscriber session");
  74.       return(-1);
  75.    }
  76.    return(0);
  77. }
  78. /***************************************************************
  79. ****************************************************************
  80. * P U B L I C   F U N C T I O N S   C O D E   S E C T I O N    *
  81. ****************************************************************
  82. ***************************************************************/
  83. void userTransaction_T1(UserHandle        *uh,
  84.                         SubscriberNumber number,
  85.                         Location         new_location,
  86.                         ChangedBy        changed_by,
  87.                         ChangedTime      changed_time)
  88. {
  89.    SQLRETURN rc;
  90.    if(!uh) return;
  91.    /*---------------------------------------------*/
  92.    /* Update the subscriber information           */
  93.    /*                                             */
  94.    /* UPDATE SUBSCRIBER                           */
  95.    /* SET location=x, changedBy=x, changedTime=x  */
  96.    /* WHERE subscriberNumber=x;                   */             
  97.    /*---------------------------------------------*/
  98.    strcpy(uh->updateSubscriber.values.number, number);
  99.    uh->updateSubscriber.values.location = new_location;
  100.    strcpy(uh->updateSubscriber.values.changedBy, changed_by);
  101.    strcpy(uh->updateSubscriber.values.changedTime, changed_time);
  102.    rc = SQLExecute(uh->updateSubscriber.stmt);
  103.    if( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  104.       printf("T1 Unable to execute update subscribern");
  105.       return;
  106.    }
  107.    userDbCommit(uh);
  108. }
  109. void userTransaction_T2(UserHandle        *uh,
  110.                         SubscriberNumber  number,
  111.                         Location         *new_location,
  112.                         ChangedBy         changed_by,
  113.                         ChangedTime       changed_time,
  114.                         SubscriberName    subscriberName)
  115. {
  116.    SQLRETURN rc;
  117.    if(!uh) return;
  118.    /*------------------------------------------------------*/
  119.    /* Read the information from the subscriber table       */
  120.    /*                                                      */
  121.    /* SELECT location,subscriberName,changedBy,changedTime */
  122.    /* FROM SUBSCRIBER                                      */
  123.    /* WHERE subscriberNumber=x;                            */
  124.    /*------------------------------------------------------*/
  125.    strcpy(uh->readSubscriber.values.number,number);
  126.    rc = SQLExecute(uh->readSubscriber.stmt);
  127.    if( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  128.       printf("T2 Unable to execute read subscribern");
  129.       return;
  130.    }
  131.    rc = SQLFetch(uh->readSubscriber.stmt);
  132.    if( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  133.       printf("T2 Unable to fetch read subscribern");
  134.       return;
  135.    }
  136.    userDbCommit(uh);
  137.    strcpy(subscriberName, uh->readSubscriber.values.name);
  138.    *new_location = uh->readSubscriber.values.location;
  139.    strcpy(changed_by, uh->readSubscriber.values.changedBy);
  140.    strcpy(changed_time, uh->readSubscriber.values.changedTime);
  141. }
  142. void userTransaction_T3(UserHandle        *uh,
  143.                         SubscriberNumber number,
  144.                         ServerId         server_id,
  145.                         ServerBit        server_bit,
  146.                         SessionDetails   session_details,
  147.                         unsigned int    *branch_executed)
  148. {
  149.    SQLRETURN rc;
  150.    if(!uh) return;
  151.    *branch_executed = 0;
  152.    /*--------------------------------------*/
  153.    /* Read active sessions from subscriber */
  154.    /*--------------------------------------*/
  155.    if( readSubscriberSessions(uh, number, "T3") < 0 )
  156.       return;
  157.    /*-----------------------------------------------*/
  158.    /* Read the 'read' Permissions for the userGroup */
  159.    /*                                               */
  160.    /* SELECT allowRead                              */
  161.    /* FROM USERGROUP                                */
  162.    /* WHERE groupId=x                               */
  163.    /*-----------------------------------------------*/
  164.    uh->readGroupAllowRead.values.groupId = uh->readSubscriberSession.values.groupId;
  165.    rc = SQLExecute(uh->readGroupAllowRead.stmt);
  166.    if( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  167.       printf("T3 Unable to execute read group allow readn");
  168.       return;
  169.    }
  170.    rc = SQLFetch(uh->readGroupAllowRead.stmt);
  171.    if( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  172.       printf("T3 Unable to fetch read group allow readn");
  173.       return;
  174.    }
  175.    if( uh->readGroupAllowRead.values.allowRead & server_bit &&
  176.        uh->readSubscriberSession.values.activeSessions & server_bit ) {
  177.       /*----------------------------------------------------*/
  178.       /* Read the sessionDetails from the userSession table */
  179.       /*                                                    */
  180.       /* SELECT sessionData                                 */
  181.       /* FROM userSession                                   */
  182.       /* WHERE subscriberNumber=x, serverId=x               */
  183.       /*----------------------------------------------------*/
  184.       strcpy(uh->readSessionDetails.values.number,number);
  185.       uh->readSessionDetails.values.serverId = server_id;
  186.       rc = SQLExecute(uh->readSessionDetails.stmt);
  187.       if( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  188.          printf("T3 Unable to execute read session detailsn");
  189.          return;
  190.       }
  191.       rc = SQLFetch(uh->readSessionDetails.stmt);
  192.       if( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  193.          printf("T3 Unable to fetch read session detailsn");
  194.          return;
  195.       }
  196.       strcpy(session_details, uh->readSessionDetails.values.details);
  197.       /*----------------------------------------*/
  198.       /* Increment noOfRead field in the server */
  199.       /*                                        */
  200.       /* UPDATE server                          */
  201.       /* SET noOfRead=noOfRead+1                */
  202.       /* WHERE serverId=x,subscriberSuffix=x    */
  203.       /*----------------------------------------*/
  204.       uh->updateServerNoOfRead.values.serverId = server_id;
  205.       strcpy(uh->updateServerNoOfRead.values.suffix, 
  206.              &number[SUBSCRIBER_NUMBER_LENGTH-SUBSCRIBER_NUMBER_SUFFIX_LENGTH]);
  207.       rc = SQLExecute(uh->updateServerNoOfRead.stmt);
  208.       if( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  209.          printf("T3 Unable to execute read no of readn");
  210.          return;
  211.       }
  212.       *branch_executed = 1;
  213.    }
  214.    userDbCommit(uh);
  215. }
  216. void userTransaction_T4(UserHandle        *uh,
  217.                         SubscriberNumber number,
  218.                         ServerId         server_id,
  219.                         ServerBit        server_bit,
  220.                         SessionDetails   session_details,
  221.                         unsigned int     do_rollback,
  222.                         unsigned int    *branch_executed)
  223. {
  224.    SQLRETURN rc;
  225.    if(!uh) return;
  226.    *branch_executed = 0;
  227.    /*--------------------------------------*/
  228.    /* Read active sessions from subscriber */
  229.    /*--------------------------------------*/
  230.    if( readSubscriberSessions(uh, number, "T4") < 0 )
  231.       return;
  232.    /*-------------------------------------------------*/
  233.    /* Read the 'insert' Permissions for the userGroup */
  234.    /*                                                 */
  235.    /* SELECT allowInsert                              */
  236.    /* FROM USERGROUP                                  */
  237.    /* WHERE groupId=x                                 */
  238.    /*-------------------------------------------------*/
  239.    uh->readGroupAllowInsert.values.groupId = uh->readSubscriberSession.values.groupId;
  240.    rc = SQLExecute(uh->readGroupAllowInsert.stmt);
  241.    if( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  242.       printf("T4 Unable to execute read group allow insertn");
  243.       return;
  244.    }
  245.    rc = SQLFetch(uh->readGroupAllowInsert.stmt);
  246.    if( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  247.       printf("T4 Unable to fetch read group allow insertn");
  248.       return;
  249.    }
  250.    if( uh->readGroupAllowInsert.values.allowInsert & server_bit &&
  251.        !(uh->readSubscriberSession.values.activeSessions & server_bit) ) {
  252.       /*---------------------------------------------*/
  253.       /* Insert the session to the userSession table */
  254.       /*                                             */
  255.       /* INSERT INTO userSession                     */
  256.       /* VALUES (x,x,x)                              */
  257.       /*---------------------------------------------*/
  258.       strcpy(uh->insertSession.values.number, number);
  259.       uh->insertSession.values.serverId = server_id;
  260.       strcpy(uh->insertSession.values.details, session_details);
  261.       rc = SQLExecute(uh->insertSession.stmt);
  262.       if( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  263. handle_error(uh->hdbc, uh->henv, uh->insertSession.stmt, rc, __FILE__, __LINE__);
  264.          printf("T4 Unable to execute insert session n");
  265.          return;
  266.       }
  267.       /*----------------------------------------*/
  268.       /* Update subscriber activeSessions field */
  269.       /*                                        */
  270.       /* UPDATE subscriber                      */
  271.       /* SET activeSessions=x                   */
  272.       /* WHERE subscriberNumber=x               */
  273.       /*----------------------------------------*/
  274.       strcpy(uh->updateSubscriberSession.values.number, number);
  275.       uh->updateSubscriberSession.values.activeSessions = 
  276.          uh->readSubscriberSession.values.activeSessions | server_bit;
  277.       rc = SQLExecute(uh->updateSubscriberSession.stmt);
  278.       if( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  279.          printf("T4 Unable to execute update session n");
  280.          return;
  281.       }
  282.       /*------------------------------------------*/
  283.       /* Increment noOfInsert field in the server */
  284.       /*                                          */
  285.       /* UPDATE server                            */
  286.       /* SET noOfInsert=noOfInsert+1              */
  287.       /* WHERE serverId=x,subscriberSuffix=x      */
  288.       /*------------------------------------------*/
  289.       uh->updateServerNoOfInsert.values.serverId = server_id;
  290.       strcpy(uh->updateServerNoOfInsert.values.suffix, 
  291.              &number[SUBSCRIBER_NUMBER_LENGTH-SUBSCRIBER_NUMBER_SUFFIX_LENGTH]);
  292.       rc = SQLExecute(uh->updateServerNoOfInsert.stmt);
  293.       if( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  294.          printf("T4 Unable to execute update no of readn");
  295.          return;
  296.       }
  297.       *branch_executed = 1;
  298.    }
  299.    if(do_rollback)
  300.       userDbRollback(uh);
  301.    else
  302.       userDbCommit(uh);
  303. }
  304. void userTransaction_T5(UserHandle        *uh,
  305.                         SubscriberNumber number,
  306.                         ServerId         server_id,
  307.                         ServerBit        server_bit,
  308.                         unsigned int     do_rollback,
  309.                         unsigned int    *branch_executed)
  310. {
  311.    SQLRETURN rc;
  312.    if(!uh) return;
  313.    *branch_executed = 0;
  314.    /*--------------------------------------*/
  315.    /* Read active sessions from subscriber */
  316.    /*--------------------------------------*/
  317.    if( readSubscriberSessions(uh, number, "T5") < 0 )
  318.       return;
  319.    /*-------------------------------------------------*/
  320.    /* Read the 'delete' Permissions for the userGroup */
  321.    /*                                                 */
  322.    /* SELECT allowDelete                              */
  323.    /* FROM USERGROUP                                  */
  324.    /* WHERE groupId=x                                 */
  325.    /*-------------------------------------------------*/
  326.    uh->readGroupAllowDelete.values.groupId = uh->readSubscriberSession.values.groupId;
  327.    rc = SQLExecute(uh->readGroupAllowDelete.stmt);
  328.    if( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  329.       printf("T5 Unable to execute read group allow deleten");
  330.       return;
  331.    }
  332.    rc = SQLFetch(uh->readGroupAllowDelete.stmt);
  333.    if( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  334.       printf("T5 Unable to fetch read group allow deleten");
  335.       return;
  336.    }
  337.    if( uh->readGroupAllowDelete.values.allowDelete & server_bit &&
  338.        uh->readSubscriberSession.values.activeSessions & server_bit ) {
  339.       /*-----------------------------------------------*/
  340.       /* Delete the session from the userSession table */
  341.       /*                                               */
  342.       /* DELETE FROM userSession                       */
  343.       /* WHERE subscriberNumber=x,serverId=x           */
  344.       /*-----------------------------------------------*/
  345.       strcpy(uh->deleteSession.values.number,number);
  346.       uh->deleteSession.values.serverId = server_id;
  347.       rc = SQLExecute(uh->deleteSession.stmt);
  348.       if( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  349.          printf("T5 Unable to execute delete sessionn");
  350.          return;
  351.       }
  352.       /*----------------------------------------*/
  353.       /* Update subscriber activeSessions field */
  354.       /*                                        */
  355.       /* UPDATE subscriber                      */
  356.       /* SET activeSessions=x                   */
  357.       /* WHERE subscriberNumber=x               */
  358.       /*----------------------------------------*/
  359.       strcpy(uh->updateSubscriberSession.values.number, number);
  360.       uh->updateSubscriberSession.values.activeSessions = 
  361.          uh->readSubscriberSession.values.activeSessions & ~server_bit;
  362.       rc = SQLExecute(uh->updateSubscriberSession.stmt);
  363.       if( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  364.          printf("T5 Unable to execute update subscriber session n");
  365.          return;
  366.       }
  367.       /*------------------------------------------*/
  368.       /* Increment noOfDelete field in the server */
  369.       /*                                          */
  370.       /* UPDATE server                            */
  371.       /* SET noOfDelete=noOfDelete+1              */
  372.       /* WHERE serverId=x,subscriberSuffix=x      */
  373.       /*------------------------------------------*/
  374.       uh->updateServerNoOfDelete.values.serverId = server_id;
  375.       strcpy(uh->updateServerNoOfDelete.values.suffix, 
  376.              &number[SUBSCRIBER_NUMBER_LENGTH-SUBSCRIBER_NUMBER_SUFFIX_LENGTH]);
  377.       rc = SQLExecute(uh->updateServerNoOfDelete.stmt);
  378.       if( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  379.          printf("T5 Unable to execute update no of deleten");
  380.          return;
  381.       }
  382.       *branch_executed = 1;
  383.    }
  384.    if(do_rollback)
  385.       userDbRollback(uh);
  386.    else
  387.       userDbCommit(uh);
  388. }