Beispiel #1
0
static void
meta_changed_cb (G_GNUC_UNUSED BrowserConnection *bcnc, GdaMetaStruct *mstruct, TablePreferences *tpref)
{
	gtk_list_store_clear (tpref->priv->columns_store);

	tpref->priv->current_table = NULL;
	tpref->priv->current_column = NULL;

	if (!mstruct)
		return;

	GdaMetaDbObject *dbo;
	GValue *schema_v = NULL, *name_v;
	const gchar *str;
	
	str = table_info_get_table_schema (tpref->priv->tinfo);
	if (str)
		g_value_set_string ((schema_v = gda_value_new (G_TYPE_STRING)), str);
	str = table_info_get_table_name (tpref->priv->tinfo);
	g_value_set_string ((name_v = gda_value_new (G_TYPE_STRING)), str);
	dbo = gda_meta_struct_get_db_object (mstruct, NULL, schema_v, name_v);
	if (schema_v)
		gda_value_free (schema_v);
	gda_value_free (name_v);

	if (dbo) {
		GdaMetaTable *mtable = GDA_META_TABLE (dbo);
		GSList *list;

		tpref->priv->current_table = mtable;
		for (list = mtable->columns; list; list = list->next) {
			GdaMetaTableColumn *column = GDA_META_TABLE_COLUMN (list->data);
			GtkTreeIter iter;
			gchar *eprops;
			GError *error = NULL;
			eprops = browser_connection_get_table_column_attribute (tpref->priv->bcnc,
										tpref->priv->current_table,
										column,
										BROWSER_CONNECTION_COLUMN_PLUGIN,
										&error);
			if (error) {
				TO_IMPLEMENT; /* FIXME: add a notice somewhere in the UI */
				g_warning ("Error: %s\n", error->message);
				g_clear_error (&error);
			}

			gtk_list_store_append (tpref->priv->columns_store, &iter);
			gtk_list_store_set (tpref->priv->columns_store, &iter,
					    COLUMN_POINTER, column,
					    COLUMN_GTYPE, column->gtype,
					    COLUMN_PLUGIN, eprops,
					    -1);
			g_free (eprops);
		}
	}
}
Beispiel #2
0
static GSList *
mgr_columns_update_children (GdaTreeManager *manager, GdaTreeNode *node, const GSList *children_nodes,
			     gboolean *out_error, GError **error)
{
	MgrColumns *mgr = MGR_COLUMNS (manager);
	GSList *nodes_list = NULL;
	GHashTable *ehash = NULL;

	if (out_error)
		*out_error = FALSE;

	if (children_nodes)
		ehash = hash_for_existing_nodes (children_nodes);

	GdaMetaStruct *mstruct;
	mstruct = t_connection_get_meta_struct (mgr->priv->tcnc);
	if (!mstruct) {
		g_set_error (error, MGR_COLUMNS_ERROR, MGR_COLUMNS_NO_META_STRUCT,
                             "%s", _("Not ready"));
                if (out_error)
                        *out_error = TRUE;
		return NULL;
	}

	GdaMetaDbObject *dbo;
	GValue *schema_v = NULL, *name_v;
	if (mgr->priv->schema)
		g_value_set_string ((schema_v = gda_value_new (G_TYPE_STRING)), mgr->priv->schema);
	g_value_set_string ((name_v = gda_value_new (G_TYPE_STRING)), mgr->priv->table_name);
	dbo = gda_meta_struct_get_db_object (mstruct, NULL, schema_v, name_v);
	if (schema_v)
		gda_value_free (schema_v);
	gda_value_free (name_v);

	if (!dbo) {
		g_set_error (error, MGR_COLUMNS_ERROR, MGR_COLUMNS_TABLE_NOT_FOUND,
                             "%s", _("Table not found"));
                if (out_error)
                        *out_error = TRUE;
		return NULL;
	}
	if ((dbo->obj_type != GDA_META_DB_TABLE) && (dbo->obj_type != GDA_META_DB_VIEW)) {
		g_set_error (error, MGR_COLUMNS_ERROR, MGR_COLUMNS_WRONG_OBJ_TYPE,
                             "%s", _("Requested object is not a table or view"));
                if (out_error)
                        *out_error = TRUE;
		return NULL;
	}
	
	GdaMetaTable *dbotable;
	GSList *columns;
	gint index;
	dbotable = GDA_META_TABLE (dbo);
	
	for (columns = dbotable->columns, index = 0;
	     columns;
	     columns = columns->next, index ++) {
		GdaMetaTableColumn *col;
		col = GDA_META_TABLE_COLUMN (columns->data);
		GdaTreeNode* snode = NULL;
		GValue *av;

		if (ehash)
			snode = g_hash_table_lookup (ehash, col->column_name);
		if (snode) {
			/* use the same node */
			g_object_ref (G_OBJECT (snode));
		}
		else {
			/* column's name */
			snode = gda_tree_manager_create_node (manager, node, NULL);
			if (col->pkey)
				g_value_take_string ((av = gda_value_new (G_TYPE_STRING)),
						     g_strdup_printf ("<b>%s</b>", col->column_name));
			else
				g_value_set_string ((av = gda_value_new (G_TYPE_STRING)), col->column_name);
			gda_tree_node_set_node_attribute (snode, MGR_COLUMNS_COL_NAME_ATT_NAME, av, NULL);
			gda_value_free (av);
		}
			
		/* column's type */
		g_value_set_string ((av = gda_value_new (G_TYPE_STRING)), col->column_type);
		gda_tree_node_set_node_attribute (snode, MGR_COLUMNS_COL_TYPE_ATT_NAME, av, NULL);
		gda_value_free (av);
		
		/* NOT NULL */
		g_value_set_boolean ((av = gda_value_new (G_TYPE_BOOLEAN)), !col->nullok);
		gda_tree_node_set_node_attribute (snode, MGR_COLUMNS_COL_NOTNULL_ATT_NAME, av, NULL);
		gda_value_free (av);
		
		/* default value */
		g_value_set_string ((av = gda_value_new (G_TYPE_STRING)), col->default_value);
		gda_tree_node_set_node_attribute (snode, MGR_COLUMNS_COL_DEFAULT_ATT_NAME, av, NULL);
		gda_value_free (av);
		
		/* icon */
		UiIconType type = UI_ICON_COLUMN;
		GdkPixbuf *pixbuf;
		gboolean is_fk = column_is_fk_part (dbo, index);
		if (col->pkey)
			type = UI_ICON_COLUMN_PK;
		else if (!col->nullok) {
			if (is_fk)
				type = UI_ICON_COLUMN_FK_NN;
			else
				type = UI_ICON_COLUMN_NN;
		}
		else if (is_fk)
			type = UI_ICON_COLUMN_FK;
		
		pixbuf = ui_get_pixbuf_icon (type);
		av = gda_value_new (G_TYPE_OBJECT);
		g_value_set_object (av, pixbuf);
		gda_tree_node_set_node_attribute (snode, "icon", av, NULL);
		gda_value_free (av);

		/* details */
		GString *details = NULL;
		if (col->pkey)
			details = g_string_new (_("Primary key"));
		if (is_fk) {
			if (details)
				g_string_append (details, ", ");
			else
				details = g_string_new ("");
			g_string_append (details, _("Foreign key"));
		}
		const GValue *autoinc;
		autoinc = gda_meta_table_column_get_attribute (col, GDA_ATTRIBUTE_AUTO_INCREMENT);
		if (autoinc) {
			if (details)
				g_string_append (details, ", ");
			else
				details = g_string_new ("");
			g_string_append (details, _("Auto incremented"));
		}
		if (details) {
			g_value_set_string ((av = gda_value_new (G_TYPE_STRING)), details->str);
			gda_tree_node_set_node_attribute (snode, MGR_COLUMNS_COL_DETAILS, av, NULL);
			gda_value_free (av);
			g_string_free (details, TRUE);
		}

		nodes_list = g_slist_prepend (nodes_list, snode);		
	}

	if (ehash)
		g_hash_table_destroy (ehash);

	return g_slist_reverse (nodes_list);
}
Beispiel #3
0
/*
 * Load data from file @file into table @table
 */
gboolean
test_cnc_load_data_from_file (GdaConnection *cnc, const gchar *table, const gchar *full_file, GError **error)
{
	GdaStatement *stmt = NULL;
	GdaSet *params = NULL;
	GdaDataModel *import;
	gint nrows, ncols, i;
	GdaMetaStruct *mstruct = NULL;
	GSList *list;
	gboolean retval = TRUE;

	/* loading XML file */
	import = gda_data_model_import_new_file (full_file, TRUE, NULL);
	if (gda_data_model_import_get_errors (GDA_DATA_MODEL_IMPORT (import))) {
		g_set_error (error, TEST_ERROR, TEST_ERROR_GENERIC, "Error loading '%s' file", full_file);
		return FALSE;
	}

	/* retrieving meta data info */
	GdaMetaDbObject *table_dbo;
	GValue *name_value;
	g_value_set_string ((name_value = gda_value_new (G_TYPE_STRING)), table);
	mstruct = gda_meta_struct_new (gda_connection_get_meta_store (cnc), GDA_META_STRUCT_FEATURE_NONE);
	table_dbo = gda_meta_struct_complement (mstruct, GDA_META_DB_TABLE,
						NULL, NULL, name_value, error);
	gda_value_free (name_value);
	if (! table_dbo) {
		retval = FALSE;
		goto out;
	}

	/* creating INSERT statement */
	GdaSqlStatement *st;
	GdaSqlStatementInsert *ist;
	GSList *insert_values_list = NULL;
	
	ist = g_new0 (GdaSqlStatementInsert, 1);
        GDA_SQL_ANY_PART (ist)->type = GDA_SQL_ANY_STMT_INSERT;
	ist->table = gda_sql_table_new (GDA_SQL_ANY_PART (ist));
        ist->table->table_name = g_strdup (table);

	GdaMetaTable *mtable = GDA_META_TABLE (table_dbo);
	for (list = mtable->columns; list; list = list->next) {
		GdaMetaTableColumn *tcol = GDA_META_TABLE_COLUMN (list->data);
		GdaSqlField *field;

		/* field */
		field = gda_sql_field_new (GDA_SQL_ANY_PART (ist));
		field->field_name = g_strdup (tcol->column_name);
		ist->fields_list = g_slist_append (ist->fields_list, field);

		/* value */
		GdaSqlParamSpec *pspec = g_new0 (GdaSqlParamSpec, 1);
		GdaSqlExpr *expr;
		pspec->name = g_strdup (tcol->column_name);
		pspec->g_type = tcol->gtype;
		pspec->nullok = tcol->nullok;
		expr = gda_sql_expr_new (GDA_SQL_ANY_PART (ist));
		expr->param_spec = pspec;
		insert_values_list = g_slist_append (insert_values_list, expr);
	}

        ist->values_list = g_slist_append (NULL, insert_values_list);
        st = gda_sql_statement_new (GDA_SQL_STATEMENT_INSERT);
        st->contents = ist;
	stmt = g_object_new (GDA_TYPE_STATEMENT, "structure", st, NULL);
        gda_sql_statement_free (st);
	g_object_unref (mstruct);

	if (! gda_statement_get_parameters (stmt, &params, error)) {
		retval = FALSE;
		goto out;
	}

	/* executing inserts */
	nrows = gda_data_model_get_n_rows (import);
	ncols = gda_data_model_get_n_columns (import);
	if (!gda_connection_begin_transaction (cnc, NULL, GDA_TRANSACTION_ISOLATION_UNKNOWN, error)) {
		retval = FALSE;
		goto out;
	}
	for (i = 0; i < nrows; i++) {
		gint j;
		GSList *list;
		for (list = params->holders, j = 0; list && (j < ncols); list = list->next, j++) {
			const GValue *cvalue = gda_data_model_get_value_at (import, j, i, error);
			if (!cvalue) {
				gda_connection_rollback_transaction (cnc, NULL, NULL);
				retval = FALSE;
				goto out;
			}
			if (! gda_holder_set_value (GDA_HOLDER (list->data), cvalue, error)) {
				gda_connection_rollback_transaction (cnc, NULL, NULL);
				retval = FALSE;
				goto out;
			}
		}

		if (list || (j < ncols)) {
			g_set_error (error, TEST_ERROR, TEST_ERROR_GENERIC, "%s", 
				     "Incoherent number of columns in table and imported data");
			gda_connection_rollback_transaction (cnc, NULL, NULL);
			retval = FALSE;
			goto out;
		}

		if (gda_connection_statement_execute_non_select (cnc, stmt, params, NULL, error) == -1) {
			gda_connection_rollback_transaction (cnc, NULL, NULL);
			retval = FALSE;
			goto out;
		}
	}

	if (! gda_connection_commit_transaction (cnc, NULL, error))
		retval = FALSE;

 out:
	if (import)
		g_object_unref (import);
	if (stmt)
		g_object_unref (stmt);
	if (params)
		g_object_unref (params);

	return retval;
}