Now running pgsql, added rollbacks for failed insert/updates/deletes
This commit is contained in:
parent
45c4b55cf2
commit
8821161f71
|
@ -4,46 +4,69 @@ import irc3
|
|||
from docopt import Dict as DocOptDict
|
||||
from irc3.plugins.command import command
|
||||
from irc3.utils import IrcString
|
||||
from psycopg2 import Error
|
||||
|
||||
from . import DatabasePlugin
|
||||
from ..utils import parse_int
|
||||
|
||||
|
||||
# TODO: fix sql shit
|
||||
@irc3.plugin
|
||||
class McManiac(DatabasePlugin):
|
||||
requires = ['irc3.plugins.command',
|
||||
'nxy.plugins.database']
|
||||
table = 'mcmaniacs'
|
||||
'nxy.plugins.storage']
|
||||
|
||||
@command(options_first=True)
|
||||
def mcmaniac(self, mask: IrcString, channel: IrcString, args: DocOptDict):
|
||||
def mcmaniac(self, mask: IrcString, target: IrcString, args: DocOptDict):
|
||||
"""Get a random McManiaC or by index.
|
||||
|
||||
%%mcmaniac [<index>]
|
||||
"""
|
||||
index = args.get('<index>')
|
||||
|
||||
if index:
|
||||
index = parse_int(index)
|
||||
if not index:
|
||||
return
|
||||
index, order, _ = index
|
||||
order = 'id {order}'.format(order=order)
|
||||
extra = 'offset ?'
|
||||
binds = [index]
|
||||
offset = 'offset %s'
|
||||
else:
|
||||
order = 'random()'
|
||||
extra = ''
|
||||
binds = []
|
||||
self.cur.execute('''select item,
|
||||
(select count(id) from mcmaniacs b where a.id >= b.id) as idx,
|
||||
(select count(id) from mcmaniacs) as len
|
||||
from mcmaniacs a order by {order} limit 1 {extra}
|
||||
'''.format(order=order, extra=extra), binds)
|
||||
result = self.cur.fetchone()
|
||||
if result:
|
||||
return '[{idx}/{len}] {item}'.format(**result)
|
||||
offset = ''
|
||||
|
||||
@irc3.event(r'(?i)^:(?P<mask>\S+) PRIVMSG \S+ :.*(?P<item>Mc\S+iaC).*')
|
||||
# Fetch result from database
|
||||
self.cur.execute('''
|
||||
select
|
||||
item,
|
||||
rank() over (order by id),
|
||||
count(*) over (rows between unbounded preceding
|
||||
and unbounded following) as total
|
||||
from
|
||||
mcmaniacs
|
||||
order by
|
||||
{order}
|
||||
limit
|
||||
1
|
||||
{offset}
|
||||
'''.format(order=order, offset=offset), [index])
|
||||
result = self.cur.fetchone()
|
||||
|
||||
if result:
|
||||
return '[{rank}/{total}] {item}'.format(**result)
|
||||
|
||||
@irc3.event(r'^:(?P<mask>\S+) PRIVMSG \S+ :.*(?P<item>Mc\S+iaC).*')
|
||||
def save(self, mask: str, item: str):
|
||||
if IrcString(mask).nick != self.bot.nick:
|
||||
self.cur.execute('insert into mcmaniacs (item) values (?)', [item])
|
||||
try:
|
||||
# Insert into database
|
||||
self.cur.execute('''
|
||||
insert into
|
||||
mcmaniacs (item)
|
||||
values
|
||||
(%s)
|
||||
''', [item])
|
||||
self.con.commit()
|
||||
except Error:
|
||||
# Rollback transaction on error
|
||||
self.con.rollback()
|
||||
|
|
|
@ -4,18 +4,85 @@ import irc3
|
|||
from docopt import Dict as DocOptDict
|
||||
from irc3.plugins.command import command
|
||||
from irc3.utils import IrcString
|
||||
from psycopg2 import Error
|
||||
|
||||
from . import DatabasePlugin
|
||||
from ..utils import NICK_REGEX, parse_int
|
||||
|
||||
|
||||
"""
|
||||
delete from
|
||||
quotes
|
||||
where
|
||||
id = (
|
||||
select
|
||||
id
|
||||
from
|
||||
quotes a
|
||||
where
|
||||
nick like %s and %s = (
|
||||
select
|
||||
count(id)
|
||||
from
|
||||
quotes b where a.id {op} b.id
|
||||
)
|
||||
order by
|
||||
id {order}
|
||||
)
|
||||
"""
|
||||
|
||||
|
||||
@irc3.plugin
|
||||
class Quotes(DatabasePlugin):
|
||||
requires = ['irc3.plugins.command',
|
||||
'nxy.plugins.database']
|
||||
'nxy.plugins.storage']
|
||||
|
||||
def add_quote(self, mask, nick, quote):
|
||||
# Parse nick from "<@foobar>" like strings
|
||||
nick = NICK_REGEX.match(nick).group(1)
|
||||
|
||||
if not nick:
|
||||
self.bot.notice(mask.nick, '[Quotes] Error parsing nick')
|
||||
else:
|
||||
# Insert quote into database
|
||||
self.cur.execute('''
|
||||
insert into
|
||||
quotes (nick, item)
|
||||
values
|
||||
(%s, %s)
|
||||
''', [nick, quote])
|
||||
|
||||
def delete_quote(self, nick, quote):
|
||||
index, order, _ = parse_int(quote, select=False)
|
||||
|
||||
if index:
|
||||
# Delete from database
|
||||
self.cur.execute('''
|
||||
with ranked_quotes as (
|
||||
select
|
||||
id,
|
||||
rank() over (partition by nick order by id {order})
|
||||
from
|
||||
quotes
|
||||
where
|
||||
nick = %s
|
||||
)
|
||||
|
||||
delete from
|
||||
quotes
|
||||
where
|
||||
id = (
|
||||
select
|
||||
id
|
||||
from
|
||||
ranked_quotes
|
||||
where
|
||||
rank = %s
|
||||
)
|
||||
'''.format(order=order), [nick, index])
|
||||
|
||||
@command(options_first=True)
|
||||
def q(self, mask: IrcString, channel: IrcString, args: DocOptDict):
|
||||
def q(self, mask: IrcString, target: IrcString, args: DocOptDict):
|
||||
"""Get, add or delete quots for a user
|
||||
|
||||
%%q <cmd> <nick> <quote>...
|
||||
|
@ -23,48 +90,62 @@ class Quotes(DatabasePlugin):
|
|||
"""
|
||||
cmd = args.get('<cmd>')
|
||||
nick = args['<nick>']
|
||||
item = args.get('<quote>')
|
||||
if cmd and item:
|
||||
if self.guard.has_permission(mask, 'admin'):
|
||||
quote = ''.join(args.get('<quote>'))
|
||||
|
||||
if cmd and quote:
|
||||
try:
|
||||
# Anybody can add
|
||||
if cmd == 'add':
|
||||
nick = NICK_REGEX.match(nick).group(1)
|
||||
if not nick:
|
||||
return '[Quotes] Error parsing nick'
|
||||
self.cur.execute('insert into quotes (nick, item) values '
|
||||
'(?, ?', [nick, ' '.join(item)])
|
||||
self.add_quote(mask, nick, quote)
|
||||
# But only admins can delete
|
||||
elif cmd == 'del' and self.guard.has_permission(mask, 'admin'):
|
||||
self.delete_quote(nick, quote)
|
||||
|
||||
self.con.commit()
|
||||
if cmd == 'del':
|
||||
index, order, op = parse_int(''.join(item), select=False)
|
||||
if not index:
|
||||
return
|
||||
self.cur.execute('''delete from quotes where id =
|
||||
(select id from quotes a where nick like ? and ? =
|
||||
(select count(id) from quotes b where a.id {op} b.id)
|
||||
order by id {order})
|
||||
'''.format(op=op, order=order), [nick, index])
|
||||
self.con.commit()
|
||||
else:
|
||||
self.bot.notice(mask.nick, 'Permission denied')
|
||||
except Error:
|
||||
# Rollback transaction on error
|
||||
self.con.rollback()
|
||||
else:
|
||||
index = args.get('<index>')
|
||||
binds = [nick, nick, nick]
|
||||
values = [nick]
|
||||
|
||||
if index:
|
||||
index = parse_int(index)
|
||||
if not index:
|
||||
return
|
||||
index, order, _ = index
|
||||
order = 'id {order}'.format(order=order)
|
||||
extra = 'offset ?'
|
||||
binds.append(index)
|
||||
order = 'rank {order}'.format(order=order)
|
||||
offset = 'offset %s'
|
||||
values.append(index)
|
||||
else:
|
||||
order = 'random()'
|
||||
extra = ''
|
||||
self.cur.execute('''select nick, item,
|
||||
(select count(id) from quotes b where b.nick like ? and
|
||||
a.id >= b.id) as idx,
|
||||
(select count(id) from quotes where nick like ?) as len
|
||||
from quotes a where nick like ? order by {order} limit 1 {extra}
|
||||
'''.format(order=order, extra=extra), binds)
|
||||
offset = ''
|
||||
|
||||
# Fetch quote from database
|
||||
self.cur.execute("""
|
||||
with ranked_quotes as (
|
||||
select
|
||||
nick,
|
||||
item,
|
||||
rank() over (partition by nick order by id),
|
||||
count(*) over (partition by nick) as total
|
||||
from
|
||||
quotes
|
||||
)
|
||||
|
||||
select
|
||||
*
|
||||
from
|
||||
ranked_quotes
|
||||
where
|
||||
nick like %s
|
||||
order by
|
||||
{order}
|
||||
limit
|
||||
1
|
||||
{offset}
|
||||
""".format(order=order, offset=offset), values)
|
||||
result = self.cur.fetchone()
|
||||
|
||||
if result:
|
||||
return '[{idx}/{len}] <{nick}> {item}'.format(**result)
|
||||
return '[{rank}/{total}] <{nick}> {item}'.format(**result)
|
||||
|
|
|
@ -5,6 +5,7 @@ import irc3
|
|||
from docopt import Dict as DocOptDict
|
||||
from irc3.plugins.command import command
|
||||
from irc3.utils import IrcString
|
||||
from psycopg2 import Error
|
||||
|
||||
from . import DatabasePlugin
|
||||
|
||||
|
@ -56,6 +57,7 @@ class Rape(DatabasePlugin):
|
|||
else:
|
||||
fine = random.randint(1, 500)
|
||||
|
||||
try:
|
||||
# Insert or add fine to database and return total owe
|
||||
self.cur.execute('''
|
||||
insert into
|
||||
|
@ -80,3 +82,6 @@ class Rape(DatabasePlugin):
|
|||
fine=fine,
|
||||
reason=reason,
|
||||
total=self.cur.fetchone()['amount']))
|
||||
except Error:
|
||||
# Rollback transaction on error
|
||||
self.con.rollback()
|
||||
|
|
|
@ -6,6 +6,7 @@ import irc3
|
|||
from docopt import Dict as DocOptDict
|
||||
from irc3.plugins.command import command
|
||||
from irc3.utils import IrcString
|
||||
from psycopg2 import Error
|
||||
|
||||
from . import DatabasePlugin
|
||||
|
||||
|
@ -52,6 +53,7 @@ class Seen(DatabasePlugin):
|
|||
|
||||
@irc3.event(r'(?i)^:(?P<mask>\S+) PRIVMSG (?P<target>\S+) :(?P<msg>.*)')
|
||||
def save(self, mask: str, target: str, msg: str):
|
||||
try:
|
||||
# Insert or update if user writes a message
|
||||
self.cur.execute('''
|
||||
insert into
|
||||
|
@ -64,3 +66,6 @@ class Seen(DatabasePlugin):
|
|||
message = excluded.message
|
||||
''', [IrcString(mask).nick, target, msg, datetime.now()])
|
||||
self.con.commit()
|
||||
except Error:
|
||||
# Rollback transaction on error
|
||||
self.con.rollback()
|
||||
|
|
|
@ -3,6 +3,7 @@ import irc3
|
|||
from docopt import Dict as DocOptDict
|
||||
from irc3.plugins.command import command
|
||||
from irc3.utils import IrcString
|
||||
from psycopg2 import Error
|
||||
|
||||
from . import DatabasePlugin
|
||||
|
||||
|
@ -49,6 +50,7 @@ class Tell(DatabasePlugin):
|
|||
self.tell_queue[nick] = []
|
||||
self.tell_queue[nick].append(tell)
|
||||
|
||||
try:
|
||||
# Insert tell into database
|
||||
self.cur.execute('''
|
||||
insert into
|
||||
|
@ -57,6 +59,9 @@ class Tell(DatabasePlugin):
|
|||
(%s, %s, %s)
|
||||
''', tell)
|
||||
self.con.commit()
|
||||
except Error:
|
||||
# Rollback transaction on error
|
||||
self.con.rollback()
|
||||
|
||||
@irc3.event(r'(?i)^:(?P<mask>.*) PRIVMSG .* :.*')
|
||||
def check(self, mask: str):
|
||||
|
@ -72,6 +77,7 @@ class Tell(DatabasePlugin):
|
|||
# Remove nick from queue
|
||||
del self.tell_queue[nick]
|
||||
|
||||
try:
|
||||
# Remove tells from database
|
||||
self.cur.execute('''
|
||||
delete from
|
||||
|
@ -80,3 +86,6 @@ class Tell(DatabasePlugin):
|
|||
to_nick = %s
|
||||
''', [nick])
|
||||
self.con.commit()
|
||||
except Error:
|
||||
# Rollback transaction on error
|
||||
self.con.rollback()
|
||||
|
|
|
@ -6,6 +6,7 @@ import irc3
|
|||
from docopt import Dict as DocOptDict
|
||||
from irc3.plugins.command import command
|
||||
from irc3.utils import IrcString
|
||||
from psycopg2 import Error
|
||||
|
||||
from . import DatabasePlugin
|
||||
from ..utils import time_delta
|
||||
|
@ -51,6 +52,7 @@ class Timer(DatabasePlugin):
|
|||
message = ' '.join(args['<message>'])
|
||||
values = [mask, target, message, delay]
|
||||
|
||||
try:
|
||||
# Insert into database (add now + delta)
|
||||
self.cur.execute('''
|
||||
insert into
|
||||
|
@ -68,6 +70,9 @@ class Timer(DatabasePlugin):
|
|||
# Send notice to user that timer has been set
|
||||
self.bot.notice(mask.nick, 'Timer in {delay} set: {message}'
|
||||
.format(delay=delay, message=message))
|
||||
except Error:
|
||||
# Rollback transaction on error
|
||||
self.con.rollback()
|
||||
|
||||
def start_timer(self, mask: IrcString, target: IrcString, message: str,
|
||||
delay: str, delta: timedelta, row_id: int):
|
||||
|
@ -85,6 +90,7 @@ class Timer(DatabasePlugin):
|
|||
nick=mask.nick,
|
||||
delay=delay))
|
||||
|
||||
try:
|
||||
# Delete timer from database
|
||||
self.cur.execute('''
|
||||
delete from
|
||||
|
@ -93,5 +99,8 @@ class Timer(DatabasePlugin):
|
|||
id = %s
|
||||
''', [row_id])
|
||||
self.con.commit()
|
||||
except Error:
|
||||
# Rollback transaction on error
|
||||
self.con.rollback()
|
||||
|
||||
asyncio.ensure_future(callback())
|
||||
|
|
23
schema.sql
23
schema.sql
|
@ -45,26 +45,3 @@ create table if not exists owes (
|
|||
amount integer not null,
|
||||
unique (nick)
|
||||
);
|
||||
|
||||
|
||||
with ranked_quotes as (
|
||||
select
|
||||
id,
|
||||
rank() over (partition by nick order by id desc)
|
||||
from
|
||||
quotes
|
||||
where
|
||||
nick = 'mrhanky'
|
||||
)
|
||||
|
||||
delete from
|
||||
quotes
|
||||
where
|
||||
id = (
|
||||
select
|
||||
id
|
||||
from
|
||||
ranked_quotes
|
||||
where
|
||||
rank = 1
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue
Block a user