4 DATABASE_NAME = 'contacts.db'
7 MIDDLELEVEL_FIELD = 'ou'
18 create index i_data on data (id);
21 id integer primary key,
24 create index i_fields on fields (id);
26 create table relation(
30 create index i_relation_data on relation(data_id);
33 id integer primary key,
37 create index i_struct_id on struct(parent);
44 def __init__(self, basedir):
45 self._path = os.path.join(basedir, DATABASE_NAME)
47 if not os.path.exists(self._path):
53 """Connects to database."""
55 self.conn = sqlite3.connect(self._path, isolation_level="EXCLUSIVE")
58 """Creates new databse."""
61 self.conn.executescript(SCHEMA)
64 """Closes connection with database."""
69 """Save all changes."""
74 """Clear all database tables."""
76 execute = self.conn.execute
77 execute("DELETE from data")
78 execute("DELETE from fields")
79 execute("DELETE from struct")
80 execute("DELETE from relation")
84 """Returns all fields from FIELDS table."""
86 return [item[0] for item in self.conn.execute("SELECT name FROM \
87 fields").fetchall() if item[0]!='image']
89 # operations with DATA table
90 def add_entry(self, entry):
91 """Adds new entry to database."""
93 execute = self.conn.execute
96 _id = execute("SELECT MAX(id) FROM data").fetchone()[0] \
101 for field, values in entry.items():
102 # update FIELDS table
103 field_id = execute("SELECT id FROM fields WHERE name=?", \
106 execute("INSERT INTO fields values(NULL, ?)", (field,))
107 field_id = execute("SELECT last_insert_rowid()").fetchone()[0]
109 field_id = field_id[0]
113 execute("INSERT INTO data values(?,?,?)", \
114 (_id, field_id, value))
116 # update STRUCT table
117 name = entry[TOPLEVEL_FIELD][0]
118 parent_id = execute("SELECT id FROM struct WHERE name=? ", \
120 if parent_id is None: # update STRUCT table (TOPLEVEL_FIELD)
121 execute("INSERT INTO struct values(NULL,?,0)", (name,))
122 parent_id = execute("SELECT last_insert_rowid()").fetchone()[0]
124 parent_id = parent_id[0]
126 name = entry[MIDDLELEVEL_FIELD][0]
127 child_id = execute("SELECT id FROM struct WHERE name=? AND parent=?", \
128 (name, parent_id)).fetchone()
129 if child_id is None: # update STRUCT table (MIDDLELEVEL_FIELD)
130 execute("INSERT INTO struct values(NULL,?,?)", (name, parent_id))
131 child_id = execute("SELECT last_insert_rowid()").fetchone()[0]
133 child_id = child_id[0]
135 # update RELATION table
136 execute("INSERT INTO relation values(?,?)", (_id, child_id))
138 def get_folders(self, parent=None):
140 Returns list of all folders (items with folders and files)
144 if parent is None: # return all folders on level2
145 return self.conn.execute("SELECT DISTINCT name, id FROM struct \
146 WHERE parent!=0 ORDER BY name ASC").fetchall()
148 return self.conn.execute("SELECT DISTINCT name, id FROM struct \
149 WHERE parent=? ORDER BY name ASC", (parent,)).fetchall()
151 def get_files(self, fields, parent=0):
152 """Returns list of all files from DATA table."""
155 execute = self.conn.execute
156 fields = execute("SELECT id, name FROM fields WHERE name IN (%s)" \
157 % ','.join('%r' % (field,) for field in fields)).fetchall()
158 if parent == 0: # get all files
159 data_ids = [_id[0] for _id in execute("SELECT DISTINCT id FROM \
161 else: # get files for selected parent
162 data_ids = [_id[0] for _id in execute("SELECT data_id FROM \
163 relation WHERE struct_id=?", (parent,)).fetchall()]
164 for data_id in data_ids:
165 items_dict[data_id] = {}
166 for field_id, field_name in fields:
167 field_value = execute("SELECT value FROM data WHERE id=? \
168 AND field_id=?", (data_id, field_id)).fetchone()[0]
169 items_dict[data_id][field_name] = field_value
172 def get_entry(self, _id):
173 """Returns full entry by it id."""
175 execute = self.conn.execute
177 for field_id, value in execute("SELECT field_id, value FROM data WHERE \
178 id=?", (_id,)).fetchall():
179 field_name = execute("SELECT name FROM fields WHERE id=?", \
180 (field_id,)).fetchone()[0]
181 entry_dict[field_name] = value