]> gitweb.factorcode.org Git - factor.git/commitdiff
commit local changes before pulling
authorDoug Coleman <doug.coleman@gmail.com>
Fri, 30 May 2008 23:00:42 +0000 (18:00 -0500)
committerDoug Coleman <doug.coleman@gmail.com>
Fri, 30 May 2008 23:00:42 +0000 (18:00 -0500)
extra/db/errors/errors.factor [new file with mode: 0644]
extra/db/queries/queries.factor
extra/db/sqlite/lib/lib.factor
extra/db/tuples/tuples.factor

diff --git a/extra/db/errors/errors.factor b/extra/db/errors/errors.factor
new file mode 100644 (file)
index 0000000..1e0d1e7
--- /dev/null
@@ -0,0 +1,11 @@
+! Copyright (C) 2008 Doug Coleman.
+! See http://factorcode.org/license.txt for BSD license.
+USING: kernel ;
+IN: db.errors
+
+ERROR: db-error ;
+ERROR: sql-error ;
+
+
+ERROR: table-exists ;
+ERROR: bad-schema ;
index 6dab4f80b8119183edfda59c7a581bfb792039db..e2d452d657dfb950fbd8e5c58fd1dcf5a5dd5da1 100644 (file)
@@ -1,9 +1,8 @@
 ! Copyright (C) 2008 Doug Coleman.
 ! See http://factorcode.org/license.txt for BSD license.
 USING: accessors kernel math namespaces sequences random
-strings
-math.bitfields.lib namespaces.lib db db.tuples db.types
-math.intervals ;
+strings math.parser math.intervals combinators
+math.bitfields.lib namespaces.lib db db.tuples db.types ;
 IN: db.queries
 
 GENERIC: where ( specs obj -- )
@@ -15,7 +14,7 @@ GENERIC: where ( specs obj -- )
 
 : query-make ( class quot -- )
     >r sql-props r>
-    [ 0 sql-counter rot with-variable ";" 0% ] { "" { } { } } nmake
+    [ 0 sql-counter rot with-variable ] { "" { } { } } nmake
     <simple-statement> maybe-make-retryable ; inline
 
 M: db begin-transaction ( -- ) "BEGIN" sql-command ;
@@ -127,3 +126,35 @@ M: db <select-by-slots-statement> ( tuple class -- statement )
         " from " 0% 0%
         where-clause
     ] query-make ;
+
+: do-group ( tuple groups -- )
+    [
+        ", " join " group by " prepend append
+    ] curry change-sql drop ;
+
+: do-order ( tuple order -- )
+    [
+        ", " join " order by " prepend append
+    ] curry change-sql drop ;
+
+: do-offset ( tuple n -- )
+    [
+        number>string " offset " prepend append
+    ] curry change-sql drop ;
+
+: do-limit ( tuple n -- )
+    [
+        number>string " limit " prepend append
+    ] curry change-sql drop ;
+
+: make-advanced-statement ( tuple advanced -- )
+    {
+        [ group>> [ do-group ] [ drop ] if* ]
+        [ order>> [ do-order ] [ drop ] if* ]
+        [ limit>> [ do-limit ] [ drop ] if* ]
+        [ offset>> [ do-offset ] [ drop ] if* ]
+    } 2cleave ;
+
+M: db <advanced-select-statement> ( tuple class advanced -- tuple )
+    >r <select-by-slots-statement> r>
+    dupd make-advanced-statement ;
index e92c4bbd8a415506fd6477bcf16a6063fd669956..f2e603b0499cc9ade20dc3bc17b945533aee77c3 100755 (executable)
@@ -4,24 +4,25 @@ USING: alien.c-types arrays assocs kernel math math.parser
 namespaces sequences db.sqlite.ffi db combinators
 continuations db.types calendar.format serialize
 io.streams.byte-array byte-arrays io.encodings.binary
-io.backend ;
+io.backend db.errors ;
 IN: db.sqlite.lib
 
-: sqlite-error ( n -- * )
-    sqlite-error-messages nth throw ;
+ERROR: sqlite-error < db-error n string ;
+ERROR: sqlite-sql-error < sql-error n string ;
 
-: sqlite-statement-error-string ( -- str )
-    db get db-handle sqlite3_errmsg ;
+: throw-sqlite-error ( n -- * )
+    dup sqlite-error-messages nth sqlite-error ;
 
 : sqlite-statement-error ( -- * )
-    sqlite-statement-error-string throw ;
+    SQLITE_ERROR
+    db get db-handle sqlite3_errmsg sqlite-sql-error ;
 
 : sqlite-check-result ( n -- )
     {
-        { [ dup SQLITE_OK = ] [ drop ] }
-        { [ dup SQLITE_ERROR = ] [ sqlite-statement-error ] }
-        [ sqlite-error ]
-    } cond ;
+        { SQLITE_OK [ ] }
+        { SQLITE_ERROR [ sqlite-statement-error ] }
+        [ throw-sqlite-error ]
+    } case ;
 
 : sqlite-open ( path -- db )
     normalize-path
index 0ffbd5bd47bd2f9d27a5a298fa5fadf408776ed9..10010ba759fb80c3da5161e3f9ea49b2b1fb3823 100755 (executable)
@@ -42,6 +42,8 @@ HOOK: <insert-user-assigned-statement> db ( class -- obj )
 HOOK: <update-tuple-statement> db ( class -- obj )
 HOOK: <delete-tuples-statement> db ( tuple class -- obj )
 HOOK: <select-by-slots-statement> db ( tuple class -- tuple )
+TUPLE: advanced-statement group order offset limit ;
+HOOK: <advanced-select-statement> db ( tuple class advanced -- tuple )
 
 HOOK: insert-tuple* db ( tuple statement -- )