1 /* MDB Tools - A library for reading MS Access database files
2  * Copyright (C) 2004 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  * Temp table routines.  These are currently used to generate mock results for
28  * commands like "list tables" and "describe table"
29  */
30 
31 void
mdb_fill_temp_col(MdbColumn * tcol,char * col_name,int col_size,int col_type,int is_fixed)32 mdb_fill_temp_col(MdbColumn *tcol, char *col_name, int col_size, int col_type, int is_fixed)
33 {
34 	memset(tcol,0,sizeof(MdbColumn));
35 	strcpy(tcol->name, col_name);
36 	tcol->col_type = col_type;
37 	if ((col_type == MDB_TEXT) || (col_type == MDB_MEMO)) {
38 		tcol->col_size = col_size;
39 	} else {
40 		tcol->col_size = mdb_col_fixed_size(tcol);
41 	}
42 	tcol->is_fixed = is_fixed;
43 }
44 void
mdb_fill_temp_field(MdbField * field,void * value,int siz,int is_fixed,int is_null,int start,int colnum)45 mdb_fill_temp_field(MdbField *field, void *value, int siz, int is_fixed, int is_null, int start, int colnum)
46 {
47    	field->value = value;
48    	field->siz = siz;
49    	field->is_fixed = is_fixed;
50    	field->is_null = is_null;
51    	field->start = start;
52    	field->colnum = colnum;
53 }
54 MdbTableDef *
mdb_create_temp_table(MdbHandle * mdb,char * name)55 mdb_create_temp_table(MdbHandle *mdb, char *name)
56 {
57 	MdbCatalogEntry *entry;
58 	MdbTableDef *table;
59 
60 	/* dummy up a catalog entry */
61 	entry = (MdbCatalogEntry *) g_malloc0(sizeof(MdbCatalogEntry));
62 	entry->mdb = mdb;
63 	entry->object_type = MDB_TABLE;
64 	entry->table_pg = 0;
65 	strcpy(entry->object_name, name);
66 
67 	table = mdb_alloc_tabledef(entry);
68 	table->columns = g_ptr_array_new();
69 	table->is_temp_table = 1;
70 	table->temp_table_pages = g_ptr_array_new();
71 
72 	return table;
73 }
74 void
mdb_temp_table_add_col(MdbTableDef * table,MdbColumn * col)75 mdb_temp_table_add_col(MdbTableDef *table, MdbColumn *col)
76 {
77 	col->col_num = table->num_cols;
78 	if (!col->is_fixed)
79 		col->var_col_num = table->num_var_cols++;
80 	g_ptr_array_add(table->columns, g_memdup(col, sizeof(MdbColumn)));
81 	table->num_cols++;
82 }
83 /*
84  * Should be called after setting up all temp table columns
85  */
mdb_temp_columns_end(MdbTableDef * table)86 void mdb_temp_columns_end(MdbTableDef *table)
87 {
88 	MdbColumn *col;
89 	unsigned int i;
90 	unsigned int start = 0;
91 
92 	for (i=0; i<table->num_cols; i++) {
93 		col = g_ptr_array_index(table->columns, i);
94 		if (col->is_fixed) {
95 			col->fixed_offset = start;
96 			start += col->col_size;
97 		}
98 	}
99 }
100