def random_folders(conn, rows): cur = conn.cursor() cur.execute("DELETE FROM folders;") SQL = "INSERT INTO folders (folders_id, email_id, folder_name, folder_type) VALUES (%s, %s, %s, %s);" cur.execute("SELECT max(email_id) FROM email") eml_id_max = cur.fetchone() cur.execute("SELECT email_id FROM email") eml_ids = cur.fetchall() if (cur.rowcount == 0) or (eml_id_max is None): raise mvc_exc.ParentTabError( f"You can't create random data for folders table if parent table doesn't exist" ) cur.execute( f"SELECT trunc(random()*({eml_id_max[0]}-1) + 1) FROM generate_series(1,1000)" ) rand_id = cur.fetchall() i = 0 for item in rand_id: f = False for keys in eml_ids: if item == keys: f = True if f is False: rand_id[i] = eml_id_max[0] i = i + 1 cur.execute( "SELECT chr(trunc(65+random()*25)::int) || " "trunc(random()*(1000) + 1)::int || chr(trunc(65+random()*25)::int) FROM generate_series(1,1000) " ) rand_name1 = cur.fetchall() cur.execute( "SELECT chr(trunc(65+random()*25)::int) || chr(trunc(65+random()*25)::int) " "FROM generate_series(1,1000)") rand_name2 = cur.fetchall() i = 1 while i < rows: data = (i, rand_id[i], rand_name1[i], rand_name2[i]) cur.execute(SQL, data) i = i + 1 cur.execute("SELECT * FROM folders;") rows = cur.fetchall() print("========= FOLDERS TABLE =========") print("ID|E_ID|F_Name|F_Type") for row in rows: print(row) conn.commit() cur.close()
def random_email(conn, rows): cur = conn.cursor() cur.execute("DELETE FROM email;") SQL = "INSERT INTO email (email_id, user_id, email_adress, email_type) VALUES (%s, %s, %s, %s);" cur.execute("SELECT max(user_id) FROM users") usr_id_max = cur.fetchone() if (cur.rowcount == 0) or (usr_id_max is None): raise mvc_exc.ParentTabError( f"You can't create random data for email table if parent table doesn't exist" ) cur.execute(f"SELECT user_id FROM users") user_ids = cur.fetchall() cur.execute( f"SELECT trunc(random()*({usr_id_max[0]}-1) + 1) FROM generate_series(1,1000)" ) rand_id = cur.fetchall() i = 0 for item in rand_id: f = False for keys in user_ids: if item == keys: f = True if f is False: rand_id[i] = usr_id_max[0] i = i + 1 cur.execute( "SELECT trunc(random()*(1000))::int ||" "chr(trunc(65+random()*25)::int) || chr(trunc(65+random()*25)::int)FROM generate_series(1,1000) " ) rand_num = cur.fetchall() cur.execute( "SELECT chr(trunc(65+random()*25)::int) || chr(trunc(65+random()*25)::int) " "FROM generate_series(1,1000)") rand_name = cur.fetchall() i = 1 while i < rows: data = (i, rand_id[i], rand_num[i], rand_name[i]) cur.execute(SQL, data) i = i + 1 cur.execute("SELECT * FROM email;") rows = cur.fetchall() print("========= EMAIL TABLE =========") print("ID|U_ID|Adress|E_Type ") for row in rows: print(row) conn.commit() cur.close()
def random_fm(conn, rows): cur = conn.cursor() cur.execute("DELETE FROM folders_messages;") SQL = "INSERT INTO folders_messages (folders_id, messages_id) VALUES (%s, %s);" cur.execute("SELECT max(messages_id) FROM messages") mes_id_max = cur.fetchone() cur.execute("SELECT messages_id FROM messages") mes_ids = cur.fetchall() cur.execute("SELECT max(folders_id) FROM folders") fold_id_max = cur.fetchone() cur.execute("SELECT folders_id FROM folders") fold_ids = cur.fetchall() if ((cur.rowcount == 0) or (mes_id_max is None)) or ((cur.rowcount == 0) or (fold_id_max is None)): raise mvc_exc.ParentTabError(f"You can't create random data for folders_messages table if parent table " f"doesn't exist") cur.execute(f"SELECT trunc(random()*({fold_id_max[0]}-1) + 1) FROM generate_series(1,100010)") rand_f_id = cur.fetchall() i = 0 for item in rand_f_id: f = False for keys in fold_ids: if item == keys: f = True if f is False: rand_f_id[i] = fold_id_max[0] i = i + 1 cur.execute(f"SELECT trunc(random()*({mes_id_max[0]}-1) + 1) FROM generate_series(1,100010)") rand_m_id = cur.fetchall() i = 0 for item in rand_m_id: f = False for keys in mes_ids: if item == keys: f = True if f is False: rand_m_id[i] = mes_id_max[0] i = i + 1 i = 1 while i < rows: data = (rand_f_id[i], rand_m_id[i]) cur.execute(SQL, data) i = i + 1 cur.execute("SELECT * FROM folders_messages;") rows = cur.fetchall() print("========= FOLDERS_MESSAGES TABLE =========") print("FoldersID|Messages_ID") for row in rows: print(row) conn.commit() cur.close()