Beispiel #1
0
int
main (int argc, char** argv)
{
	gchar *file;
	GError *error = NULL;
	GdaDataModel *model;
	guint i, nrows;
	guint nfailed = 0;
	gda_init ();

	/* generic parser */
	GdaSqlParser *parser;
	parser = gda_sql_parser_new ();
	nfailed += do_a_test (NULL, parser);
	g_object_unref (parser);

	/* test other parsers only if generic one is Ok */
	if (nfailed == 0) {
		model = gda_config_list_providers ();
		nrows = gda_data_model_get_n_rows (model);
		for (i = 0; i < nrows; i++) {
			const GValue *cvalue;
			cvalue = gda_data_model_get_value_at (model, 0, i, NULL);
			g_assert (cvalue && (G_VALUE_TYPE (cvalue) == G_TYPE_STRING));
			if (!g_ascii_strcasecmp (g_value_get_string (cvalue), "Oracle"))
				continue; /* ignore Oracle for now */
			g_print ("Testing database provider '%s'\n", g_value_get_string (cvalue));

			GdaServerProvider *prov;
			prov = gda_config_get_provider (g_value_get_string (cvalue), NULL);
			g_assert (prov);

			GdaSqlParser *parser;
			parser = gda_server_provider_create_parser (prov, NULL);
			if (!parser)
				parser = gda_sql_parser_new ();

			nfailed += do_a_test (prov, parser);
			g_object_unref (parser);
		}
		g_object_unref (model);
	}

	if (nfailed == 0) {
		g_print ("Ok\n");
		return EXIT_SUCCESS;
	}
	else {
		g_print ("%u failed\n", nfailed);
		return EXIT_FAILURE;
	}
}
static void
__initialize_statement_delete_query_parameters (MidgardDBObjectClass *klass, const gchar *query_string, gboolean add_workspace)
{
	GdaSqlParser *parser = gda_sql_parser_new ();
	GdaStatement *stmt;
	GError *error = NULL;
	stmt = gda_sql_parser_parse_string (parser, query_string, NULL, &error);

	if (!stmt) {
		g_error ("Couldn't create %s class DELETE(UPDATE) prepared statement. %s", 
				G_OBJECT_CLASS_NAME (klass), error && error->message ? error->message : "Unknown reason");
		return;
	}

	GdaSet *params; 
	if (!gda_statement_get_parameters (stmt, &params, &error)) {
		g_error ("Failed to create DELETE(UPDATE) GdaSet for %s class. %s", 
				G_OBJECT_CLASS_NAME (klass), error && error->message ? error->message : "Unknown reason");
	}
	
	if (add_workspace) {
		klass->dbpriv->_workspace_statement_delete = stmt;
		klass->dbpriv->_workspace_statement_delete_params = params;
		return;
	}
	
	klass->dbpriv->_statement_delete = stmt;
	klass->dbpriv->_statement_delete_params = params;

	return;
}
Beispiel #3
0
/*
 * Open a connection to the example.db file
 */
GdaConnection *
open_connection ()
{
        GdaConnection *cnc;
        GError *error = NULL;

	/* open connection */
        cnc = gda_connection_open_from_dsn ("SalesTest", NULL,
					    GDA_CONNECTION_OPTIONS_NONE,
					    &error);
        if (!cnc) {
                g_print ("Could not open connection: %s\n",
                         error && error->message ? error->message : "No detail");
                exit (1);
        }

	/* create an SQL parser */
	parser = gda_connection_create_parser (cnc);
	if (!parser) /* @cnc does not provide its own parser => use default one */
		parser = gda_sql_parser_new ();
	/* attach the parser object to the connection */
	g_object_set_data_full (G_OBJECT (cnc), "parser", parser, g_object_unref);

        return cnc;
}
Beispiel #4
0
/**
 * wmud_db_init:
 * @err: a GError to put error messages in it
 *
 * Initializes the wMUD database system. Checks and opens database files.
 */
gboolean
wmud_db_init(GError **err)
{
    GError *local_err = NULL;

    g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Initializing database");

    gda_init();

    g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
          "Database DSN is \"%s\"",
          active_config->database_dsn);
    dbh = gda_connection_open_from_string(NULL,
                                          active_config->database_dsn,
                                          NULL,
                                          GDA_CONNECTION_OPTIONS_THREAD_SAFE,
                                          &local_err);

    if (dbh == NULL) {
        g_set_error(err,
                    WMUD_DB_ERROR, WMUD_DB_ERROR_CANTOPEN,
                    "Can not open databsae (%s): %s",
                    active_config->database_dsn,
                    local_err->message);

        return FALSE;
    }

    parser = gda_sql_parser_new();

    g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Database initialization finished.");

    return TRUE;
}
Beispiel #5
0
static GdaStatement *
create_statement (const gchar *sql)
{
	GdaSqlParser *parser;
	GdaStatement *stmt;
	parser = gda_sql_parser_new ();
	stmt = gda_sql_parser_parse_string (parser,  sql, NULL, NULL);
	g_object_unref (parser);
	return stmt;
}
/**
 * gda_server_provider_internal_get_parser:
 * @prov: a #GdaServerProvider
 *
 * This is a factory method to get a unique instance of a #GdaSqlParser object
 * for each #GdaServerProvider object
 * Don't unref it.
 *
 * Returns: (transfer none): a #GdaSqlParser
 */
GdaSqlParser *
gda_server_provider_internal_get_parser (GdaServerProvider *prov)
{
	g_return_val_if_fail (GDA_IS_SERVER_PROVIDER (prov), NULL);
	if (prov->priv->parser)
		return prov->priv->parser;
	prov->priv->parser = gda_server_provider_create_parser (prov, NULL);
	if (!prov->priv->parser)
		prov->priv->parser = gda_sql_parser_new ();
	return prov->priv->parser;
}
Beispiel #7
0
/*
 * get_parameters()
 */
static gboolean
test1 (GError **error)
{
	GdaSqlParser *parser;
	guint i;

	parser = gda_sql_parser_new ();
	
	for (i = 0; i < sizeof (test1_data) / sizeof (ATest); i++) {
		ATest *test = &(test1_data[i]);
		GdaStatement *stmt;
		GError *lerror = NULL;

		stmt = gda_sql_parser_parse_string (parser, test->sql, NULL, &lerror);
		if (!stmt) {
			if (test->result) {
				if (lerror) 
					g_propagate_error (error, lerror);
				return FALSE;
			}
		}
		else { 
			GdaSet *set;
			if (!gda_statement_get_parameters (stmt, &set, &lerror)) {
				if (test->result) {
					if (lerror) 
						g_propagate_error (error, lerror);
					return FALSE;
				}
			}
			else if (set) {
				if (!tests_common_check_set (data, test->id, set, &lerror)) {
					if (lerror) 
						g_propagate_error (error, lerror);
					return FALSE;
				}
				g_object_unref (set);
			}
			g_object_unref (stmt);
		}

		if (lerror)
			g_error_free (lerror);
	}

	g_object_unref (parser);

	return TRUE;
}
Beispiel #8
0
static GdaSqlParser *
create_parser_for_provider (const gchar *prov_name)
{
	GdaServerProvider *prov;
	GdaSqlParser *parser;
	GError *error = NULL;

	prov = gda_config_get_provider (prov_name, &error);
	if (!prov) 
		g_error ("Could not create provider for '%s': %s\n", prov_name,
			 error && error->message ? error->message : "No detail");

	parser = gda_server_provider_create_parser (prov, NULL);
	if (!parser)
		parser = gda_sql_parser_new ();

	return parser;
}
Beispiel #9
0
/*
 * Returns: the number of failures
 */
static gint
do_test (const xmlChar *id, const xmlChar *sql, gboolean valid_expected) 
{
	static GdaSqlParser *parser = NULL;
	GdaStatement *stmt;
	gboolean is_valid;
	GError *error = NULL;

	if (!parser) {
		parser = gda_connection_create_parser (cnc);
		if (!parser)
			parser = gda_sql_parser_new ();
	}

#ifdef GDA_DEBUG
	g_print ("===== TEST %s SQL: @%s@\n", id, sql);
#endif

	stmt = gda_sql_parser_parse_string (parser, (const gchar*) sql, NULL, NULL);
	if (!stmt) {
		g_print ("ERROR for test '%s': could not parse statement\n", id);
		return FALSE;
	}
	is_valid = gda_statement_check_validity (stmt, cnc, &error);
	if (is_valid && !valid_expected) {
		g_print ("ERROR for test '%s': statement is valid but test expected it invalid\n", id);
		g_object_unref (stmt);
		return FALSE;
	}
	if (!is_valid && valid_expected) {
		g_print ("ERROR for test '%s': statement is invalid but test expected it valid: %s\n", id,
			 error && error->message ? error->message : "No detail");
		g_object_unref (stmt);
		return FALSE;
	}
	/*g_print ("EXP %d, got %d\n", valid_expected, is_valid);*/
	/*g_print ("PARSED: %s\n", gda_statement_serialize (stmt));*/

	g_object_unref (stmt);
	return TRUE;
}
Beispiel #10
0
GdaDataModel *
run_sql_select (GdaConnection *cnc, const gchar *sql)
{
        GdaStatement *stmt;
        GError *error = NULL;
        GdaDataModel *res;
        GdaSqlParser *parser;

        parser = gda_connection_create_parser (cnc);
	if (!parser)
		parser = gda_sql_parser_new ();

        stmt = gda_sql_parser_parse_string (parser, sql, NULL, NULL);
        g_object_unref (parser);

        res = gda_connection_statement_execute_select (cnc, stmt, NULL, &error);
        g_object_unref (stmt);
        if (!res)
                g_print ("Could not execute query: %s\n",
                         error && error->message ? error->message : "no detail");
        return res;
}
Beispiel #11
0
/* 
 * display the contents of the 'products' table 
 */
void
display_products_contents (GdaConnection *cnc)
{
	GdaDataModel *data_model;
	GdaStatement *stmt;
	gchar *sql = "SELECT * FROM products";
	GError *error = NULL;
	GdaSqlParser *parser;

	parser = gda_connection_create_parser (cnc);
        if (!parser) /* @cnc doe snot provide its own parser => use default one */
                parser = gda_sql_parser_new ();

	stmt = gda_sql_parser_parse_string (parser, sql, NULL, NULL);
	data_model = gda_connection_statement_execute_select (cnc, stmt, NULL, &error);
	g_object_unref (stmt);
        if (!data_model) 
                g_error ("Could not get the contents of the 'products' table: %s\n",
                         error && error->message ? error->message : "No detail");
	gda_data_model_dump (data_model, stdout);
	g_object_unref (data_model);
}
Beispiel #12
0
gboolean
run_sql_non_select (GdaConnection *cnc, const gchar *sql)
{
        GdaStatement *stmt;
        GError *error = NULL;
        gint nrows;
        GdaSqlParser *parser;

        parser = gda_connection_create_parser (cnc);
	if (!parser)
		parser = gda_sql_parser_new ();
        stmt = gda_sql_parser_parse_string (parser, sql, NULL, NULL);
        g_object_unref (parser);

        nrows = gda_connection_statement_execute_non_select (cnc, stmt, NULL, NULL, &error);
        g_object_unref (stmt);
        if (nrows == -1) {
                g_print ("NON SELECT error: %s\n", error && error->message ? error->message : "no detail");
		if (error)
			g_error_free (error);
		return FALSE;
	}
	return TRUE;
}
Beispiel #13
0
static GSList *
gda_tree_mgr_columns_update_children (GdaTreeManager *manager, GdaTreeNode *node, G_GNUC_UNUSED const GSList *children_nodes,
				      gboolean *out_error, GError **error)
{
	GdaTreeMgrColumns *mgr = GDA_TREE_MGR_COLUMNS (manager);
	GdaMetaStore *store;
	GdaDataModel *model;
	GSList *list = NULL;
	GdaConnection *scnc;
	GdaTreeMgrColumnsPrivate *priv = gda_tree_mgr_columns_get_instance_private (mgr);

	if (!priv->cnc && !priv->mstore) {
		g_set_error (error, GDA_TREE_MANAGER_ERROR, GDA_TREE_MANAGER_UNKNOWN_ERROR,
			     "%s", _("No connection and no GdaMetaStore specified"));
		if (out_error)
			*out_error = TRUE;
		return NULL;
	}
	else if (priv->mstore)
		store = priv->mstore;
	else
		store = gda_connection_get_meta_store (priv->cnc);

	scnc = gda_meta_store_get_internal_connection (store);

	/* create statements if necessary */
	if (!priv->stmt) {
		GdaSqlParser *parser;
		GdaStatement *stmt;

		parser = gda_connection_create_parser (scnc);
		if (! parser)
			parser = gda_sql_parser_new ();

		stmt = gda_sql_parser_parse_string (parser,
						    "SELECT column_name FROM _columns WHERE "
						    "table_schema= ##schema::string AND "
						    "table_name= ##table_name::string "
						    "ORDER BY ordinal_position", NULL, error);
		g_object_unref (parser);
		if (!stmt) {
			if (out_error)
				*out_error = TRUE;
			return NULL;
		}

		if (!gda_statement_get_parameters (stmt, &(priv->params), error)) {
			if (out_error)
				*out_error = TRUE;
			g_object_unref (stmt);
			return NULL;
		}
		priv->stmt = stmt;
	}


	gboolean schema_specified = FALSE;
	gboolean table_specified = FALSE;
	if (priv->schema) {
		schema_specified = TRUE;
		g_assert (gda_set_set_holder_value (priv->params, NULL, "schema",
						    priv->schema));
	}
	if (priv->table_name) {
		table_specified = TRUE;
		g_assert (gda_set_set_holder_value (priv->params, NULL, "table_name",
						    priv->table_name));
	}
	if (!schema_specified && node) {
		/* looking for a schema in @node's attributes */
		const GValue *cvalue;
		cvalue = gda_tree_node_fetch_attribute (node, "schema");
		if (cvalue) {
			GdaHolder *h = gda_set_get_holder (priv->params, "schema");
			if (!gda_holder_set_value (h, cvalue, error)) {
				if (out_error)
					*out_error = TRUE;
				return NULL;
			}
			schema_specified = TRUE;
		}
	}
	if (!table_specified && node) {
		/* looking for a table in @node's attributes */
		const GValue *cvalue;
		cvalue = gda_tree_node_fetch_attribute (node, "table_name");
		if (cvalue) {
			GdaHolder *h = gda_set_get_holder (priv->params, "table_name");
			if (!gda_holder_set_value (h, cvalue, error)) {
				if (out_error)
					*out_error = TRUE;
				return NULL;
			}
			table_specified = TRUE;
		}
	}

	if (!schema_specified) {
		g_set_error (error, GDA_TREE_MANAGER_ERROR, GDA_TREE_MANAGER_UNKNOWN_ERROR,
			     "%s", _("No schema specified"));
		if (out_error)
			*out_error = TRUE;
		return NULL;
	}

	if (!table_specified) {
		g_set_error (error, GDA_TREE_MANAGER_ERROR, GDA_TREE_MANAGER_UNKNOWN_ERROR,
			     "%s", _("No table specified"));
		if (out_error)
			*out_error = TRUE;
		return NULL;
	}

	model = gda_connection_statement_execute_select (scnc, priv->stmt, priv->params, error);

	if (!model) {
		if (out_error)
			*out_error = TRUE;
		return NULL;
	}

	GdaDataModelIter *iter;
	iter = gda_data_model_create_iter (model);
	for (; iter && gda_data_model_iter_move_next (iter);) {
		GdaTreeNode* snode;
		const GValue *cvalue;

		cvalue = gda_data_model_iter_get_value_at (iter, 0);
		if (!cvalue) {
			if (list) {
				g_slist_free_full (list, (GDestroyNotify) g_object_unref);
			}
			if (out_error)
				*out_error = TRUE;
			g_set_error (error, GDA_TREE_MANAGER_ERROR, GDA_TREE_MANAGER_UNKNOWN_ERROR,
				     "%s", _("Unable to get column name"));
			return NULL;
		}

		snode = gda_tree_manager_create_node (manager, node, g_value_get_string (cvalue));
		gda_tree_node_set_node_attribute (snode, "column_name", cvalue, NULL);
		list = g_slist_prepend (list, snode);
	}
	if (iter)
		g_object_unref (iter);
	g_object_unref (model);

	return list;
}
Beispiel #14
0
/*
 * render SQL
 */
static gboolean
test2 (GError **error)
{
	GdaSqlParser *parser = NULL;
	GHashTable *parsers_hash;
	GdaDataModel *providers_model;
	gint i;

	/* create parsers */
	parsers_hash = g_hash_table_new (g_str_hash, g_str_equal);
	providers_model = gda_config_list_providers ();
	for (i = 0; i < gda_data_model_get_n_rows (providers_model); i++) {
		const GValue *pname;
		pname = gda_data_model_get_value_at (providers_model, 0, i, error);
		if (!pname)
			return FALSE;
		parser = create_parser_for_provider (g_value_get_string (pname));
		g_hash_table_insert (parsers_hash, g_strdup (g_value_get_string (pname)), parser);
		g_print ("Created parser for provider %s\n", g_value_get_string (pname));
	}
	g_object_unref (providers_model);
	g_hash_table_insert (parsers_hash, "", gda_sql_parser_new ());
	
	xmlDocPtr doc;
        xmlNodePtr root, node;
	gchar *fname;
	
	fname = g_build_filename (ROOT_DIR, "tests", "parser", "testdata.xml", NULL);
	if (! g_file_test (fname, G_FILE_TEST_EXISTS)) {
                g_set_error (error, TEST_ERROR, TEST_ERROR_GENERIC, "File '%s' does not exist\n", fname);
		return FALSE;
        }
	doc = xmlParseFile (fname);
        g_free (fname);
        g_assert (doc);
        root = xmlDocGetRootElement (doc);
	g_assert (!strcmp ((gchar*) root->name, "testdata"));
        for (node = root->children; node; node = node->next) {
		if (strcmp ((gchar*) node->name, "test"))
                        continue;
		xmlNodePtr snode;
                xmlChar *sql = NULL;
                xmlChar *id;
                xmlChar *prov_name;

		prov_name = xmlGetProp (node, BAD_CAST "provider");
		if (prov_name) {
			parser = g_hash_table_lookup (parsers_hash, (gchar *) prov_name);
			xmlFree (prov_name);
		}
		else
			parser = g_hash_table_lookup (parsers_hash, "");
		if (!parser)
			continue;

		for (snode = node->children; snode && strcmp ((gchar*) snode->name, "sql"); snode = snode->next);
		if (!snode)
			continue;
		sql = xmlNodeGetContent (snode);
		if (!sql)
			continue;
		
		GdaStatement *stmt;
		GError *lerror = NULL;
		
		stmt = gda_sql_parser_parse_string (parser, sql, NULL, &lerror);
		xmlFree (sql);
		id = xmlGetProp (node, BAD_CAST "id");
		g_print ("===== TEST %s\n", id);

		if (!stmt) {
			/* skip that SQL if it can't be parsed */
			g_error_free (lerror);
			continue;
		}
		else { 
			GdaStatement *stmt2;
			gchar *rsql;
			gchar *ser1, *ser2;
			
			rsql = gda_statement_to_sql_extended (stmt, NULL, NULL, 0, NULL, &lerror);
			if (!rsql) {
				g_print ("REM: test '%s' can't be rendered: %s\n", id,
					 lerror && lerror->message ? lerror->message : "No detail");
				xmlFree (id);
				continue;
			}
			
			/*g_print ("--> rendered SQL: %s\n", rsql);*/
			stmt2 = gda_sql_parser_parse_string (parser, rsql, NULL, error);
			if (!stmt2) 
				return FALSE;
			
			GdaSqlStatement *sqlst;
			
			g_object_get (G_OBJECT (stmt), "structure", &sqlst, NULL);
			g_free (sqlst->sql);
			sqlst->sql = NULL;
			ser1 = gda_sql_statement_serialize (sqlst);
			gda_sql_statement_free (sqlst);
			
			g_object_get (G_OBJECT (stmt2), "structure", &sqlst, NULL);
			g_free (sqlst->sql);
			sqlst->sql = NULL;
			ser2 = gda_sql_statement_serialize (sqlst);
			gda_sql_statement_free (sqlst);
			
			if (strcmp (ser1, ser2)) {
				g_set_error (error, TEST_ERROR, TEST_ERROR_GENERIC,
					     "Statement failed, ID: %s\nSQL: %s\nSER1: %s\nSER2 :%s", 
					     id, rsql, ser1, ser2);
				g_free (ser1);
				g_free (ser2);
				return FALSE;
			}
			
			g_free (rsql);
			g_free (ser1);
			g_free (ser2);
			g_object_unref (stmt);
			g_object_unref (stmt2);
		}
		
		xmlFree (id);

		if (lerror)
			g_error_free (lerror);
	}

	g_object_unref (parser);

	return TRUE;
}
Beispiel #15
0
/*
 * Creates an SQLite .db file from the definitions in @sqlfile
 */
gboolean
create_sqlite_db (const gchar *dir, const gchar *dbname, const gchar *sqlfile, GError **error)
{
	GdaBatch *batch;
	GdaSqlParser *parser;
	GdaServerProvider *prov;
	GdaConnection *cnc;

	/* create batch */
	prov = gda_config_get_provider ("SQLite", NULL);
	if (!prov) {
		g_set_error (error, TEST_ERROR, TEST_ERROR_GENERIC, "%s", 
			     "Cannot find the SQLite provider");
		return FALSE;
	}
	parser = gda_server_provider_create_parser (prov, NULL);
	if (!parser)
		parser = gda_sql_parser_new ();
	
	batch = gda_sql_parser_parse_file_as_batch (parser, sqlfile, error);
	g_object_unref (parser);
	if (!batch)
		return FALSE;

	/* clean any previous DB file */
	gchar *fname, *tmp;
	tmp = g_strdup_printf ("%s.db", dbname);
	fname = g_build_filename (dir, tmp, NULL);
	g_free (tmp);
	g_unlink (fname);
	g_free (fname);

	/* open a connection */
	gchar *cnc_string;
	gchar *edir, *edbname;

	edir = gda_rfc1738_encode (dir);
	edbname = gda_rfc1738_encode (dbname);
	cnc_string = g_strdup_printf ("DB_DIR=%s;DB_NAME=%s", edir, edbname);
	g_free (edir);
	g_free (edbname);
	cnc = gda_connection_open_from_string ("SQLite", cnc_string, NULL, 
					       GDA_CONNECTION_OPTIONS_NONE, error);
	g_free (cnc_string);
	if (!cnc) {
		g_object_unref (batch);
		return FALSE;
	}

	/* execute batch */
	GSList *list;
	const GSList *stmt_list;
	gboolean retval = TRUE;
	list = gda_connection_batch_execute (cnc, batch, NULL, GDA_STATEMENT_MODEL_RANDOM_ACCESS, error);
	stmt_list = gda_batch_get_statements (batch);
	if (g_slist_length (list) != g_slist_length ((GSList *) stmt_list))
		retval = FALSE;

	g_slist_foreach (list, (GFunc) g_object_unref, NULL);
	g_slist_free (list);

	g_assert (gda_connection_close (cnc, NULL));
	g_object_unref (cnc);
	g_object_unref (batch);
	return retval;
}
Beispiel #16
0
static gboolean
do_test (ATest *test)
{
	GdaSqlParser *parser;
	GdaStatement *stmt;
	GError *error = NULL;
	gchar *tmp;

	g_print ("** test %s\n", test->id);
	parser = gda_sql_parser_new ();

	GdaSet *params;
	GValue *nv;
	GdaHolder *holder;
	stmt = gda_sql_parser_parse_string (parser, test->sql, NULL, &error);
	g_object_unref (parser);
	if (!stmt) {
		g_print ("Parsing error: %s\n", error && error->message ? error->message : "No detail");
		return FALSE;
	}

	if (! gda_statement_get_parameters (stmt, &params, &error)) {
		g_print ("Error: %s\n", error && error->message ? error->message : "No detail");
		return FALSE;
	}
	g_assert (gda_set_set_holder_value (params, NULL, "name", "zzz"));
	nv = gda_value_new_null ();
	holder = gda_set_get_holder (params, "id");
	g_assert (gda_holder_set_value (holder, nv, NULL));
	gda_value_free (nv);

	GdaSqlStatement *sqlst;
	g_object_get (stmt, "structure", &sqlst, NULL);

	sqlst = gda_rewrite_sql_statement_for_null_parameters (sqlst, params, NULL, &error);
	if (!sqlst) {
		g_print ("Rewrite error: %s\n", error && error->message ? error->message : "No detail");
		return FALSE;
	}
	g_object_set (stmt, "structure", sqlst, NULL);

	/* SQL rendering */
	tmp = gda_statement_to_sql_extended (stmt, NULL, NULL, GDA_STATEMENT_SQL_PARAMS_SHORT,
					     NULL, &error);
	if (!tmp) {
		g_print ("Rendering error: %s\n", error && error->message ? error->message : "No detail");
		return FALSE;
	}
	/*g_print ("SQL after mod: [%s]\n", tmp);*/
	g_free (tmp);

	tmp = gda_sql_statement_serialize (sqlst);
	if (!tmp) {
		g_print ("Error: gda_sql_statement_serialize() failed\n");
		return FALSE;
	}
	else if (strcmp (test->result, tmp)) {
		g_print ("Exp: [%s]\nGot: [%s]\n", test->result, tmp);
		return FALSE;
	}

	g_free (tmp);
	gda_sql_statement_free (sqlst);

	g_object_unref (stmt);
	return TRUE;
}
Beispiel #17
0
static GSList *
gda_tree_mgr_schemas_update_children (GdaTreeManager *manager, GdaTreeNode *node,
				      G_GNUC_UNUSED const GSList *children_nodes, gboolean *out_error,
				      GError **error)
{
	GdaTreeMgrSchemas *mgr = GDA_TREE_MGR_SCHEMAS (manager);
	GdaMetaStore *store;
	GdaDataModel *model;
	GSList *list = NULL;
	GdaConnection *scnc;

	if (!mgr->priv->cnc && !mgr->priv->mstore) {
		g_set_error (error, GDA_TREE_MANAGER_ERROR, GDA_TREE_MANAGER_UNKNOWN_ERROR,
			     "%s", _("No connection and no GdaMetaStore specified"));
		if (out_error)
			*out_error = TRUE;
		return NULL;
	}
	else if (mgr->priv->mstore)
		store = mgr->priv->mstore;
	else
		store = gda_connection_get_meta_store (mgr->priv->cnc);

	scnc = gda_meta_store_get_internal_connection (store);

	if (!mgr->priv->stmt) {
		GdaSqlParser *parser;
		GdaStatement *stmt;

		parser = gda_connection_create_parser (scnc);
		if (! parser)
			parser = gda_sql_parser_new ();

		stmt = gda_sql_parser_parse_string (parser,
						    "SELECT schema_name FROM _schemata "
						    "WHERE schema_internal = FALSE OR schema_name LIKE 'info%' "
						    "ORDER BY schema_name DESC", NULL, error);
		g_object_unref (parser);
		if (!stmt) {
			if (out_error)
				*out_error = TRUE;
			return NULL;
		}
		mgr->priv->stmt = stmt;
	}

	model = gda_connection_statement_execute_select (scnc, mgr->priv->stmt, NULL, error);
	if (!model) {
		if (out_error)
			*out_error = TRUE;
		return NULL;
	}

	GdaDataModelIter *iter;
	iter = gda_data_model_create_iter (model);
	for (; iter && gda_data_model_iter_move_next (iter);) {
		GdaTreeNode* snode;
		const GValue *cvalue;

		cvalue = gda_data_model_iter_get_value_at (iter, 0);
		if (!cvalue) {
			if (list) {
				g_slist_foreach (list, (GFunc) g_object_unref, NULL);
				g_slist_free (list);
			}
			if (out_error)
				*out_error = TRUE;
			g_set_error (error, GDA_TREE_MANAGER_ERROR, GDA_TREE_MANAGER_UNKNOWN_ERROR,
				     "%s", _("Unable to get schema name"));
			return NULL;
		}

		snode = gda_tree_manager_create_node (manager, node, g_value_get_string (cvalue));
		gda_tree_node_set_node_attribute (snode, "schema", cvalue, NULL);
		list = g_slist_prepend (list, snode);
	}
	if (iter)
		g_object_unref (iter);
	g_object_unref (model);

	return list;
}
gboolean __midgard_connection_open(
		MidgardConnection *mgd, 
		GHashTable **hashtable, gboolean init_schema)
{
	g_return_val_if_fail(mgd != NULL, FALSE);

	MIDGARD_ERRNO_SET (mgd, MGD_ERR_OK);

	gchar *host, *dbname, *dbuser, *dbpass, *loglevel, *tmpstr;
	guint port = 0;
	gchar *auth = NULL;
	MidgardConfig *config = mgd->priv->config;
	host = config->host;
	dbname = config->database;
	dbuser = config->dbuser;
	dbpass = config->dbpass;
	loglevel = config->loglevel;
	port = config->dbport;
	gboolean enable_threads = config->gdathreads;

	/* Get 30% performance boost for non threaded applications */
	if(!enable_threads) 
		g_setenv("LIBGDA_NO_THREADS", "yes", TRUE);

	/* Initialize libgda */
	gda_init ();

	midgard_connection_set_loglevel(mgd, loglevel, NULL);

	if(config->priv->dbtype == MIDGARD_DB_TYPE_SQLITE) {

		gchar *path = NULL;
		gchar *dbdir = config->dbdir;
		if (!dbdir || *dbdir == '\0') {
			const gchar *sqlite_dir[] = {"data", NULL};
			path = midgard_core_config_build_path(sqlite_dir, NULL, TRUE);
		} else {
			path = g_strdup(dbdir);
		}

		tmpstr = g_strconcat("DB_DIR=", path, ";", "DB_NAME=", dbname, NULL);
		g_free(path);

	} else if (config->priv->dbtype == MIDGARD_DB_TYPE_ORACLE) {

		GString *cnc = g_string_sized_new(100);
		cnc_add_part(cnc, "TNSNAME", dbname, MGD_MYSQL_HOST);
		cnc_add_part(cnc, "HOST", host, MGD_MYSQL_HOST);
		cnc_add_part(cnc, "DB_NAME", dbname, MGD_MYSQL_DATABASE);
		tmpstr = g_string_free(cnc, FALSE);
		cnc = g_string_sized_new(100);
		cnc_add_part(cnc, "USERNAME", dbuser, MGD_MYSQL_USERNAME);
		cnc_add_part(cnc, "PASSWORD", dbpass, MGD_MYSQL_PASSWORD);
		auth = g_string_free(cnc, FALSE);

	} else { 
		
		GString *cnc = g_string_sized_new(100);
		cnc_add_part(cnc, "HOST", host, MGD_MYSQL_HOST);

		if (port > 0) {

			GString *_strp = g_string_new("");
			g_string_append_printf (_strp, "%d", port);
			cnc_add_part (cnc, "PORT", _strp->str, "");
			g_string_free (_strp, TRUE);
		}

		cnc_add_part(cnc, "DB_NAME", dbname, MGD_MYSQL_DATABASE);
		tmpstr = g_string_free(cnc, FALSE);
		GString *auth_str = g_string_sized_new(100);
		cnc_add_part(auth_str, "USERNAME", dbuser, MGD_MYSQL_USERNAME);
		cnc_add_part(auth_str, "PASSWORD", dbpass, MGD_MYSQL_PASSWORD);
		auth = g_string_free(auth_str, FALSE);
	}

	GError *error = NULL;
	GdaConnection *connection = gda_connection_open_from_string(
			config->dbtype, tmpstr, auth, GDA_CONNECTION_OPTIONS_NONE, &error);
	g_free(auth);	

	if(connection == NULL) {

		MIDGARD_ERRNO_SET_STRING (mgd, MGD_ERR_NOT_CONNECTED, 
				" Database [%s]. %s", tmpstr, error->message);

		g_free(tmpstr);

		return FALSE;
	
	} 

	g_free(tmpstr);

	mgd->priv->parser = gda_connection_create_parser (connection);
	if (!mgd->priv->parser)
		mgd->priv->parser = gda_sql_parser_new();
	g_assert (mgd->priv->parser != NULL);

	mgd->priv->connection = connection;
	midgard_core_connection_connect_error_callback (mgd);	

	if(init_schema) {
		
		if(!g_type_from_name("midgard_quota")) {
			
			MidgardSchema *schema = g_object_new(MIDGARD_TYPE_SCHEMA, NULL);
			gchar *path = g_build_path(G_DIR_SEPARATOR_S, config->sharedir, "MidgardObjects.xml", NULL);
			midgard_schema_init(schema, (const gchar *)path);
			g_free(path);
			midgard_schema_read_dir(schema, config->sharedir);
			
			mgd->priv->schema = schema;
		}
	}

	//midgard_connection_set_loglevel(mgd, loglevel, NULL);

	/* Loads available authentication types */
	midgard_core_connection_initialize_auth_types(mgd);

	g_signal_emit (mgd, MIDGARD_CONNECTION_GET_CLASS (mgd)->signal_id_connected, 0);

	return TRUE;
}
Beispiel #19
0
int
main (int argc, char** argv)
{
    xmlDocPtr doc;
    xmlNodePtr root, node;
    GdaSqlParser *parser;
    gint failures = 0;
    gint ntests = 0;
    gchar *fname;
    GHashTable *parsers_hash;
    GdaDataModel *providers_model;
    gint i;

    gda_init ();

    /* load file */
    fname = g_build_filename (ROOT_DIR, "tests", "parser", "testdata.xml", NULL);
    if (! g_file_test (fname, G_FILE_TEST_EXISTS)) {
        g_print ("File '%s' does not exist\n", fname);
        exit (1);
    }

    /* create parsers */
    parsers_hash = g_hash_table_new (g_str_hash, g_str_equal);
    providers_model = gda_config_list_providers ();
    for (i = 0; i < gda_data_model_get_n_rows (providers_model); i++) {
        const GValue *pname;
        GError *lerror = NULL;
        pname = gda_data_model_get_value_at (providers_model, 0, i, &lerror);
        if (!pname) {
            g_print ("Can't get data model's value: %s",
                     lerror && lerror->message ? lerror->message : "No detail");
            exit (1);
        }
        parser = create_parser_for_provider (g_value_get_string (pname));
        g_hash_table_insert (parsers_hash, g_strdup (g_value_get_string (pname)), parser);
        g_print ("Created parser for provider %s\n", g_value_get_string (pname));
    }
    g_object_unref (providers_model);
    g_hash_table_insert (parsers_hash, "", gda_sql_parser_new ());

    /* use test data */
    doc = xmlParseFile (fname);
    g_free (fname);
    g_assert (doc);
    root = xmlDocGetRootElement (doc);
    g_assert (!strcmp ((gchar*) root->name, "testdata"));
    for (node = root->children; node; node = node->next) {
        if (strcmp ((gchar*) node->name, "test"))
            continue;
        xmlNodePtr snode;
        xmlChar *sql = NULL;
        xmlChar *id;
        xmlChar *prov_name;

        prov_name = xmlGetProp (node, BAD_CAST "provider");
        if (prov_name) {
            parser = g_hash_table_lookup (parsers_hash, (gchar *) prov_name);
            xmlFree (prov_name);
        }
        else
            parser = g_hash_table_lookup (parsers_hash, "");
        if (!parser)
            continue;

        id = xmlGetProp (node, BAD_CAST "id");
        for (snode = node->children; snode; snode = snode->next) {
            if (!strcmp ((gchar*) snode->name, "sql"))
                sql = xmlNodeGetContent (snode);
            else if (!strcmp ((gchar*) snode->name, "expected")) {
                xmlChar *expected;
                xmlChar *mode;

                expected = xmlNodeGetContent (snode);
                mode = xmlGetProp (snode, BAD_CAST "mode");
                if (sql) {
                    g_object_set (G_OBJECT (parser), "mode",
                                  mode && !strcmp ((gchar *) mode, "delim") ?
                                  GDA_SQL_PARSER_MODE_DELIMIT : GDA_SQL_PARSER_MODE_PARSE,
                                  NULL);
                    failures += do_test (parser, id,  sql, expected, NULL, NULL);
                    ntests++;
                }
                else
                    g_print ("===== Test '%s' doe not have any <sql> tag!\n", id);
                if (expected) xmlFree (expected);
                if (mode) xmlFree (mode);
            }
            else if (!strcmp ((gchar*) snode->name, "error")) {
                xmlChar *error_line, *error_col;
                xmlChar *mode;
                mode = xmlGetProp (snode, BAD_CAST "mode");
                error_line = xmlGetProp (snode, BAD_CAST "line");
                error_col = xmlGetProp (snode, BAD_CAST "col");
                if (sql) {
                    g_object_set (G_OBJECT (parser), "mode",
                                  mode && !strcmp ((gchar *) mode, "delim") ?
                                  GDA_SQL_PARSER_MODE_DELIMIT : GDA_SQL_PARSER_MODE_PARSE,
                                  NULL);
                    failures += do_test (parser, id, sql, NULL, error_line, error_col);
                    ntests++;
                }
                else
                    g_print ("===== Test '%s' doe not have any <sql> tag!\n", id);

                if (mode) xmlFree (mode);
                if (error_line)	xmlFree (error_line);
                if (error_col) xmlFree (error_col);
            }
        }

        /* mem free */
        if (sql) xmlFree (sql);
        if (id)	xmlFree (id);
    }
    xmlFreeDoc (doc);

    g_print ("TESTS COUNT: %d\n", ntests);
    g_print ("FAILURES: %d\n", failures);

    return failures != 0 ? 1 : 0;
}