~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to kexi/migration/mdb/src/mdbtools/libmdb/table.c

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2010-09-21 15:36:35 UTC
  • mfrom: (1.4.1 upstream) (60.2.11 maverick)
  • Revision ID: james.westby@ubuntu.com-20100921153635-6tejqkiro2u21ydi
Tags: 1:2.2.2-0ubuntu3
Add kubuntu_03_fix-crash-on-closing-sqlite-connection-2.2.2.diff and
kubuntu_04_support-large-memo-values-for-msaccess-2.2.2.diff as
recommended by upstream http://kexi-
project.org/wiki/wikiview/index.php@Kexi2.2_Patches.html#sqlite_stab
ility

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* MDB Tools - A library for reading MS Access database file
 
2
 * Copyright (C) 2000 Brian Bruns
 
3
 *
 
4
 * This library is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU Library General Public
 
6
 * License as published by the Free Software Foundation; either
 
7
 * version 2 of the License, or (at your option) any later version.
 
8
 *
 
9
 * This library is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 * Library General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU Library General Public
 
15
 * License along with this library; if not, write to the
 
16
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
17
 * Boston, MA 02110-1301, USA.
 
18
 */
 
19
 
 
20
#include "mdbtools.h"
 
21
 
 
22
#ifdef DMALLOC
 
23
#include "dmalloc.h"
 
24
#endif
 
25
 
 
26
 
 
27
static gint mdb_col_comparer(MdbColumn **a, MdbColumn **b)
 
28
{
 
29
        if ((*a)->col_num > (*b)->col_num)
 
30
                return 1;
 
31
        else if ((*a)->col_num < (*b)->col_num)
 
32
                return -1;
 
33
        else
 
34
                return 0;
 
35
}
 
36
 
 
37
unsigned char mdb_col_needs_size(int col_type)
 
38
{
 
39
        if (col_type == MDB_TEXT) {
 
40
                return TRUE;
 
41
        } else {
 
42
                return FALSE;
 
43
        }
 
44
}
 
45
 
 
46
MdbTableDef *mdb_alloc_tabledef(MdbCatalogEntry *entry)
 
47
{
 
48
        MdbTableDef *table;
 
49
 
 
50
        table = (MdbTableDef *) g_malloc0(sizeof(MdbTableDef));
 
51
        table->entry=entry;
 
52
        strcpy(table->name, entry->object_name);
 
53
 
 
54
        return table;   
 
55
}
 
56
void mdb_free_tabledef(MdbTableDef *table)
 
57
{
 
58
        if (!table) return;
 
59
        if (table->is_temp_table) {
 
60
                unsigned int i;
 
61
                /* Temp table pages are being stored in memory */
 
62
                for (i=0; i<table->temp_table_pages->len; i++)
 
63
                        g_free(g_ptr_array_index(table->temp_table_pages,i));
 
64
                g_ptr_array_free(table->temp_table_pages, TRUE);
 
65
                /* Temp tables use dummy entries */
 
66
                g_free(table->entry);
 
67
        }
 
68
        mdb_free_columns(table->columns);
 
69
        mdb_free_indices(table->indices);
 
70
        g_free(table->usage_map);
 
71
        g_free(table->free_usage_map);
 
72
        g_free(table);
 
73
}
 
74
MdbTableDef *mdb_read_table(MdbCatalogEntry *entry)
 
75
{
 
76
        MdbTableDef *table;
 
77
        MdbHandle *mdb = entry->mdb;
 
78
        MdbFormatConstants *fmt = mdb->fmt;
 
79
        int len, row_start, pg_row;
 
80
        void *buf, *pg_buf = mdb->pg_buf;
 
81
 
 
82
        mdb_read_pg(mdb, entry->table_pg);
 
83
        if (mdb_get_byte(pg_buf, 0) != 0x02)  /* not a valid table def page */
 
84
                return NULL;
 
85
        table = mdb_alloc_tabledef(entry);
 
86
 
 
87
        len = mdb_get_int16(pg_buf, 8);
 
88
 
 
89
        table->num_rows = mdb_get_int32(pg_buf, fmt->tab_num_rows_offset);
 
90
        table->num_var_cols = mdb_get_int16(pg_buf, fmt->tab_num_cols_offset-2);
 
91
        table->num_cols = mdb_get_int16(pg_buf, fmt->tab_num_cols_offset);
 
92
        table->num_idxs = mdb_get_int32(pg_buf, fmt->tab_num_idxs_offset);
 
93
        table->num_real_idxs = mdb_get_int32(pg_buf, fmt->tab_num_ridxs_offset);
 
94
 
 
95
        /* grab a copy of the usage map */
 
96
        pg_row = mdb_get_int32(pg_buf, fmt->tab_usage_map_offset);
 
97
        mdb_find_pg_row(mdb, pg_row, &buf, &row_start, &(table->map_sz));
 
98
        table->usage_map = g_memdup((char*)buf + row_start, table->map_sz);
 
99
        if (mdb_get_option(MDB_DEBUG_USAGE)) 
 
100
                buffer_dump(buf, row_start, table->map_sz);
 
101
        mdb_debug(MDB_DEBUG_USAGE,"usage map found on page %ld row %d start %d len %d",
 
102
                pg_row >> 8, pg_row & 0xff, row_start, table->map_sz);
 
103
 
 
104
        /* grab a copy of the free space page map */
 
105
        pg_row = mdb_get_int32(pg_buf, fmt->tab_free_map_offset);
 
106
        mdb_find_pg_row(mdb, pg_row, &buf, &row_start, &(table->freemap_sz));
 
107
        table->free_usage_map = g_memdup((char*)buf + row_start, table->freemap_sz);
 
108
        mdb_debug(MDB_DEBUG_USAGE,"free map found on page %ld row %d start %d len %d\n",
 
109
                pg_row >> 8, pg_row & 0xff, row_start, table->freemap_sz);
 
110
 
 
111
        table->first_data_pg = mdb_get_int16(pg_buf, fmt->tab_first_dpg_offset);
 
112
 
 
113
        return table;
 
114
}
 
115
MdbTableDef *mdb_read_table_by_name(MdbHandle *mdb, gchar *table_name, int obj_type)
 
116
{
 
117
        unsigned int i;
 
118
        MdbCatalogEntry *entry;
 
119
 
 
120
        mdb_read_catalog(mdb, obj_type);
 
121
 
 
122
        for (i=0; i<mdb->num_catalog; i++) {
 
123
                entry = g_ptr_array_index(mdb->catalog, i);
 
124
                if (!strcasecmp(entry->object_name, table_name))
 
125
                        return mdb_read_table(entry);
 
126
        }
 
127
 
 
128
        return NULL;
 
129
}
 
130
 
 
131
 
 
132
guint32 
 
133
read_pg_if_32(MdbHandle *mdb, int *cur_pos)
 
134
{
 
135
        char c[4];
 
136
 
 
137
        read_pg_if_n(mdb, c, cur_pos, 4);
 
138
        return mdb_get_int32(c, 0);
 
139
}
 
140
guint16 
 
141
read_pg_if_16(MdbHandle *mdb, int *cur_pos)
 
142
{
 
143
        char c[2];
 
144
 
 
145
        read_pg_if_n(mdb, c, cur_pos, 2);
 
146
        return mdb_get_int16(c, 0);
 
147
}
 
148
guint8
 
149
read_pg_if_8(MdbHandle *mdb, int *cur_pos)
 
150
{
 
151
        guint8 c;
 
152
 
 
153
        read_pg_if_n(mdb, &c, cur_pos, 1);
 
154
        return c;
 
155
}
 
156
/*
 
157
 * Read data into a buffer, advancing pages and setting the
 
158
 * page cursor as needed.  In the case that buf in NULL, pages
 
159
 * are still advanced and the page cursor is still updated.
 
160
 */
 
161
void * 
 
162
read_pg_if_n(MdbHandle *mdb, void *buf, int *cur_pos, size_t len)
 
163
{
 
164
        char *buf_char = (char *)buf;
 
165
        /* Advance to page which contains the first byte */
 
166
        while (*cur_pos >= mdb->fmt->pg_size) {
 
167
                mdb_read_pg(mdb, mdb_get_int32(mdb->pg_buf,4));
 
168
                *cur_pos -= (mdb->fmt->pg_size - 8);
 
169
        }
 
170
        /* Copy pages into buffer */
 
171
        while (*cur_pos + len >= mdb->fmt->pg_size) {
 
172
                int piece_len = mdb->fmt->pg_size - *cur_pos;
 
173
                if (buf_char) {
 
174
                        memcpy(buf_char, mdb->pg_buf + *cur_pos, piece_len);
 
175
                        buf_char += piece_len;
 
176
                }
 
177
                len -= piece_len;
 
178
                mdb_read_pg(mdb, mdb_get_int32(mdb->pg_buf,4));
 
179
                *cur_pos = 8;
 
180
        }
 
181
        /* Copy into buffer from final page */
 
182
        if (len && buf_char) {
 
183
                memcpy(buf_char, mdb->pg_buf + *cur_pos, len);
 
184
        }
 
185
        *cur_pos += len;
 
186
        return buf_char;
 
187
}
 
188
 
 
189
 
 
190
void mdb_append_column(GPtrArray *columns, MdbColumn *in_col)
 
191
{
 
192
        g_ptr_array_add(columns, g_memdup(in_col,sizeof(MdbColumn)));
 
193
}
 
194
void mdb_free_columns(GPtrArray *columns)
 
195
{
 
196
        unsigned int i;
 
197
 
 
198
        if (!columns) return;
 
199
        for (i=0; i<columns->len; i++)
 
200
                g_free (g_ptr_array_index(columns, i));
 
201
        g_ptr_array_free(columns, TRUE);
 
202
}
 
203
GPtrArray *mdb_read_columns(MdbTableDef *table)
 
204
{
 
205
        MdbHandle *mdb = table->entry->mdb;
 
206
        MdbFormatConstants *fmt = mdb->fmt;
 
207
        MdbColumn *pcol;
 
208
        unsigned char *col;
 
209
        unsigned int i;
 
210
        int cur_pos;
 
211
        size_t name_sz;
 
212
        
 
213
        table->columns = g_ptr_array_new();
 
214
 
 
215
        col = (unsigned char *) g_malloc(fmt->tab_col_entry_size);
 
216
 
 
217
        cur_pos = fmt->tab_cols_start_offset + 
 
218
                (table->num_real_idxs * fmt->tab_ridx_entry_size);
 
219
 
 
220
        /* new code based on patch submitted by Tim Nelson 2000.09.27 */
 
221
 
 
222
        /* 
 
223
        ** column attributes 
 
224
        */
 
225
        for (i=0;i<table->num_cols;i++) {
 
226
#ifdef MDB_DEBUG
 
227
        /* printf("column %d\n", i);
 
228
        buffer_dump(mdb->pg_buf, cur_pos, fmt->tab_col_entry_size); */
 
229
#endif
 
230
                read_pg_if_n(mdb, col, &cur_pos, fmt->tab_col_entry_size);
 
231
                pcol = (MdbColumn *) g_malloc0(sizeof(MdbColumn));
 
232
 
 
233
                pcol->col_type = col[0];
 
234
 
 
235
                
 
236
                pcol->col_num = col[fmt->col_num_offset];
 
237
 
 
238
                
 
239
                
 
240
                pcol->var_col_num = mdb_get_int16(col, fmt->tab_col_offset_var);
 
241
                
 
242
 
 
243
                
 
244
                pcol->row_col_num = mdb_get_int16(col, fmt->tab_row_col_num_offset);
 
245
                
 
246
 
 
247
                /* FIXME: can this be right in Jet3 and Jet4? */
 
248
                if (pcol->col_type == MDB_NUMERIC) {
 
249
                        pcol->col_prec = col[11];
 
250
                        pcol->col_scale = col[12];
 
251
                }
 
252
 
 
253
                
 
254
                pcol->is_fixed = col[fmt->col_fixed_offset] & 0x01 ? 1 : 0;
 
255
 
 
256
                
 
257
                pcol->fixed_offset = mdb_get_int16(col, fmt->tab_col_offset_fixed);
 
258
                
 
259
                
 
260
 
 
261
                if (pcol->col_type != MDB_BOOL) {
 
262
                        
 
263
                        pcol->col_size = mdb_get_int16(col, fmt->col_size_offset);
 
264
                } else {
 
265
                        pcol->col_size=0;
 
266
                }
 
267
                
 
268
                g_ptr_array_add(table->columns, pcol);
 
269
        }
 
270
 
 
271
        g_free (col);
 
272
 
 
273
        /* 
 
274
        ** column names - ordered the same as the column attributes table
 
275
        */
 
276
        for (i=0;i<table->num_cols;i++) {
 
277
                char *tmp_buf;
 
278
                pcol = g_ptr_array_index(table->columns, i);
 
279
 
 
280
                if (IS_JET4(mdb)) {
 
281
                        name_sz = read_pg_if_16(mdb, &cur_pos);
 
282
                } else if (IS_JET3(mdb)) {
 
283
                        name_sz = read_pg_if_8(mdb, &cur_pos);
 
284
                } else {
 
285
                        fprintf(stderr,"Unknown MDB version\n");
 
286
                        continue;
 
287
                }
 
288
                tmp_buf = (char *) g_malloc(name_sz);
 
289
                read_pg_if_n(mdb, tmp_buf, &cur_pos, name_sz);
 
290
                mdb_unicode2ascii(mdb, tmp_buf, name_sz, pcol->name, MDB_MAX_OBJ_NAME);
 
291
                g_free(tmp_buf);
 
292
 
 
293
        }
 
294
 
 
295
        /* Sort the columns by col_num */
 
296
        g_ptr_array_sort(table->columns, (GCompareFunc)mdb_col_comparer);
 
297
 
 
298
        table->index_start = cur_pos;
 
299
        return table->columns;
 
300
}
 
301
 
 
302
#if !MDB_NO_BACKENDS
 
303
void mdb_table_dump(MdbCatalogEntry *entry)
 
304
{
 
305
MdbTableDef *table;
 
306
MdbColumn *col;
 
307
int coln;
 
308
MdbIndex *idx;
 
309
MdbHandle *mdb = entry->mdb;
 
310
unsigned int i, bitn;
 
311
guint32 pgnum;
 
312
 
 
313
        table = mdb_read_table(entry);
 
314
        fprintf(stdout,"definition page     = %lu\n",entry->table_pg);
 
315
        fprintf(stdout,"number of datarows  = %d\n",table->num_rows);
 
316
        fprintf(stdout,"number of columns   = %d\n",table->num_cols);
 
317
        fprintf(stdout,"number of indices   = %d\n",table->num_real_idxs);
 
318
 
 
319
        mdb_read_columns(table);
 
320
        mdb_read_indices(table);
 
321
 
 
322
        for (i=0;i<table->num_cols;i++) {
 
323
                col = g_ptr_array_index(table->columns,i);
 
324
        
 
325
                fprintf(stdout,"column %d Name: %-20s Type: %s(%d)\n",
 
326
                        i, col->name,
 
327
                        mdb_get_coltype_string(mdb->default_backend, col->col_type),
 
328
                        col->col_size);
 
329
        }
 
330
 
 
331
        for (i=0;i<table->num_idxs;i++) {
 
332
                idx = g_ptr_array_index (table->indices, i);
 
333
                mdb_index_dump(table, idx);
 
334
        }
 
335
        if (table->usage_map) {
 
336
                printf("pages reserved by this object\n");
 
337
                printf("usage map pg %" G_GUINT32_FORMAT "\n",
 
338
                        table->map_base_pg);
 
339
                printf("free map pg %" G_GUINT32_FORMAT "\n",
 
340
                        table->freemap_base_pg);
 
341
                pgnum = mdb_get_int32(table->usage_map,1);
 
342
                /* the first 5 bytes of the usage map mean something */
 
343
                coln = 0;
 
344
                for (i=5;i<table->map_sz;i++) {
 
345
                        for (bitn=0;bitn<8;bitn++) {
 
346
                                if (table->usage_map[i] & 1 << bitn) {
 
347
                                        coln++;
 
348
                                        printf("%6" G_GUINT32_FORMAT, pgnum);
 
349
                                        if (coln==10) {
 
350
                                                printf("\n");
 
351
                                                coln = 0;
 
352
                                        } else {
 
353
                                                printf(" ");
 
354
                                        }
 
355
                                }
 
356
                                pgnum++;
 
357
                        }
 
358
                }
 
359
                printf("\n");
 
360
        }
 
361
}
 
362
#endif
 
363
 
 
364
int mdb_is_user_table(MdbCatalogEntry *entry)
 
365
{
 
366
        return ((entry->object_type == MDB_TABLE)
 
367
         && !(entry->flags & 0x80000002)) ? 1 : 0;
 
368
}
 
369
int mdb_is_system_table(MdbCatalogEntry *entry)
 
370
{
 
371
        return ((entry->object_type == MDB_TABLE)
 
372
         && (entry->flags & 0x80000002)) ? 1 : 0;
 
373
}