File: Views.html

package info (click to toggle)
sqlobject 3.1.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 9,280 kB
  • ctags: 17,912
  • sloc: python: 16,713; sh: 18; makefile: 13
file content (113 lines) | stat: -rw-r--r-- 4,402 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <meta content="text/html; charset=utf-8" http-equiv="content-type">
<title>Views and SQLObjects</title>
    <link href="layout.css" type="text/css" rel="stylesheet">
  </head>
  <body>
    <div id="page">
      <h1 class="doc-title"><a></a></h1>
      <div id="navcontainer">
		    <ul id="navlist">
          <li class="pagenav">
            <ul>
              <li class="page_item">
                <a href="index.html" title="Project Home / Index">SQLObject</a>
              </li>
              <li class="page_item">
                <a href="module-index.html" title="sqlobject package and module reference">Modules</a>
              </li>
              <li>
                <a href="community.html" title="Mailing List">Discuss</a>
              </li>
	      <li>
	        <a href="SQLObject.html">Documentation</a>
	      </li>
            </ul>
          </li>
        </ul>
      </div>
      <hr>
      <div id="content"><div class="rst-doc">
  <h1 class="pudge-member-page-heading">Views and SQLObjects</h1>
  <p>In general, if your database backend supports defining views
you may define them outside of SQLObject and treat them
as a regular table when defining your SQLObject class.</p>
<div class="section" id="viewsqlobject">
<h1>ViewSQLObject</h1>
<p>The rest of this document is experimental.</p>
<p><tt class="docutils literal">from sqlobject.views import *</tt></p>
<p><tt class="docutils literal">ViewSQLObject</tt> is an attempt to allow defining
views that allow you to define a SQL query that acts
like a SQLObject class. You define columns based on
other SQLObject classes .q SQLBuilder columns, have columns
that are aggregates of other columns, and join
multiple SQLObject classes into one and add restrictions
using SQLBuilder expressions.</p>
<p>The resulting classes are currently read only, if you find
use for this idea please bring discussion to the mailing list.</p>
<p>A short example from the tests will suffice for now.</p>
<p>Base classes:</p>
<pre class="literal-block">
class PhoneNumber(SQLObject):
  number = StringCol()
  calls = SQLMultipleJoin('PhoneCall')
  incoming = SQLMultipleJoin('PhoneCall', joinColumn='toID')

class PhoneCall(SQLObject):
  phoneNumber = ForeignKey('PhoneNumber')
  to = ForeignKey('PhoneNumber')
  minutes = IntCol()
</pre>
<p>View classes:</p>
<pre class="literal-block">
class ViewPhoneCall(ViewSQLObject):
  class sqlmeta:
      idName = PhoneCall.q.id
      clause = PhoneCall.q.phoneNumberID==PhoneNumber.q.id

  minutes = IntCol(dbName=PhoneCall.q.minutes)
  number = StringCol(dbName=PhoneNumber.q.number)
  phoneNumber = ForeignKey('PhoneNumber', dbName=PhoneNumber.q.id)
  call = ForeignKey('PhoneCall', dbName=PhoneCall.q.id)

class ViewPhone(ViewSQLObject):
  class sqlmeta:
      idName = PhoneNumber.q.id
      clause = PhoneCall.q.phoneNumberID==PhoneNumber.q.id

  minutes = IntCol(dbName=func.SUM(PhoneCall.q.minutes))
  numberOfCalls = IntCol(dbName=func.COUNT(PhoneCall.q.phoneNumberID))
  number = StringCol(dbName=PhoneNumber.q.number)
  phoneNumber = ForeignKey('PhoneNumber', dbName=PhoneNumber.q.id)
  calls = SQLMultipleJoin('PhoneCall', joinColumn='phoneNumberID')
  vCalls = SQLMultipleJoin('ViewPhoneCall', joinColumn='phoneNumberID')
</pre>
<a href="https://sourceforge.net/projects/sqlobject" class="reference external image-reference"><img src="https://sourceforge.net/sflogo.php?group_id=74338&amp;type=10" alt="Get SQLObject at SourceForge.net. Fast, secure and Free Open Source software downloads" style="width: 80px; height: 15px;" class="noborder align-center"></a>
</div>
</div></div>
      <div id="footer">
        <p style="float: left;">
          built with 
          <a href="http://lesscode.org/projects/pudge/">pudge/0.1.3</a> |
		      original design by 
          <a href="http://blog.ratterobert.com/">ratter / robert</a>
	      </p>
        <div>
        <br> <!--
        <a name="search">
          <form method="get" id="searchform" 
                action="http://lesscode.org/blog/index.php">
            <div>
              <input type="text" value="" name="s" id="s" />
              <input type="submit" id="searchsubmit" value="Search" />
            </div>
          </form>
        </a> -->
        <br>
        </div>
      </div>
    </div>
  </body>
</html>