"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "lib/src/dbread.c" between
gretl-2020a.tar.xz and gretl-2020b.tar.xz

About: gretl (Gnu Regression, Econometrics and Time-series Library) is a cross-platform statistical package mainly for econometric analysis.

dbread.c  (gretl-2020a.tar.xz):dbread.c  (gretl-2020b.tar.xz)
skipping to change at line 30 skipping to change at line 30
/* dbread.c for gretl */ /* dbread.c for gretl */
#include "libgretl.h" #include "libgretl.h"
#include "swap_bytes.h" #include "swap_bytes.h"
#include "libset.h" #include "libset.h"
#include "uservar.h" #include "uservar.h"
#include "matrix_extra.h" #include "matrix_extra.h"
#include "usermat.h" #include "usermat.h"
#include "dbread.h" #include "dbread.h"
#include "gretl_midas.h" #include "gretl_midas.h"
#include "gretl_typemap.h"
#ifdef USE_CURL #ifdef USE_CURL
# include "gretl_www.h" # include "gretl_www.h"
#endif #endif
#include <glib.h> #include <glib.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#if G_BYTE_ORDER == G_BIG_ENDIAN #if G_BYTE_ORDER == G_BIG_ENDIAN
# include <netinet/in.h> # include <netinet/in.h>
skipping to change at line 2147 skipping to change at line 2148
} else { } else {
err = strings_array_add(&gretl_odinfo.fmts, err = strings_array_add(&gretl_odinfo.fmts,
&gretl_odinfo.obscols, &gretl_odinfo.obscols,
chunk); chunk);
free(chunk); free(chunk);
} }
*ps = p; *ps = p;
#if 1 #if 1
fprintf(stderr, "set coltype[%d] = %d, fmt='%s'\n", i, fprintf(stderr, "set obs coltype[%d] = %d (%s), fmt='%s'\n", i,
gretl_odinfo.coltypes[i], gretl_odinfo.fmts[i]); gretl_odinfo.coltypes[i],
gretl_type_get_name(gretl_odinfo.coltypes[i]),
gretl_odinfo.fmts[i]);
#endif #endif
return err; return err;
} }
static int parse_odbc_format (char *fmt) static int parse_odbc_format (char *fmt)
{ {
char *s = fmt; char *s = fmt;
int i, err = 0; int i, err = 0;
skipping to change at line 2276 skipping to change at line 2279
ret = series_table_get_value(stl, sr); ret = series_table_get_value(stl, sr);
if (na(ret)) { if (na(ret)) {
/* not found: so try adding it to the LHS table */ /* not found: so try adding it to the LHS table */
series_table_add_string(stl, sr); series_table_add_string(stl, sr);
ret = series_table_get_value(stl, sr); ret = series_table_get_value(stl, sr);
} }
return ret; return ret;
} }
static int m2q (int m)
{
if (m == 1) return 1;
else if (m == 4) return 2;
else if (m == 7) return 3;
else if (m == 10) return 4;
else return -1;
}
static int try_iso_8601 (const char *s, DATASET *dset)
{
int t = -1;
if (dataset_is_time_series(dset)) {
char obsstr[OBSLEN] = {0};
int y, m, d;
if (sscanf(s, "%d-%d-%d", &y, &m, &d) == 3) {
if (dset->pd == 4 && d == 1) {
sprintf(obsstr, "%04d:%d", y, m2q(m));
} else if (dset->pd == 12 && d == 1) {
sprintf(obsstr, "%04d:%02d", y, m);
} else if (dset->pd == 1 && m == 1 && d == 1) {
sprintf(obsstr, "%04d", y);
}
t = dateton(obsstr, dset);
}
}
return t;
}
static int odbc_transcribe_data (char **vnames, DATASET *dset, static int odbc_transcribe_data (char **vnames, DATASET *dset,
int vmin, int newvars, int vmin, int newvars,
PRN *prn) gretlopt opt, PRN *prn)
{ {
char label[MAXLABEL]; char label[MAXLABEL];
int *gstlist = NULL;
int nv = gretl_odinfo.nvars; int nv = gretl_odinfo.nvars;
int n = gretl_odinfo.nrows; int n = gretl_odinfo.nrows;
int nrepl = nv - newvars; int nrepl = nv - newvars;
int simple_fill = (opt & OPT_F);
int i, s, t, v; int i, s, t, v;
int err = 0; int err = 0;
if (gretl_odinfo.gst != NULL) {
gstlist = string_table_copy_list(gretl_odinfo.gst);
}
for (i=0; i<nv && !err; i++) { for (i=0; i<nv && !err; i++) {
series_table *str = NULL; series_table *str = NULL;
series_table *stl = NULL; series_table *stl = NULL;
int vnew = 1; /* is this a new series? */ int vnew = 1; /* is this a new series? */
int obs_used = 0; int obs_used = 0;
if (nrepl > 0) { if (nrepl > 0) {
/* we're replacing some series */ /* we're replacing some series */
v = current_series_index(dset, vnames[i]); v = current_series_index(dset, vnames[i]);
} else { } else {
skipping to change at line 2313 skipping to change at line 2354
v = vmin++; v = vmin++;
strcpy(dset->varname[v], vnames[i]); strcpy(dset->varname[v], vnames[i]);
sprintf(label, "ODBC series %d", i + 1); sprintf(label, "ODBC series %d", i + 1);
series_set_label(dset, v, label); series_set_label(dset, v, label);
} else { } else {
/* an existing series */ /* an existing series */
vnew = 0; vnew = 0;
stl = series_get_string_table(dset, v); stl = series_get_string_table(dset, v);
} }
if (in_string_table(gretl_odinfo.gst, i+1)) { if (in_gretl_list(gstlist, i+1)) {
/* the imported data are string-valued */ /* the imported data are string-valued */
if (vnew) { if (vnew) {
gretl_string_table_reset_column_id(gretl_odinfo.gst, i+1, v); gretl_string_table_reset_column_id(gretl_odinfo.gst, i+1, v);
} else if (stl == NULL) { } else if (stl == NULL) {
gretl_errmsg_sprintf("%s: can't mix numeric and string data", gretl_errmsg_sprintf("%s: can't mix numeric and string data",
dset->varname[v]); dset->varname[v]);
err = E_TYPES; err = E_TYPES;
} else { } else {
str = gretl_string_table_detach_col(gretl_odinfo.gst, i+1); str = gretl_string_table_detach_col(gretl_odinfo.gst, i+1);
} }
skipping to change at line 2347 skipping to change at line 2388
if (gretl_odinfo.S != NULL) { if (gretl_odinfo.S != NULL) {
/* got obs identifiers via ODBC */ /* got obs identifiers via ODBC */
if (vnew) { if (vnew) {
for (t=0; t<dset->n; t++) { for (t=0; t<dset->n; t++) {
dset->Z[v][t] = NADBL; dset->Z[v][t] = NADBL;
} }
} }
for (s=0; s<n; s++) { for (s=0; s<n; s++) {
t = dateton(gretl_odinfo.S[s], dset); t = dateton(gretl_odinfo.S[s], dset);
if (t < 0) {
t = try_iso_8601(gretl_odinfo.S[s], dset);
}
if (t >= dset->t1 && t <= dset->t2) { if (t >= dset->t1 && t <= dset->t2) {
if (str != NULL) { if (str != NULL) {
dset->Z[v][t] = s_tab_get(i, s, stl, str); dset->Z[v][t] = s_tab_get(i, s, stl, str);
} else { } else {
dset->Z[v][t] = gretl_odinfo.X[i][s]; dset->Z[v][t] = gretl_odinfo.X[i][s];
} }
obs_used++; obs_used++;
} else { } else {
fprintf(stderr, "Rejecting obs '%s'\n", gretl_odinfo.S[s]); fprintf(stderr, "Rejecting obs '%s'\n", gretl_odinfo.S[s]);
} }
} }
} else { } else {
/* no obs identifiers via ODBC */ /* no obs identifiers via ODBC */
int ns = dset->t2 - dset->t1 + 1; int ns = dset->t2 - dset->t1 + 1;
if (n == ns) { if (n == ns || simple_fill) {
s = 0; s = 0;
} else if (n == dset->n) { } else if (n == dset->n) {
s = dset->t1; s = dset->t1;
} else { } else {
gretl_errmsg_sprintf("%s: don't know how to align the data!", gretl_errmsg_sprintf("%s: don't know how to align the data!",
dset->varname[v]); dset->varname[v]);
err = E_DATA; err = E_DATA;
} }
for (t=0; t<dset->n && !err; t++) { for (t=0; t<dset->n && !err; t++) {
if (t >= dset->t1 && t <= dset->t2 && s < n) { if (t >= dset->t1 && t <= dset->t2 && s < n) {
skipping to change at line 2395 skipping to change at line 2439
if (str != NULL) { if (str != NULL) {
series_table_destroy(str); series_table_destroy(str);
} }
if (!err && vnew && obs_used == 0) { if (!err && vnew && obs_used == 0) {
gretl_warnmsg_sprintf("ODBC import: '%s': no valid observations in sa mple range", gretl_warnmsg_sprintf("ODBC import: '%s': no valid observations in sa mple range",
vnames[i]); vnames[i]);
} }
} }
if (gretl_odinfo.gst != NULL) { free(gstlist);
if (err) {
dataset_drop_last_variables(dset, newvars);
if (gretl_odinfo.gst != NULL) {
gretl_string_table_destroy(gretl_odinfo.gst);
gretl_odinfo.gst = NULL;
}
} else if (gretl_odinfo.gst != NULL) {
gretl_string_table_save(gretl_odinfo.gst, dset); gretl_string_table_save(gretl_odinfo.gst, dset);
} }
return err; return err;
} }
static int odbc_count_new_vars (char **vnames, int nv, static int odbc_count_new_vars (char **vnames, int nv,
const DATASET *dset) const DATASET *dset)
{ {
int newv = nv; int newv = nv;
skipping to change at line 2423 skipping to change at line 2475
} }
} }
} }
return newv; return newv;
} }
/* data series [obs-format=format-string] [query=]query-string */ /* data series [obs-format=format-string] [query=]query-string */
static int odbc_get_series (const char *line, DATASET *dset, static int odbc_get_series (const char *line, DATASET *dset,
PRN *prn) gretlopt opt, PRN *prn)
{ {
int (*get_data) (ODBC_info *); int (*get_data) (ODBC_info *, gretlopt, PRN *);
char **vnames = NULL; char **vnames = NULL;
char *format = NULL; char *format = NULL;
int err = 0; int err = 0;
if (gretl_odinfo.dsn == NULL) { if (gretl_odinfo.dsn == NULL) {
gretl_errmsg_set(_("No database has been opened")); gretl_errmsg_set(_("No database has been opened"));
return 1; return 1;
} } else if (dset->n == 0) {
return E_NODATA;
if (dset->n == 0) {
gretl_errmsg_set(_("No series length has been defined"));
return 1;
} }
/* get "series" field */ /* get "series" field */
vnames = odbc_get_varnames(&line, &err); vnames = odbc_get_varnames(&line, &err);
if (err) { if (err) {
return err; return err;
} }
/* optional "obs-format" field */ /* optional "obs-format" field */
if (!strncmp(line, "obs-format=", 11)) { if (!strncmp(line, "obs-format=", 11)) {
skipping to change at line 2467 skipping to change at line 2516
/* now the query to pass to the database */ /* now the query to pass to the database */
if (!err) { if (!err) {
line += strspn(line, " "); line += strspn(line, " ");
if (!strncmp(line, "query=", 6)) { if (!strncmp(line, "query=", 6)) {
line += 6; line += 6;
} }
gretl_odinfo.query = odbc_get_query(line, &err); gretl_odinfo.query = odbc_get_query(line, &err);
} }
if (!err) { if (!err) {
fprintf(stderr, "SQL query: '%s'\n", gretl_odinfo.query); if (opt & OPT_V) {
pprintf(prn, "SQL query: '%s'\n", gretl_odinfo.query);
}
gretl_error_clear(); gretl_error_clear();
get_data = get_plugin_function("gretl_odbc_get_data"); get_data = get_plugin_function("gretl_odbc_get_data");
if (get_data == NULL) { if (get_data == NULL) {
err = 1; err = 1;
} else { } else {
err = (*get_data) (&gretl_odinfo); err = (*get_data) (&gretl_odinfo, opt, prn);
} }
} }
if (!err) { if (!err) {
int n = gretl_odinfo.nrows; int n = gretl_odinfo.nrows;
int nv = gretl_odinfo.nvars; int nv = gretl_odinfo.nvars;
int newvars, vmin = 1; int newvars, vmin = 1;
if (gretl_messages_on()) { if (gretl_messages_on()) {
pprintf(prn, "Retrieved %d observations on %d series via ODBC\n", pprintf(prn, "Retrieved %d observations on %d series via ODBC\n",
skipping to change at line 2503 skipping to change at line 2554
err = start_new_Z(dset, 0); err = start_new_Z(dset, 0);
} else { } else {
newvars = odbc_count_new_vars(vnames, nv, dset); newvars = odbc_count_new_vars(vnames, nv, dset);
vmin = dset->v; vmin = dset->v;
if (newvars > 0) { if (newvars > 0) {
err = dataset_add_series(dset, newvars); err = dataset_add_series(dset, newvars);
} }
} }
if (!err) { if (!err) {
err = odbc_transcribe_data(vnames, dset, vmin, newvars, prn); err = odbc_transcribe_data(vnames, dset, vmin, newvars, opt, prn);
} }
} }
strings_array_free(vnames, gretl_odinfo.nvars); strings_array_free(vnames, gretl_odinfo.nvars);
ODBC_info_clear_read(); ODBC_info_clear_read();
return err; return err;
} }
/* dbnomics function in separate file */ /* dbnomics function in separate file */
skipping to change at line 2665 skipping to change at line 2716
char altname[VNAMELEN] = {0}; char altname[VNAMELEN] = {0};
char **vnames = NULL; char **vnames = NULL;
char *idxname = NULL; char *idxname = NULL;
CompactMethod cmethod; CompactMethod cmethod;
int i, nnames = 0; int i, nnames = 0;
int from_scratch = 0; int from_scratch = 0;
int interpolate = 0; int interpolate = 0;
int err = 0; int err = 0;
if (opt & OPT_O) { if (opt & OPT_O) {
return odbc_get_series(line, dset, prn); return odbc_get_series(line, dset, opt, prn);
} }
if (opt & OPT_N) { if (opt & OPT_N) {
/* --name=whatever */ /* --name=whatever */
err = process_import_name_option(altname); err = process_import_name_option(altname);
if (err) { if (err) {
return err; return err;
} }
} }
 End of changes. 18 change blocks. 
17 lines changed or deleted 68 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)