Querytextedit::Querytextedit(QWidget *parent) :
    QTextEdit(parent)
{
	this->mainwindow = (Mainwindow*) parent;
	this->highlighter = new MySQLHighlighter(this->document());

	this->mainwindow->hist_menu = new QMenu();

	this->drinlassen = new QAction(tr("So lassen"), this);
	this->drinlassen->setCheckable(true);
	this->einzeln = new QAction(tr("nur eins"), this);
	this->einzeln->setCheckable(true);

	this->behavior = new QActionGroup(this);
	this->behavior->addAction(this->drinlassen);
	this->behavior->addAction(this->einzeln);
	this->einzeln->setChecked(true);

	this->save = new QAction(tr("Save"), this);
	this->save->setShortcut(QKeySequence::Save);
	this->load = new QAction(tr("Load"), this);
	this->load->setShortcut(QKeySequence::Open);

	this->mainwindow->hist_menu->addAction(this->drinlassen);
	this->mainwindow->hist_menu->addAction(this->einzeln);
	this->mainwindow->hist_menu->addSeparator();
	this->mainwindow->hist_menu->addAction(this->save);
	this->mainwindow->hist_menu->addAction(this->load);

	connect(this->save, SIGNAL(triggered()), this, SLOT(save_query()));
	connect(this->load, SIGNAL(triggered()), this, SLOT(load_query()));
	connect(this, SIGNAL(textChanged()), this, SLOT(something_changed()));

	this->hist_position = 0;
	hist_something_changed = false;
}
Exemple #2
0
int main(int argc, char** argv){
  int i;
  char buffer[256];
  char* tk;
  FILTERCHAIN* tmp_chn;
  FILTERCHAIN* del_chn;  
	HARNESS_INSTANCE* hinstance;

	if(harness_init(argc,argv,&hinstance)){
		printf("Error: Initialization failed.\n");
		skygw_log_write(LOGFILE_ERROR,"Error: Initialization failed.\n");
		skygw_logmanager_done();
		skygw_logmanager_exit();
		return 1;
	}

  if(instance.verbose){
    printf("\n\n\tFilter Test Harness\n\n");
  }
  

  while(instance.running){
    printf("Harness> ");
    memset(buffer,0,256);
    fgets(buffer,256,stdin);
    tk = strtok(buffer," \n");
    switch(user_input(tk))
      {
      case RUNFILTERS:
	if(instance.head->next == NULL){
	  printf("No filters loaded.\n");
	  break;
	}
	if(instance.buffer == NULL){
	  if(instance.infile<0){
	    manual_query();
	  }else{
	    load_query();
	  }
	}
	
	route_buffers();
	break;

      case LOAD_FILTER:

	tk = strtok(NULL," \n");
	tmp_chn = load_filter_module(tk);
	if(!tmp_chn || !load_filter(tmp_chn,instance.conf)){
	  printf("Error creating filter instance.\n");	  
	  skygw_log_write(LOGFILE_ERROR,"Error: Error creating filter instance.\n");
	}else{
	  instance.head =  tmp_chn;
	}
	break;

      case DELETE_FILTER:

	tk = strtok(NULL," \n\0");
	tmp_chn = instance.head;
	del_chn = instance.head;
	if(tk){
	  if(strcmp(instance.head->name,tk) == 0){

	    instance.head = instance.head->next;

	  }else{
	
	    while(del_chn->next){

	      if(strcmp(del_chn->name,tk) == 0){

		tmp_chn->next = del_chn->next;
		break;
	      
	      }else{
		tmp_chn = del_chn;
		del_chn = del_chn->next;
	      

	      }

	    }
	  }

	  if(del_chn && del_chn->next){

	    printf("Deleted %s.\n",del_chn->name);

	    if(del_chn->instance){

	      del_chn->instance->freeSession(del_chn->filter,del_chn->session);

	    }

	    free(del_chn->filter);
	    free(del_chn->down);
	    free(del_chn->name);
	    free(del_chn);
	  }else{
	    printf("No matching filter found.\n");
	  }
	}
	break;

      case LOAD_CONFIG:
	tk = strtok(NULL,"  \n\0");
	if(!load_config(tk)){
	  free_filters();
	}
	break;

      case SET_INFILE:

	tk = strtok(NULL,"  \n\0");
	if(instance.infile >= 0){
	  close(instance.infile);
	  free(instance.infile_name);
	}
	if(tk!= NULL){
	  free_buffers();
	  instance.infile = open_file(tk,0);
	  if(instance.infile >= 0){
	    load_query();
	    instance.infile_name = strdup(tk);
	    if(instance.verbose){
	      printf("Loaded %d queries from file '%s'\n",instance.buffer_count,instance.infile_name);
	    }
	  }
	}else{
	  instance.infile = -1;
	  printf("Queries are read from: command line\n");
	}

	break;

      case SET_OUTFILE:

	tk = strtok(NULL,"  \n\0");
	if(instance.outfile >= 0){
	  close(instance.outfile);
	  free(instance.outfile_name);
	}
	if(tk!= NULL){
	  
	  instance.outfile = open_file(tk,1);
	  if(instance.outfile >= 0){
	    instance.outfile_name = strdup(tk);
	    printf("Output is logged to: %s\n",tk);
	  }
	}else{
	  instance.outfile = -1;
	  printf("Output logging disabled.\n");
	}

	break;

      case SESS_COUNT:

	tk = strtok(NULL,"  \n\0");
	free_buffers();
	free_filters();
	instance.session_count = atoi(tk);
	printf("Sessions set to: %d\n", instance.session_count);
	break;

      case THR_COUNT:

	instance.running = 0;
	pthread_mutex_unlock(&instance.work_mtx);
	for(i = 0;i<instance.thrcount;i++){
	  pthread_join(instance.thrpool[i],NULL);
	}
	pthread_mutex_lock(&instance.work_mtx);

	instance.running = 1;
	tk = strtok(NULL,"  \n\0");
	instance.thrcount = atoi(tk);
	void* t_thr_pool;

	if(!(t_thr_pool = realloc(instance.thrpool,instance.thrcount * sizeof(pthread_t)))){
	  printf("Error: Out of memory\n");
	  skygw_log_write(LOGFILE_ERROR,"Error: Out of memory\n");
	  instance.running = 0;
	  break;
	}

	instance.thrpool = t_thr_pool;
	int thr_num = 1;

	for(i = 0;i<instance.thrcount;i++){

	  pthread_create(&instance.thrpool[i],
			 NULL,
			 (void*)work_buffer,
			 (void*)thr_num++);

	}
	printf("Threads set to: %d\n", instance.thrcount);

	break;

      case QUIT:

	instance.running = 0;
	pthread_mutex_unlock(&instance.work_mtx);
	for(i = 0;i<instance.thrcount;i++){
	  pthread_join(instance.thrpool[i],NULL);
	}
	break;
      case UNDEFINED:

	printf("Command not found, enter \"help\" for a list of commands\n");

	break;
      default:
	
	break;
	
      }  
  }

  if(instance.infile >= 0){
    close(instance.infile);
  }
  if(instance.outfile >= 0){
    close(instance.outfile);
  }

  free_buffers();
  free_filters();
  skygw_logmanager_done();
  skygw_logmanager_exit();
  free(instance.head);

  return 0;
}
int main( int argc, char* argv[] ) {

	// Parse command line

	const char* idl_file_name = NULL;
	const char* query_file_name = NULL;
	int verbose = 0;                        // boolean

	int opt;
	opterr = 0;
	const char optstring[] = ":f:i:v";

	while( ( opt = getopt( argc, argv, optstring ) ) != -1 ) {
		switch( opt )
		{
			case 'f' :  // get file name of query
				if( query_file_name ) {
					fprintf( stderr, "Multiple input files not allowed\n" );
					return EXIT_FAILURE;
				}
				else
					query_file_name = optarg;
				break;
			case 'i' :  // get name of IDL file
				if( idl_file_name ) {
					fprintf( stderr, "Multiple IDL file names not allowed\n" );
					return EXIT_FAILURE;
				}
				else
					idl_file_name = optarg;
				break;
			case 'v' :  // Verbose
				verbose = 1;
				break;
			case '?' :  // Invalid option
				fprintf( stderr, "Invalid option '-%c' on command line\n",
						 (char) optopt );
				return EXIT_FAILURE;
			default :  // Huh?
				fprintf( stderr, "Internal error: unexpected value '%c'"
						"for optopt", (char) optopt );
				return EXIT_FAILURE;

		}
	}

	// If the command line doesn't specify an IDL file, get it
	// from an environmental variable, or apply a default
	if( NULL == idl_file_name ) {
		idl_file_name = getenv( "OILS_IDL_FILENAME" );
		if( NULL == idl_file_name )
			idl_file_name = "/openils/conf/fm_IDL.xml";
	}

	if( verbose )
		printf( "IDL file: %s\n", idl_file_name );

	char* loaded_json = NULL;
	const char* json_query = NULL;

	// Get the JSON query into a string
	if( query_file_name ) {   // Got a file?  Load it
		if( optind < argc )
			fprintf( stderr, "Extra parameter(s) ignored\n" );
		loaded_json = load_query( query_file_name );
		if( !loaded_json )
			return EXIT_FAILURE;
		json_query = loaded_json;
	} else {                  // No file?  Use command line parameter
		if ( optind == argc ) {
			fprintf( stderr, "No JSON query specified\n" );
			return EXIT_FAILURE;
		} else
			json_query = argv[ optind ];
	}

	if( verbose )
		printf( "JSON query: %s\n", json_query );

	osrfLogSetLevel( OSRF_LOG_WARNING );    // Suppress informational messages
	(void) oilsIDLInit( idl_file_name );    // Load IDL into memory

	// Load a database driver, connect to it, and install the connection in
	// the cstore module.  We don't actually connect to a database, but we
	// need the driver to process quoted strings correctly.
	if( dbi_initialize( NULL ) < 0 ) {
		printf( "Unable to load database driver\n" );
		return EXIT_FAILURE;
	};

	dbi_conn conn = dbi_conn_new( "pgsql" );  // change string if ever necessary
	if( !conn ) {
		printf( "Unable to establish dbi connection\n" );
		dbi_shutdown();
		return EXIT_FAILURE;
	}

	oilsSetDBConnection( conn );

	// The foregoing is an inelegant kludge.  The true, proper, and uniquely
	// correct thing to do is to load the system settings and then call
	// osrfAppInitialize() and osrfAppChildInit().  Maybe we'll actually
	// do that some day, but this will do for now.

	// Translate the JSON into SQL
	int rc = test_json_query( json_query );

	dbi_conn_close( conn );
	dbi_shutdown();
	if( loaded_json )
		free( loaded_json );

	return rc ? EXIT_FAILURE : EXIT_SUCCESS;
}
Exemple #4
0
int process_opts(int argc, char** argv)
{
	int fd, buffsize = 1024;
	int rd,rdsz, rval = 0, error = 0;
	size_t fsize;
        char* saveptr;
	char *buff = calloc(buffsize,sizeof(char)), *tok = NULL;

	/**Parse 'harness.cnf' file*/

	if(buff == NULL){
		printf("Error: Call to malloc() failed.\n");
		return 1;
	}

	if((fd = open_file("harness.cnf",1)) < 0){
		printf("Failed to open configuration file.\n");
		free(buff);
		return 1;
	}

	
	if( (rval = lseek(fd,0,SEEK_END)) < 0 || 
		lseek(fd,0,SEEK_SET) < 0){
		printf("Error: Cannot seek file.\n");
		close(fd);
		free(buff);
		return 1;
	}

	fsize = (size_t)rval;

	instance.thrcount = 1;
	instance.session_count = 1;
	rdsz = read(fd,buff,fsize);
    buff[rdsz] = '\0';
	tok = strtok_r(buff,"=",&saveptr);
	while(tok){
		if(!strcmp(tok,"threads")){
			tok = strtok_r(NULL,"\n\0",&saveptr);
			instance.thrcount = strtol(tok,0,0);
		}else if(!strcmp(tok,"sessions")){
			tok = strtok_r(NULL,"\n\0",&saveptr);
			instance.session_count = strtol(tok,0,0);
		}
		tok = strtok_r(NULL,"=",&saveptr);
	}
  
  
   
	free(buff);
	instance.verbose = 1;

	if(argc < 2){
		close(fd);
		return 1;
	}

	char* conf_name = NULL;
	rval = 0;

	while((rd = getopt(argc,argv,"e:m:c:i:o:s:t:d:qh")) > 0){
		switch(rd){

		case 'e':
			instance.expected = open_file(optarg,0);
			printf("Expected output is read from: %s\n",optarg);
			break;

		case 'o':
			instance.outfile = open_file(optarg,1);
			printf("Output is written to: %s\n",optarg);
			break;

		case 'i':
			instance.infile = open_file(optarg,0);
			printf("Input is read from: %s\n",optarg);
			break;

		case 'c':
			if(conf_name){
				free(conf_name);
			}
			conf_name = strdup(optarg);
			printf("Configuration: %s\n",optarg);
			break;

		case 'q':
			instance.verbose = 0;
			break;

		case 's':
			instance.session_count = atoi(optarg);
			printf("Sessions: %i\n",instance.session_count);
			break;

		case 't':
			instance.thrcount = atoi(optarg);
			printf("Threads: %i\n",instance.thrcount);
			break;

		case 'd':
			instance.rt_delay = atoi(optarg);
			printf("Routing delay: %i ",instance.rt_delay);
			break;

		case 'h':
			printf(
				   "\nOptions for the configuration file 'harness.cnf'':\n\n"
				   "\tthreads\tNumber of threads to use when routing buffers\n"
				   "\tsessions\tNumber of sessions\n\n"
				   "Options for the command line:\n\n"
				   "\t-h\tDisplay this information\n"
				   "\t-c\tPath to the MaxScale configuration file to parse for filters\n"
				   "\t-i\tName of the input file for buffers\n"
				   "\t-o\tName of the output file for results\n"
				   "\t-q\tSuppress printing to stdout\n"
				   "\t-s\tNumber of sessions\n"
				   "\t-t\tNumber of threads\n"
				   "\t-d\tRouting delay\n");
			break;

		case 'm':
			instance.mod_dir = strdup(optarg);
			printf("Module directory: %s",optarg);
			break;

		default:
	
			break;

		}
	}
	printf("\n");

	if(conf_name && (error = load_config(conf_name))){
		load_query();
	}else{
		instance.running = 0;
	}
	free(conf_name);
	close(fd);

    if(!error)
    {
        rval = 1;
    }

	return rval;
}
Exemple #5
0
bool CConfigLoader::load(const std::string& filepath)
{
    Json::Reader reader;
    Json::Value root;
    std::ifstream fs(filepath.c_str());
    struct DbInfo* db_info_array[MAX_DB_CONNECTION] = { NULL };
    struct QueryInfo* query_info_array[MAX_SQL_TEMPLATE] = { NULL };
    struct UpdateInfo* update_info_array[MAX_SQL_TEMPLATE] = { NULL };

    if (_md5_sum.empty())
    {
        MYLOG_INFO("loading %s\n", filepath.c_str());
    }
    else
    {
        MYLOG_DETAIL("loading %s\n", filepath.c_str());
    }
    if (!fs)
    {
        MYLOG_ERROR("load %s failed: %s\n", filepath.c_str(), strerror(errno));
        return false;
    }
    if (!reader.parse(fs, root))
    {
        MYLOG_ERROR("parse %s failed: %s\n", filepath.c_str(), reader.getFormattedErrorMessages().c_str());
        return false;
    }

    // 检查文件是否有修改过
    std::string md5_sum = utils::CMd5Helper::lowercase_md5("%s", root.toStyledString().c_str());
    if (md5_sum == _md5_sum)
    {
        MYLOG_DETAIL("not changed: (%s)%s\n", md5_sum.c_str(), filepath.c_str());
        return true; // 未发生变化
    }

    init_db_info_array(db_info_array);
    init_query_info_array(query_info_array);
    init_update_info_array(update_info_array);
    if (!load_database(root["database"], db_info_array))
        return false;
    if (!load_query(root["query"], query_info_array))
        return false;
    if (!load_update(root["update"], update_info_array))
        return false;

    int i; // 加写锁
    sys::WriteLockHelper write_lock(_read_write_lock);

    release_db_info_array(_db_info_array);
    release_query_info_array(_query_info_array);
    release_update_info_array(_update_info_array);
    for (i=0; i<MAX_DB_CONNECTION; ++i)
    {
        if (db_info_array[i] != NULL)
        {
            // 启动时即连接一下,以早期发现配置等问题
            _db_info_array[i] = new struct DbInfo(*db_info_array[i]);
            sys::DBConnection* db_connection = init_db_connection(i, false);
            if (db_connection != NULL)
            {
                delete db_connection;
                db_connection = NULL;
            }
        }
    }
    for (i=0; i<MAX_SQL_TEMPLATE; ++i)
    {
        if (query_info_array[i] != NULL)
            _query_info_array[i] = new struct QueryInfo(*query_info_array[i]);
        if (update_info_array[i] != NULL)
            _update_info_array[i] = new struct UpdateInfo(*update_info_array[i]);
    }

    _md5_sum = md5_sum;
    MYLOG_INFO("loaded %s[%s] successfully\n", filepath.c_str(), _md5_sum.c_str());
    return true;
}