 	   (into-array
 	    Object
 	    (map 
-	     (fn [#^Integer column] (.getObject result-set column))
-	     (range 1 (+ 1 (count (.getAttributes metadata)))))))
+	     (fn [reader #^Integer column-index] (reader result-set column-index))
+	     readers
+	     (iterate inc 1))))
 	  output))
 	output))))
 

;;--------------------------------------------------------------------------------

(defn sql-attributes 
  "Derive a sequence of MetaData attributes from a SQL ResultSet"
  [#^ResultSetMetaData sql-metadata]
  (map
   (fn [column]
       [(keyword (.getColumnName sql-metadata column))
	(Class/forName (.getColumnClassName sql-metadata column))
	true])			 ;we don't know - so assume mutability
   (range 1 (+ 1 (.getColumnCount sql-metadata)))))

(defn sql-data
  "Create a sequence from some MetaData and a SQL ResultSet"
  [#^Metadata metadata #^ResultSet result-set]
  (let [creator (.getCreator metadata)]
    (loop [output nil]
      (if (.next result-set)
	(recur


(defn sql-model
  "make a SQL query and return the ResultSet as a Model"
  [model-name #^Connection connection #^String sql primary-keys version-keys version-comparator & [type-translations]]
  (let [result-set (.executeQuery (.prepareStatement connection sql))
	metadata (custom-metadata 
		  (name (gensym "org.dada.core.SQLModel"))
		  Object
		  primary-keys
		  version-keys
		  version-comparator
		  (sql-attributes (.getMetaData result-set) type-translations))
	data (sql-data metadata result-set type-translations)
	sql-model (model model-name metadata)]
    (.close result-set)
    (insert-n sql-model data)
