コード例 #1
0
ファイル: catalog.c プロジェクト: JeremiasE/KFormula
GPtrArray *mdb_read_catalog (MdbHandle *mdb, int objtype)
{
	MdbCatalogEntry *entry, msysobj;
	MdbTableDef *table;
	char obj_id[256];
	char obj_name[256];
	char obj_type[256];
	char obj_flags[256];
	int type;

	if (!mdb) return NULL;
	if (mdb->catalog) mdb_free_catalog(mdb);
	mdb->catalog = g_ptr_array_new();
	mdb->num_catalog = 0;

	/* dummy up a catalog entry so we may read the table def */
	memset(&msysobj, 0, sizeof(MdbCatalogEntry));
	msysobj.mdb = mdb;
	msysobj.object_type = MDB_TABLE;
	msysobj.table_pg = 2;
	strcpy(msysobj.object_name, "MSysObjects");

	/* mdb_table_dump(&msysobj); */

	table = mdb_read_table(&msysobj);
	if (!table) return NULL;

	mdb_read_columns(table);

	mdb_bind_column_by_name(table, "Id", obj_id, NULL);
	mdb_bind_column_by_name(table, "Name", obj_name, NULL);
	mdb_bind_column_by_name(table, "Type", obj_type, NULL);
	mdb_bind_column_by_name(table, "Flags", obj_flags, NULL);

	mdb_rewind_table(table);

	while (mdb_fetch_row(table)) {
		type = atoi(obj_type);
		if (objtype==MDB_ANY || type == objtype) {
			
			
			entry = (MdbCatalogEntry *) g_malloc0(sizeof(MdbCatalogEntry));
			entry->mdb = mdb;
			strcpy(entry->object_name, obj_name);
			entry->object_type = (type & 0x7F);
			entry->table_pg = atol(obj_id) & 0x00FFFFFF;
			entry->flags = atol(obj_flags);
			mdb->num_catalog++;
			g_ptr_array_add(mdb->catalog, entry); 
		}
	}
	
 
	mdb_free_tabledef(table);

	return mdb->catalog;
}
コード例 #2
0
ファイル: updrow.c プロジェクト: abougouffa/mdbtools
int
main(int argc, char **argv)
{
MdbHandle *mdb;
MdbTableDef *table;
char *colname, *tabname;
char *colval;
int colnum;
char *sargname = NULL;
char *updstr = NULL;
char data[255];
int len;


	if (argc<4) {
		fprintf(stderr,"Usage: %s <file> <table> <sargs> <updstr>\n",argv[0]);
		exit(1);
	}
	
	mdb = mdb_open(argv[1], MDB_WRITABLE);
	tabname = argv[2];
	sargname = argv[3];
	updstr = g_strdup(argv[4]);

	table = mdb_read_table_by_name(mdb, tabname, MDB_TABLE);

	if (table) {
		mdb_read_columns(table);
		mdb_read_indices(table);
		printf("updstr %s\n",updstr);
		colname = strtok(updstr,"=");
		colval = strtok(NULL,"=");
		colnum = mdb_bind_column_by_name(table, colname, data, &len);
		printf("column %d\n", colnum);
		read_to_row(table, sargname);
		printf("current value of %s is %s, changing to %s\n", colname, data, colval);
		len = strlen(colval);
		strcpy(data,colval);
		mdb_update_row(table);
		mdb_free_tabledef(table);
	}

	mdb_close(mdb);
	return 0;
}
コード例 #3
0
ファイル: prole.c プロジェクト: agworld/mdbtools
int
main(int argc, char **argv)
{
	MdbHandle *mdb;
	MdbTableDef *table;
	char *dot, *colname, *tabname;
	char *sargname = NULL;


	if (argc<2) {
		fprintf(stderr,"Usage: %s <file> <table.column> [sargs]\n",argv[0]);
		exit(1);
	}
	
	mdb_init();
	mdb = mdb_open(argv[1], MDB_NOFLAGS);
	dot = strchr(argv[2],'.');
	if (argc>3) sargname = argv[3];

	if (!dot) {
		fprintf(stderr,"Usage: %s <file> <table.column> [sarg]\n",argv[0]);
		exit(1);
	}
	tabname = argv[2];
	*dot='\0';
	colname = ++dot;

	table = mdb_read_table_by_name(mdb, tabname, MDB_TABLE);

	if (table) {
		mdb_read_columns(table);
		dump_ole(table, colname, sargname);
		mdb_free_tabledef(table);
	}

	mdb_close(mdb);
	mdb_exit();

	exit(0);
}
コード例 #4
0
ファイル: sargtest.c プロジェクト: Byclosure/mdbtools
main(int argc, char **argv)
{
	MdbHandle *mdb;
	MdbTableDef *table;

	mdb_init();

	if (!(mdb = mdb_open(MDB_FILE, MDB_NOFLAGS))) {
		exit(1);
	}
	
	table = mdb_read_table_by_name(mdb, TABLE_NAME, MDB_TABLE);

	if (table) {
		print_table(table);
		mdb_free_tabledef(table);
	}

	mdb_close(mdb);
	mdb_exit();

	exit(0);
}
コード例 #5
0
ファイル: backend.c プロジェクト: agworld/mdbtools
static void
generate_table_schema(FILE *outfile, MdbCatalogEntry *entry, char *dbnamespace, guint32 export_options)
{
	MdbTableDef *table;
	MdbHandle *mdb = entry->mdb;
	MdbColumn *col;
	unsigned int i;
	char* quoted_table_name;
	char* quoted_name;
	MdbProperties *props;
	const char *prop_value;

	quoted_table_name = mdb->default_backend->quote_schema_name(dbnamespace, entry->object_name);

	/* drop the table if it exists */
	if (export_options & MDB_SHEXP_DROPTABLE)
		fprintf (outfile, mdb->default_backend->drop_statement, quoted_table_name);

	/* create the table */
	fprintf (outfile, "CREATE TABLE %s\n", quoted_table_name);
	fprintf (outfile, " (\n");

	table = mdb_read_table (entry);

	/* get the columns */
	mdb_read_columns (table);

	/* loop over the columns, dumping the names and types */
	for (i = 0; i < table->num_cols; i++) {
		col = g_ptr_array_index (table->columns, i);

		quoted_name = mdb->default_backend->quote_schema_name(NULL, col->name);
		fprintf (outfile, "\t%s\t\t\t%s", quoted_name,
			mdb_get_colbacktype_string (col));
		free(quoted_name);

		if (mdb_colbacktype_takes_length(col)) {

			/* more portable version from DW patch */
			if (col->col_size == 0)
	    			fputs(" (255)", outfile);
			else
	    			fprintf(outfile, " (%d)", col->col_size);
		}

		if (export_options & MDB_SHEXP_CST_NOTNULL) {
			if (col->col_type == MDB_BOOL) {
				/* access booleans are never null */
				fputs(" NOT NULL", outfile);
			} else {
				const gchar *not_null = mdb_col_get_prop(col, "Required");
				if (not_null && not_null[0]=='y')
					fputs(" NOT NULL", outfile);
			}
		}

		if (export_options & MDB_SHEXP_DEFVALUES) {
			int done = 0;
			if (col->props) {
				gchar *defval = g_hash_table_lookup(col->props->hash, "DefaultValue");
				if (defval) {
					size_t def_len = strlen(defval);
					fputs(" DEFAULT ", outfile);
					/* ugly hack to detect the type */
					if (defval[0]=='"' && defval[def_len-1]=='"') {
						/* this is a string */
						gchar *output_default = malloc(def_len-1);
						gchar *output_default_escaped = malloc(def_len-1);
						memcpy(output_default, defval+1, def_len-2);
						output_default[def_len-2] = 0;
						output_default_escaped = quote_with_squotes(output_default);
						fputs(output_default_escaped, outfile);
						g_free(output_default_escaped);
						free(output_default);
					} else if (!strcmp(defval, "Yes"))
						fputs("TRUE", outfile);
					else if (!strcmp(defval, "No"))
						fputs("FALSE", outfile);
					else if (!strcasecmp(defval, "date()")) {
						if (!strcmp(mdb_col_get_prop(col, "Format"), "Short Date"))
							fputs(mdb->default_backend->short_now, outfile);
						else
							fputs(mdb->default_backend->long_now, outfile);
					}
					else
						fputs(defval, outfile);
					done = 1;
				}
			}
			if (!done && col->col_type == MDB_BOOL)
				/* access booleans are false by default */
				fputs(" DEFAULT FALSE", outfile);
		}
		if (i < table->num_cols - 1)
			fputs(", \n", outfile);
		else
			fputs("\n", outfile);
	} /* for */

	fputs(");\n", outfile);

	/* Add the constraints on columns */
	for (i = 0; i < table->num_cols; i++) {
		col = g_ptr_array_index (table->columns, i);
		props = col->props;
		if (!props)
			continue;

		quoted_name = mdb->default_backend->quote_schema_name(NULL, col->name);

		if (export_options & MDB_SHEXP_CST_NOTEMPTY) {
			prop_value = mdb_col_get_prop(col, "AllowZeroLength");
			if (prop_value && prop_value[0]=='n')
					fprintf(outfile,
						mdb->default_backend->constaint_not_empty_statement,
						quoted_table_name, quoted_name);
		}

		if (export_options & MDB_SHEXP_COMMENTS) {
			prop_value = mdb_col_get_prop(col, "Description");
			if (prop_value) {
				char *comment = quote_with_squotes(prop_value);
				fprintf(outfile,
					mdb->default_backend->column_comment_statement,
					quoted_table_name, quoted_name, comment);
				free(comment);
			}
		}

		free(quoted_name);
	}

	/* Add the constraints on table */
	if (export_options & MDB_SHEXP_COMMENTS) {
		prop_value = mdb_table_get_prop(table, "Description");
		if (prop_value) {
			char *comment = quote_with_squotes(prop_value);
			fprintf(outfile,
				mdb->default_backend->table_comment_statement,
				quoted_table_name, comment);
			free(comment);
		}
	}
	fputc('\n', outfile);


	if (export_options & MDB_SHEXP_INDEXES)
		// prints all the indexes of that table
		mdb_print_indexes(outfile, table, dbnamespace);

	free(quoted_table_name);

	mdb_free_tabledef (table);
}
コード例 #6
0
ファイル: mdb-header.c プロジェクト: agworld/mdbtools
int
main (int argc, char **argv)
{
unsigned int   i, j, k;
unsigned int unsupported = 0;
MdbHandle *mdb;
MdbCatalogEntry *entry;
MdbTableDef *table;
MdbColumn *col;
FILE *typesfile;
FILE *headerfile;
FILE *cfile;

 if (argc < 2) {
   fprintf (stderr, "Usage: %s <file>\n",argv[0]);
   exit (1);
 }

 mdb_init();

 /* open the database */

 mdb = mdb_open (argv[1], MDB_NOFLAGS);
 if (!mdb) {
 	mdb_exit();
	exit(1);
 }

 typesfile = fopen ("types.h", "w");
 headerfile = fopen ("dumptypes.h", "w");
 cfile = fopen ("dumptypes.c", "w");

 copy_header (typesfile);
 copy_header (headerfile);
 fprintf (headerfile, "#include \"types.h\"\n");
 copy_header (cfile);
 fprintf (cfile, "#include <stdio.h>\n");
 fprintf (cfile, "#include \"dumptypes.h\"\n");
 
 /* read the catalog */
 
 mdb_read_catalog (mdb, MDB_TABLE);

 /* loop over each entry in the catalog */

 for (i=0; i < mdb->num_catalog; i++) 
   {
     entry = g_ptr_array_index (mdb->catalog, i);

     if (!mdb_is_user_table(entry))
          continue;

	       fprintf (typesfile, "typedef struct _%s\n", entry->object_name);
	       fprintf (typesfile, "{\n");

	       fprintf (headerfile, "void dump_%s (%s x);\n",
			entry->object_name, entry->object_name);
	       fprintf (cfile, "void dump_%s (%s x)\n{\n",
			entry->object_name, entry->object_name);
	       fprintf (cfile, "\tfprintf (stdout, \"**************** %s ****************\\n\");\n", entry->object_name);
	       table = mdb_read_table (entry);

	       /* get the columns */
	       mdb_read_columns (table);

	       /* loop over the columns, dumping the names and types */

	       for (k = 0; k < table->num_cols; k++)
		 {
		   col = g_ptr_array_index (table->columns, k);
		   fprintf (cfile, "\tfprintf (stdout, \"x.");
		   for (j = 0; j < strlen (col->name); j++)
		     {
		       fprintf (cfile, "%c", tolower (col->name [j]));
		     }
		   fprintf (cfile, " = \");\n");
		   switch (col->col_type)
		     {
		     case MDB_INT:
		       fprintf (typesfile, "\tint\t");
		       fprintf (cfile, "\tdump_int (x.");
		       break;
		     case MDB_LONGINT:
		       fprintf (typesfile, "\tlong\t");
		       fprintf (cfile, "\tdump_long (x.");
		       break;
		     case MDB_TEXT:
		     case MDB_MEMO:
		       fprintf (typesfile, "\tchar *\t");
		       fprintf (cfile, "\tdump_string (x.");
		       break;
		     default:
		       unsupported = 1;
		       break;
		     }
		   for (j = 0; j < strlen (col->name); j++)
		     {
		       fprintf (typesfile, "%c", tolower (col->name [j]));
		       fprintf (cfile, "%c", tolower (col->name [j]));
		     }
		   fprintf (typesfile, ";\n");
		   fprintf (cfile, ");\n");
		 }

	       fprintf (typesfile, "\n} %s ;\n", entry->object_name);
	       fprintf (typesfile, "\n");
	       fprintf (cfile, "}\n\n");

	       mdb_free_tabledef(table);
   }

 fclose (headerfile);
 fclose (typesfile);
 fclose (cfile);
 
 mdb_close (mdb);
 mdb_exit();

 if (unsupported)
  fputs("ERROR: unsupported type.\n", stderr);
 exit(unsupported);
}
コード例 #7
0
ファイル: mdb-prop.c プロジェクト: agworld/mdbtools
int
main(int argc, char **argv)
{
	MdbHandle *mdb;
	MdbTableDef *table;
	gchar name[256];
	gchar *propColName;
	void *buf;
	int col_num;
	int found = 0;

	if (argc < 3) {
		fprintf(stderr,"Usage: %s <file> <object name> [<prop col>]\n",
			argv[0]);
		return 1;
	}
	if (argc < 4)
		propColName = "LvProp";
	else
		propColName = argv[3];

	mdb_init();

	mdb = mdb_open(argv[1], MDB_NOFLAGS);
	if (!mdb) {
		mdb_exit();
		return 1;
	}

	table = mdb_read_table_by_name(mdb, "MSysObjects", MDB_ANY);
	if (!table) {
		mdb_close(mdb);
		mdb_exit();
		return 1;
	}
	mdb_read_columns(table);
	mdb_rewind_table(table);

	mdb_bind_column_by_name(table, "Name", name, NULL);
	buf = g_malloc(MDB_BIND_SIZE);
	col_num = mdb_bind_column_by_name(table, propColName, buf, NULL);
	if (col_num < 1) {
		g_free(buf);
		mdb_free_tabledef(table);
		mdb_close(mdb);
		mdb_exit();
		printf("Column %s not found in MSysObjects!\n", argv[3]);
		return 1;
	}

	while(mdb_fetch_row(table)) {
		if (!strcmp(name, argv[2])) {
			found = 1;
			break;
		}
	}

	if (found) {
		MdbColumn *col = g_ptr_array_index(table->columns, col_num-1);
		size_t size;
		void *kkd = mdb_ole_read_full(mdb, col, &size);
		dump_kkd(mdb, kkd, size);
		free(kkd);
	}

	g_free(buf);
	mdb_free_tabledef(table);
	mdb_close(mdb);
	mdb_exit();

	return 0;
}
コード例 #8
0
ファイル: mdb-array.c プロジェクト: Byclosure/mdbtools
int
main (int argc, char **argv)
{
unsigned int j;
MdbHandle *mdb;
MdbTableDef *table;
MdbColumn *col;
/* doesn't handle tables > 256 columns.  Can that happen? */
char *bound_values [256]; 
char delimiter [] = ", ";
char quote_text = 1;
int count = 0;
int started;

 if  (argc < 3) 
   {
     fprintf (stderr, "Usage: %s <file> <table>\n", argv [0]);
     exit (1);
   }

 mdb_init();
 mdb = mdb_open (argv [1], MDB_NOFLAGS);
 
 table = mdb_read_table_by_name (mdb, argv[2], MDB_TABLE);
 if (table)
       {
	 mdb_read_columns (table);
	 mdb_rewind_table (table);
	 
	 for (j = 0; j < table->num_cols; j++) 
	   {
	     bound_values [j] =  (char *) g_malloc (MDB_BIND_SIZE);
	     bound_values [j] [0] = '\0';
	     mdb_bind_column (table, j+1, bound_values[j], NULL);
	   }

	 fprintf (stdout, "/******************************************************************/\n");
	 fprintf (stdout, "/* THIS IS AN AUTOMATICALLY GENERATED FILE.  DO NOT EDIT IT!!!!!! */\n");
	 fprintf (stdout, "/******************************************************************/\n");
	 fprintf (stdout, "\n");
	 fprintf (stdout, "#include <stdio.h>\n");
	 fprintf (stdout, "#include \"types.h\"\n");
	 fprintf (stdout, "#include \"dump.h\"\n");
	 fprintf (stdout, "\n");
	 fprintf (stdout, "const %s %s_array [] = {\n", argv [2], argv [2]);

	 count = 0;
	 started = 0;
	 while (mdb_fetch_row (table)) 
	   {
	     if (started != 0)
	       {
		 fprintf (stdout, ",\n");
	       }
	     started = 1;
	     fprintf (stdout, "{\t\t\t\t/* %6d */\n\t", count);
	     for  (j = 0; j < table->num_cols; j++) 
	       {
		 fprintf (stdout, "\t");
		 col = g_ptr_array_index (table->columns, j);
		 if  (quote_text && 
		      (col->col_type == MDB_TEXT ||
		       col->col_type == MDB_MEMO)) 
		   {
		     fprintf (stdout, "\"%s\"", bound_values [j]);
		   } 
		 else 
		   {
		     fprintf (stdout, "%s", bound_values [j]);
		   }
		 if (j != table->num_cols - 1)
		   {
		     fprintf (stdout, "%s\n", delimiter);
		   }
		 else
		   {
		     fprintf (stdout, "\n");
		   }
	       }
	     fprintf (stdout, "}");
	     count++;
	   }
	 fprintf (stdout, "\n};\n\n");

	 for  (j = 0; j < table->num_cols; j++) 
	   {
	     g_free (bound_values [j]);
	   }
	 mdb_free_tabledef(table);
       }
 
 mdb_close (mdb);
 mdb_exit();

 fprintf (stdout, "const int %s_array_length = %d;\n", 
	  argv [2],
	  count);

 exit(0);
}
コード例 #9
0
ファイル: mdb-import.c プロジェクト: ASAPPinc/mdbtools
int
main(int argc, char **argv)
{
	int i, row;
	MdbHandle *mdb;
	MdbTableDef *table;
	MdbField fields[256];
	char line[MAX_ROW_SIZE];
	int num_fields;
	/* doesn't handle tables > 256 columns.  Can that happen? */
	int  opt;
	FILE *in;
	char delimiter[2] = ",";
	char header_rows = 0;

	while ((opt=getopt(argc, argv, "H:d:"))!=-1) {
		switch (opt) {
		case 'H':
			header_rows = atol(optarg);
		break;
		case 'd':
			delimiter[0] = optarg[0];
		break;
		default:
		break;
		}
	}
	
	/* 
	** optind is now the position of the first non-option arg, 
	** see getopt(3) 
	*/
	if (argc-optind < 3) {
		fprintf(stderr,"Usage: %s [options] <database> <table> <csv file>\n",argv[0]);
		fprintf(stderr,"where options are:\n");
		fprintf(stderr,"  -H <rows>      skip <rows> header rows\n");
		fprintf(stderr,"  -d <delimiter> specify a column delimiter\n");
		exit(1);
	}

	if (!(mdb = mdb_open(argv[optind], MDB_WRITABLE))) {
		exit(1);
	}
	
	table = mdb_read_table_by_name(mdb, argv[argc-2], MDB_TABLE);
	if (!table) {
		fprintf(stderr,"Table %s not found in database\n", argv[argc-2]);
		exit(1);
	}
	mdb_read_columns(table);
	mdb_read_indices(table);
	mdb_rewind_table(table);

	/*
	 * open the CSV file and read any header rows
	 */
	in = fopen(argv[argc-1], "r");
	if (!in) {
		fprintf(stderr, "Can not open file %s\n", argv[argc-1]);
		exit(1);
	}
	for (i=0;i<header_rows;i++)
		if (!fgets(line, MAX_ROW_SIZE, in)) {
			fprintf(stderr, "Error while reading header column #%d. Check -H parameter.\n", i);
			exit(1);
		}

	row = 1;
	while (fgets(line, MAX_ROW_SIZE, in)) {
		num_fields = prep_row(table, line, fields, delimiter);
		if (!num_fields) {
			fprintf(stderr, "Aborting import at row %d\n", row);
			exit(1);
		}
		/*
	 	* all the prep work is done, let's add the row
	 	*/
		mdb_insert_row(table, num_fields, fields);
	}

	mdb_free_tabledef(table);
	fclose(in);
	mdb_close(mdb);
	return 0;
}