aboutsummaryrefslogtreecommitdiff
path: root/ezdb.py
blob: eb81f96ecb5556be4a6be35f6f50c01a82b67565 (plain)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
##########################################################
#
# ezdb.py - SQLite database functions
#
# NOTE: This only works with data of type text and varchar
#
# Import libraries
# from ezdb import TableDef, DatabaseDef
#
# Create a table object with fields:
# newtable = TableDef("people")
# newtable.AddFields("name","text")
# newtable.AddFields("age","text")
#
# Create database object
# newdb = DatabaseDef("db/test.db")
#
# Add table to database
# newdb.AddTable(newtable)
#
# Initialize database schema (write table schema to database)
# newdb.Initialize()
#
# Alter Table (Add column only):
#   SQL:  ALTER TABLE people ADD COLUMN address text;
#   EZBD: newdb.Alter("address","text")
#
# Insert into table
#	  SQL:  INSERT INTO people(name, age) VALUES("Bob","24");
# 	EZDB: newdb.Insert("people",["Bob","24"])
#
#	Return True on success, False on failure
#
# Update table entry
# 	SQL:  UPDATE people SET name='Robert', age='26' WHERE name='Bob' AND age='24';
# 	EZDB: newdb.Update("people",["name","age"],["Robert","26"],["name","age"],["Bob","24"])
#	
#	returns True on success, False on failure
#
# Select Row(s) from table
#	  SQL:  SELECT * FROM people WHERE name='Robert';
# 	EZDB: newdb.Select("people",["name"],["Robert"])
#
#	  SQL:  SELECT * FROM people WHERE name='Robert' AND age='26';
# 	EZDB: newdb.Select("people",["name","age"],["Robert","26"])
#
#	returns [{"name": "Robert", "age":"26"}]
#	(additional rows will be pushed onto the array as additional dictonaries)
#
# Select all items from table
#	  SQL:  SELECT * FROM people;
# 	EZDB: newdb.Select("people")
#
# returns [{"name": "Robert", "age":"26"}]
# (additional rows will be pushed onto the array as additional dictonaries)
#
# Delete specific item from table
#   SQL:  DELETE FROM people WHERE name='Robert';
#   EZDB: newdb.Delete("people",["name"],["Robert"])
#
# Delete all items from table:
#   SQL:  DELETE FROM people WHERE 1
#   EZDB: newdb.Delete("people")
# 
##########################################################

import sqlite3
import os
import re

class TableDef:
  def __init__(self,name):
    self.NAME = name
    self.FIELDS = []

  def AddField(self, name, fieldtype):
    self.FIELDS.append({ "name" : name, "type" : fieldtype })

  def Create(self):
    createlist = [a["name"] + " " + a["type"] for a in self.FIELDS]
    if len(createlist) >= 1:
      return str("CREATE TABLE " + self.NAME + "(" + ",".join(createlist) + ");")
    else:
      return False

  def Alter(self, columnname, columntype):
    if columnname and columntype:
      return str("ALTER TABLE " + self.NAME + " ADD COLUMN " + columnname + " " + columntype + ";")
    else:
      return False

  def Insert(self, valuelist):
    if len(valuelist) == len([a["name"] for a in self.FIELDS]):
      return str("INSERT INTO " + self.NAME + "(" + ",".join([a["name"] for a in self.FIELDS]) + ") VALUES('" + "','".join(valuelist) + "');")
    else:
      return False

  def Update(self, valuefieldlist, valuevaluelist, wherefieldlist, wherevaluelist):
    if len(valuefieldlist) == len(valuevaluelist) and len(wherefieldlist) == len(wherevaluelist):
      return str("UPDATE " + self.NAME + " SET " + ", ".join([valuefieldlist[idx] + "='" + valuevaluelist[idx] + "'" for idx in range(len(valuefieldlist))]) + " WHERE " + " AND ".join([wherefieldlist[idx] + "='" + wherevaluelist[idx] + "'" for idx in range(len(wherevaluelist))]) + ";")
    else:
      return False

  def Select(self, fieldlist, valuelist):
    if len([f for f in fieldlist if f in [a["name"] for a in self.FIELDS]]) > 0 and len(fieldlist) == len(valuelist):
      return str("SELECT * FROM " + self.NAME + " WHERE " + " AND ".join([fieldlist[idx] + "='" + valuelist[idx] + "'" for idx in range(len(fieldlist))]) + ";") 
    else:
      return False

  def SelectAll(self):
    return str("SELECT * FROM " + self.NAME + ";")

  def Delete(self, fieldlist, valuelist):
    if len([f for f in fieldlist if f in [a["name"] for a in self.FIELDS]]) > 0 and len(fieldlist) == len(valuelist):
      return str("DELETE FROM " + self.NAME + " WHERE " + " AND ".join([fieldlist[idx] + "='" + valuelist[idx] + "'" for idx in range(len(fieldlist))]) + ";")
    else:
      return False

  def DeleteAll(self):
    return str("DELETE FROM " + self.NAME + " WHERE 1;")

class DatabaseDef:
  def __init__(self, filename):
    self.DATABASE = filename
    self.TABLES = []

  def AddTable(self, tabledef):
    self.TABLES.append({"name" : tabledef.NAME, "obj" : tabledef })

  def ExistDB(self):
    return os.path.exists(self.DATABASE)

  def GetDB(self):
    return sqlite3.connect(self.DATABASE)

  def Initialize(self):
    try:
      if not self.ExistDB():
        db = self.GetDB()
        for table in self.TABLES:
          result = db.cursor().execute(table["obj"].Create())
        db.commit()
        db.close()
      else:
        for table in self.TABLES:
          db = self.GetDB()
          result = db.cursor().execute("SELECT name, sql FROM sqlite_master WHERE type='table' AND name='" + table["name"] + "';")
          tableinfo = [a for a in result if a[0] == table["name"]]
          if len(tableinfo) == 0:
            db.cursor().execute(table["obj"].Create())
          else:
            if not tableinfo[0][1] + ";"  == table["obj"].Create():
              oldcolumns = re.sub("^.*\(","", re.sub("\)$","",tableinfo[0][1])).split(",")
              newcolumns = re.sub("^.*\(","", re.sub("\);$","",table["obj"].Create())).split(",")
              if len(newcolumns) > len(oldcolumns):
                newcolumns = [a for a in newcolumns if a not in set(oldcolumns)]
                for thiscolumn in newcolumns:
                  self.Alter(table["name"], thiscolumn.split(" ")[0], thiscolumn.split(" ")[1])
              else:
                raise NameError("Cannot remove columns or change column type")
      return True
    except:
      return False

  def Alter(self, tablename, columnname, columntype):
    try:
      db = self.GetDB()
      db.cursor().execute([a for a in self.TABLES if a["name"] == tablename][0]["obj"].Alter(columnname, columntype))
      db.commit()
      db.close()
    except:
      return False

  def Insert(self, tablename, valuelist):
    try:
      db = self.GetDB()
      db.cursor().execute([a for a in self.TABLES if a["name"] == tablename][0]["obj"].Insert(valuelist))
      db.commit()
      db.close()
      return True
    except:
      return False

  def Update(self, tablename, valuefieldlist, valuevaluelist, wherefieldlist, wherevaluelist):
    try:
      db = self.GetDB()
      db.cursor().execute([a for a in self.TABLES if a["name"] == tablename][0]["obj"].Update(valuefieldlist,valuevaluelist,wherefieldlist,wherevaluelist))
      db.commit()
      db.close()
      return True
    except:
      return False

  def Delete(self, tablename, fieldlist = None, valuelist = None):
    try:
      db = self.GetDB()
      if fieldlist and valuelist:
        db.cursor().execute([a for a in self.TABLES if a["name"] == tablename][0]["obj"].Delete(fieldlist,valuelist))
      else:
        db.cursor().execute([a for a in self.TABLES if a["name"] == tablename][0]["obj"].DeleteAll())

      db.commit()
      db.close()
        
    except:
      return False

  def Select(self, tablename, fieldlist= None, valuelist = None):
    try:
      db = self.GetDB()
      returnval = None
      if fieldlist and valuelist:
        returnval = []
        result = db.cursor().execute([a for a in self.TABLES if a["name"] == tablename][0]["obj"].Select(fieldlist,valuelist)).fetchall()
        for thisresult in result:
          returnval.append(None)
          returnval[len(returnval)-1] = {}
          for i in range(0,len([a for a in self.TABLES if a["name"] == tablename][0]["obj"].FIELDS)):
            returnval[len(returnval)-1][[a for a in self.TABLES if a["name"] == tablename][0]["obj"].FIELDS[i]["name"]] = str(thisresult[i])
      else:
        returnval = []
        result = db.cursor().execute([a for a in self.TABLES if a["name"] == tablename][0]["obj"].SelectAll()).fetchall()
        for thisresult in result:
          returnval.append(None)
          returnval[len(returnval)-1] = {}
          for i in range(0,len([a for a in self.TABLES if a["name"] == tablename][0]["obj"].FIELDS)):
            returnval[len(returnval)-1][[a for a in self.TABLES if a["name"] == tablename][0]["obj"].FIELDS[i]["name"]] = str(thisresult[i])

      db.close()
      return returnval
    except:
      return False