예제 #1
0
/*
 * execute_sql_statement						      
 *									      
 * arguments:								      
 *	statement: statement to be executed				      
 *									      
 * returns/side-effects: int 					      
 *									      
 * description: 							      
 */
int
execute_sql_statement (FILE * fp, char *statement)
{
  int db_error = NO_ERROR;

  DB_SESSION *session;
  DB_QUERY_RESULT *query_result;
  int stmtid;
  CUBRID_STMT_TYPE stmt_type;

  session = db_open_buffer (statement);
  if (session == NULL)
    {
      return db_error_code ();
    }

  stmtid = db_compile_statement (session);
  if (stmtid < 0)
    {
      return db_error_code ();
    }

  stmt_type = db_get_statement_type (session, stmtid);

  db_error = db_execute_statement (session, stmtid, &query_result);
  if (db_error < 0)
    {
      return db_error_code ();
    }
  else if (stmt_type == CUBRID_STMT_UPDATE || stmt_type == CUBRID_STMT_DELETE)
    {
      fprintf (fp, "%d row%s %s\n", db_error, (db_error > 1 ? "s" : ""),
	       (stmt_type == CUBRID_STMT_SELECT ? "selected" : "affected"));
    }
  if (query_result != NULL)
    {
      if (stmt_type == CUBRID_STMT_SELECT || stmt_type == CUBRID_STMT_CALL)
	{
	  my_db_csql_debug_print_result (fp, query_result, stmt_type);
	}

      fprintf (fp, "%d row%s %s\n", db_error, (db_error > 1 ? "s" : ""),
	       (stmt_type == CUBRID_STMT_SELECT ? "selected" : "affected"));

      db_error = db_query_end (query_result);
      if (db_error != NO_ERROR)
	{
	  db_close_session (session);
	  return db_error;
	}
    }
  db_close_session (session);

  return NO_ERROR;
}
예제 #2
0
파일: loaddb.c 프로젝트: dong1/testsize
/*
 * ldr_exec_query_from_file - execute queries from file
 *    return: 0 if successful, non-zero otherwise
 *    file_name(in): file path
 *    file(in): FILE *
 *    start_line(in): start line
 *    commit_period(in): commit period
 */
static int
ldr_exec_query_from_file (const char *file_name, FILE * input_stream,
			  int *start_line, int commit_period)
{
  DB_SESSION *session = NULL;
  DB_QUERY_RESULT *res = NULL;
  int error = 0;
  int stmt_cnt, stmt_id = 0, stmt_type;
  int executed_cnt = 0;
  int parser_start_line_no;
  int parser_end_line_no = 1;
  int check_line_no = true;

  if ((*start_line) > 1)
    {
      int line_count = *start_line - 1;

      do
	{
	  int c = fgetc (input_stream);
	  if (c == EOF)
	    {
	      print_log_msg (1, msgcat_message (MSGCAT_CATALOG_UTILS,
						MSGCAT_UTIL_SET_LOADDB,
						LOADDB_MSG_UNREACHABLE_LINE),
			     file_name, *start_line);
	      error = ER_GENERIC_ERROR;
	      goto end;
	    }
	  else if (c == '\n')
	    {
	      line_count--;
	    }
	}
      while (line_count > 0);
    }

  check_line_no = false;
  session = db_make_session_for_one_statement_execution (input_stream);
  if (session == NULL)
    {
      print_log_msg (1, "ERROR: %s\n", db_error_string (3));
      error = er_errid ();
      goto end;
    }

  util_arm_signal_handlers (&ldr_exec_query_interrupt_handler,
			    &ldr_exec_query_interrupt_handler);

  while (1)
    {
      if (interrupt_query)
	{
	  if (er_errid () != ER_INTERRUPTED)
	    {
	      er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_INTERRUPTED, 0);
	    }
	  error = er_errid ();
	  db_close_session (session);
	  goto end;
	}
      parser_start_line_no = parser_end_line_no;

      stmt_cnt = db_parse_one_statement (session);
      if (stmt_cnt > 0)
	{
	  db_get_parser_line_col (session, &parser_end_line_no, NULL);
	  stmt_id = db_compile_statement (session);
	}

      if (stmt_cnt <= 0 || stmt_id <= 0)
	{
	  DB_SESSION_ERROR *session_error;
	  int line, col;
	  if ((session_error = db_get_errors (session)) != NULL)
	    {
	      do
		{
		  session_error =
		    db_get_next_error (session_error, &line, &col);
		  if (line >= 0)
		    {
		      print_log_msg (1, "In %s line %d,\n", file_name,
				     line + (*start_line));
		      print_log_msg (1, "ERROR: %s \n", db_error_string (3));
		      error = er_errid ();
		    }
		}
	      while (session_error);
	    }
	  db_close_session (session);
	  break;
	}

      stmt_type = db_get_statement_type (session, stmt_id);

      res = (DB_QUERY_RESULT *) NULL;
      error = db_execute_statement (session, stmt_id, &res);

      if (error < 0)
	{
	  print_log_msg (1, "ERROR: %s\n", db_error_string (3));
	  db_close_session (session);
	  break;
	}
      executed_cnt++;
      error = db_query_end (res);
      if (error < 0)
	{
	  print_log_msg (1, "ERROR: %s\n", db_error_string (3));
	  db_close_session (session);
	  break;
	}

      if (stmt_type == CUBRID_STMT_COMMIT_WORK ||
	  (commit_period && (executed_cnt % commit_period == 0)))
	{
	  db_commit_transaction ();
	  print_log_msg (Verbose_commit,
			 "%8d statements executed. Commit transaction at line %d\n",
			 executed_cnt, parser_end_line_no);
	  *start_line = parser_end_line_no + 1;
	}
      print_log_msg ((int) Verbose, "Total %8d statements executed.\r",
		     executed_cnt);
      fflush (stdout);
    }

end:
  if (error < 0)
    {
      db_abort_transaction ();
    }
  else
    {
      *start_line = parser_end_line_no + 1;
      print_log_msg (1, "Total %8d statements executed.\n", executed_cnt);
      fflush (stdout);
      db_commit_transaction ();
    }
  return error;
}