Beispiel #1
0
/*
 * Destroy a table in a database, by free all allocated data
 * structures.  Does not release the table in the backend adapter.
 */
void
database_table_free(Database *database, DbTable *table)
{
  if (database && table) {
    logdebug("%s: Freeing table '%s'\n", database->name, table->schema->name);
    schema_free (table->schema);
    oml_free(table);
  } else {
    logwarn("%s: Tried to free a NULL table (or database was NULL).\n",
            (database ? database->name : "NONE"));
  }
}
Beispiel #2
0
/** Deallocate a TableDescr array
 *
 * \param tables beginning of the array
 * \param n size of the array
 */
void
table_descr_array_free (TableDescr* tables, int n)
{
  int i = 0;
  for (i = 0; i < n; i++) {
      oml_free (tables[i].name);
      if (tables[i].schema) {
        schema_free (tables[i].schema);
      }
  }

  oml_free(tables);
}
Beispiel #3
0
/** Deallocate a TableDescr linked list
 *
 * Also free the encapsulated schema structures.
 *
 * \param tables head of the list
 */
void
table_descr_list_free (TableDescr* tables)
{
  TableDescr* t = tables;

  while (t) {
      TableDescr* next = t->next;
      oml_free (t->name);
      if (t->schema) {
        schema_free (t->schema);
      }
      oml_free (t);
      t = next;
  }
}
Beispiel #4
0
int main(int argc, char* argv[])
{
	int opt;
	const char* infilename  = NULL;
	const char* outfilename = NULL;
	int test = 0;
	FILE*   out;

	while ((opt = getopt(argc, argv, "o:th")) != -1) {

		switch (opt) {
		case 'o':
			outfilename = optarg;
			break;
		case 't':
			test = 1;
			break;
		case 'h':
			usage(argv[0]);
			return EXIT_SUCCESS;
			break;
		default:
			usage(argv[0]);
			return EXIT_FAILURE;
		}
	}

	if (argc - optind > 1) {
		usage(argv[0]);
		return EXIT_FAILURE;
	} else if (argc - optind == 1) {
		infilename = argv[optind];
	}

	if (outfilename)
		out = fopen(outfilename, "w");
	else
		out = stdout;

	if (!out) {
		fprintf(stderr, "ERROR unable to open %s: %m\n", outfilename);
		return EXIT_FAILURE;
	}

	////////////////////////////////////////////////////////////////////////

	Schema* s = NULL;
	char*   b;
	char*   end;

	s = schema_fparse(infilename);
	b = schema_pack(s, &end);
	schema_free(s);

	if (test) {
		s = schema_unpack(b);
		schema_printf(s, out);
		schema_free(s);
	} else {
		fwrite(b, end-b, 1, out);
	}

	free(b);
	fclose(out);

	return EXIT_SUCCESS;
}
Beispiel #5
0
/** Search a Database's registered DbTables for one matchng the given schema.
 *
 * If none is found, the table is created. If one is found, but the schema
 * differs, try to append a number to the name (up to MAX_TABLE_RENAME), create
 * that table, and update the schema.
 *
 * If the table search/creation is successful, the returned DbTable is already
 * added to the list of the Database.
 *
 * \param database Database to search
 * \param schema schema structure for the table to add
 * \return a newly created DbTable for that table, or NULL on error
 *
 * \see MAX_TABLE_RENAME, database_create_table
 */
DbTable*
database_find_or_create_table(Database *database, struct schema *schema)
{
  if (database == NULL) return NULL;
  if (schema == NULL) return NULL;

  DbTable *table = NULL;
  struct schema *s = schema_copy(schema);
  int i = 1;
  int diff = 0, tnlen;

  tnlen = strlen(schema->name);

  do {
    table = database_find_table (database, s->name);

    if (table) {
      diff = schema_diff (s, table->schema);
      if (!diff) {
        if(database->semantic){
          if (database->table_create (database, table, 0)) {
              logerror ("%s: Couldn't create table '%s'\n", database->name, schema->name);
            }
        }
        schema_free(s);
        return table;

      } else if (diff == -1) {
        logerror ("%s: Schema error table '%s'\n", database->name, s->name);
        logdebug (" One of the server schema %p or the client schema %p is probably NULL\n", s, table->schema);

      } else if (diff > 0) {
        logdebug ("%s: Schema differ for table '%s', at or after column %d\n",
            database->name, s->name, diff);
      }

      if (i == 1) {
        /* First time we need to increase the size */
        /* Add space for 2 characters and null byte, that is up to '_9', with MAX_TABLE_RENAME = 10 */
        s->name = oml_realloc(s->name, tnlen + 3);
        strncpy(s->name, schema->name, tnlen);
      }
      snprintf(&s->name[tnlen], 3, "_%d", ++i);
    }

  } while(table && diff && (i < MAX_TABLE_RENAME));

  if (table && diff) {
    logerror ("%s: Too many (>%d) tables named '%s_x', giving up. Please use the rename attribute of <mp /> tags.\n",
      database->name, MAX_TABLE_RENAME, schema->name);
    schema_free(s);
    return NULL;
  }

  if(i>1) {
    /* We had to change the table name*/
    logwarn("%s: Creating table '%s' for new stream '%s' with incompatible schema\n", database->name, s->name, schema->name);
    oml_free(schema->name);
    schema->name = oml_strndup(s->name, tnlen+3);
  }
  schema_free(s);

  /* No table by that name exists, so we create it */
  table = database_create_table (database, schema);
  if (!table)
    return NULL;
  if (database->table_create (database, table, 0)) {
    logerror ("%s: Couldn't create table '%s'\n", database->name, schema->name);
    /* Unlink the table from the experiment's list */
    DbTable* t = database->first_table;
    if (t == table)
      database->first_table = t->next;
    else {
      while (t && t->next != table)
        t = t->next;
      if (t && t->next)
        t->next = t->next->next;
    }
    database_table_free (database, table);
    return NULL;
  }
  return table;
}