ocarina/src/core/ct/db.py

133 lines
3.1 KiB
Python

#! /usr/bin/python
# To change this template, choose Tools | Templates
# and open the template in the editor.
__author__="bjschuma"
__date__ ="$Jan 27, 2010 6:21:27 PM$"
from bt.message import write
from sqlite3 import *
from bt import sql
import settings
def init():
if sql.dbexists() == True:
return
table = sql.CTable("library")
table.addcol("id","INTEGER","PRIMARY KEY AUTOINCREMENT")
table.addcol("name","TEXT","UNIQUE")
table.addcol("path","TEXT","UNIQUE")
table.execute()
table = sql.CTable("artist")
table.addcol("id","INTEGER","PRIMARY KEY AUTOINCREMENT")
table.addcol("name","TEXT","UNIQUE")
table.execute()
table = sql.CTable("album")
table.addcol("id","INTEGER","PRIMARY KEY AUTOINCREMENT")
table.addcol("name","TEXT","UNIQUE")
table.execute()
table = sql.CTable("track")
table.addcol("id","INTEGER","PRIMARY KEY AUTOINCREMENT")
table.addcol("artist","INTEGER")
table.addcol("album","INTEGER")
table.addcol("count","INTEGER")
table.addcol("length","INTEGER")
table.addcol("name","TEXT")
table.addcol("path","TEXT","UNIQUE")
table.execute()
table = sql.CTable("libtrack")
table.addcol("library","INTEGER")
table.addcol("track","INTEGER")
table.execute()
def newlib(name,path):
try:
ins = sql.Insert('library',[None,name,path])
ins.execute()
except:
pass
def rmtrk(trid):
# Get the artist and album of the track
sel = sql.Select("artist,album","track","id="+str(trid))
(arid,alid) = sel.execute().fetchone()
rm = sql.Remove("track","id="+str(trid))
rm.execute()
# Remove artist if no other tracks are using this artist
sel = sql.Select("count(*)","track","artist="+str(arid))
count = sel.execute().fetchone()[0]
if count == 0:
rm = sql.Remove("artist","id="+str(arid))
rm.execute()
# Remove album if no other tracks are using this album
sel = sql.Select("count(*)","track","album="+str(alid))
count = sel.execute().fetchone()[0]
if count == 0:
rm = sql.Remove("album","id="+str(alid))
rm.execute()
def rmlib(name):
sel = sql.Select("id,name","library","name='"+name+"'")
result = sel.execute().fetchone()
if result == []:
return
libid = result[0]
name = result[1]
sel = sql.Select("track","libtrack","library="+str(libid))
result = sel.execute().fetchall()
for track in result:
where = "track="+str(track[0])+" AND library!="+str(libid)
sel = sql.Select("track,library","libtrack",where )
r = sel.execute().fetchall()
# If track is not in any other library, it can be removed
if r == []:
rmtrk(track[0])
rm = sql.Remove("library","name='"+name+"'")
rm.execute()
rm = sql.Remove("libtrack","library="+str(libid))
rm.execute()
write("Removed library: "+name)
def libid(libname):
sel = sql.Select("id","library","name='"+libname+"'")
result = sel.execute().fetchone()
if result == None:
return None
return result[0]
def listlib():
sel = sql.Select("*","library")
result = sel.execute().fetchall()
id = settings.get("curlib")
curname = ""
write("Id Name Path")
write("------------------")
for row in result:
if row[0] == id:
curname = row[1]
write( str(row[0]) + " " +
row[1] + " " + row[2] )
#if not curname == "":
#write("")
write("Current: "+curname)