libdballe  5.18
Public Member Functions | Static Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
dballe::DB Class Reference

DB-ALLe database connection. More...

#include <db.h>

Public Member Functions

void connect (const char *dsn, const char *user, const char *password)
 Start a session with DB-All.e.
void connect_generic (const char *config)
 Start a session with DB-All.e.
void connect_from_file (const char *pathname)
 Create from a SQLite file pathname.
void connect_from_url (const char *url)
 Create from an url-like specification, that can be:
void connect_test ()
 Start a test session with DB-All.e.
db::Repinforepinfo ()
 Access the repinfo table.
db::Stationstation ()
 Access the station table.
db::Contextcontext ()
 Access the context table.
db::Datadata ()
 Access the data table.
db::Attrattr ()
 Access the data table.
void reset (const char *repinfo_file=0)
 Reset the database, removing all existing DBALLE tables and re-creating them empty.
void delete_tables ()
 Delete all the DB-ALLe tables from the database.
void update_repinfo (const char *repinfo_file, int *added, int *deleted, int *updated)
 Update the repinfo table in the database, with the data found in the given file.
int rep_cod_from_memo (const char *memo)
 Get the report code from a report mnemonic.
const std::string & rep_memo_from_cod (int rep_cod)
 Get the report mnemonic from a report code.
bool check_rep_cod (int rep_cod)
 Verify that a rep_cod is supported by the database.
int last_station_insert_id ()
 Return the ID of the last inserted station.
int last_context_insert_id ()
 Return the ID of the last inserted context.
int get_rep_cod (Record &rec)
 Get the report id from this record.
int obtain_station (Record &rec, bool can_add=true)
int obtain_context (Record &rec)
void insert (Record &rec, bool can_replace, bool station_can_add)
 Insert a record into the database.
void remove (const Record &rec)
 Remove data from the database.
void remove_orphans ()
 Remove orphan values from the database.
std::auto_ptr< db::Cursorquery (const Record &query, unsigned int wanted, unsigned int modifiers)
 Create and execute a database query.
std::auto_ptr< db::Cursorquery_stations (const Record &query)
 Start a query on the station archive.
std::auto_ptr< db::Cursorquery_data (const Record &rec)
 Query the database.
unsigned query_attrs (int id_context, wreport::Varcode id_var, const db::AttrList &qcs, Record &attrs)
 Query attributes.
void attr_insert_or_replace (int id_context, wreport::Varcode id_var, const Record &attrs, bool can_replace)
 Insert new attributes into the database.
void attr_insert (int id_context, wreport::Varcode id_var, const Record &attrs)
 Insert new attributes into the database.
void attr_insert_new (int id_context, wreport::Varcode id_var, const Record &attrs)
 Insert new attributes into the database.
void attr_remove (int id_context, wreport::Varcode id_var, const db::AttrList &qcs)
 Delete QC data for the variable `var' in record `rec' (coming from a previous dba_query)
void import_msg (const Msg &msg, const char *repmemo, int flags)
 Import a Msg message into the DB-All.e database.
void import_msgs (const Msgs &msgs, const char *repmemo, int flags)
 Import Msgs messages into the DB-All.e database.
void export_msgs (const Record &query, MsgConsumer &cons)
 Perform the query in `query', and return the results as a NULL-terminated array of dba_msg.
void dump (FILE *out)
 Dump the entire contents of the database to an output stream.

Static Public Member Functions

static bool is_url (const char *str)
 Return TRUE if the string looks like a DB URL.

Data Fields

db::Connectionconn
 ODBC database connection.

Protected Member Functions

void init_after_connect ()
void run_sql (const char *query)
 Run a one-off SQL query.
void drop_table_if_exists (const char *name)
 Delete a table in the database if it exists, otherwise do nothing.
void drop_sequence_if_exists (const char *name)
 Delete a sequence in the database if it exists, otherwise do nothing.
void fill_ana_layer (Msg &msg, int id_station, int id_report)
 Fill a message station info layer with information from the given station and report IDs.

Protected Attributes

db::Statementstm_last_insert_id
 Precompiled LAST_INSERT_ID (or equivalent) SQL statement.
DBALLE_SQL_C_SINT_TYPE m_last_insert_id
 ID of the last autogenerated primary key.
struct db::Repinfom_repinfo
 Accessors for the various parts of the database.
struct db::Stationm_station
 Station information.
struct db::Contextm_context
 Variable context.
struct db::Datam_data
 Variable data.
struct db::Attrm_attr
 Variable attributes.
db::Sequenceseq_station
 Sequence accessors.
db::Sequenceseq_context
 Context ID sequence.

Detailed Description

DB-ALLe database connection.


Member Function Documentation

void dballe::DB::attr_insert ( int  id_context,
wreport::Varcode  id_var,
const Record attrs 
)

Insert new attributes into the database.

If the same attribute exists for the same data, it is overwritten

Parameters:
id_contextThe database id of the context related to the attributes to insert
id_varThe varcode of the variable related to the attributes to add. See vartable::h
attrsThe record with the attributes to be added
void dballe::DB::attr_insert_new ( int  id_context,
wreport::Varcode  id_var,
const Record attrs 
)

Insert new attributes into the database.

If the same attribute exists for the same data, the function fails.

Parameters:
id_contextThe database id of the context related to the attributes to insert
id_varThe varcode of the variable related to the attributes to add. See vartable::h
attrsThe record with the attributes to be added
void dballe::DB::attr_insert_or_replace ( int  id_context,
wreport::Varcode  id_var,
const Record attrs,
bool  can_replace 
)

Insert new attributes into the database.

Parameters:
id_contextThe database id of the context related to the attributes to insert
id_varThe varcode of the variable related to the attributes to add. See vartable::h
attrsThe record with the attributes to be added
can_replaceIf true, then existing data can be rewritten, else data can only be added.
void dballe::DB::attr_remove ( int  id_context,
wreport::Varcode  id_var,
const db::AttrList &  qcs 
)

Delete QC data for the variable `var' in record `rec' (coming from a previous dba_query)

Parameters:
id_contextThe database id of the context related to the attributes to remove
id_varThe varcode of the variable related to the attributes to remove. See vartable::h
qcsArray of WMO codes of the attributes to delete. If empty, all attributes associated to id_data will be deleted.
bool dballe::DB::check_rep_cod ( int  rep_cod)

Verify that a rep_cod is supported by the database.

Parameters:
rep_codThe report code to verify
Returns:
true if the report code is supported, false if not
void dballe::DB::connect ( const char *  dsn,
const char *  user,
const char *  password 
)

Start a session with DB-All.e.

Parameters:
dsnThe ODBC DSN of the database to use
userThe user name to use to connect to the DSN
passwordThe password to use to connect to the DSN. To specify an empty password, pass "" or NULL
Return values:
dbThe dba_db handle returned by the function
Returns:
The error indicator for the function (See error::h)
void dballe::DB::connect_from_file ( const char *  pathname)

Create from a SQLite file pathname.

Parameters:
pathnameThe pathname to a SQLite file
void dballe::DB::connect_from_url ( const char *  url)

Create from an url-like specification, that can be:

sqlite:[//]foo.sqlite odbc://[user[:pass]@]dsn test:[//]

Parameters:
urlThe url-like connection descriptor
void dballe::DB::connect_generic ( const char *  config)

Start a session with DB-All.e.

Parameters:
configThe string with the configuration to use to start the connection. This is passed as is to SQLDriverConnect, so see ODBC documentation for its format.

Start a test session with DB-All.e.

Take information from the environment (

See also:
dba_db_create_from_env) and default to ./test.sqlite if nothing is specified.
void dballe::DB::export_msgs ( const Record query,
MsgConsumer cons 
)

Perform the query in `query', and return the results as a NULL-terminated array of dba_msg.

Parameters:
queryThe query to perform
consThe MsgsConsumer that will handle the resulting messages

Get the report id from this record.

If rep_memo is specified instead, the corresponding report id is queried in the database and set as "rep_cod" in the record.

void dballe::DB::import_msg ( const Msg msg,
const char *  repmemo,
int  flags 
)

Import a Msg message into the DB-All.e database.

Parameters:
dbThe DB-All.e database to write the data into
msgThe Msg containing the data to import
repmemoReport mnemonic to which imported data belong. If NULL is passed, then it will be chosen automatically based on the message type.
flagsCustomise different aspects of the import process. It is a bitmask of the various DBA_IMPORT_* macros.
void dballe::DB::import_msgs ( const Msgs msgs,
const char *  repmemo,
int  flags 
)

Import Msgs messages into the DB-All.e database.

Parameters:
dbThe DB-All.e database to write the data into
msgsThe Msgs containing the data to import
repmemoReport mnemonic to which imported data belong. If NULL is passed, then it will be chosen automatically based on the message type.
flagsCustomise different aspects of the import process. It is a bitmask of the various DBA_IMPORT_* macros.
void dballe::DB::insert ( Record rec,
bool  can_replace,
bool  station_can_add 
)

Insert a record into the database.

In a record with the same phisical situation already exists, the function fails.

ana_id and context_id will be set in the record at the end of this function.

Parameters:
recThe record to insert.
can_replaceIf true, then existing data can be rewritten, else data can only be added.
station_can_addIf true, then it is allowed to add new station records to the database. Otherwise, data can be added only by reusing existing ones.
static bool dballe::DB::is_url ( const char *  str) [static]

Return TRUE if the string looks like a DB URL.

Parameters:
strThe string to test
Returns:
true if it looks like a URL, else false
std::auto_ptr<db::Cursor> dballe::DB::query ( const Record query,
unsigned int  wanted,
unsigned int  modifiers 
)

Create and execute a database query.

The results are retrieved by iterating the cursor.

Parameters:
queryThe record with the query data (see technical specifications, par. 1.6.4 "parameter output/input"
wantedThe values wanted in output
modifiersOptional modifiers to ask for special query behaviours
Returns:
The cursor to use to iterate over the results
unsigned dballe::DB::query_attrs ( int  id_context,
wreport::Varcode  id_var,
const db::AttrList &  qcs,
Record attrs 
)

Query attributes.

Parameters:
id_contextThe database id of the context related to the attributes to retrieve
id_varThe varcode of the variable related to the attributes to retrieve. See vartable::h
qcsThe WMO codes of the QC values requested. If it is empty, then all values are returned.
attrsThe Record that will hold the resulting attributes
Returns:
Number of attributes returned in attrs
std::auto_ptr<db::Cursor> dballe::DB::query_data ( const Record rec)

Query the database.

When multiple values per variable are present, the results will be presented in increasing order of priority.

Parameters:
queryThe record with the query data (see technical specifications, par. 1.6.4 "parameter output/input")
Returns:
The cursor to use to iterate over the results
std::auto_ptr<db::Cursor> dballe::DB::query_stations ( const Record query)

Start a query on the station archive.

Parameters:
queryThe record with the query data (see Keywords used by dba_record)
Returns:
The cursor to use to iterate over the results
void dballe::DB::remove ( const Record rec)

Remove data from the database.

Parameters:
recThe record with the query data (see technical specifications, par. 1.6.4 "parameter output/input") to select the items to be deleted

Remove orphan values from the database.

Orphan values are currently:

  • context values for which no data exists
  • station values for which no context exists

Depending on database size, this routine can take a few minutes to execute.

void dballe::DB::reset ( const char *  repinfo_file = 0)

Reset the database, removing all existing DBALLE tables and re-creating them empty.

Parameters:
repinfo_fileThe name of the CSV file with the report type information data to load. The file is in CSV format with 6 columns: report code, mnemonic id, description, priority, descriptor, table A category. If repinfo_file is NULL, then the default of /etc/dballe/repinfo.csv is used.
void dballe::DB::update_repinfo ( const char *  repinfo_file,
int *  added,
int *  deleted,
int *  updated 
)

Update the repinfo table in the database, with the data found in the given file.

Parameters:
repinfo_fileThe name of the CSV file with the report type information data to load. The file is in CSV format with 6 columns: report code, mnemonic id, description, priority, descriptor, table A category. If repinfo_file is NULL, then the default of /etc/dballe/repinfo.csv is used.
Return values:
addedThe number of repinfo entryes that have been added
deletedThe number of repinfo entryes that have been deleted
updatedThe number of repinfo entryes that have been updated

Field Documentation

struct db::Repinfo* dballe::DB::m_repinfo [protected]

Accessors for the various parts of the database.

Warning:
Before using these 5 pointers, ensure they are initialised using one of the dba_db_need_* functions Report information

Sequence accessors.

They are NULL for databases such as MySQL that do not use sequences. Station ID sequence


The documentation for this class was generated from the following file: