1 # -*- coding: latin-1; -*-
3 # PgWorksheet - PostgreSQL Front End
4 # http://pgworksheet.projects.postgresql.org/
6 # Copyright © 2004-2008 Henri Michelon & CML http://www.e-cml.org/
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License
10 # as published by the Free Software Foundation; either version 2
11 # of the License, or (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details (read LICENSE.txt).
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 # $Id: Execute.py,v 1.28 2008/03/12 20:26:23 hmichelon Exp $
35 """SQL query and psql commands handling"""
38 def __init__(self
, db
):
40 self
.lexical
= pgw
.Lexical
.Lexical()
43 def execute_one(self
, sql
):
44 """Execute one query"""
48 sql
= string
.replace(sql
, '\\\\', '\\')
51 sql
= string
.strip(sql
)
54 if (self
.db
.user
is not None):
55 cmd
+= ' -U "' + self
.db
.user
+ '"'
56 if (self
.db
.host
is not None):
57 cmd
+= ' -h "' + self
.db
.host
+ '"'
58 if (self
.db
.port
is not None):
59 cmd
+= ' -p ' + self
.db
.port
60 if (self
.db
.database
is not None):
61 cmd
+= ' -d "' + self
.db
.database
+ '"'
62 cmd
+= ' -c "' + sql
+ '"'
63 if (self
.db
.password
is not None):
64 os
.putenv("PGPASSWORD", self
.db
.password
)
65 #os.putenv("PGCLIENTENCODING", pgw.get_user_encoding())
66 output
, input = popen2
.popen4(cmd
)
67 result
= output
.read()
70 #os.putenv("PGCLIENTENCODING", "")
71 os
.putenv("PGPASSWORD", "")
72 return { 'text' : result
}
75 return self
.db
.query(sql
)
78 def execute(self
, buffer):
79 """Execute SQL queries or psql commands"""
80 if not self
.db
.is_connected() : return
82 sqls
= self
.split(buffer)
87 sql
= string
.strip(sql
)
89 results
.append(self
.execute_one(sql
))
90 if (len(results
) == 1):
95 return self
.execute_one(sqls
[0])
98 def split(self
, oldbuffer
):
99 """Split multiple queries"""
100 # a new textbuffer, because we don't want to modify the original
101 buffer = gtk
.TextBuffer()
102 buffer.set_text(oldbuffer
.get_text(oldbuffer
.get_start_iter(),
103 oldbuffer
.get_end_iter()))
105 tokens
= self
.lexical
.analyse(buffer, buffer.get_start_iter(),
106 buffer.get_end_iter())
108 if (token
.token
== 'comment'):
109 buffer.delete(token
.start_iter
, token
.end_iter
)
111 # restart without the comments
112 buffer.set_text(buffer.get_text(buffer.get_start_iter(),
113 buffer.get_end_iter()))
114 start
= buffer.get_start_iter()
115 end
= buffer.get_end_iter()
116 tokens
= self
.lexical
.analyse(buffer, start
, end
)
118 # create a list of queries
119 sql
= buffer.get_text(start
, end
)
122 while (len(tokens
) > 0):
123 token
= tokens
.pop(0)
124 if (token
.token
== 'psql'):
127 parts
.append(buffer.get_text(stop
, i
))
128 parts
.append(token
.value
)
129 stop
= token
.end_iter
130 elif (token
.value
== ';'):
132 parts
.append(buffer.get_text(stop
, i
))
133 stop
= token
.end_iter
135 parts
.append(buffer.get_text(stop
, end
))