File: mdb2rec.c

package info (click to toggle)
recutils 1.9-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,328 kB
  • sloc: ansic: 80,756; sh: 11,649; yacc: 1,978; makefile: 323; lex: 225; sed: 16
file content (496 lines) | stat: -rw-r--r-- 13,091 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/* mdb2rec.c - mdb to rec converter.  */

/* Copyright (C) 2010-2022 Jose E. Marchesi */

/* 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 3 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, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include <getopt.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <xalloc.h>
#include <gettext.h>
#define _(str) gettext (str)

#include <glib.h>
#include <mdbtools.h>

#include <rec.h>
#include <recutl.h>

/* Forward declarations. */
static void parse_args (int argc, char **argv);
static rec_db_t process_mdb (void);
static rec_rset_t process_table (MdbCatalogEntry *entry);
static char *get_field_name (MdbHandle *mdb,
                             const char *table_name, const char *col_name);
static void get_relationships (MdbHandle *mdb);

/*
 * Data types
 */

struct relationship_s
{
  char *table;
  char *column;
  char *referenced_table;
  char *referenced_column;
};

/*
 * Global variables
 */

char *mdb2rec_mdb_file = NULL;
char *mdb2rec_mdb_table = NULL;
bool mdb2rec_include_system = false;
bool mdb2rec_keep_empty_fields = false;
bool mdb2rec_list_tables = false;
struct relationship_s *relationships;
size_t num_relationships = 0;

/*
 * Command line options management
 */

enum
  {
    COMMON_ARGS,
    SYSTEM_TABLES_ARG,
    KEEP_EMPTY_FIELDS_ARG,
    LIST_TABLES_ARG
  };

static const struct option GNU_longOptions[] =
  {
    COMMON_LONG_ARGS,
    {"system-tables", no_argument, NULL, SYSTEM_TABLES_ARG},
    {"keep-empty-fields", no_argument, NULL, KEEP_EMPTY_FIELDS_ARG},
    {"list-tables", no_argument, NULL, LIST_TABLES_ARG},
    {NULL, 0, NULL, 0}
  };

/*
 * Functions.
 */

void
recutl_print_help (void)
{
  /* TRANSLATORS: --help output, mdb2rec synopsis.
     no-wrap */
  printf (_("\
Usage: mdb2rec [OPTIONS]... MDB_FILE [TABLE]\n"));

  /* TRANSLATORS: --help output, mdb2rec short description.
     no-wrap */
  fputs (_("\
Convert an mdb file into a rec file.\n"), stdout);

  puts ("");
  /* TRANSLATORS: --help output, mdb2rec options.
     no-wrap */
  fputs (_("\
  -s, --system-tables                 include system tables.\n\
  -e, --keep-empty-fields             don't prune empty fields in the rec\n\
                                        output.\n\
  -l, --list-tables                   dump a list of the table names contained\n\
                                        in the mdb file.\n"),
         stdout);

  recutl_print_help_common ();
  puts ("");
  recutl_print_help_footer ();
}

static void
parse_args (int argc,
            char **argv)
{
  int ret;
  char c;

  while ((ret = getopt_long (argc,
                             argv,
                             "sel",
                             GNU_longOptions,
                             NULL)) != -1)
    {
      c = ret;
      switch (c)
        {
          COMMON_ARGS_CASES
        case SYSTEM_TABLES_ARG:
        case 's':
          mdb2rec_include_system = true;
          break;
        case KEEP_EMPTY_FIELDS_ARG:
        case 'e':
          mdb2rec_keep_empty_fields = true;
          break;
        case LIST_TABLES_ARG:
        case 'l':
          mdb2rec_list_tables = true;
          break;
        default:
          exit (EXIT_FAILURE);
        }
    }

  /* Read the name of the mdb file.  */
  if ((argc - optind) > 2)
    {
      recutl_print_help ();
      exit (EXIT_FAILURE);
    }
  else
    {
      mdb2rec_mdb_file = argv[optind++];

      if ((argc - optind) > 0)
        mdb2rec_mdb_table = argv[optind++];
    }
}

static void
get_relationships (MdbHandle *mdb)
{
  char *bound[4];
  MdbTableDef *table;
  size_t i;

  table = mdb_read_table_by_name (mdb,
                                  "MsysRelationships",
                                  MDB_TABLE);
  if ((!table) || (table->num_rows == 0))
    return;

  mdb_read_columns (table);
  for (i = 0; i < 4; i++)
    bound[i] = xmalloc (MDB_BIND_SIZE);

  mdb_bind_column_by_name (table, "szColumn", bound[0], NULL);
  mdb_bind_column_by_name (table, "szObject", bound[1], NULL);
  mdb_bind_column_by_name (table, "szReferencedColumn", bound[2], NULL);
  mdb_bind_column_by_name (table, "szReferencedObject", bound[3], NULL);
  mdb_rewind_table (table);

  num_relationships = table->num_rows;
  relationships = xmalloc (sizeof (struct relationship_s) * num_relationships);

  i = 0;
  while (mdb_fetch_row (table))
    {
      relationships[i].column = xstrdup (bound[0]);
      relationships[i].table = xstrdup (bound[1]);
      relationships[i].referenced_column = xstrdup (bound[2]);
      relationships[i].referenced_table = xstrdup (bound[3]);
      i++;
    }
}

static char *
get_field_name (MdbHandle *mdb,
                const char *table_name,
                const char *col_name)
{
  char *field_name;
  char *referenced_table;
  char *referenced_column;
  size_t i;

  /* If this field is a relationship to other table, build a compound
     field name.  */

  referenced_table = NULL;
  referenced_column = NULL;
  for (i = 0; i < num_relationships; i++)
    {
      if ((strcmp (relationships[i].table, table_name) == 0)
          && (strcmp (relationships[i].column, col_name) == 0))
        {
          referenced_table =
            rec_field_name_normalise (relationships[i].referenced_table);
          if (!referenced_table)
            recutl_fatal (_("failed to normalise record type name %s\n"),
                          relationships[i].referenced_table);

          referenced_column =
            rec_field_name_normalise (relationships[i].referenced_column);
          if (!referenced_column)
            recutl_fatal (_("failed to normalise field name %s\n"),
                          relationships[i].referenced_column);

          break;
        }
    }

  field_name = rec_field_name_normalise (col_name);
  if (!field_name)
    recutl_fatal (_("failed to normalise field name %s\n"),
                  table_name);

  if (referenced_table && referenced_column)
    {
      /* TODO: handle foreign keys.  */
    }

  return field_name;
}

static rec_rset_t
process_table (MdbCatalogEntry *entry)
{
  rec_rset_t rset;
  MdbTableDef *table;
  MdbHandle *mdb;
  size_t i;
  MdbColumn *col;
  char *table_name;
  char *column_name;
  char *field_name;
  char *field_value;
  char **bound_values;
  char *normalised;
  int *bound_lens;
#define TYPE_VALUE_SIZE 256
  char type_value[TYPE_VALUE_SIZE];
  rec_record_t record;
  rec_field_t field;

  mdb = entry->mdb;
  table_name = entry->object_name;
  table = mdb_read_table (entry);

  /* Create the record set.  */
  rset = rec_rset_new ();
  if (!rset)
    recutl_out_of_memory ();

  /* Create the record descriptor and add the %rec: entry.  */
  field_name = rec_field_name_normalise (table_name);
  if (!field_name)
    recutl_fatal (_("failed to normalise record type name %s\n"),
                  table_name);

  record = rec_record_new ();
  field = rec_field_new ("%rec", field_name);
  rec_mset_append (rec_record_mset (record), MSET_FIELD, (void *) field, MSET_ANY);
  free (field_name);

  /* Get the columns of the table.  */
  mdb_read_columns (table);

  /* Loop on the columns.  We will define the key and the types.  */
  for (i = 0; i < table->num_cols; i++)
    {
      col = g_ptr_array_index (table->columns, i);
      column_name = col->name;
      type_value[0] = 0;
      normalised = rec_field_name_normalise (column_name);
      if (!normalised)
        recutl_fatal (_("failed to normalise the field name %s\n"),
                      column_name);

      /* Emit a field type specification.  */
      switch (col->col_type)
        {
        case MDB_BOOL:
          snprintf (type_value, TYPE_VALUE_SIZE,
                    "%s bool", normalised);
          break;
        case MDB_BYTE:
          snprintf (type_value, TYPE_VALUE_SIZE,
                    "%s range 256", normalised);
          break;
        case MDB_INT:
        case MDB_LONGINT:
        case MDB_NUMERIC:
          snprintf (type_value, TYPE_VALUE_SIZE,
                    "%s int", normalised);
          break;
        case MDB_MONEY:
        case MDB_FLOAT:
        case MDB_DOUBLE:
          snprintf (type_value, TYPE_VALUE_SIZE,
                    "%s real", normalised);
          break;
        case MDB_DATETIME:
          snprintf (type_value, TYPE_VALUE_SIZE,
                    "%s date", normalised);
          break;
        case MDB_TEXT:
          if (col->col_size > 0)
            snprintf (type_value, TYPE_VALUE_SIZE,
                      "%s size %d", normalised, col->col_size);
          break;
        case MDB_REPID:
        case MDB_MEMO:
        case MDB_OLE:
        default:
          break;
        }

      if (type_value[0] != 0)
        {
          /* Insert a type field for this column.  */
          field = rec_field_new ("%type", type_value);
          rec_mset_append (rec_record_mset (record),
                           MSET_FIELD, (void *) field, MSET_ANY);
        }
    }

  rec_rset_set_descriptor (rset, record);

  /* Add the records for this table.  */
  mdb_rewind_table (table);

  bound_values = xmalloc (table->num_cols * sizeof(char *));
  bound_lens = xmalloc(table->num_cols * sizeof(int));
  for (i = 0; i < table->num_cols; i++)
    {
      bound_values[i] = xmalloc (MDB_BIND_SIZE);
      mdb_bind_column (table, i+1, bound_values[i], &bound_lens[i]);
    }

  while (mdb_fetch_row (table))
    {
      record = rec_record_new ();
      if (!record)
        recutl_out_of_memory ();

      for (i = 0; i < table->num_cols; i++)
        {
          col = g_ptr_array_index (table->columns, i);

          if (col->col_type == MDB_OLE)
            continue;

          /* Compute the value of the field.  */
          field_value = xmalloc (bound_lens[i] + 1);
          memcpy (field_value, bound_values[i], bound_lens[i]);
          field_value[bound_lens[i]] = '\0';

          if (mdb2rec_keep_empty_fields || (strlen (field_value) > 0))
            {
              /* Create the field and append it into the record.  */
              field = rec_field_new (get_field_name (mdb, table_name, column_name),
                                     field_value);
              if (!field)
                {
                  recutl_fatal (_("invalid field name %s.\n"), column_name);
                }

              rec_mset_append (rec_record_mset (record), MSET_FIELD,
                               (void *) field, MSET_ANY);
            }

          free (field_value);
        }

      rec_record_set_container (record, rset);
      rec_mset_append (rec_rset_mset (rset), MSET_RECORD,
                       (void *) record, MSET_ANY);
    }

  mdb_free_tabledef (table);

  return rset;
}

static rec_db_t
process_mdb (void)
{
  rec_db_t db;
  MdbHandle *mdb;
  MdbCatalogEntry *entry;
  int i;
  char *table_name;

  /* Create the rec database.  */
  db = rec_db_new ();
  if (!db)
    recutl_out_of_memory ();

  /* Initialize libmdb and open the input file.  */
  mdb_init();
  mdb_set_date_fmt ("%Y-%m-%dT%H:%M:%S%z"); /* ISO 8601 */

  mdb = mdb_open (mdb2rec_mdb_file, MDB_NOFLAGS);
  if (!mdb)
    recutl_fatal (_("could not open file %s\n"),
                  mdb2rec_mdb_file);

  /* Read the catalog.  */
  if (!mdb_read_catalog (mdb, MDB_TABLE))
    recutl_fatal (_("file does not appear to be an Access database\n"));

  /* Read relationships from the database.  Relationships in mdb files
     are stored in the MSysRelationships table.  */
  get_relationships (mdb);

  /* Iterate on the catalogs.  */
  for (i = 0; i < mdb->num_catalog; i++)
    {
      entry = g_ptr_array_index (mdb->catalog, i);

      table_name = rec_field_name_normalise (entry->object_name);

      if ((entry->object_type == MDB_TABLE)
          && (mdb_is_user_table (entry) || mdb2rec_include_system)
          && (!mdb2rec_mdb_table || (strcmp (mdb2rec_mdb_table, table_name) == 0)))
        {
          if (mdb2rec_list_tables)
            fprintf (stdout, "%s\n", table_name);
          else
            rec_db_insert_rset (db,
                                process_table (entry),
                                rec_db_size (db));
        }
    }

  return db;
}

int
main (int argc, char *argv[])
{
  int ret;
  rec_db_t db;
  rec_writer_t writer;

  recutl_init ("mdb2rec");

  parse_args (argc, argv);
  db = process_mdb ();

  if (db)
    {
      writer = rec_writer_new (stdout);
      rec_write_db (writer, db);

      rec_writer_destroy (writer);
      rec_db_destroy (db);

      ret = EXIT_SUCCESS;
    }
  else
    ret = EXIT_FAILURE;

  return ret;
}