On Sun, 2006-11-19 at 20:30 +0100, Stian Skjelstad wrote: > I tried to update one of the samples. I haven't tried it yet, since I > don't have postgresql installed. Is this patch correct? I ment firebird in that email. Resending patch, now with updates for both firebird and postgresql.
Stian Skjelstad
diff -ur libgda-mysql/samples/sample-firebird.c libgda/samples/sample-firebird.c --- libgda-mysql/samples/sample-firebird.c 2006-09-10 16:19:21.000000000 +0200 +++ libgda/samples/sample-firebird.c 2006-11-19 20:25:26.000000000 +0100 @@ -44,38 +44,70 @@ GdaDataModel *dm_schema = NULL; GdaParameterList *prm_list; GdaParameter *prm; - GdaRow *row; - GValue *value; + const GValue *value; gint n_row; + gint n_col; + GError *error = NULL; prm_list = gda_parameter_list_new (NULL); prm = gda_parameter_new_boolean ("systables", show_systables); gda_parameter_list_add_param (prm_list, prm); - dm_schema = gda_connection_get_schema (cnc, cnc_schema, prm_list, NULL); + dm_schema = gda_connection_get_schema (cnc, cnc_schema, prm_list, &error); if (dm_schema) { + for (n_col = 0; n_col < gda_data_model_get_n_columns (dm_schema); n_col++) { + g_print ("%s\n", gda_data_model_get_column_title (dm_schema, n_col)); + } + g_print ("\n"); + for (n_row = 0; n_row < gda_data_model_get_n_rows (dm_schema); n_row++) { - row = (GdaRow *) gda_data_model_get_row (dm_schema, n_row); - value = gda_row_get_value (row, 0); - g_print (" %s\n", g_value_get_string (value)); + for (n_col = 0; n_col < gda_data_model_get_n_columns (dm_schema); n_col++) { + value = gda_data_model_get_value_at (dm_schema, n_col, n_row); + if (value && !gda_value_is_null (value)) + g_print ("%s\n", g_value_get_string (value)); + else + g_print ("NULL\n"); + } + g_print ("\n"); } - g_print ("\n"); - + g_object_unref (dm_schema); } else { g_print ("* Error getting schema *\n"); + if (error) + { + g_print ("%s\n", error->message); + g_error_free (error); + error = 0; + show_errors (cnc); + } } - g_object_unref (dm_schema); g_object_unref (prm_list); } gboolean -create_database (GdaConnection *cnc) +create_database (GdaClient *client) { - return (gda_connection_create_database (cnc, "127.0.0.1:/tmp/test_firebird.fdb")); + GError *error = 0; + GdaServerOperation* operation = gda_client_prepare_create_database(client, "127.0.0.1:/tmp/test_firebird.fdb"); + if ( ! operation ) + { + g_print ("gda_client_prepare_create_database failed\n"); + return FALSE; + } + if ( ! gda_client_perform_create_database (client, operation, &error)) + { + g_print ("gda_client_permform_create_database failed: %s\n", error->message); + g_error_free (error); + error = 0; + g_object_unref (operation); + return FALSE; + } + g_object_unref (operation); + return FALSE; } void @@ -84,9 +116,9 @@ gchar *dsn; dsn = g_strdup_printf ("DATABASE=127.0.0.1:%s", FIREBIRD_RO_DATABASE); - gda_config_save_data_source ("test_firebird_dummy", "Firebird", dsn, "Dummy read only database", NULL, NULL); + gda_config_save_data_source ("test_firebird_dummy", "Firebird", dsn, "Dummy read only database", NULL, NULL, FALSE); gda_config_save_data_source ("test_firebird", "Firebird", "DATABASE=127.0.0.1:/tmp/test_firebird.fdb", - "Test database", NULL, NULL); + "Test database", NULL, NULL, FALSE); g_free (dsn); } @@ -101,12 +133,11 @@ create_some_stuff (GdaConnection *cnc) { GdaCommand *command; - GdaTransaction *transaction; gboolean res = FALSE; + GError *error = 0; g_print ("Creating some tables and a view ... "); - transaction = gda_transaction_new ("some_stuff"); - if (gda_connection_begin_transaction (cnc, transaction)) { + if (gda_connection_begin_transaction (cnc, "mytrans", GDA_TRANSACTION_ISOLATION_SERIALIZABLE, &error)) { command = gda_command_new ("CREATE TABLE BIG(AN_INTEGER INTEGER NOT NULL," "A_VARCHAR VARCHAR(30), A_SMALLINT SMALLINT NOT NULL," "A_DATE DATE, A_TIME TIME, A_TS TIMESTAMP," @@ -115,21 +146,18 @@ "A_DOUBLE DOUBLE PRECISION, A_NULL CHAR(1))", GDA_COMMAND_TYPE_SQL, GDA_COMMAND_OPTION_STOP_ON_ERRORS); - gda_command_set_transaction (command, transaction); gda_connection_execute_non_select_command (cnc, command, NULL, NULL); gda_command_free (command); command = gda_command_new ("CREATE TABLE CHICA(EN_ESPANIOL INTEGER NOT NULL)", GDA_COMMAND_TYPE_SQL, GDA_COMMAND_OPTION_STOP_ON_ERRORS); - gda_command_set_transaction (command, transaction); gda_connection_execute_non_select_command (cnc, command, NULL, NULL); gda_command_free (command); command = gda_command_new ("CREATE TABLE KLEINE(AUS_DEUTSCHE INTEGER NOT NULL)", GDA_COMMAND_TYPE_SQL, GDA_COMMAND_OPTION_STOP_ON_ERRORS); - gda_command_set_transaction (command, transaction); gda_connection_execute_non_select_command (cnc, command, NULL, NULL); gda_command_free (command); @@ -137,20 +165,24 @@ "SELECT * FROM BIG", GDA_COMMAND_TYPE_SQL, GDA_COMMAND_OPTION_STOP_ON_ERRORS); - gda_command_set_transaction (command, transaction); - gda_connection_execute_nonselect_command (cnc, command, NULL, NULL); + gda_connection_execute_non_select_command (cnc, command, NULL, NULL); gda_command_free (command); - gda_connection_commit_transaction (cnc, transaction); - - res = TRUE; - g_print ("OK\n"); - + if ( ! gda_connection_commit_transaction (cnc, "mytrans", &error)) + { + g_print ("gda_connection_commit_transaction failed: %s\n", error->message); + g_error_free (error); + error = 0; + } else { + res = TRUE; + g_print ("OK\n"); + } } else { - g_print ("ERROR\n"); + g_print ("gda_connection_begin_transaction failed: %s\n", error->message); + g_error_free (error); + error = 0; } show_errors (cnc); - g_object_unref (transaction); return res; } @@ -159,17 +191,15 @@ populate_table_big (GdaConnection *cnc) { GdaCommand *command; - GdaTransaction *transaction; + GError *error = 0; g_print ("Populating table BIG ... "); - transaction = gda_transaction_new ("populate"); - if (gda_connection_begin_transaction (cnc, transaction)) { + if (gda_connection_begin_transaction (cnc, "mytrans2", GDA_TRANSACTION_ISOLATION_SERIALIZABLE, &error)) { command = gda_command_new ("INSERT INTO BIG VALUES(11,'Some varying string',0,'8-Nov-1977'," "'17:20','NOW','Some fixed char','134.5','67923.45','1234.5'," "'78123.45',NULL)", GDA_COMMAND_TYPE_SQL, GDA_COMMAND_OPTION_STOP_ON_ERRORS); - gda_command_set_transaction (command, transaction); gda_connection_execute_non_select_command (cnc, command, NULL, NULL); gda_command_free (command); @@ -178,7 +208,6 @@ "'67891.45',NULL)", GDA_COMMAND_TYPE_SQL, GDA_COMMAND_OPTION_STOP_ON_ERRORS); - gda_command_set_transaction (command, transaction); gda_connection_execute_non_select_command (cnc, command, NULL, NULL); gda_command_free (command); @@ -188,26 +217,29 @@ "'6923.45',NULL)", GDA_COMMAND_TYPE_SQL, GDA_COMMAND_OPTION_STOP_ON_ERRORS); - gda_command_set_transaction (command, transaction); gda_connection_execute_non_select_command (cnc, command, NULL, NULL); gda_command_free (command); - gda_connection_commit_transaction (cnc, transaction); - g_print ("OK\n"); - + if ( ! gda_connection_commit_transaction (cnc, "mytrans2", &error) ) + { + g_print ("gda_connection_commit_transaction failed: %s\n", error->message); + g_error_free (error); + error = 0; + show_errors (cnc); + } else + g_print ("OK\n"); } else { - g_print ("ERROR\n"); + g_print ("gda_connection_begin_transaction failed: %s\n", error->message); + g_error_free (error); + error = 0; show_errors (cnc); } - - g_object_unref (transaction); } void select_table_big (GdaConnection *cnc) { GdaCommand *command; - GdaTransaction *transaction; GdaDataModel *dm; GList *list, *node; GValue *valor; @@ -216,16 +248,15 @@ const GDate *d; const GdaTimestamp *ts; const GdaNumeric *numeric; + GError *error = 0; g_print("Selecting data from table BIG ... "); - transaction = gda_transaction_new ("show_big"); - if (gda_connection_begin_transaction (cnc, transaction)) { + if (gda_connection_begin_transaction (cnc, "mytrans3", GDA_TRANSACTION_ISOLATION_SERIALIZABLE, &error)) { command = gda_command_new ( "SELECT * FROM LIST_BIG", GDA_COMMAND_TYPE_SQL, GDA_COMMAND_OPTION_STOP_ON_ERRORS); - gda_command_set_transaction (command, transaction); list = gda_connection_execute_command (cnc, command, NULL, NULL); if (list != NULL) { g_print ("OK\n"); @@ -292,7 +323,12 @@ } - gda_connection_rollback_transaction (cnc, transaction); + if ( ! gda_connection_rollback_transaction (cnc, "mytrans3", &error) ) + { + g_print ("gda_connection_rollback_transaction failed: %s\n", error->message); + g_error_free (error); + error = 0; + } gda_command_free (command); } @@ -301,10 +337,10 @@ } } else { - g_print ("ERROR\n"); + g_print ("gda_connection_begin_transaction failed: %s\n", error->message); + g_error_free (error); + error = 0; } - - g_object_unref (transaction); } void @@ -312,6 +348,7 @@ { GdaClient *client; GdaConnection *connection; + GError *error = 0; g_print ("* Creating datasources (test_firebird and dummy_test_firebird) *\n"); save_datasource (); @@ -323,11 +360,12 @@ "test_firebird_dummy", FIREBIRD_USER, FIREBIRD_PWD, - GDA_CONNECTION_OPTIONS_READ_ONLY); - + GDA_CONNECTION_OPTIONS_READ_ONLY, + &error); + if (connection) { g_print ("OK\nCreating database \"/tmp/test_firebird.fdb\" ..."); - if (!create_database (connection)) { + if (!create_database (client)) { g_print ("ERROR\nCan't create test database :-( .Removing datasources, reporting error and exiting.\n"); show_errors (connection); remove_datasource (connection); @@ -340,10 +378,13 @@ connection = NULL; g_print ("Connecting to test database ... "); - connection = gda_client_open_connection (client, "test_firebird", FIREBIRD_USER, FIREBIRD_PWD, 0); + connection = gda_client_open_connection (client, "test_firebird", FIREBIRD_USER, FIREBIRD_PWD, 0, &error); if (!connection) { g_print ("ERROR\n"); + g_print ("%s\n", error->message); + g_error_free (error); + error = 0; remove_datasource (connection); return; @@ -379,6 +420,9 @@ remove_datasource (connection); } else { g_print ("ERROR\nCan't connect to dummy read-only database :-( .\n"); + g_print ("%s\n", error->message); + g_error_free (error); + error = 0; } gda_connection_close (connection); Only in libgda/samples: sample-postgresql diff -ur libgda-mysql/samples/sample-postgresql.c libgda/samples/sample-postgresql.c --- libgda-mysql/samples/sample-postgresql.c 2006-09-10 16:19:21.000000000 +0200 +++ libgda/samples/sample-postgresql.c 2006-11-19 20:57:14.000000000 +0100 @@ -48,7 +48,7 @@ for (node = g_list_first (list); node != NULL; node = g_list_next (node)) { error = (GdaConnectionEvent *) node->data; - g_print ("Error no: %d\t", gda_connection_event_get_code (error)); + g_print ("Error no: %ld\t", gda_connection_event_get_code (error)); g_print ("desc: %s\t", gda_connection_event_get_description (error)); g_print ("source: %s\t", gda_connection_event_get_source (error)); g_print ("sqlstate: %s\n", gda_connection_event_get_sqlstate (error)); @@ -59,11 +59,10 @@ static void show_table2 (GdaDataModel * dm) { - gint row_id; - gint column_id; - GValue *value; - GdaRow *row; - gchar *string; + gint row_id; + gint column_id; + gint n_col; + const GValue *value; g_print ("Data on the sample table:\n"); @@ -76,16 +75,16 @@ g_print ("\n---------------------------------------\n"); for (row_id = 0; row_id < gda_data_model_get_n_rows (dm); row_id++) { - row = (GdaRow *) gda_data_model_get_row (dm, row_id); - for (column_id = 0; column_id < gda_data_model_get_n_columns (dm); - column_id++) { - value = gda_row_get_value (row, column_id); - string = gda_value_stringify (value); - g_print ("%s\t", string); - g_free (string); + for (n_col = 0; n_col < gda_data_model_get_n_columns (dm); n_col++) { + value = gda_data_model_get_value_at (dm, n_col, row_id); + if (value && !gda_value_is_null (value)) + g_print ("%s\n", g_value_get_string (value)); + else + g_print ("NULL\n"); } g_print ("\n"); } + g_print ("\n"); } int @@ -101,12 +100,12 @@ GList *res_cmd; GList *node; gboolean errors = FALSE; - gint rows; /* Connection data */ gchar *dsn_name = "gda-auto"; gchar *username = "acs"; gchar *password = ""; + GError *error = 0; /* 1. The first: init libgda */ gda_init ("planner-gda", NULL, argc, argv); @@ -115,22 +114,25 @@ gda_config_save_data_source (dsn_name, "PostgreSQL", "DATABASE=test", - "postgresql test", username, password); + "postgresql test", username, password, FALSE); /* 3. Create a gda client */ client = gda_client_new (); /* 4. Open the connection */ - con = gda_client_open_connection (client, dsn_name, NULL, NULL, 0); + con = gda_client_open_connection (client, dsn_name, NULL, NULL, 0, &error); if (!GDA_IS_CONNECTION (con)) { g_print ("** ERROR: could not open connection to %s\n", dsn_name); + g_print ("%s\n", error->message); + g_error_free (error); + error = 0; get_errors (con); return 0; } /* 5. Our first query with results */ cmd = gda_command_new ("SELECT * from test1", GDA_COMMAND_TYPE_SQL, GDA_COMMAND_OPTION_STOP_ON_ERRORS); - res_cmd = gda_connection_execute_command (con, cmd, NULL); + res_cmd = gda_connection_execute_command (con, cmd, NULL, &error); if (res_cmd != NULL) for (node = g_list_first (res_cmd); node != NULL; node = g_list_next (node)) { @@ -145,13 +147,16 @@ } else { errors = TRUE; + g_print ("gda_connection_execute_command failed: %s\n", error->message); + g_error_free (error); + error = 0; get_errors (con); } gda_command_free (cmd); /* 6. Our first query with no SQL results to analyze */ cmd = gda_command_new ("INSERT INTO test1 VALUES ('fool_txt')", GDA_COMMAND_TYPE_SQL, GDA_COMMAND_OPTION_STOP_ON_ERRORS); - (gda_connection_execute_select_command (con, cmd, NULL, NULL) == GDA_CONNECTION_EXEC_FAILED) + if ( ! gda_connection_execute_select_command (con, cmd, NULL, NULL) ) get_errors (con); gda_command_free (cmd);
_______________________________________________ gnome-db-list mailing list gnome-db-list@gnome.org http://mail.gnome.org/mailman/listinfo/gnome-db-list