Greg Slepak avatar Greg Slepak committed c415a26

can now use find-dbobj to return multiple objects

Comments (0)

Files changed (1)

example-site/dragonfly-framework/plugins-inactive/db/database_orm.lsp

 
 (new-class 'DB.OBJ)
 
-(set (global 'DBOBJ_SELECT_SQL)   "SELECT %s FROM %s WHERE %s LIMIT 1"
+(set (global 'DBOBJ_SELECT_SQL)   "SELECT %s FROM %s WHERE %s LIMIT %d"
      (global 'DBOBJ_SELECT_SQL2)  "SELECT * FROM %s" ; quasi-hack to obtain the col names b/c INSERT doesn't tell us. we don't actually retrieve the rows.
      (global 'DBOBJ_UPDATE_SQL)   "UPDATE %s SET %s=? WHERE %s" ; LIMIT isn't supported for UPDATE unless sqlite3 was compiled with the option
      (global 'DBOBJ_INSERT_SQL)   "INSERT INTO %s (%s) VALUES (%s)"
 )
 
 ; The returned object is NOT autoreleased! YOU are responsible for releasing it when you're done with it!
-(define (find-dbobj db table cols finder , data)
+(define (find-dbobj db table cols finder (limit 1) , data)
 	(when (integer? finder) (setf finder (string DBOBJ_ROWID_COL finder)))
-	(when (setf data (dbobj-assoc-row db table cols finder))
-		(instantiate DB.OBJ db table data finder))
+	(when (setf data (dbobj-assoc-rows db table cols finder limit))
+		(if (> (length data) 1)
+			(map (fn (x) (instantiate DB.OBJ db table x finder)) data)
+			(instantiate DB.OBJ db table (first data) finder)
+		)
+	)
 )
 
 
 
 (define (dbobj-refetch obj)
 	(set 'obj:dirty      nil
-	     'obj:revert-set (dbobj-assoc-row obj:db obj:table (map first obj:revert-set) obj:finder)
+	     'obj:revert-set (dbobj-assoc-row obj:db obj:table (map first obj:revert-set) obj:finder 1)
 	     'obj:change-set obj:revert-set
 	)
 )
 	(join (map (fn(x)(string (first x) "=?")) finder) DBOBJ_WHERE_COMB)
 )
 
-(define (dbobj-assoc-row db table cols finder)
+(define (dbobj-assoc-rows db table cols finder limit)
 	(setf cols (join cols ","))
 	(if (list? finder)
-		(assoc-row-with-db db (format DBOBJ_SELECT_SQL cols table (dbobj-finder-binder finder)) (map last finder))
-		(assoc-row-with-db db (format DBOBJ_SELECT_SQL cols table finder))
+		(assoc-rows-with-db db (format DBOBJ_SELECT_SQL cols table (dbobj-finder-binder finder) limit) (map last finder))
+		(assoc-rows-with-db db (format DBOBJ_SELECT_SQL cols table finder limit))
 	)
 )
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.