+ All Categories
Home > Documents > XML-RPC vs Psycopg2

XML-RPC vs Psycopg2

Date post: 02-Jul-2015
Category:
Upload: odoo-italia
View: 636 times
Download: 1 times
Share this document with a friend
22
Python script XML-RPC vs Psycopg2 24 ottobre 2013.openerp.it XML-RPC vs Psycopg2 Dr. Piero Cecchi OpenERP Analisi Prestazionale
Transcript
Page 1: XML-RPC vs Psycopg2

Python script

XML-RPC vs Psycopg2

24 ottobre 2013.openerp.it XML-RPC vs Psycopg2 Dr. Piero Cecchi

OpenERP

Analisi Prestazionale

Page 2: XML-RPC vs Psycopg2

2

24 ottobre 2013.openerpday.it XML-RPC vs Psycopg2 Dr. Piero Cecchi

PROBLEMA REALE

In un database nella Tabella:

● account_move

il Campo:

● id_partner

Non è sempre presente

Page 3: XML-RPC vs Psycopg2

3

24 ottobre 2013.openerpday.it XML-RPC vs Psycopg2 Dr. Piero Cecchi

TUTTI I MOVIMENTI SENZA PARTNER SONO ABBINATI AL PRIMO ID_PARTNER DELLA TABELLA account_move_line

Page 4: XML-RPC vs Psycopg2

4

24 ottobre 2013.openerpday.it XML-RPC vs Psycopg2 Dr. Piero Cecchi

TABELLA account_move_line

Page 5: XML-RPC vs Psycopg2

5

24 ottobre 2013.openerpday.it XML-RPC vs Psycopg2 Dr. Piero Cecchi

SCRITTO PROGRAMMA IN PYTHON E XML-RPC….......# SEARCH ID ACCOUNT MOVE LINEargs = [('state', '=', 'valid')]ids = sock.execute(dbname, uid, pwd, 'account.move.line', 'search', args)splitids=str(ids).split(',')print splitidsrowcount=1for row in splitids:

if rowcount == len(ids):idmove = int(row[1:-1])

else:idmove=int(row[1:])

print idmovefields = ['partner_id','move_id'] #fields to readdata = sock.execute(dbname, uid, pwd, 'account.move.line', 'read',idmove, fields) splitdata = str(data).split(',')if splitdata[0][15:] <> 'False':

idpartner = int(splitdata[0][16:])print data

…................................esempio read xml-rpc{'partner_id': [231, 'Sguasxxxx Licia'], 'id': 19, 'move_id': [10, 'BNK2/2013/0004']}

Page 6: XML-RPC vs Psycopg2

6

24 ottobre 2013.openerpday.it XML-RPC vs Psycopg2 Dr. Piero Cecchi

TABELLA account_move CORRETTAMENTE ABBINATA

Page 7: XML-RPC vs Psycopg2

7

24 ottobre 2013.openerpday.it XML-RPC vs Psycopg2 Dr. Piero Cecchi

XML-RPC TIME: 3.27

Page 8: XML-RPC vs Psycopg2

8

24 ottobre 2013.openerpday.it XML-RPC vs Psycopg2 Dr. Piero Cecchi

SCRITTO PROGRAMMA IN PYTHON E PSYCOPG2…......c_line.execute("SELECT move_id, partner_id, credit, debit FROM account_move_line WHERE (partner_id > 0) ORDER by partner_id")c_line.execute("SELECT DISTINCT move_id, partner_id FROM account_move_line ORDER by partner_id")row_count = 0for row in c_line:

row_count += 1update_line="UPDATE account_move SET partner_id=%s where id=%s"

try:c_move.execute(update_line,(row[1],row[0],))print update_lineprint "Cursor_row:%s - Partner_id:%s - Move_id:%s" %(row_count, row[1],

row[0],)except psycopg2.DatabaseError, e:

print e.pgcodeprint e.pgerrorsys.exit()

c_move.execute("COMMIT")

Page 9: XML-RPC vs Psycopg2

9

24 ottobre 2013.openerpday.it XML-RPC vs Psycopg2 Dr. Piero Cecchi

>>> import psycopg2# Connect to an existing database>>> conn = psycopg2.connect("dbname=test user=postgres")# Open a cursor to perform database operations>>> cur = conn.cursor()# Execute a command: this creates a new table>>> cur.execute("CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);")# Pass data to fill a query placeholders and let Psycopg perform# the correct conversion (no more SQL injections!)>>> cur.execute("INSERT INTO test (num, data) VALUES (%s, %s)",... (100, "abc'def"))# Query the database and obtain data as Python objects>>> cur.execute("SELECT * FROM test;")>>> cur.fetchone()(1, 100, "abc'def")# Make the changes to the database persistent>>> conn.commit()# Close communication with the database>>> cur.close()>>> conn.close()

BASIC MODULE USAGE - PSYCOPG2

Page 10: XML-RPC vs Psycopg2

10

24 ottobre 2013.openerpday.it XML-RPC vs Psycopg2 Dr. Piero Cecchi

#!/usr/bin/python# Replace <USERNAME_DATABASE>, <USERNAME>, and <PASSWORD> below with your actual DB, user, and password.import psycopg2import syscon = Nonetry: con = psycopg2.connect(database='<USERNAME_DATABASE>', user='<USERNAME>', password='<PASSWORD>') cur = con.cursor() cur.execute("SELECT * FROM testschema.testtable")1 rows = cur.fetchall() for row in rows: print rowexcept psycopg2.DatabaseError, e: print 'Error %s' % e sys.exit(1)finally: if con: con.close()

DATABASE ERROR TEST- PSYCOPG2

Page 11: XML-RPC vs Psycopg2

11

24 ottobre 2013.openerpday.it XML-RPC vs Psycopg2 Dr. Piero Cecchi

SELECT PSYCOPG2 - TIME: 0.25

Page 12: XML-RPC vs Psycopg2

12

24 ottobre 2013.openerpday.it XML-RPC vs Psycopg2 Dr. Piero Cecchi

SELECT DISTINCT PSYCOPG2 - TIME: 0.06

Page 13: XML-RPC vs Psycopg2

13

24 ottobre 2013.openerpday.it XML-RPC vs Psycopg2 Dr. Piero Cecchi

SQL EXISTS - UPDATE EXAMPLE

UPDATE suppliersSET supplier_name = (select customers.name from customers where customers.customer_id = suppliers.supplier_id)WHERE EXISTS (select customers.name from customers where customers.customer_id = suppliers.supplier_id);

Page 14: XML-RPC vs Psycopg2

14

24 ottobre 2013.openerpday.it XML-RPC vs Psycopg2 Dr. Piero Cecchi

SCRITTO PROGRAMMA IN PYTHON E SQL EXISTS…......

try:conn = psycopg2.connect(conn_string)# print the connection string we will use to connectprint "Connecting to database\n->%s" % (conn_string)cur = conn.cursor()update_set = "UPDATE account_move SET partner_id = "update_select = "(SELECT DISTINCT partner_id FROM account_move_line

WHERE account_move.id = account_move_line.move_id)"cur.execute(update_set + update_select + " WHERE EXISTS " +update_select)cur.execute("COMMIT")cur.close()conn.close()

except psycopg2.DatabaseError, e:print e.pgcodeprint e.pgerrorsys.exit()

Page 15: XML-RPC vs Psycopg2

15

24 ottobre 2013.openerpday.it XML-RPC vs Psycopg2 Dr. Piero Cecchi

SQL EXISTS PSYCOPG2 – TIME 0.02

Page 16: XML-RPC vs Psycopg2

16

24 ottobre 2013.openerpday.it XML-RPC vs Psycopg2 Dr. Piero Cecchi

SCRIPT PSQL E SQL EXISTS

T="$(date +%s)"psql -U demo -d acsi7demo -c "UPDATE account_move SET partner_id = (SELECT DISTINCT partner_id FROM account_move_line WHERE account_move.id = account_move_line.move_id) WHERE EXISTS (SELECT DISTINCT partner_id FROM account_move_line WHERE account_move.id = account_move_line.move_id)"T="$(($(date +%s)-T))"echo $T

Page 17: XML-RPC vs Psycopg2

17

24 ottobre 2013.openerpday.it XML-RPC vs Psycopg2 Dr. Piero Cecchi

SCRIPT PSQL E SQL EXISTS TIME: 0

Page 18: XML-RPC vs Psycopg2

18

24 ottobre 2013.openerpday.it XML-RPC vs Psycopg2 Dr. Piero Cecchi

PSQL E SQL EXISTS TIME: 0,034

Page 19: XML-RPC vs Psycopg2

19

24 ottobre 2013.openerpday.it XML-RPC vs Psycopg2 Dr. Piero Cecchi

ANALISI PRESTAZIONALE

Page 20: XML-RPC vs Psycopg2

20

24 ottobre 2013.openerpday.it XML-RPC vs Psycopg2 Dr. Piero Cecchi

XML-RPC VS PSYCOPG2

XML-RPC VANTAGGI:

● SICUREZZA

● CONTROLLO ORM OPENERP

● UTILIZZO “Etichette” CAMPI

PSYCOPG2 VANTAGGI:

● VELOCITA'

● UTILIZZO SQL STANDARD

● PORTABILITA' QUERY

Page 21: XML-RPC vs Psycopg2

21

24 ottobre 2013.openerpday.it XML-RPC vs Psycopg2 Dr. Piero Cecchi

RINGRAZIAMENTI

Associazione OpenERP Italiahttp://www.openerp-italia.org

2013.openerpday.it

Italian PostgreSQL Users Grouphttp://www.itpug.org

2013.pgday.it

Page 22: XML-RPC vs Psycopg2

22

24 ottobre 2013.openerpday.it XML-RPC vs Psycopg2 Dr. Piero Cecchi

Q&A

CONTATTI

[email protected]

[email protected]

https://github.com/cecchip/openerpday2013.git


Recommended