Пример #1
0
/**
 * gda_sql_statement_update_take_set_value
 * @stmt: a #GdaSqlStatement pointer
 * @fname: a field name, as a G_TYPE_STRING #GValue
 * @expr: a #GdaSqlExpr pointer
 *
 * Specifies that the field named @fname will be updated with the expression @expr.
 *
 * @fname and @expr's responsibility are transferred to
 * @stmt (which means @stmt is then responsible for freeing them when no longer needed).
 */
void
gda_sql_statement_update_take_set_value (GdaSqlStatement *stmt, GValue *fname, GdaSqlExpr *expr)
{
	GdaSqlStatementUpdate *update = (GdaSqlStatementUpdate *) stmt->contents;
	GdaSqlField *sf;

	sf = gda_sql_field_new (GDA_SQL_ANY_PART (update));
	gda_sql_field_take_name (sf, fname);
	update->fields_list = g_slist_append (update->fields_list, sf);

	update->expr_list = g_slist_append (update->expr_list, expr);
	gda_sql_any_part_set_parent (expr, update);
}
Пример #2
0
/**
 * gda_sql_field_copy
 * @field: a #GdaSqlAnyPart
 *
 * Creates a new GdaSqlField structure initiated with the values stored in @field.
 *
 * Returns: a new #GdaSqlField structure.
 */
GdaSqlField *
gda_sql_field_copy (GdaSqlField *field)
{
	GdaSqlField *copy;
	if (!field) return NULL;

	copy = gda_sql_field_new (NULL);
	if (field->field_name)
		copy->field_name = g_strdup (field->field_name);
	copy->validity_meta_table_column = field->validity_meta_table_column;

	return copy;
}
Пример #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;
}