ยปCore Development>Code coverage>Doc/includes/sqlite3/converter_point.py

Python code coverage for Doc/includes/sqlite3/converter_point.py

#countcontent
1n/aimport sqlite3
2n/a
3n/aclass Point:
4n/a def __init__(self, x, y):
5n/a self.x, self.y = x, y
6n/a
7n/a def __repr__(self):
8n/a return "(%f;%f)" % (self.x, self.y)
9n/a
10n/adef adapt_point(point):
11n/a return ("%f;%f" % (point.x, point.y)).encode('ascii')
12n/a
13n/adef convert_point(s):
14n/a x, y = list(map(float, s.split(b";")))
15n/a return Point(x, y)
16n/a
17n/a# Register the adapter
18n/asqlite3.register_adapter(Point, adapt_point)
19n/a
20n/a# Register the converter
21n/asqlite3.register_converter("point", convert_point)
22n/a
23n/ap = Point(4.0, -3.2)
24n/a
25n/a#########################
26n/a# 1) Using declared types
27n/acon = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES)
28n/acur = con.cursor()
29n/acur.execute("create table test(p point)")
30n/a
31n/acur.execute("insert into test(p) values (?)", (p,))
32n/acur.execute("select p from test")
33n/aprint("with declared types:", cur.fetchone()[0])
34n/acur.close()
35n/acon.close()
36n/a
37n/a#######################
38n/a# 1) Using column names
39n/acon = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_COLNAMES)
40n/acur = con.cursor()
41n/acur.execute("create table test(p)")
42n/a
43n/acur.execute("insert into test(p) values (?)", (p,))
44n/acur.execute('select p as "p [point]" from test')
45n/aprint("with column names:", cur.fetchone()[0])
46n/acur.close()
47n/acon.close()