AddrXOnNonClosedWayWriter(const std::string& dir_name) :
		Writer(dir_name, "osmi_addresses_addrx_on_nonclosed_way", USE_TRANSACTIONS, wkbLineString) {

		std::vector<field_config> field_configurations;
		field_configurations.push_back({"way_id",     OFTString, NO_WIDTH});
		field_configurations.push_back({"lastchange", OFTString, NO_WIDTH});
		create_fields(field_configurations);
	}
	BuildingsWriter(OGRDataSource* data_source) :
		Writer(data_source, "osmi_addresses_buildings", USE_TRANSACTIONS, wkbPolygon) {

		std::vector<field_config> field_configurations;
		field_configurations.push_back({"way_id",     OFTString, NO_WIDTH});
		field_configurations.push_back({"lastchange", OFTString, NO_WIDTH});
		create_fields(field_configurations);
	}
	WaysWithAddressesWriter(OGRDataSource* data_source) :
		Writer(data_source, "osmi_addresses_ways_with_addresses", USE_TRANSACTIONS, wkbPolygon) {

		std::vector<field_config> field_configurations;
		field_configurations.push_back({"way_id",     OFTString,  NO_WIDTH});
		field_configurations.push_back({"street",     OFTString,  NO_WIDTH});
		field_configurations.push_back({"houseno",    OFTString,  NO_WIDTH});
		field_configurations.push_back({"postcode",   OFTString,  NO_WIDTH});
		field_configurations.push_back({"city",       OFTString,  NO_WIDTH});
		field_configurations.push_back({"country",    OFTString,  NO_WIDTH});
		field_configurations.push_back({"fulladdr",   OFTString,  NO_WIDTH});
		field_configurations.push_back({"place",      OFTString,  NO_WIDTH});
		field_configurations.push_back({"lastchange", OFTString,  NO_WIDTH});
		create_fields(field_configurations);
	}
	InterpolationWriter(
			const std::string& dir_name,
			node_map_type* node_map_type_p,
			NodesWithAddressesWriter& nwa_writer,
			ConnectionLinePreprocessor& clpp) :
		Writer(dir_name, "osmi_addresses_interpolation", USE_TRANSACTIONS, wkbLineString),
		m_addr_interpolation_node_map(node_map_type_p),
		m_nwa_writer(nwa_writer),
		m_clpp(clpp){

		std::vector<field_config> field_configurations;
		field_configurations.push_back({"way_id",     OFTString, NO_WIDTH});
		field_configurations.push_back({"typename",   OFTString, NO_WIDTH});
		field_configurations.push_back({"firstid",    OFTString, NO_WIDTH});
		field_configurations.push_back({"lastid",     OFTString, NO_WIDTH});
		field_configurations.push_back({"firstno",    OFTString, NO_WIDTH});
		field_configurations.push_back({"lastno",     OFTString, NO_WIDTH});
		field_configurations.push_back({"error",      OFTString, NO_WIDTH});
		field_configurations.push_back({"lastchange", OFTString, NO_WIDTH});
		create_fields(field_configurations);
	}
/**
 * Handle the proxy.response to con.
 * proxy.response
 *   .type can be either ERR, OK or RAW
 *   .resultset (in case of OK)
 *     .fields
 *     .rows
 *   .errmsg (in case of ERR)
 *   .packet (in case of nil)
 *  Note: if error occurred, should set the error string.
 */
int network_mysqld_con_python_handle_proxy_response(network_mysqld_con *con,
			PyObject *proxy){
	assert(proxy);

	PyObject *response = PyObject_GetAttrString(proxy, "response");
	//Note: the response is fetched through the tp_getset, and is a new reference.
	assert(response);
	Py_DECREF(response);

	PyObject *res_type = PyObject_GetAttrString(response, "type");
	if(!res_type){
		network_mysqld_con_send_error(con->client,
					C("Cannot get proxy.response.type"));
		return -1;
	}
	int res_type_int = PyInt_AsLong(res_type);
	Py_DECREF(res_type);

	switch(res_type_int){
	case MYSQLD_PACKET_OK:{
		PyObject *resultset = PyObject_GetAttrString(response, "resultset");
		if(!resultset){
			PyErr_Clear();
			guint64 affected_rows = 0;
			guint64 insert_id = 0;

			PyObject *ar = PyObject_GetAttrString(response, "affected_rows");
			if(!ar)
				PyErr_Clear();
			else if(PyLong_Check(ar))
				affected_rows = PyLong_AsLong(ar);
			else if(PyInt_Check(ar))
				affected_rows = PyInt_AsLong(ar);
			Py_XDECREF(ar);

			PyObject *ii = PyObject_GetAttrString(response, "insert_id");
			if(!ii)
				PyErr_Clear();
			else if(PyLong_Check(ii))
				insert_id = PyLong_AsLong(ii);
			else if(PyInt_Check(ii))
				insert_id = PyInt_AsLong(ii);
			Py_XDECREF(ii);

			network_mysqld_con_send_ok_full(con->client, affected_rows, insert_id, 0x0002, 0);
		}
		else{
			Py_DECREF(resultset);

			GPtrArray *fields = create_fields(resultset);
			if(!fields){
				network_mysqld_con_send_error(con->client,
							C("Cannot get proxy.response.resultset.fields!"));
				PyErr_Print();
				PyErr_Clear();
				return -1;
			}
			if(fields->len <= 0){
				network_mysqld_con_send_error(con->client,
							C("Size of proxy.response.resultset.fields is 0"));
				network_mysqld_proto_fielddefs_free(fields);
				return -1;
			}
			GPtrArray *rows = create_rows(resultset);
			if(!rows){
				network_mysqld_con_send_error(con->client,
							C("Cannot get proxy.response.resultset.rows"));
				PyErr_Print();
				network_mysqld_proto_fielddefs_free(fields);
				return -1;
			}

			network_mysqld_con_send_resultset(con->client, fields, rows);

			if (fields) {
				network_mysqld_proto_fielddefs_free(fields);
				fields = NULL;
			}

			if (rows) {
				guint i;
				for (i = 0; i < rows->len; i++) {
					GPtrArray *row = rows->pdata[i];
					guint j;
					for (j = 0; j < row->len; j++)
						if (row->pdata[j])
							g_free(row->pdata[j]);
					g_ptr_array_free(row, TRUE);
				}
				g_ptr_array_free(rows, TRUE);
				rows = NULL;
			}
		}
		break;}

	case MYSQLD_PACKET_ERR:{
		gint errcode = ER_UNKNOWN_ERROR;
		/** let's call ourself Dynamic SQL ... 07000 is "dynamic SQL error" */
		const gchar *sqlstate = "07000";
		gchar *errmsg = NULL;

		PyObject *err_code = PyObject_GetAttrString(response, "errcode");
		if(!err_code)
			//Here use the default error code: ER_UNKNOWN_ERROR
			PyErr_Clear();
		else{
			errcode = PyInt_AsLong(err_code);
			Py_DECREF(err_code);
		}

		PyObject *sql_state = PyObject_GetAttrString(response, "sqlstate");
		if(!sql_state)
			//Here use the default sql state: 07000
			PyErr_Clear();
		else{
			sqlstate = PyString_AsString(sql_state);
			Py_DECREF(sql_state);
		}

		PyObject *err_msg = PyObject_GetAttrString(response, "errmsg");
		if(!err_msg){
			PyErr_Clear();
			network_mysqld_con_send_error(con->client,
						C("(python) proxy.response.errmsg is nil"));
		}
		else{
			errmsg = PyString_AsString(err_msg);
			Py_DECREF(err_msg);
			network_mysqld_con_send_error_full(con->client, errmsg,
						strlen(errmsg), errcode, sqlstate);
		}

	    break;}

	case MYSQLD_PACKET_RAW:{
		PyObject *packets = PyObject_GetAttrString(response, "packets");
		if(!packets)
			goto queue_reset;

		int i;
		for(i = 0; i < PySequence_Size(packets); i++){
			PyObject *item = PySequence_GetItem(packets, i);
			//If invalid items doesn't influces valid ones before them.
			if(!item)
				goto queue_reset;
			if(!PyString_Check(item)){
				PyErr_SetString(PyExc_ValueError, "proxy.response.packets' "
							"items should be strings.");
				Py_DECREF(item);
				Py_DECREF(packets);
				goto queue_reset;
			}

			network_mysqld_queue_append(con->client, con->client->send_queue,
						PyString_AsString(item), PyString_Size(item));
			Py_DECREF(item);
		}
		Py_DECREF(packets);
queue_reset:
		/* reset the packet-id checks */
		network_mysqld_queue_reset(con->client);
		break;}
	default:
		g_critical("Now the response type is unknown: %d", res_type_int);
		return -1;
	}
	return 0;
}
Beispiel #6
0
static GwyContainer*
sly_load(const gchar *filename,
         G_GNUC_UNUSED GwyRunType mode,
         GError **error)
{
    GwyContainer *container = NULL, *meta = NULL;
    gchar *buffer = NULL;
    GError *err = NULL;
    GHashTable *hash = NULL;
    gchar *p, *line, *value;
    guint expecting_data = 0;
    SensolyticsChannel *channels = NULL;
    Dimensions dimensions;
    gint ndata = 0, i;

    if (!g_file_get_contents(filename, &buffer, NULL, &err)) {
        err_GET_FILE_CONTENTS(error, &err);
        return NULL;
    }

    p = buffer;
    line = gwy_str_next_line(&p);
    g_strstrip(line);
    if (!gwy_strequal(line, MAGIC)) {
        err_FILE_TYPE(error, "Sensolytics");
        goto fail;
    }

    hash = g_hash_table_new(g_str_hash, g_str_equal);
    for (line = gwy_str_next_line(&p); line; line = gwy_str_next_line(&p)) {
        if (!line[0])
            continue;

        if (expecting_data) {
            expecting_data--;

            /* The columns are comma-separated and numbers use decimal points.
             * Do not tempt the number parsing functions more than necessary
             * and fix commas to tab characters. */
            g_strdelimit(line, ",", '\t');

            /* Ignore X, Y and Z, each is two values */
            for (i = 0; i < 6; i++)
                g_ascii_strtod(line, &line);

            for (i = 0; i < ndata; i++)
                channels[i].data[expecting_data]
                    = channels[i].q * g_ascii_strtod(line, &line);
        }
        else {
            g_strstrip(line);
            if (line[0] != '#') {
                g_warning("Comment line does not start with #.");
                continue;
            }

            do {
                line++;
            } while (g_ascii_isspace(*line));

            if (g_str_has_prefix(line, "X [")) {
                if (channels) {
                    g_warning("Multiple data headers!?");
                    continue;
                }

                if (!read_dimensions(hash, &ndata, &dimensions, error)
                    || !(channels = create_fields(hash, line,
                                                  ndata, &dimensions)))
                    goto fail;
                expecting_data = dimensions.xres * dimensions.yres;
                continue;
            }

            value = strchr(line, ':');
            if (!value) {
                if (!gwy_strequal(line, "ArrayScan"))
                    g_warning("Non-parameter-like line %s", line);
                continue;
            }
            *value = '\0';
            g_strchomp(line);
            do {
                value++;
            } while (g_ascii_isspace(*value));

            if (gwy_strequal(line, "Warning"))
                continue;

            gwy_debug("<%s>=<%s>", line, value);
            g_hash_table_insert(hash, line, value);
        }
    }

    if (!channels) {
        err_NO_DATA(error);
        goto fail;
    }

    container = gwy_container_new();
    for (i = 0; i < ndata; i++) {
        GQuark key = gwy_app_get_data_key_for_id(i);

        gwy_data_field_invert(channels[i].dfield, FALSE, TRUE, FALSE);
        gwy_container_set_object(container, key, channels[i].dfield);
        gwy_app_channel_check_nonsquare(container, i);
        if (channels[i].name) {
            gchar *s = g_strconcat(g_quark_to_string(key), "/title", NULL);
            gwy_container_set_string_by_name(container, s,
                                             g_strdup(channels[i].name));
            g_free(s);
        }
        else
            gwy_app_channel_title_fall_back(container, i);

        gwy_file_channel_import_log_add(container, i, NULL, filename);
    }

    meta = get_meta(hash);
    clone_meta(container, meta, ndata);
    g_object_unref(meta);

fail:
    g_free(buffer);
    if (hash)
        g_hash_table_destroy(hash);
    if (channels) {
        for (i = 0; i < ndata; i++)
            g_object_unref(channels[i].dfield);
        g_free(channels);
    }

    return container;
}