src/lib/asParse.c 1.7

1.7 2009/03/17 18:25:47 kent
Adding asColumnToSqlType
Index: src/lib/asParse.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/lib/asParse.c,v
retrieving revision 1.6
retrieving revision 1.7
diff -b -B -U 4 -r1.6 -r1.7
--- src/lib/asParse.c	11 Feb 2007 21:43:15 -0000	1.6
+++ src/lib/asParse.c	17 Mar 2009 18:25:47 -0000	1.7
@@ -2,8 +2,9 @@
 
 #include "common.h"
 #include "linefile.h"
 #include "tokenizer.h"
+#include "dystring.h"
 #include "asParse.h"
 
 static char const rcsid[] = "$Id$";
 
@@ -46,8 +47,38 @@
 tokenizerErrAbort(tkz, "Unknown type '%s'", s);
 return NULL;
 }
 
+static void sqlSymDef(struct asColumn *col, struct dyString *dy)
+/* print symbolic column definition for sql */
+{
+dyStringPrintf(dy, "%s(", col->lowType->sqlName);
+struct slName *val;
+for (val = col->values; val != NULL; val = val->next)
+    {
+    dyStringPrintf(dy, "\"%s\"", val->name);
+    if (val->next != NULL)
+        dyStringAppend(dy, ", ");
+    }
+dyStringPrintf(dy, ") ");
+}
+
+struct dyString *asColumnToSqlType(struct asColumn *col)
+/* Convert column to a sql type spec in returned dyString */
+{
+struct asTypeInfo *lt = col->lowType;
+struct dyString *type = dyStringNew(32);
+if ((lt->type == t_enum) || (lt->type == t_set))
+    sqlSymDef(col, type);
+else if (col->isList || col->isArray)
+    dyStringPrintf(type, "longblob");
+else if (lt->type == t_char)
+    dyStringPrintf(type, "char(%d)", col->fixedSize ? col->fixedSize : 1);
+else
+    dyStringPrintf(type, "%s", lt->sqlName);
+return type;
+}
+
 static struct asColumn *mustFindColumn(struct asObject *table, char *colName)
 /* Return column or die. */
 {
 struct asColumn *col;