26368b16a37d3845623054270770df7e905c32f1 kent Fri Apr 19 10:06:02 2013 -0700 Adding index and auto keywords to autoSql columns. diff --git src/hg/autoSql/autoSql.c src/hg/autoSql/autoSql.c index 8daa0aa..f4d1225 100644 --- src/hg/autoSql/autoSql.c +++ src/hg/autoSql/autoSql.c @@ -50,67 +50,108 @@ static struct optionSpec optionSpecs[] = { {"dbLink", OPTION_BOOLEAN}, {"withNull", OPTION_BOOLEAN}, {"defaultZeros", OPTION_BOOLEAN}, {"json", OPTION_BOOLEAN}, {"django", OPTION_BOOLEAN}, {NULL, 0} }; void sqlColumn(struct asColumn *col, FILE *f) /* Print out column in SQL. */ { fprintf(f, " %s ", col->name); struct dyString *type = asColumnToSqlType(col); fprintf(f, "%s", type->string); +if (col->autoIncrement) + { + fprintf(f, " auto_increment"); + } +else + { if (!withNull) { if (defaultZeros) { char *defaultVal = ""; if (!col->isList && !col->isArray) { if (col->lowType->stringy) { if (col->lowType->type == t_string) defaultVal = " default ''"; } else defaultVal = " default 0"; } fprintf(f, "%s", defaultVal); } else fprintf(f, " not null"); } + } fputc(',', f); fprintf(f, "\t# %s\n", col->comment); dyStringFree(&type); } void sqlTable(struct asObject *table, FILE *f) /* Print out structure of table in SQL. */ { struct asColumn *col; fprintf(f, "\n#%s\n", table->comment); fprintf(f, "CREATE TABLE %s (\n", table->name); for (col = table->columnList; col != NULL; col = col->next) sqlColumn(col, f); fprintf(f," #Indices\n"); +boolean gotIndex = FALSE; +for (col = table->columnList; col != NULL; col = col->next) + { + struct asIndex *index = col->index; + if (index != NULL) + { + char *type = index->type; + char *sqlType = NULL; + if (sameString(type, "primary")) + sqlType = "PRIMARY KEY"; + else if (sameString(type, "index")) + sqlType = "INDEX"; + else if (sameString(type, "unique")) + sqlType = "UNIQUE"; + else + errAbort("Unrecognized index type %s", type); + if (!gotIndex) + { + gotIndex = TRUE; + } + else + { + fprintf(f, ",\n"); + } + fprintf(f, " %s(%s", sqlType, col->name); + if (index->size != 0) + fprintf(f, "(%d)", index->size); + fprintf(f, ")"); + } + } + +if (!gotIndex) fprintf(f, " PRIMARY KEY(%s)\n", table->columnList->name); +else + fprintf(f, "\n"); fprintf(f, ");\n"); } void djangoEnumChoices(struct asColumn *col, FILE *f) /* Write out a list of choices to use with a django enum. */ { fprintf(f, " %sChoices = (\n", col->name); struct slName *val; for (val = col->values; val != NULL; val = val->next) fprintf(f, " ('%s', '%s'),\n", val->name, val->name); fprintf(f, " )\n"); } int longestValue(struct asColumn *col) /* Return length of longest value in col->values list */