Switch to side-by-side view

--- a/tcsq.tcl
+++ b/tcsq.tcl
@@ -624,11 +624,19 @@
     return [mysqlinfo $hdl tables]
 }
 
+# We use the mysqltcl function to list fields. It uses a
+# C-API interface which is more or less deprecated, and lacks some
+# info (Ex: auto_increment). 
+# In addition, we execute the mysql statement statement 
+#  "show fields from table". This returns the result as if from a 
+# "select fieldname, fieldtype, isnull, iskey, defaultvalue, extra"
+# We get the auto_increment values from there. But the fieldtype field
+# is a little difficult to parse (more difficult than separate type
+# and width fields), which is why we keep using the old interface for now.
  proc MYSQLcolinfo {hdl tbl arnm} {
     upvar $arnm ar
     global sqlsc_def_maxlen
 #    puts "getcolinfo: arnm: $arnm, table: $tbl"
-
     # Fetch info from mysql
     set tabdesc [mysqlcol $hdl $tbl name type length prim_key]
     set names [lindex $tabdesc 0]
@@ -636,13 +644,10 @@
     set lens  [lindex $tabdesc 2]
     set prim_keys [lindex $tabdesc 3]
 
-    # For some unknown reason, mysql capitalizes the column names
-    foreach nm $names {
-    	lappend tnm [string tolower $nm]
-    }
-    set names $tnm
-    unset tnm
-
+    # mysql capitalizes the column names. Using "string tolower" on a
+    # list is not exactly clean, but shouldn't damage it
+    set names [string tolower $names]
+    # If the column list was not specified, we use all columns
     if {![info exists ar(columns)]} {
     	set ar(columns) $names
     	set autocols 1
@@ -650,27 +655,25 @@
     	set autocols 0
     }
 
-    # Look for primary index, possibly build updateindex
-    set pos 0
-    foreach flag $prim_keys {
-    	if {$flag == 0} {
-    	    continue
-    	}
-    	set nm [lindex $names $pos]
-    	# If this is an integer, we make the assumption it's a serial
-    	# There seems to be no way to retrieve the AUTO_INCREMENT 
-    	# attribute from the API
-    	set typ [lindex $typs $pos]
-#    	puts "Type of primary index: $typ"
-    	if {[string match {*int} $typ] || $typ == "long"} {
-    	    set ar(tabcolserial) $nm
-#    	    puts "tabcolserial for $tbl:  $nm"
-    	}
-    	if {$autocols} {
-    	    lappend ar(updateindex) $nm
-    	}
-    	incr pos
-    }
+    # Possibly build updateindex from the primary key. We do this
+    # only if the column list was not specified, else we'd also have
+    # to check that the whole primary key is included in the field
+    # list, which we are too lazy to do. We let the caller override
+    # what we would do if he has set updateindex before
+    if {$autocols && ![info exists ar(updateindex)]} {
+	set pos 0
+	foreach flag $prim_keys {
+	    if {$flag == 1} {
+		set nm [lindex $names $pos]
+		lappend ar(updateindex) $nm
+	    }
+	    incr pos
+	}
+    }
+#    if {[info exists ar(updateindex)]} {
+#	puts "$tbl: updateindex: $ar(updateindex)"
+#    }
+    # Set column types and length in the array
     foreach col $ar(columns) {
 	set scol [_tcsqsimplecolname $tbl $col]
 	if {$scol == ""} {
@@ -722,6 +725,21 @@
     	}
 #    	puts "name: $col, pos $pos, typ $ar($typind) len $ar($lenind)"
     }
+    # Look for an auto_increment column. We do this with the show
+    # fields statement because it's not in the c api flags
+    set qry [MYSQLopensel $hdl "show fields from $tbl"]
+    for {set r [MYSQLnext $qry]} {$r != ""} {set r [MYSQLnext $qry]} {
+	set nm [string trim [lindex $r 0]]
+	set extra [lindex $r 5]
+	if {[regexp -nocase {auto_increment} $extra] == 1} {
+	    set ar(tabcolserial) $nm
+	    break
+	}
+    }
+    MYSQLclosel $qry
+#    if {[info exists ar(tabcolserial)]} {
+#	puts "$tbl: tabcolserial: $ar(tabcolserial)"
+#    }
 }
  proc MYSQLuniqueid {hdl tbl} {
     global MYSQLdatabase tcsqHOST