|
1 |
| -from general import bake |
| 1 | +import pymysql, datetime, socket |
| 2 | + |
| 3 | +host_ = socket.gethostbyname(socket.gethostname()) |
| 4 | +port_ = 80 # Default XAMPP Apache server port. |
| 5 | +user_ = "root" |
| 6 | +pasw_ = "" |
| 7 | +db_ = None |
| 8 | +creds_ = "" |
| 9 | + |
| 10 | +# Make this differently, for the love of god! |
| 11 | +def bake(host=host_, port=port_, user=user_, pasw=pasw_, db=db_, creds=creds_): |
| 12 | + global host_, port_, user_, pasw_, db_, creds_ |
| 13 | + if host != "": host_=host |
| 14 | + if port != "": port_=port |
| 15 | + if user != "": user_=user |
| 16 | + if pasw != "": pasw_=pasw |
| 17 | + if db != "": db_=db |
| 18 | + if creds != "": creds_=creds |
| 19 | + |
| 20 | +# A query. |
| 21 | +def query(query): |
| 22 | + conn = pymysql.connect(host=host_, port=port_, user=user_, passwd=pasw_, db=db_) |
| 23 | + conn_object = conn.cursor() |
| 24 | + conn_object.execute(query) |
| 25 | + str = conn_object.fetchall() |
| 26 | + conn.commit() |
| 27 | + conn.close() |
| 28 | + return str |
| 29 | + |
| 30 | +# Call a stored procedure. |
| 31 | +def call(func, *args): |
| 32 | + call = "CALL " + func + "(" |
| 33 | + if len(args) > 0: |
| 34 | + for i in range(len(args)): |
| 35 | + call += (str(args[i]) if type(args[i])==type(1) else parseNull(args[i])) + (", " if i<len(args)-1 else ");") |
| 36 | + else: call += ");" |
| 37 | + q = query(call) |
| 38 | + return q[0][0] if len(q)==1 else q |
| 39 | + |
| 40 | +# Quick query. |
| 41 | +def all(table): |
| 42 | + return query(f"SELECT * from {table};") |
| 43 | + |
| 44 | +def getToday(): |
| 45 | + dat = datetime.date.today() + datetime.timedelta(days=1) |
| 46 | + return dat.strftime("%Y-%m-%d") |
| 47 | + |
| 48 | +def getTomorrow(): |
| 49 | + dat = datetime.date.today() + datetime.timedelta(days=1) |
| 50 | + return dat.strftime("%Y-%m-%d") |
| 51 | + |
| 52 | +def parseNull(val): |
| 53 | + return "NULL" if val in ["", "NULL"] else "'"+val+"'" |
0 commit comments