示例#1
0
// This method is called when a message from the master executor
// arrives informing the compiler to establish a new user
// identity. The return value is a SQLCODE. When a value other than
// zero is returned, error information is found in CmpCommon::diags().
// 
// The method performs the following steps
// 1. The method is a no-op if the new user ID is the same as the
//    current user ID
// 2. Call CLI with the new integer user ID and username. This establishes 
//    the new user identity.
// 3. Call a helper method that will retrieve the current user ID and
//    user name from CLI and store copies of those values in data 
//    members.
Lng32 CmpSqlSession::setDatabaseUser(Int32 userID, const char *userName)
{

  NABoolean doDebug = FALSE;
#if defined(NA_DEBUG_C_RUNTIME)
  doDebug = (getenv("DBUSER_DEBUG") ? TRUE : FALSE);
  if (doDebug)
  {
    printf("[DBUSER:%d] BEGIN CmpSqlSession::setDatabaseUser\n",
           (int) getpid());
    printf("[DBUSER:%d]   Current user ID %d, new user ID %d\n",
           (int) getpid(), (int) databaseUserID_, (int) userID);
  }
#endif

  // 1. The method is a no-op if the new user ID is the same as the
  //    current user ID.  This assumes that if the user ID match so
  //    do the usernames.
  Int32 currentUserAsInt = (Int32) databaseUserID_;
  if (currentUserAsInt == userID)
  {
    if (doDebug)
      printf("[DBUSER:%d] END CmpSqlSession::setDatabaseUser\n",
             (int) getpid());
    return 0;
  }

  Lng32 sqlcode = 0;

  // 2. Call CLI with the new integer user identity
  sqlcode = SQL_EXEC_SetSessionAttr_Internal(SESSION_DATABASE_USER,
                                             userID, (char *)userName);
  if (sqlcode != 0)
  {
    SQL_EXEC_MergeDiagnostics_Internal(*CmpCommon::diags());
    SQL_EXEC_ClearDiagnostics(NULL);
  }

  if (doDebug)
    printf("[DBUSER:%d]   SQL_EXEC_SetSessionAttr returned %d\n",
           (int) getpid(), (int) sqlcode);
  
  // 3. Call a helper method that will retrieve the current user ID and
  //    user name from CLI and store copies
  if (sqlcode >= 0)
    sqlcode = getUserInfoFromCLI();

  if (doDebug)
    printf("[DBUSER:%d] END CmpSqlSession::setDatabaseUserID\n",
           (int) getpid());

  return sqlcode;

}
// If a user name was specified on the command line, call CLI to set
// the database user identity.
void SqlciEnv::setUserIdentityInCLI()
{
  if (userNameFromCommandLine_.length() > 0)
  {
    // Within this function we do not want CLI errors to be
    // fatal. Setting specialError_ to 0 ensures they are not. We also
    // save the current value of specialError_ and restore if after
    // the CLI calls.
    Lng32 previousSpecialError = specialError_;
    specialError_ = 0;

    HandleCLIErrorInit();

    Lng32 sqlcode =
      SQL_EXEC_SetSessionAttr_Internal(SESSION_DATABASE_USER_NAME,
                                       0,
                                       (char *) userNameFromCommandLine_.data());
    HandleCLIError(sqlcode, this);

    if (sqlcode >= 0)
      printf("\nDatabase user: %s\n\n", userNameFromCommandLine_.data());

    if (sqlcode != 0)
      SQL_EXEC_ClearDiagnostics(NULL);

    specialError_ = previousSpecialError;
  }
  else
  {
    // Call CLI to retrieve the current user identity. This is only
    // done to see if CLI generates errors or warnings that we should
    // display. For example, CLI was not able to establish a default
    // user identity, perhaps metadata is corrupt, we should display
    // that information.
    Int32 uid = 0;
    getDatabaseUserID(uid);
  }
}