![]() |
![]() |
![]() |
GNOME Data Access 3.0 manual | ![]() |
---|
Depending on the use you're going to get out of libgda, you may have to dig deep into its internals, but don't be afraid, things have been implemented to be easy to use.
If you want to develop applications using libgda, you should install the libgda-dev[el] package if you do a RPM or Debian-based installation. If you compiled the source code, development files are installed in your system.
The only step you need to do to make sure everything is well
installed, is to check that libgda libraries and binaries are seen
by your system. That is, make sure that the libgda
bin/
directory is in your
PATH
environment variable, as well as the
lib/
in your
LD_LIBRARY_PATH
(or
/etc/ld.so.conf
file).
You have to include a headers file, and it is:
#include <libgda/libgda.h>
If you want to access a data source through a GDA provider, you must first of all have access to this provider, and most importantly, this provider should have access to its specific data source. So, first have your database up and running. For this, you'll have to check your specific data source documentation, or see the libgda providers' specific documentation.
Once you've got your GDA provider installed, whether on your machine or on another one on the network, you must configure your local system to have access to it. If you're on a local installation, once you have installed the GDA provider (by compiling it or by installing its RPM or Debian package), the provider is visible in your machine. This is because the provider installs itself in a well known location that makes libgda itself know about the new provider.
Then, the next step is to configure the data sources you want available on your system. For doing this, you should, as for now, use GNOME-DB, which is a front-end to libgda for the GNOME project.
Command-line tools will be provided in libgda for doing so in a not-too-distant future, so you may want to know what information you need to setup a data source.
One of the problem GDA solves is the naming of data sources. Every database system has it's own way of defining names for it's databases. For example MySQL uses the hostname, port number, and the name of the database. Other databases, like Solid use the hostname and port number only. There is no support for multiple databases per server. Because the client does not need all these details, the libgda configuration defines all the properties of such a data source, so that the correct data base server can be contacted. This information is accessed by the client library and sent to the provider, which in turn will parse the string to decide which database must be connected to. The data stored for each data source is as follows:
[sales] Provider=MySQL DSN=DATABASE=test;HOST=localhost;PORT=1111 Description=MySQL Test Database in native mode Username=username Password=password
The provider for this database is the gda-mysql provider. The value of this entry is used as the object ID for the plug-in activation. |
|
This is the most important entry. The value of this entry is the string sent to the provider so that it knows which datasource to access. How this entry is interpreted by the providers is described in the provider section. There are, though, a set of default properties that can be used for the connection string for all providers. Those are:
|
|
The value of this entry is a short description of the datasource. It is here for convenience only and it is not used for any purpose. |
|
The user name to be used when connecting to the database. |
|
The password to be used when connecting to the database. This is stored in plain text, so be sure you restrict access to the configuration files to any "dangerous" users. |
For each user, libgda has two possible configuration files: one local to the user
which is stored in ~/.libgda/config
and one
which is for all the users on the system which is
<prefix>/etc/libgda/config
where
<prefix> is where the libgda library has been installed.
It is not recommended to modify configuration files by hand (instead use the gnome-database-properties application from the GNOME-DB library. For our example, a configuarion file is:
<?xml version="1.0"?> <libgda-config> <section path="/apps/libgda/Datasources/sales"> <entry name="DSN" type="string" value="PORT=1111;DATABASE=test;HOST=localhost"/> <entry name="Description" type="string" value="MySQL Test Database in native mode"/> <entry name="Password" type="string" value="password"/> <entry name="Provider" type="string" value="MySQL"/> <entry name="Username" type="string" value="username"/> </section> </libgda-config>
To create a data source you must use the function gda-config-save-data-source ()
Here you see how to create a data source named foo_ds. If you do not need to give an username or password to enter the database, you could put NULL.
gda_config_save_data_source ("foo_ds", "PostgreSQL", "DATABASE=foo_db", "description of foo_ds", "foo_username", "foo_password", FALSE); gda_config_save_data_source ("other_foo_ds", "MySQL", "DATABASE=other_foo_db,HOST=db.foo.com", "description of other_foo_ds", "foo", NULL, TRUE);
For more details about provider specific information see in the section about providers specific information.
There is no problem about calling several times to this function because if you save an existing data source, it is replaced.
To remove a data source you must use the function gda-config-remove-data-source ()
Here you see how to remove a data source named foo_ds.
gda_config_remove_data_source("foo_ds");
To list available data sources you must use the function gda_config_get_data_source_list ()
Here you see a function which lists the available data sources.
void list_datasources (void) { GList *ds_list; GList *node; GdaDataSourceInfo *info; ds_list = gda_config_get_data_source_list (); g_print ("\n"); for (node = g_list_first (ds_list); node != NULL; node = g_list_next (node)) { info = (GdaDataSourceInfo *) node->data; g_print ("NAME: %s PROVIDER: %s CNC: %s DESC: %s USER: %s PASSWORD: %s\n", info->name, info->provider, info->cnc_string, info->description, info->username, info->password); } g_print ("\n"); gda_config_free_data_source_list (ds_list); }
Our function. |
|
Note that you must free the list when you finish using it. |
To list the available providers you must use the function gda_config_get_provider_list ()
Here you see a function which lists available providers.
void list_providers (void) { GList *prov_list; GList *node; GdaProviderInfo *info; prov_list = gda_config_get_provider_list (); for (node = g_list_first (prov_list); node != NULL; node = node->next) { info = (GdaProviderInfo *) node->data; g_print ("ID: %s\n", info->id); } gda_config_free_provider_list (prov_list); }
Our function. |
|
Note that you must free the list when you finish using it. |
This section provides information specific to each of the available libgda providers.
The GDA default provider (based on an SQLite database file) is always installed
with libgda, which means that you've got always a default
database system available for you. Specifically the first time you use
libgda, a test database is copied into $HOME/.libgda
and
named "SalesTest".
The ODBC provider is a special case, since ODBC is itself a data access layer, the same as libgda, So, in the case of the GDA ODBC provider, the DSN string is completely up to the ODBC driver manager. That is, the GDA ODBC provider does not parse it all, nor does it try to understand what it means; it simply passes it over to the ODBC library.
So, if you want to use libgda with ODBC, you should first know how to set up an ODBC data source, and then just specify the DSN string you would pass to the ODBC library in the DSN string of the GDA data sources.
There is a project called unixODBC, which provides some graphical tools to help you in setting up ODBC data sources. You may find it interesting to give it a try.
Each provider exports a list of the arguments it requires in
its connection string which is used internally by libgda to
establish each connection. If you don't use a GUI which proposes
you valid choices (such as GNOME-DB), you can get that list in
each file named <provider_name>_specs_dsn.xml
installed in the $PREFIX/share/libgda
directory.
This file is easy to parse and read.