Commits

Greg Slepak committed 5bc5d38

more improvements to the ORM stuff

Comments (0)

Files changed (2)

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

 
 (new-class 'DB.OBJ)
 
-(constant (global 'DBOBJ_SELECT_SQL)	"SELECT %s FROM %s WHERE %s LIMIT 1"
-		  (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)"
-		  (global 'DBOBJ_INSERT_SQL2)	"INSERT INTO %s VALUES (%s)"
-		  (global 'DBOBJ_ROWID_COL)		"ROWID")
+(set (global 'DBOBJ_SELECT_SQL)   "SELECT %s FROM %s WHERE %s LIMIT 1"
+     (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)"
+     (global 'DBOBJ_INSERT_SQL2)  "INSERT INTO %s VALUES (%s)"
+     (global 'DBOBJ_ROWID_COL)    "ROWID=")
 
 ; The returned object is NOT autoreleased! YOU are responsible for releasing it when you're done with it!
 (define (create-dbobj db table data , qs sql cols result)
 	(setf qs (join (dup "?" (length data) true) ","))
 	(if (list? (first data))
 		(when (db:execute-update (format DBOBJ_INSERT_SQL table (join (map first data) ",") qs) (map last data))
-			(instantiate DB.OBJ db table data (string DBOBJ_ROWID_COL "=" (db:rowid))))
+			(instantiate DB.OBJ db table data (string DBOBJ_ROWID_COL (db:rowid))))
 		(when (setf sql (db:prepare-sql (format DBOBJ_SELECT_SQL2 table)))
 			(setf cols (map sql:col-name (sequence 0 (-- (sql:col-count)))))
 			(when (db:execute-update (format DBOBJ_INSERT_SQL2 table qs) data)
-				(setf result (instantiate DB.OBJ db table (transpose (list cols data)) (string DBOBJ_ROWID_COL "=" (db:rowid))))
+				(setf result (instantiate DB.OBJ db table (transpose (list cols data)) (string DBOBJ_ROWID_COL (db:rowid))))
 			)
 			(deallocate sql)
 			result
 
 ; 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)
+	(when (integer? finder) (setf finder (string DBOBJ_ROWID_COL finder)))
 	(when (setf data (assoc-row-with-db db (format DBOBJ_SELECT_SQL (join cols ",") table finder)))
 		(instantiate DB.OBJ db table data finder)))
 

example-site/views/dragonfly_sqlite3.html

 					</tr>
 				<% )) %>
 			</table>
-			<%
-				(setf obj (find-dbobj db "people" '("name" "age") "ROWID=1"))
-			%>
+			<% (setf obj (find-dbobj db "people" '("name" "age") 1)) %>
 			<p>
 				I've found a person called <%=(obj:name)%> in the table '<%=obj:table%>' at location <%=obj:finder%>, this person is <%=(obj:age)%> years old.
 			</p>
 			</p>
 			<p>
 				Further, I can create a new person. See:
-				<%
-					(release obj)
-					(setf obj (create-dbobj db "people" '("Sue" 57)))
-				%>
+				<% (release obj) (setf obj (create-dbobj db "people" '("Sue" 57))) %>
 			</p>
 			<p>
 			<table>
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.