static void
alter_blob_values (GdaThreadRecordset *rs, GdaRow **prow)
{
	gint i;
	for (i = 0; i < rs->priv->nblobs; i++) {
		GValue *value = gda_row_get_value (*prow, rs->priv->blobs_conv[i]);
		if (G_VALUE_TYPE (value) == GDA_TYPE_BLOB) {
			GdaBlob *blob;
			blob = (GdaBlob*) gda_value_get_blob (value);
			if (blob->op) {
				GdaBlobOp *nop;
				nop = _gda_thread_blob_op_new (rs->priv->wrapper, blob->op);
				g_object_unref (blob->op);
				blob->op = nop;
			}
		}
	}
}
Ejemplo n.º 2
0
GdaOracleValue *
_gda_value_to_oracle_value (const GValue *value)
{
	GdaOracleValue *ora_value;
	OCIDate *oci_date;
	GType type;

	ora_value = g_new0 (GdaOracleValue, 1);

	ora_value->g_type = G_VALUE_TYPE (value);
	ora_value->indicator = 0;
	ora_value->hdef = (OCIDefine *) 0;
	ora_value->pard = (OCIParam *) 0;
	type = ora_value->g_type;

	if (type == GDA_TYPE_NULL)
		ora_value->indicator = -1;
	else if ((type == G_TYPE_INT64) ||
		 (type == G_TYPE_UINT64) ||
		 (type == G_TYPE_DOUBLE) ||
		 (type == G_TYPE_INT) ||
		 (type == G_TYPE_UINT) ||
		 (type == GDA_TYPE_NUMERIC) ||
		 (type == G_TYPE_FLOAT) ||
		 (type == GDA_TYPE_SHORT) ||
		 (type == GDA_TYPE_USHORT) ||
		 (type == G_TYPE_LONG) ||
		 (type == G_TYPE_ULONG) ||
		 (type == G_TYPE_CHAR) ||
		 (type == G_TYPE_UCHAR)) {
		gchar *val_str;
		val_str = gda_value_stringify ((GValue *) value);
		if (!val_str)
			return NULL;
		
		ora_value->sql_type = SQLT_CHR;
		ora_value->value = (void *) val_str;
		ora_value->defined_size = strlen (val_str);
	}
	else if (type == G_TYPE_DATE) {
		GDate *gda_date;
		ora_value->sql_type = SQLT_ODT;
		gda_date = (GDate*) g_value_get_boxed (value);
		oci_date = g_new0(OCIDate, 1);
		OCIDateSetDate(oci_date, gda_date->year, gda_date->month, gda_date->day);
		ora_value->defined_size = sizeof (OCIDate);
		ora_value->value = oci_date;
	}
	else if (type == GDA_TYPE_TIME) {
		GdaTime *gda_time;
		ora_value->sql_type = SQLT_ODT;
		gda_time = (GdaTime *) gda_value_get_time ((GValue *) value);
		oci_date = g_new0(OCIDate, 1);
		OCIDateSetTime(oci_date, gda_time->hour, gda_time->minute, gda_time->second);
		ora_value->defined_size = sizeof (OCIDate);
		ora_value->value = oci_date;
	}
	else if (type == GDA_TYPE_TIMESTAMP) {
		GdaTimestamp *gda_timestamp;
		ora_value->sql_type = SQLT_ODT;
		gda_timestamp = (GdaTimestamp *) gda_value_get_timestamp ((GValue *) value);
		oci_date = g_new0(OCIDate, 1);
		OCIDateSetDate(oci_date, gda_timestamp->year, gda_timestamp->month, gda_timestamp->day);
		OCIDateSetTime(oci_date, gda_timestamp->hour, gda_timestamp->minute, gda_timestamp->second);
		ora_value->defined_size = sizeof (OCIDate);
		ora_value->value = oci_date;
	}
	else if (type == GDA_TYPE_BLOB) {
		GdaBinary *bin;

		bin = (GdaBinary *) gda_value_get_blob ((GValue *) value);
		if (bin) {
			ora_value->sql_type = SQLT_LNG;
			ora_value->value = bin->data;
			ora_value->defined_size = bin->binary_length;
		}
		else {
			ora_value->sql_type = SQLT_CHR;
			ora_value->value = g_strdup ("");
			ora_value->defined_size = 0;
		}
	}
	else if (type == GDA_TYPE_BINARY) {
		GdaBinary *bin;

		bin = (GdaBinary *) gda_value_get_binary ((GValue *) value);
		if (bin) {
			ora_value->sql_type = SQLT_LNG;
			ora_value->value = bin->data;
			ora_value->defined_size = bin->binary_length;
		}
		else {
			ora_value->sql_type = SQLT_CHR;
			ora_value->value = g_strdup ("");
			ora_value->defined_size = 0;
		}
	}
	else {
		gchar *val_str;
		val_str = gda_value_stringify ((GValue *) value);
		if (!val_str)
			return NULL;

		ora_value->sql_type = SQLT_CHR;
		ora_value->value = val_str;
		ora_value->defined_size = strlen (val_str);
	}

	return ora_value;
}
static gboolean
gda_data_model_dir_set_values (GdaDataModel *model, gint row, GList *values, GError **error)
{
	GdaDataModelDir *imodel;
	GList *list;
	gint col;
	FileRow *frow;
	gboolean has_changed = FALSE;

	g_return_val_if_fail (GDA_IS_DATA_MODEL_DIR (model), FALSE);
	g_return_val_if_fail (row >= 0, FALSE);
	imodel = (GdaDataModelDir *) model;
	g_return_val_if_fail (imodel->priv, FALSE);
	if (!values)
		return TRUE;

	if ((guint)row >= imodel->priv->rows->len) {
		gchar *str;
		if (imodel->priv->rows->len > 0)
			str = g_strdup_printf (_("Row %d out of range (0-%d)"), row,
					       imodel->priv->rows->len - 1);
		else
			str = g_strdup_printf (_("Row %d not found (empty data model)"), row);
		add_error (imodel, str);
		g_set_error (error, GDA_DATA_MODEL_ERROR, GDA_DATA_MODEL_ROW_OUT_OF_RANGE_ERROR,
			     "%s", str);
		g_free (str);
                return FALSE;
        }

	frow =  g_ptr_array_index (imodel->priv->rows, row);

	for (col = 0, list = values; list; list = list->next, col++) {
		GValue *value = (GValue *) list->data;
		const GValue *cvalue = gda_data_model_get_value_at (model, col, row, error);
		if (!cvalue)
			return FALSE;
		if (!value || !gda_value_compare (value, cvalue))
			continue;

		switch (col) {
		case COL_SIZE:
		case COL_MIME:
		case COL_MD5SUM:
		default:
			add_error (imodel, _("Column cannot be modified"));
			g_set_error (error, GDA_DATA_MODEL_ERROR, GDA_DATA_MODEL_ACCESS_ERROR,
				     "%s", _("Column cannot be modified"));
			return FALSE;
		case COL_DIRNAME: {
			/* check that the new dir still starts with the basedir */
			const gchar *new_path;
			gchar *old_path;
			gint len, base_len;

			new_path = value ? g_value_get_string (value) : "";
			len = strlen (new_path);
			base_len = strlen (imodel->priv->basedir);
			if ((len < base_len) ||
			    (strncmp (new_path, imodel->priv->basedir, base_len))) {
				add_error (imodel,
					   _("New path must be a subpath of the base directory"));
				g_set_error (error, GDA_DATA_MODEL_ERROR, GDA_DATA_MODEL_ACCESS_ERROR,
					     "%s",
					     _("New path must be a subpath of the base directory"));
				return FALSE;
			}

			old_path = compute_dirname (imodel, frow);
			if (dir_equal (new_path, old_path)) {
				g_free (old_path);
				g_print ("Paths are equal...\n");
				break;
			}

			if (!g_mkdir_with_parents (new_path, 0755)) {
				gchar *new_filename;
				GMappedFile *old_file;
				gboolean allok = FALSE;
				gchar *filename;

				new_filename = g_build_filename (new_path,
								 frow->raw_filename_value ? frow->raw_filename_value :
								 g_value_get_string (frow->filename_value), NULL);
				filename = compute_filename (imodel, frow);
				old_file = g_mapped_file_new (filename, FALSE, NULL);
				if (old_file) {
					if (g_file_set_contents (new_filename, g_mapped_file_get_contents (old_file),
								 g_mapped_file_get_length (old_file), NULL)) {
						g_unlink (filename);
						allok = TRUE;

						if (frow->data_value) {
							GdaBlob *blob;
							blob = (GdaBlob *) gda_value_get_blob (frow->data_value);
							if (blob && blob->op)
								_gda_dir_blob_set_filename (GDA_DIR_BLOB_OP (blob->op),
											   new_filename);
						}
					}
					g_mapped_file_unref (old_file);
				}
				if (!allok) {
					gchar *str;
					str = g_strdup_printf (_("Could not rename file '%s' to '%s'"),
							       filename, new_filename);
					add_error (imodel, str);
					g_set_error (error,
						     GDA_DATA_MODEL_ERROR, GDA_DATA_MODEL_ACCESS_ERROR,
						     "%s", str);
					g_free (str);
					g_free (new_filename);
					g_free (filename);
					g_free (old_path);
					return FALSE;
				}
				else {
					/* renaming succeeded => update FileRow */
#ifndef G_OS_WIN32
					g_rmdir (old_path);
#endif
					g_free (frow->reldir);
					frow->reldir = g_strdup (new_path + base_len);
				}
				g_free (filename);
				g_free (new_filename);
				has_changed = TRUE;
			}
			else {
				gchar *str;
				str = g_strdup_printf (_("Could not create directory '%s'"), new_path);
				add_error (imodel, str);
				g_set_error (error, GDA_DATA_MODEL_ERROR, GDA_DATA_MODEL_ACCESS_ERROR,
					     "%s", str);
				g_free (str);
				g_free (old_path);
				return FALSE;
			}
			g_free (old_path);
			break;
		}
		case COL_FILENAME: {
			gchar *new_filename;
			gchar *filename;

			new_filename = g_build_filename (imodel->priv->basedir,
							 frow->reldir,
							 g_value_get_string (value), NULL);
			filename = compute_filename (imodel, frow);
			if (g_rename (filename, new_filename)) {
				gchar *str;
				str = g_strdup_printf (_("Could not rename file '%s' to '%s'"), filename, new_filename);
				add_error (imodel, str);
				g_set_error (error, GDA_DATA_MODEL_ERROR, GDA_DATA_MODEL_ACCESS_ERROR,
					     "%s", str);
				g_free (str);
				g_free (new_filename);
				g_free (filename);
				return FALSE;
			}
			else {
				/* renaming succeeded => update FileRow */
				gda_value_free (frow->filename_value);
				frow->filename_value = gda_value_copy (value);
				if (frow->raw_filename_value) {
					g_free (frow->raw_filename_value);
					frow->raw_filename_value = g_strdup (g_value_get_string (value));
				}
				if (frow->data_value) {
					GdaBlob *blob;
					blob = (GdaBlob *) gda_value_get_blob (frow->data_value);
					if (blob && blob->op)
						_gda_dir_blob_set_filename (GDA_DIR_BLOB_OP (blob->op),
									   new_filename);
				}
			}
			g_free (new_filename);
			g_free (filename);
			has_changed = TRUE;
			break;
		}
		case COL_DATA: {
			GdaBlob *blob = NULL;
			if (gda_value_isa (value, GDA_TYPE_BLOB)) {
				blob = (GdaBlob *) gda_value_get_blob (value);
			}
			else if (gda_value_isa (value, GDA_TYPE_BINARY)) {
				blob = (GdaBlob *) gda_value_get_binary (value);
			}
			else if (gda_value_is_null (value)) {
				/* create a new empty blob */
				blob = g_new0 (GdaBlob, 1);
			}

			if (blob) {
				GdaBlobOp *op;
				gchar *filename;
				filename = compute_filename (imodel, frow);
				op = _gda_dir_blob_op_new (filename);
				if (gda_blob_op_write_all (op, blob) < 0) {
					gchar *str;
					str = g_strdup_printf (_("Could not overwrite contents of file '%s'"), filename);
					add_error (imodel, str);
					g_set_error (error, GDA_DATA_MODEL_ERROR,
						     GDA_DATA_MODEL_ACCESS_ERROR,
						     "%s", str);
					g_free (str);
					g_object_unref (op);
					g_free (filename);
					return FALSE;
				}
				g_object_unref (op);
				if (gda_value_is_null (value))
					g_free (blob);
				has_changed = FALSE;
				has_changed = update_file_size (frow, filename);
				has_changed = update_file_md5sum (frow, filename) || has_changed;
				has_changed = update_file_mime (frow, filename) || has_changed;
				g_free (filename);
			}
			else {
				add_error (imodel, _("Wrong type of data"));
				g_set_error (error, GDA_DATA_MODEL_ERROR, GDA_DATA_MODEL_ACCESS_ERROR,
					     "%s", _("Wrong type of data"));
				return FALSE;
			}
			break;
		}
		}
	}

	if (has_changed)
		/* signal changes to data model */
		gda_data_model_row_updated ((GdaDataModel *) model, row);

	return TRUE;
}
Ejemplo n.º 4
0
SimEvent *
sim_event_light_new_from_dm (GdaDataModel *dm, gint row)
{
  SimEvent *event;
  const GValue *value;
  const GdaBinary *binary;
  const GdaTimestamp *timestamp;
  struct tm tm;

  g_return_val_if_fail (GDA_IS_DATA_MODEL (dm), NULL);

  event = sim_event_new ();

  event->id = sim_uuid_new ();

  value = gda_data_model_get_value_at (dm, 0, row, NULL);
  if (!gda_value_is_null (value))
  {
    binary = (GdaBinary *) gda_value_get_blob (value);
    event->src_ia = sim_inet_new_from_db_binary (binary->data, binary->binary_length);
  }

  value = gda_data_model_get_value_at (dm, 1, row, NULL);
  if (!gda_value_is_null (value))
  {
    binary = (GdaBinary *) gda_value_get_blob (value);
    event->dst_ia = sim_inet_new_from_db_binary (binary->data, binary->binary_length);
  }

  value = gda_data_model_get_value_at (dm, 2, row, NULL);
  if (!gda_value_is_null (value))
    event->src_id = sim_uuid_new_from_blob (gda_value_get_blob (value));

  value = gda_data_model_get_value_at (dm, 3, row, NULL);
  if (!gda_value_is_null (value))
    event->dst_id = sim_uuid_new_from_blob (gda_value_get_blob (value));

  value = gda_data_model_get_value_at (dm, 4, row, NULL);
  event->src_port = gda_value_is_null (value) ? 0 : g_value_get_int (value);

  value = gda_data_model_get_value_at (dm, 5, row, NULL);
  event->dst_port = gda_value_is_null (value) ? 0 : g_value_get_int (value);

  value = gda_data_model_get_value_at (dm, 6, row, NULL);
  if (!gda_value_is_null (value))
  {
    timestamp = gda_value_get_timestamp (value);
    tm.tm_year = timestamp->year - 1900;
    tm.tm_mon = timestamp->month - 1;
    tm.tm_mday = timestamp->day;
    tm.tm_hour = timestamp->hour - 1;
    tm.tm_min = timestamp->minute;
    tm.tm_mday = timestamp->day;
    tm.tm_sec = timestamp->second;
    tm.tm_wday = 0;
    tm.tm_yday = 0;
    tm.tm_isdst = 0;
    event->time = mktime (&tm);
  }

  value = gda_data_model_get_value_at (dm, 7, row, NULL);
  event->tzone = gda_value_is_null (value) ? 0.0 : g_value_get_float (value);

  return event;
}
static gint
gda_data_model_dir_append_values (GdaDataModel *model, const GList *values, GError **error)
{
	GdaDataModelDir *imodel;
	const gchar *dirname = NULL, *filename = NULL;
	GdaBinary *bin_data = NULL;

	GList *list;
	gint col;
	g_return_val_if_fail (GDA_IS_DATA_MODEL_DIR (model), -1);
	imodel = (GdaDataModelDir *) model;
	g_return_val_if_fail (imodel->priv, -1);


	if (!values)
		return -1;
	for (col = 0, list = (GList *) values; list; list = list->next, col++) {
		GValue *value = (GValue *) list->data;
		if (!value || gda_value_is_null (value))
			continue;

		switch (col) {
		case COL_SIZE:
		case COL_MIME:
		case COL_MD5SUM:
		default:
			add_error (imodel, _("Column cannot be set"));
			g_set_error (error, GDA_DATA_MODEL_ERROR, GDA_DATA_MODEL_ACCESS_ERROR,
				     "%s",
				     _("Column cannot be set"));
			return -1;
		case COL_DIRNAME:
			if (G_VALUE_TYPE (value) == G_TYPE_STRING) {
				gint len, base_len;
				base_len = strlen (imodel->priv->basedir);
				dirname = g_value_get_string (value);
				len = strlen (dirname);
				if ((len < base_len) ||
				    (strncmp (dirname, imodel->priv->basedir, base_len))) {
					add_error (imodel, _("New path must be a subpath of the base directory"));
					g_set_error (error, GDA_DATA_MODEL_ERROR,
						     GDA_DATA_MODEL_ACCESS_ERROR,
						     "%s",
						     _("New path must be a subpath of the base directory"));
					return -1;
				}
			}
			break;
		case COL_FILENAME:
			if (G_VALUE_TYPE (value) == G_TYPE_STRING)
				filename = g_value_get_string (value);
			break;
		case COL_DATA:
			if (G_VALUE_TYPE (value) == GDA_TYPE_BLOB)
				bin_data = (GdaBinary *) gda_value_get_blob (value);
			else if (G_VALUE_TYPE (value) == GDA_TYPE_BINARY)
				bin_data = (GdaBinary *) gda_value_get_binary (value);
			break;
		}
	}

	if (dirname && filename && *filename) {
		if (!g_mkdir_with_parents (dirname, 0755)) {
			gchar *complete_filename;
			gboolean bin_to_free = FALSE;
			complete_filename = g_build_filename (dirname, filename, NULL);
			if (!bin_data) {
				bin_data = g_new0 (GdaBinary, 1);
				bin_to_free = TRUE;
			}
			if (g_file_set_contents (complete_filename, (gchar *) bin_data->data,
						 bin_data->binary_length, NULL)) {
				FileRow *row;

				row = file_row_new ();
				row->reldir = g_strdup (dirname + strlen (imodel->priv->basedir));
#ifndef G_OS_WIN32
				row->raw_filename_value = g_strdup (filename);
#else
				row->raw_filename_value = NULL; /* no need top copy on Windows */
#endif
				g_value_set_string (row->filename_value = gda_value_new (G_TYPE_STRING),
						    filename);

				/* file size */
				update_file_size (row, complete_filename);

				/* other attributes, computed only when needed */
				row->mime_value = NULL;
				row->md5sum_value = NULL;
				row->data_value = NULL;
				if (bin_to_free)
					g_free (bin_data);
				g_ptr_array_add (imodel->priv->rows, row);
				gda_data_model_row_inserted (model, imodel->priv->rows->len - 1);
				return imodel->priv->rows->len - 1;
			}
			else {
#ifndef G_OS_WIN32
				g_rmdir (dirname);
#endif
				gchar *str;
				str = g_strdup_printf (_("Cannot set contents of filename '%s'"), complete_filename);
				add_error (imodel, str);
				g_set_error (error, GDA_DATA_MODEL_ERROR, GDA_DATA_MODEL_ACCESS_ERROR,
					     "%s", str);
				g_free (str);
				if (bin_to_free)
					g_free (bin_data);
				return -1;
			}
		}
		else {
			gchar *str;
			str = g_strdup_printf (_("Cannot create directory '%s'"), dirname);
			add_error (imodel, str);
			g_set_error (error, GDA_DATA_MODEL_ERROR, GDA_DATA_MODEL_ACCESS_ERROR,
				     "%s", str);
			g_free (str);
			return -1;
		}
	}
	else {
		add_error (imodel, _("Cannot add row: filename missing"));
		g_set_error (error, GDA_DATA_MODEL_ERROR, GDA_DATA_MODEL_ACCESS_ERROR,
			     "%s", _("Cannot add row: filename missing"));
		return -1;
	}

	return -1;
}