Example #1
0
int
main(int argc, char **argv)
{
	int verbose = 0;
	CS_COMMAND *command;
	CS_CONNECTION *connection;
	CS_CONTEXT *context;
	unsigned char buffer[65536];
	CS_INT buffer_len = 8192;
	CS_SMALLINT ind = 0;
	CS_DATAFMT datafmt;
	CS_INT ret;
	int i;

	printf("-- begin --\n");

	check_ret("try_ctlogin", try_ctlogin(&context, &connection, &command, verbose));
	check_ret("cs_config", cs_config(context, CS_SET, CS_MESSAGE_CB, (CS_VOID *) csmsg_callback, unused, 0));

	execute_sql(command, "if object_id('mps_table') is not null drop table mps_table");
	execute_sql(command, "if object_id('mps_rpc') is not null drop procedure mps_rpc");
	/* if this query fails probably we are using wrong database vendor or version */
	ret = execute_sql(command, "create procedure mps_rpc (@varbinary_param varbinary(max)) as "
		    "insert mps_table values (@varbinary_param) " "select len(varbinary_data) from mps_table");
	if (ret != 0) {
		try_ctlogout(context, connection, command, verbose);
		return 0;
	}
	execute_sql(command, "create table mps_table (varbinary_data varbinary(max))");

	if (argc > 1)
		buffer_len = atoi(argv[1]);
	if (buffer_len < 0 || buffer_len > sizeof(buffer))
		return 1;

	printf("sending %d bytes\n", buffer_len);

	for (i = 0; i < buffer_len; i++)
		buffer[i] = (rand() % 16);

	memset(&datafmt, 0, sizeof(datafmt));
	strcpy(datafmt.name, "@varbinary_param");
	datafmt.namelen = nullterm;
	datafmt.datatype = CS_IMAGE_TYPE;
	datafmt.status = CS_INPUTVALUE;

	check_ret("ct_command", ct_command(command, CS_RPC_CMD, "mps_rpc", nullterm, unused));
	check_ret("ct_setparam", ct_setparam(command, &datafmt, buffer, &buffer_len, &ind));
	check_ret("ct_send", ct_send(command));

	fetch_results(command);

	execute_sql(command, "drop table mps_table");
	execute_sql(command, "drop procedure mps_rpc");
	try_ctlogout(context, connection, command, verbose);

	printf("-- end --\n");
	return (result_len == buffer_len) ? 0 : 1;
}
Example #2
0
static int
execute_sql(CS_COMMAND * command, const char *sql)
{
	printf("executing sql: %s\n", sql);
	check_ret("ct_command", ct_command(command, CS_LANG_CMD, sql, nullterm, unused));
	check_ret("ct_send", ct_send(command));
	return fetch_results(command);
}
Example #3
0
void Query::execute( Connector *conn, string s_query)
{

	this->connector = conn;
	this->query = s_query;
	this->execute();

	logger->debug("connector: %p, executing query: %s\n", conn, s_query.c_str());

	if (!this->getStatus()){
		logger->debug("Query failed.\n ");
	} else {
		logger->debug("fetching results..\n");
		fetch_results(); 
		bind_variables();// allocating and binding variables for iteration
		vector<field>::iterator iter_f = v_fields.begin();
#ifdef MSSQL
		while (dbnextrow(conn->conn_ptr) != NO_MORE_ROWS){
			iter_f = v_fields.begin();
			while(iter_f!=v_fields.end()){
				if(iter_f->type!=field::TYPE_INT)
					stripSpace(iter_f->data, iter_f->size);
				iter_f++;
			}
			num_records++;
			iterate(v_fields);
		}
#endif
#ifdef WATCHLIST_MYSQL
		if (res != NULL){// insert or update queries.
			this->num_records = mysql_num_rows(res);
			MYSQL_ROW row;
			int pos;
			if (res !=NULL && (num_records > 0))
			while( ( row = mysql_fetch_row(res))){
				// fetch each records
				pos = 0;
				iter_f = v_fields.begin();
				while(iter_f!=v_fields.end()){
					// iterating columns
					if (iter_f->type==field::TYPE_INT){
						iter_f->setData(atoi(row[pos]));
					} else {
						iter_f->setData(string(row[pos]));
					}
					pos++;
					iter_f++;
				}
				iterate(v_fields);// passing to child
			}
			mysql_free_result(res);
		} 
#endif
	}

	logger->debug("completed execute\n");
}
Example #4
0
void
mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    enum { RHS_FILENAME, RHS_QUERY, RHS_PARAMS };
    (void) nlhs;

    if (nrhs < 2) {
        mexErrMsgIdAndTxt("sqlite3:sqlite3",
                          "usage: sqlite3(file, query[, params])");
    }

    char *db_path = mxArrayToString(prhs[RHS_FILENAME]);
    char *query = mxArrayToString(prhs[RHS_QUERY]);

    sqlite3 *db;
    sqlite3_stmt *stmt;

    if (sqlite3_open(db_path, &db) != SQLITE_OK) {
        mexErrMsgIdAndTxt("sqlite3:open", "failed to open db");
    }

    if (sqlite3_prepare_v2(db, query, -1, &stmt, NULL) != SQLITE_OK) {
        mexPrintf("invalid query:\n  %s\n", query);
        mexErrMsgIdAndTxt(
            "sqlite3:prepare",
            "Failed to prepare query. Reasons could be invalid syntax or "
            "creating a table that already exists.");
    }

    int res;
    int num_columns = sqlite3_column_count(stmt);

    if (num_columns > 0) {
        /* Looks like a SELECT query */
        res = fetch_results(stmt, &plhs[0]);
    } else {
        /* If the user passes a struct array, use this to execute the statement
         * for each column */
        if (nrhs > 2) {
            res = execute_many(stmt, prhs[RHS_PARAMS]);
        } else {
            res = sqlite3_step(stmt);
        }
    }

    if (res == SQLITE_DONE) {
        /* success! */
    } else {
        mexErrMsgIdAndTxt("sqlite3:step",
                          "proper error handling is hard, eh?");
    }
    sqlite3_finalize(stmt);
    sqlite3_close(db);
}
Example #5
0
int mpi_main(int argc, char **argv,
             job_t (send_jobs)(filter_t *, unsigned char **, int, int),
             job_t (get_job)(filter_t *)) {
    int rank, image_width, image_height;
    filter_t filter;
    job_t job;
    double starttime;
    unsigned char ** result;

    MPI_Init(&argc, &argv);

    starttime = MPI_Wtime();
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    // Init
    if (rank == 0) {
        // Read in image and filter and send to workers
        if (argc < 3 || argc > 4) {
            fprintf(stderr, "USAGE: %s filter.txt input.pnm [output.pnm]\n",
                    argv[1]);
            return 1;
        }

        const char * filter_path = argv[1];
        const char * image_path  = argv[2];
        const char * output_path = argv[argc == 3 ? 2 : 3];

        unsigned char ** image;

        filter.filter = read_filter(filter_path, &filter.width, &filter.height);
        image = read_image(image_path, &image_width, &image_height);

        send_filter(&filter);
        job = send_jobs(&filter, image, image_width, image_height);

        result = do_job(&job, &filter);

        LOG("Fetching results.");
        fetch_results(result, image_width);

        LOG("Writing output");
        write_image(output_path, result, job.width, job.height);
    } else {
        // Receive filter and job
        filter = get_filter();
        job = get_job(&filter);

        result = do_job(&job, &filter);

        LOG("Sending results");
        send_result(&job, result);
    }

    // Cleanup
    /* free_filter(filter.filter, filter.height); */
    /* free_image(job.image); */
    /* free_image(result); */

    starttime = MPI_Wtime() - starttime;
    if (rank == 0)
        printf("%f\n", starttime);

    MPI_Finalize();

    return 0;
}