]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
dev
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 4 Dec 2005 02:13:55 +0000 (02:13 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 4 Dec 2005 02:13:55 +0000 (02:13 +0000)
doc/build/content/metadata.myt
doc/build/content/sqlconstruction.myt

index dcc0e5a11ddeca719c822c7da51a3680ea3502e6..461b87aa879f5f6180fcf88236f49673ffbd65bf 100644 (file)
@@ -4,7 +4,7 @@
     <&|doclib.myt:item, name="tables", description="Describing Tables with MetaData" &>
     <p>The core of SQLAlchemy's query and object mapping operations is table metadata, which are Python objects that describe tables.  Metadata objects can be created by explicitly naming the table and all its properties, using the Table, Column, ForeignKey, and Sequence objects imported from <span class="codeline">sqlalchemy.schema</span>, and a database engine constructed as described in the previous section, or they can be automatically pulled from an existing database schema.  First, the explicit version: </p>
         <&|formatting.myt:code&>
-        from sqlalchemy.schema import *
+        from sqlalchemy import *
         engine = create_engine('sqlite', {'filename':':memory:'}, **opts)
         
         users = Table('users', engine, 
@@ -21,7 +21,7 @@
             Column('pref_value', String(100))
         )
         </&>
-        <p>The specific datatypes, such as Integer, String, etc. are defined in <&formatting.myt:link, path="types", text="sqlalchemy.types"&> and are automatically pulled in when you import * from <span class="codeline">sqlalchemy.schema</span>.  Note that for Column objects, an altername name can be specified via the "key" parameter; if this parameter is given, then all programmatic references to this Column object will be based on its key, instead of its actual column name.</p>
+        <p>The specific datatypes, such as Integer, String, etc. are defined in <&formatting.myt:link, path="types", text="sqlalchemy.types"&> and are automatically pulled in when you import * from <span class="codeline">sqlalchemy</span>.  Note that for Column objects, an altername name can be specified via the "key" parameter; if this parameter is given, then all programmatic references to this Column object will be based on its key, instead of its actual column name.</p>
         
         <p>Once constructed, the Table object provides a clean interface to the table's properties as well as that of its columns:
         
@@ -168,6 +168,15 @@ DROP TABLE employees
     </&>
 
     <&|doclib.myt:item, name="sequences", description="Defining Sequences" &>
+    <P>A table with a sequence looks like:</p>
+    <&|formatting.myt:code&>
+        table = Table("cartitems", db, 
+        Column("cart_id", Integer, Sequence('cart_id_seq'), primary_key=True),
+        Column("description", String(40)),
+        Column("createdate", DateTime())
+    )
+    </&>
+    <p>Defining a Sequence means that it will be created along with the table.create() call, and more importantly the sequence will be explicitly used when inserting new rows for this table.  For databases that dont support sequences, the Sequence object has no effect.  A sequence can also be specified with <span class="codeline">optional=True</span> which indicates the Sequence should only be used on a database that requires an explicit sequence (which currently is just Oracle).  A database like Postgres, while it uses sequences to create primary keys, is often used via the SERIAL column option which removes the need for explicit access to the sequence.</p>
     </&>
 
 </&>
\ No newline at end of file
index 65dc8f0134a60732f4b68791810022e1cf482dd6..21bc5799aa12d943afaf927d4a4888470742411b 100644 (file)
@@ -251,17 +251,47 @@ AND users.user_name = :users_user_name
 </&>
 </&>            
             <&|doclib.myt:item, name="operators", description="Operators" &>
-            <p>Supported column operators so far are all the numerical comparison operators, i.e. '==', '>', '>=', etc., as well as like(), startswith(), endswith(), and in().  Boolean operators include and_() and or_().</p>
+            <p>Supported column operators so far are all the numerical comparison operators, i.e. '==', '>', '>=', etc., as well as like(), startswith(), endswith(), and in().  Boolean operators include not_(), and_() and or_(), which also can be used inline via '~', '&', and '|'.  Math operators are '+', '-', '*', '/'.</p>
             <&|formatting.myt:code &>
                 users.select(users.c.user_name.like('%ter'))
                 users.select(users.c.user_name == 'jane')
                 users.select(users.c.user_id.in_(1,2,3))
                 users.select(and_(addresses.c.street.endswith('green street'), addresses.c.zip=='11234'))
+                users.select(addresses.c.street.endswith('green street') & (addresses.c.zip=='11234'))
+                select([users.c.user_name + '_name'])
+                users.select(~(addresses.c.street == 'Green Street'))
             </&>
             </&>
 
         </&>
+        <&|doclib.myt:item, name="functions", description="Functions" &>
+        <p>Functions can be specified using the <span class="codeline">func</span> keyword:</p>
+        <&|formatting.myt:code &>
+            <&formatting.myt:poplink&>select([func.count(users.c.user_id)]).execute()
+            <&|formatting.myt:codepopper, link="sql" &>
+SELECT count(users.user_id) FROM users
+            </&>
+
+            <&formatting.myt:poplink&>users.select(func.substr(users.c.user_name, 1) == 'J').execute()
+            <&|formatting.myt:codepopper, link="sql" &>
+SELECT users.user_id, users.user_name, users.password FROM users 
+WHERE substr(users.user_name, :substr) = :substr_1
+{'substr_1': 'J', 'substr': 1}
+            </&>
 
+        </&>
+        </&>
+        <&|doclib.myt:item, name="literals", description="Literals" &>
+        <p>You can drop in a literal value anywhere there isnt a column to attach to via the <span class="codeline">literal</span> keyword:</p>
+        <&|formatting.myt:code &>
+        <&formatting.myt:poplink&>select([literal('foo') + literal('bar'), users.c.user_name]).execute()
+        <&|formatting.myt:codepopper, link="sql" &>
+        SELECT :literal + :literal_1, users.user_name 
+        FROM users
+        {'literal_1': 'bar', 'literal': 'foo'}
+        </&>
+        </&>
+        </&>
         <&|doclib.myt:item, name="orderby", description="Order By" &>
         <P>The ORDER BY clause of a select statement can be specified as individual columns to order by within an array     specified via the <span class="codeline">order_by</span> parameter, and optional usage of the asc() and desc() functions:
             <&|formatting.myt:code &>