99c155926716fd2eb4da85b9a51d987add667db0 kent Sun Apr 14 14:42:57 2013 -0700 Adding defaultZeros flag - this seems to be needed with strictor MySQL configs when inserting partial records. diff --git src/hg/autoSql/autoSql.c src/hg/autoSql/autoSql.c index f2f42b7..8daa0aa 100644 --- src/hg/autoSql/autoSql.c +++ src/hg/autoSql/autoSql.c @@ -10,69 +10,90 @@ * This file is copyright 2002-2005 Jim Kent, but license is hereby * granted for all use - public, private or commercial. */ #include "common.h" #include "errabort.h" #include "linefile.h" #include "obscure.h" #include "dystring.h" #include "asParse.h" #include "options.h" boolean withNull = FALSE; boolean makeJson = FALSE; boolean makeDjango = FALSE; +boolean defaultZeros = FALSE; void usage() /* Explain usage and exit. */ { errAbort("autoSql - create SQL and C code for permanently storing\n" "a structure in database and loading it back into memory\n" "based on a specification file\n" "usage:\n" " autoSql specFile outRoot {optional: -dbLink -withNull -json} \n" "This will create outRoot.sql outRoot.c and outRoot.h based\n" "on the contents of specFile. \n" "\n" "options:\n" " -dbLink - optionally generates code to execute queries and\n" " updates of the table.\n" " -withNull - optionally generates code and .sql to enable\n" " applications to accept and load data into objects\n" " with potential 'missing data' (NULL in SQL)\n" " situations.\n" + " -defaultZeros - will put zero and or empty string as default value\n" " -django - generate method to output object as django model Python code\n" " -json - generate method to output the object in JSON (JavaScript) format.\n"); } 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 (!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); @@ -1852,30 +1873,31 @@ char *outRoot, outTail[256]; char dotC[256]; char dotH[256]; char dotSql[256]; char dotDjango[256]; FILE *cFile; FILE *hFile; FILE *sqlFile; FILE *djangoFile = NULL; char defineName[256]; boolean doDbLoadAndSave = FALSE; optionInit(&argc, argv, optionSpecs); doDbLoadAndSave = optionExists("dbLink"); withNull = optionExists("withNull"); +defaultZeros = optionExists("defaultZeros"); makeJson = optionExists("json"); makeDjango = optionExists("django"); if (argc != 3) usage(); objList = asParseFile(argv[1]); outRoot = argv[2]; /* don't embed directories in files */ splitPath(outRoot, NULL, outTail, NULL); safef(dotC, sizeof(dotC), "%s.c", outRoot); cFile = mustOpen(dotC, "w"); safef(dotH, sizeof(dotH), "%s.h", outRoot); hFile = mustOpen(dotH, "w");