| 927 | | g_string_append_printf (sqlquery, " WHERE %s.guid = '%s'", table, guid); |
|---|
| 928 | | |
|---|
| 929 | | |
|---|
| 930 | | |
|---|
| 931 | | |
|---|
| 932 | | return TRUE; |
|---|
| | 924 | g_string_append_printf (sql, " WHERE %s.guid = '%s'", table, guid); |
|---|
| | 925 | |
|---|
| | 926 | /* Create statement and set parameters */ |
|---|
| | 927 | GdaSqlParser *parser = (MGD_OBJECT_CNC (object))->priv->parser; |
|---|
| | 928 | GdaStatement *stmt; |
|---|
| | 929 | GdaSet *params; |
|---|
| | 930 | GdaHolder *p; |
|---|
| | 931 | GError *error = NULL; |
|---|
| | 932 | |
|---|
| | 933 | stmt = gda_sql_parser_parse_string (parser, sql->str, NULL, &error); |
|---|
| | 934 | |
|---|
| | 935 | if (!stmt || error) { |
|---|
| | 936 | |
|---|
| | 937 | g_warning ("%s. Failed to create SQL statement for given query %s", |
|---|
| | 938 | error && error->message ? error->message : "Unknown reason", sql->str); |
|---|
| | 939 | if (stmt) |
|---|
| | 940 | g_object_unref (stmt); |
|---|
| | 941 | |
|---|
| | 942 | g_clear_error (&error); |
|---|
| | 943 | return FALSE; |
|---|
| | 944 | } |
|---|
| | 945 | |
|---|
| | 946 | g_string_free (sql, TRUE); |
|---|
| | 947 | if (!gda_statement_get_parameters (stmt, ¶ms, &error)) { |
|---|
| | 948 | |
|---|
| | 949 | g_warning ("Failed to get query parameters. %s", error && error->message ? error->message : "Unknown reason"); |
|---|
| | 950 | g_object_unref (stmt); |
|---|
| | 951 | if (error) g_clear_error (&error); |
|---|
| | 952 | return FALSE; |
|---|
| | 953 | } |
|---|
| | 954 | |
|---|
| | 955 | for (i = 0; i < n_prop; i++) { |
|---|
| | 956 | |
|---|
| | 957 | prop_attr = midgard_core_class_get_property_attr (klass, pspecs[i]->name); |
|---|
| | 958 | if (!prop_attr || (prop_attr && !prop_attr->field)) |
|---|
| | 959 | continue; |
|---|
| | 960 | |
|---|
| | 961 | gchar *prop_name = pspecs[i]->name; |
|---|
| | 962 | |
|---|
| | 963 | p = gda_set_get_holder (params, prop_name); |
|---|
| | 964 | |
|---|
| | 965 | if (!p) |
|---|
| | 966 | g_warning ("Failed to get holder for %s column", (gchar *) prop_name); |
|---|
| | 967 | |
|---|
| | 968 | /* FIXME, optimize this, we got property in previous loop */ |
|---|
| | 969 | g_value_init (&value, pspecs[i]->value_type); |
|---|
| | 970 | g_object_get_property (G_OBJECT (object), prop_name, &value); |
|---|
| | 971 | |
|---|
| | 972 | if (!gda_holder_set_value (p, &value, &error)) { |
|---|
| | 973 | g_warning ("Failed to set holder's value. %s", |
|---|
| | 974 | error && error->message ? error->message : "Unknown reason"); |
|---|
| | 975 | g_object_unref (stmt); |
|---|
| | 976 | if (error) g_clear_error (&error); |
|---|
| | 977 | return FALSE; |
|---|
| | 978 | } |
|---|
| | 979 | |
|---|
| | 980 | g_value_unset (&value); |
|---|
| | 981 | } |
|---|
| | 982 | |
|---|
| | 983 | gchar *debug_sql = gda_connection_statement_to_sql (cnc, stmt, params, GDA_STATEMENT_SQL_PRETTY, NULL, NULL); |
|---|
| | 984 | g_debug ("%s", debug_sql); |
|---|
| | 985 | g_free (debug_sql); |
|---|
| | 986 | |
|---|
| | 987 | gint retval = gda_connection_statement_execute_non_select (cnc, stmt, params, NULL, &error); |
|---|
| | 988 | |
|---|
| | 989 | if (error) { |
|---|
| | 990 | g_warning ("Failed to execute statement. %s", error && error->message ? error->message : "Unknown reason"); |
|---|
| | 991 | g_clear_error (&error); |
|---|
| | 992 | } |
|---|
| | 993 | |
|---|
| | 994 | g_free (pspecs); |
|---|
| | 995 | g_object_unref (params); |
|---|
| | 996 | g_object_unref (stmt); |
|---|
| | 997 | |
|---|
| | 998 | if (retval == -1) { |
|---|
| | 999 | |
|---|
| | 1000 | /* FIXME, provider error, handle this */ |
|---|
| | 1001 | return FALSE; |
|---|
| | 1002 | } |
|---|
| | 1003 | |
|---|
| | 1004 | if (retval >= 0) |
|---|
| | 1005 | return TRUE; |
|---|
| | 1006 | |
|---|
| | 1007 | return FALSE; |
|---|
| | 1008 | |
|---|
| | 1009 | |
|---|
| | 1010 | |
|---|
| | 1011 | return FALSE; |
|---|
| 1273 | | |
|---|
| 1274 | | #ifdef HAVE_LIBGDA_4 |
|---|
| 1275 | | /* create statement's parts */ |
|---|
| 1276 | | GdaSqlAnyPart *top; |
|---|
| 1277 | | |
|---|
| 1278 | | switch (query_type) { |
|---|
| 1279 | | |
|---|
| 1280 | | case GDA_SQL_STATEMENT_INSERT: |
|---|
| 1281 | | top = (GdaSqlAnyPart*) g_new0 (GdaSqlStatementInsert, 1); |
|---|
| 1282 | | break; |
|---|
| 1283 | | |
|---|
| 1284 | | case GDA_SQL_STATEMENT_UPDATE: |
|---|
| 1285 | | top = (GdaSqlAnyPart*) g_new0 (GdaSqlStatementUpdate, 1); |
|---|
| 1286 | | break; |
|---|
| 1287 | | |
|---|
| 1288 | | default: |
|---|
| 1289 | | g_assert_not_reached(); |
|---|
| 1290 | | } |
|---|
| 1291 | | |
|---|
| 1292 | | top->type = query_type; |
|---|
| 1293 | | |
|---|
| 1294 | | GdaSqlTable *sql_table; |
|---|
| 1295 | | GSList *fields_list = NULL; |
|---|
| 1296 | | GSList *expr_list = NULL; |
|---|
| 1297 | | |
|---|
| 1298 | | sql_table = gda_sql_table_new (top); |
|---|
| 1299 | | if (gda_sql_identifier_needs_quotes (table)) |
|---|
| 1300 | | sql_table->table_name = gda_sql_identifier_add_quotes (table); |
|---|
| 1301 | | else |
|---|
| 1302 | | sql_table->table_name = g_strdup (table); |
|---|
| 1303 | | for (; cols; cols = cols->next, values = values->next) { |
|---|
| 1304 | | GdaSqlField *field; |
|---|
| 1305 | | |
|---|
| 1306 | | field = gda_sql_field_new (top); |
|---|
| 1307 | | fields_list = g_slist_prepend (fields_list, field); |
|---|
| 1308 | | if (gda_sql_identifier_needs_quotes ((const gchar *) cols->data)) |
|---|
| 1309 | | field->field_name = gda_sql_identifier_add_quotes ((const gchar *) cols->data); |
|---|
| 1310 | | else |
|---|
| 1311 | | field->field_name = g_strdup ((const gchar *) cols->data); |
|---|
| 1312 | | |
|---|
| 1313 | | GdaSqlExpr *expr; |
|---|
| 1314 | | expr = gda_sql_expr_new (top); |
|---|
| 1315 | | expr_list = g_slist_prepend (expr_list, expr); |
|---|
| 1316 | | if (values->data) |
|---|
| 1317 | | expr->value = gda_value_copy ((GValue *) values->data); |
|---|
| 1318 | | else |
|---|
| 1319 | | expr->value = NULL; |
|---|
| 1320 | | } |
|---|
| 1321 | | |
|---|
| 1322 | | /* finalize statement's creation */ |
|---|
| 1323 | | switch (query_type) { |
|---|
| 1324 | | case GDA_SQL_STATEMENT_INSERT: { |
|---|
| 1325 | | GdaSqlStatementInsert *ins = (GdaSqlStatementInsert*) top; |
|---|
| 1326 | | ins->table = sql_table; |
|---|
| 1327 | | ins->fields_list = g_slist_reverse (fields_list); |
|---|
| 1328 | | ins->values_list = g_slist_prepend (NULL, g_slist_reverse (expr_list)); |
|---|
| 1329 | | break; |
|---|
| 1330 | | } |
|---|
| 1331 | | case GDA_SQL_STATEMENT_UPDATE: { |
|---|
| 1332 | | GdaSqlStatementUpdate *upd = (GdaSqlStatementUpdate*) top; |
|---|
| 1333 | | upd->table = sql_table; |
|---|
| 1334 | | upd->fields_list = g_slist_reverse (fields_list); |
|---|
| 1335 | | upd->expr_list = g_slist_reverse (expr_list); |
|---|
| 1336 | | if (where) { |
|---|
| 1337 | | gchar *tmp = g_strdup_printf ("SELECT * FROM a WHERE %s", where); |
|---|
| 1338 | | GdaStatement *tmpstmt; |
|---|
| 1339 | | |
|---|
| 1340 | | /* create a parser if necessary */ |
|---|
| 1341 | | if (!mgd->priv->parser) { |
|---|
| 1342 | | mgd->priv->parser = gda_connection_create_parser (mgd->priv->connection); |
|---|
| 1343 | | if (!mgd->priv->parser) |
|---|
| 1344 | | mgd->priv->parser = gda_sql_parser_new (); |
|---|
| 1345 | | } |
|---|
| 1346 | | tmpstmt = gda_sql_parser_parse_string (mgd->priv->parser, tmp, NULL, NULL); |
|---|
| 1347 | | g_assert (tmpstmt); |
|---|
| 1348 | | |
|---|
| 1349 | | GdaSqlStatement *sqlst; |
|---|
| 1350 | | g_object_get (tmpstmt, "structure", &sqlst, NULL); |
|---|
| 1351 | | g_object_unref (tmpstmt); |
|---|
| 1352 | | |
|---|
| 1353 | | |
|---|
| 1354 | | GdaSqlStatementSelect *sel = (GdaSqlStatementSelect*) sqlst->contents; |
|---|
| 1355 | | if (sel->where_cond) { |
|---|
| 1356 | | upd->cond = sel->where_cond; |
|---|
| 1357 | | GDA_SQL_ANY_PART (upd->cond)->parent = GDA_SQL_ANY_PART (upd); |
|---|
| 1358 | | sel->where_cond = NULL; |
|---|
| 1359 | | } |
|---|
| 1360 | | gda_sql_statement_free (sqlst); |
|---|
| 1361 | | } |
|---|
| 1362 | | break; |
|---|
| 1363 | | } |
|---|
| 1364 | | default: |
|---|
| 1365 | | g_assert_not_reached(); |
|---|
| 1366 | | } |
|---|
| 1367 | | |
|---|
| 1368 | | GdaSqlStatement *sqlst; |
|---|
| 1369 | | sqlst = gda_sql_statement_new (query_type); |
|---|
| 1370 | | sqlst->contents = top; |
|---|
| 1371 | | |
|---|
| | 1354 | /* Build SQL query string */ |
|---|
| | 1355 | GString *sql = g_string_new ("UPDATE "); |
|---|
| | 1356 | g_string_append_printf (sql, "%s SET ", table); |
|---|
| | 1357 | guint i = 0; |
|---|
| | 1358 | GList *cl, *vl; |
|---|
| | 1359 | |
|---|
| | 1360 | for (cl = cols, vl = values; cl != NULL; cl = cl->next, vl = vl->next) { |
|---|
| | 1361 | |
|---|
| | 1362 | _add_value_type_update (sql, (const gchar *) cl->data, (GValue *) vl->data, i > 0 ? TRUE : FALSE); |
|---|
| | 1363 | i++; |
|---|
| | 1364 | } |
|---|
| | 1365 | |
|---|
| | 1366 | g_string_append_printf (sql, " WHERE %s", where); |
|---|
| | 1367 | |
|---|
| | 1368 | /* Create statement and set parameters */ |
|---|
| | 1369 | GdaConnection *cnc = mgd->priv->connection; |
|---|
| | 1370 | GdaSqlParser *parser = mgd->priv->parser; |
|---|
| 1373 | | stmt = (GdaStatement*) g_object_new (GDA_TYPE_STATEMENT, "structure", sqlst, NULL); |
|---|
| 1374 | | gda_sql_statement_free (sqlst); |
|---|
| 1375 | | |
|---|
| 1376 | | gchar *debug_query = gda_statement_to_sql (stmt, NULL, NULL); |
|---|
| 1377 | | g_debug("CREATE: %s", debug_query); |
|---|
| 1378 | | g_free (debug_query); |
|---|
| 1379 | | |
|---|
| 1380 | | gint retval; |
|---|
| 1381 | | retval = gda_connection_statement_execute_non_select (mgd->priv->connection, stmt, NULL, NULL, NULL); |
|---|
| | 1372 | GdaSet *params; |
|---|
| | 1373 | GdaHolder *p; |
|---|
| | 1374 | GError *error = NULL; |
|---|
| | 1375 | |
|---|
| | 1376 | stmt = gda_sql_parser_parse_string (parser, sql->str, NULL, &error); |
|---|
| | 1377 | |
|---|
| | 1378 | if (!stmt || error) { |
|---|
| | 1379 | |
|---|
| | 1380 | g_warning ("%s. Failed to create SQL statement for given query %s", |
|---|
| | 1381 | error && error->message ? error->message : "Unknown reason", sql->str); |
|---|
| | 1382 | if (stmt) |
|---|
| | 1383 | g_object_unref (stmt); |
|---|
| | 1384 | |
|---|
| | 1385 | g_clear_error (&error); |
|---|
| | 1386 | return FALSE; |
|---|
| | 1387 | } |
|---|
| | 1388 | |
|---|
| | 1389 | g_string_free (sql, TRUE); |
|---|
| | 1390 | if (!gda_statement_get_parameters (stmt, ¶ms, &error)) { |
|---|
| | 1391 | |
|---|
| | 1392 | g_warning ("Failed to get query parameters. %s", error && error->message ? error->message : "Unknown reason"); |
|---|
| | 1393 | g_object_unref (stmt); |
|---|
| | 1394 | if (error) g_clear_error (&error); |
|---|
| | 1395 | return FALSE; |
|---|
| | 1396 | } |
|---|
| | 1397 | |
|---|
| | 1398 | for (cl = cols, vl = values; cl != NULL; cl = cl->next, vl = vl->next) { |
|---|
| | 1399 | |
|---|
| | 1400 | p = gda_set_get_holder (params, (gchar *) cl->data); |
|---|
| | 1401 | |
|---|
| | 1402 | if (!p) |
|---|
| | 1403 | g_warning ("Failed to get holder for %s column", (gchar *) cl->data); |
|---|
| | 1404 | |
|---|
| | 1405 | if (!gda_holder_set_value (p, (GValue *) vl->data, &error)) { |
|---|
| | 1406 | g_warning ("Failed to set holder's value. %s", |
|---|
| | 1407 | error && error->message ? error->message : "Unknown reason"); |
|---|
| | 1408 | g_object_unref (stmt); |
|---|
| | 1409 | if (error) g_clear_error (&error); |
|---|
| | 1410 | return FALSE; |
|---|
| | 1411 | } |
|---|
| | 1412 | } |
|---|
| | 1413 | |
|---|
| | 1414 | gchar *debug_sql = gda_connection_statement_to_sql (cnc, stmt, params, GDA_STATEMENT_SQL_PRETTY, NULL, NULL); |
|---|
| | 1415 | g_debug ("%s", debug_sql); |
|---|
| | 1416 | g_free (debug_sql); |
|---|
| | 1417 | |
|---|
| | 1418 | gint retval = gda_connection_statement_execute_non_select (cnc, stmt, params, NULL, &error); |
|---|
| | 1419 | |
|---|
| | 1420 | if (error) { |
|---|
| | 1421 | g_warning ("Failed to execute statement. %s", error && error->message ? error->message : "Unknown reason"); |
|---|
| | 1422 | g_clear_error (&error); |
|---|
| | 1423 | } |
|---|
| | 1424 | |
|---|
| | 1425 | g_object_unref (params); |
|---|