FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
sqlobject/sqlobject/index.py at master · sqlobject/sqlobject · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
sqlobject
/
sqlobject
Public
Notifications
You must be signed in to change notification settings
Fork
26
Star
156
Code
Issues
2
Pull requests
0
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
sqlobject
/
sqlobject
/
index.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
189 lines (163 loc) · 6.44 KB
Breadcrumbs
sqlobject
/
sqlobject
/
index.py
Copy path
File metadata and controls
189 lines (163 loc) · 6.44 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
from
itertools
import
count
from
.
converters
import
sqlrepr
creationOrder
=
count
()
class
SODatabaseIndex
(
object
):
def
__init__
(
self
,
soClass
,
name
,
columns
,
creationOrder
,
unique
=
False
):
self
.
soClass
=
soClass
self
.
name
=
name
self
.
descriptions
=
self
.
convertColumns
(
columns
)
self
.
creationOrder
=
creationOrder
self
.
unique
=
unique
def
get
(
self
,
*
args
,
**
kw
):
if
not
self
.
unique
:
raise
AttributeError
(
"'%s' object has no attribute 'get' "
"(index is not unique)"
%
self
.
name
)
connection
=
kw
.
pop
(
'connection'
,
None
)
if
args
and
kw
:
raise
TypeError
(
"You cannot mix named and unnamed arguments"
)
columns
=
[
d
[
'column'
]
for
d
in
self
.
descriptions
if
'column'
in
d
]
if
kw
and
len
(
kw
)
!=
len
(
columns
)
or
\
args
and
len
(
args
)
!=
len
(
columns
):
raise
TypeError
(
"get() takes exactly %d argument and an optional "
"named argument 'connection' (%d given)"
%
(
len
(
columns
),
len
(
args
)
+
len
(
kw
)))
if
args
:
kw
=
{}
for
i
in
range
(
len
(
args
)):
if
columns
[
i
].
foreignName
is
not
None
:
kw
[
columns
[
i
].
foreignName
]
=
args
[
i
]
else
:
kw
[
columns
[
i
].
name
]
=
args
[
i
]
return
self
.
soClass
.
selectBy
(
connection
=
connection
,
**
kw
).
getOne
()
def
convertColumns
(
self
,
columns
):
"""
Converts all the columns to dictionary descriptors;
dereferences string column names.
"""
new
=
[]
for
desc
in
columns
:
if
not
isinstance
(
desc
,
dict
):
desc
=
{
'column'
:
desc
}
if
'expression'
in
desc
:
assert
'column'
not
in
desc
, (
'You cannot provide both an expression and a column '
'(for %s in index %s in %s)'
%
(
desc
,
self
.
name
,
self
.
soClass
))
assert
'length'
not
in
desc
, (
'length does not apply to expressions (for %s in '
'index %s in %s)'
%
(
desc
,
self
.
name
,
self
.
soClass
))
new
.
append
(
desc
)
continue
columnName
=
desc
[
'column'
]
if
not
isinstance
(
columnName
,
str
):
columnName
=
columnName
.
name
colDict
=
self
.
soClass
.
sqlmeta
.
columns
if
columnName
not
in
colDict
:
for
possible
in
colDict
.
values
():
if
possible
.
origName
==
columnName
:
column
=
possible
break
else
:
# None found
raise
ValueError
(
"The column by the name %r was not found "
"in the class %r"
%
(
columnName
,
self
.
soClass
))
else
:
column
=
colDict
[
columnName
]
desc
[
'column'
]
=
column
new
.
append
(
desc
)
return
new
def
getExpression
(
self
,
desc
,
db
):
if
isinstance
(
desc
[
'expression'
],
str
):
return
desc
[
'expression'
]
else
:
return
sqlrepr
(
desc
[
'expression'
],
db
)
def
sqliteCreateIndexSQL
(
self
,
soClass
):
if
self
.
unique
:
uniqueOrIndex
=
'UNIQUE INDEX'
else
:
uniqueOrIndex
=
'INDEX'
spec
=
[]
for
desc
in
self
.
descriptions
:
if
'expression'
in
desc
:
spec
.
append
(
self
.
getExpression
(
desc
,
'sqlite'
))
else
:
spec
.
append
(
desc
[
'column'
].
dbName
)
ret
=
'CREATE %s %s_%s ON %s (%s)'
%
\
(
uniqueOrIndex
,
self
.
soClass
.
sqlmeta
.
table
,
self
.
name
,
self
.
soClass
.
sqlmeta
.
table
,
', '
.
join
(
spec
))
return
ret
postgresCreateIndexSQL
=
maxdbCreateIndexSQL
=
mssqlCreateIndexSQL
=
\
sybaseCreateIndexSQL
=
firebirdCreateIndexSQL
=
sqliteCreateIndexSQL
def
mysqlCreateIndexSQL
(
self
,
soClass
):
if
self
.
unique
:
uniqueOrIndex
=
'UNIQUE'
else
:
uniqueOrIndex
=
'INDEX'
spec
=
[]
for
desc
in
self
.
descriptions
:
if
'expression'
in
desc
:
spec
.
append
(
self
.
getExpression
(
desc
,
'mysql'
))
elif
'length'
in
desc
:
spec
.
append
(
'%s(%d)'
%
(
desc
[
'column'
].
dbName
,
desc
[
'length'
]))
else
:
spec
.
append
(
desc
[
'column'
].
dbName
)
return
'ALTER TABLE %s ADD %s %s (%s)'
%
\
(
soClass
.
sqlmeta
.
table
,
uniqueOrIndex
,
self
.
name
,
', '
.
join
(
spec
))
class
DatabaseIndex
(
object
):
"""
This takes a variable number of parameters, each of which is a
column for indexing. Each column may be a column object or the
string name of the column (*not* the database name). You may also
use dictionaries, to further customize the indexing of the column.
The dictionary may have certain keys:
'column':
The column object or string identifier.
'length':
MySQL will only index the first N characters if this is
given. For other databases this is ignored.
'expression':
You can create an index based on an expression, e.g.,
'lower(column)'. This can either be a string or a sqlbuilder
expression.
Further keys may be added to the column specs in the future.
The class also take the keyword argument `unique`; if true then
a UNIQUE index is created.
"""
baseClass
=
SODatabaseIndex
def
__init__
(
self
,
*
columns
,
**
kw
):
kw
[
'columns'
]
=
columns
self
.
kw
=
kw
self
.
creationOrder
=
next
(
creationOrder
)
def
setName
(
self
,
value
):
assert
self
.
kw
.
get
(
'name'
)
is
None
, \
"You cannot change a name after it has already been set "
\
"(from %s to %s)"
%
(
self
.
kw
[
'name'
],
value
)
self
.
kw
[
'name'
]
=
value
def
_get_name
(
self
):
return
self
.
kw
[
'name'
]
def
_set_name
(
self
,
value
):
self
.
setName
(
value
)
name
=
property
(
_get_name
,
_set_name
)
def
withClass
(
self
,
soClass
):
return
self
.
baseClass
(
soClass
=
soClass
,
creationOrder
=
self
.
creationOrder
,
**
self
.
kw
)
def
__repr__
(
self
):
return
'<%s %s %s>'
%
(
self
.
__class__
.
__name__
,
hex
(
abs
(
id
(
self
)))[
2
:],
self
.
kw
)
__all__
=
[
'DatabaseIndex'
]
Back
|
FazBrowse Home
|
New Git URL