check_meta_store_postgresql.c 3.09 KB
Newer Older
1
2
/*
 * Copyright (C) 2008 - 2011 Vivien Malerba <malerba@gnome-db.org>
3
 * Copyright (C) 2018 Daniel Espinosa <esodan@gmail.com>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
19
20
21
22
23
#include <libgda/libgda.h>
#include <stdio.h>
#include <glib/gstdio.h>
#include "common.h"

24
25
extern gchar *dbname;

26
int 
27
main(G_GNUC_UNUSED int argc, G_GNUC_UNUSED char ** argv)
28
{
29
	GdaConnection *cnc;
30
	GdaMetaStore *store;
31
	const gchar *cnc_string = NULL;
32
	GError *error = NULL;
33
	gboolean db_created = FALSE;
34
	const gchar *dbcreate_params = NULL;
35

36
	gda_init ();
37

38
	/*cnc_string = "DB_NAME=test;HOST=localhost;USERNAME=test;PASSWORD=test1";*/
39
40

	cnc_string = g_getenv ("POSTGRESQL_META_CNC");
41
	if (!cnc_string)
42
43
	{
		g_print ("PostgreSQL test not run, please set the POSTGRESQL_META_CNC environment variable \n"
44
			        "For example 'DB_NAME=$POSTGRES_DB;HOST=postgres;USERNAME=$POSTGRES_USER;PASSWORD=$POSTGRES_PASSWORD'\n");
45
46
		return EXIT_SUCCESS;
	}
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

	dbcreate_params = g_getenv ("POSTGRESQL_DBCREATE_PARAMS");

	if (!dbcreate_params)
	{
		g_print ("PostgreSQL test not run, please set the POSTGRESQL_DBCREATE_PARAMS environment variable \n");
		return EXIT_SUCCESS;
	}

	db_created = test_setup("PostgreSQL", dbcreate_params);

	if (!db_created) {
		g_print ("Can't setup database\n");
		return EXIT_FAILURE;
	}
// Append database name to the connection string
	GString *new_cnc_string = g_string_new(cnc_string);
	g_string_append_printf (new_cnc_string, ";DB_NAME=%s", dbname);

	g_print ("Connection string: %s\n", new_cnc_string->str);
67
	/* connection try */
68
69
70
71
72
73
74
75
	cnc = gda_connection_open_from_string ("PostgreSQL", new_cnc_string->str, NULL, GDA_CONNECTION_OPTIONS_NONE, &error);


	if (!cnc) {
	    g_warning ("Connection no established. Error: %s\n",
		       error && error->message ? error->message : "No error was set");
	    g_clear_error (&error);
	    return EXIT_FAILURE;
76
	}
77

78
	/* Clean everything which might exist in the store */
79
	gchar *str;
80
81
	str = g_strdup_printf ("PostgreSQL://%s", new_cnc_string->str);
	g_string_free (new_cnc_string, TRUE);
82
83
84
	store = gda_meta_store_new (str);
	common_drop_all_tables (store);
	g_object_unref (store);
85

86
87
88
89
90
91
92
93
94
	/* Test store setup */
	store = gda_meta_store_new (str);
	g_free (str);

	g_print ("STORE: %p, version: %d\n", store, store ? gda_meta_store_get_version (store) : 0);

	/* Tests */
	tests_group_1 (store);
	g_object_unref (store);
95
96
97
	if (db_created) {
		test_finish (cnc);
	}
98

99
	g_object_unref (cnc);
100

101
	g_print ("Test OK.\n");
102
103
104
105
106
107
108
	return EXIT_SUCCESS;
}