<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-

# AsynCode Framework - XML framework allowing developing internet
# applications without using programming languages.
# Copyright (C) 2008-2010  <NAME>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the version 3 of GNU General Public License as published by
# the Free Software Foundation.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import psycopg2 as dbapi
from ACF import globals
from ACF.errors import Error
from ACF.utils.xmlextras import escapeQuotes
import logging

log=logging.getLogger('ACF.dbHandler.pg')
D=logging.doLog

class handler(object):
	def __init__(self,conf):
		if D: log.debug("Initialized with conf=%s",conf)
		host=""
		if conf.get("host"):
			host=conf["host"]+":"+(conf.get("port","") or "5432")
		if D: log.debug("Host is %s (None means socket)",host)
		self.conn=dbapi.connect("dbname='"+conf["defaultdb"]+"' host='"+host+"' user='"+conf["user"]+"' password='"+conf["password"]+"'")
		self.conn.set_isolation_level(0)
		if D: log.info("Successfuly connected to PostgreSQL.")
		q="SET search_path TO "+globals.dbschema+", public"
		if conf.has_key("schema"):
			q+=", "+conf["schema"]
		#TODO lazy query - should be executed with first query
		if D: log.warning("Internal database query. Change debug level to 'debug' to see details.")
		try:
			self.query(q)
		except:
			pass

	def query(self,sql,dictionary=True):
		if D: log.debug("Executing with sql='%s' and dictionary=%s",sql,dictionary)
		#if globals.config.has_key("debug") and globals.config["debug"]:
		#	import time
		#	t=time.time()
		cursor=self.conn.cursor()
		try:
			cursor.execute(sql)
			#self.conn.commit()
		except Exception ,e:
			if D: log.error("SQLError %s",str(e))
			raise Error("SQLError",escapeQuotes(str(e))+": "+sql)
		if D: log.info("Query returned rows (there was SELECT)")
		#this is most most memory efficient structure but needs to be replaced with yield by merging it with pygresql
		try:
			d={
				"rows":cursor.fetchall(),<commit_msg>Port support is off for a while<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-

# AsynCode Framework - XML framework allowing developing internet
# applications without using programming languages.
# Copyright (C) 2008-2010  <NAME>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the version 3 of GNU General Public License as published by
# the Free Software Foundation.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import psycopg2 as dbapi
from ACF import globals
from ACF.errors import Error
from ACF.utils.xmlextras import escapeQuotes
import logging

log=logging.getLogger('ACF.dbHandler.pg')
D=logging.doLog

class handler(object):
	def __init__(self,conf):
		if D: log.debug("Initialized with conf=%s",conf)
		host=""
		if conf.get("host"):
			host=conf["host"]#+":"+(conf.get("port","") or "5432")
		if D: log.debug("Host is %s (None means socket)",host)
		self.conn=dbapi.connect("dbname='"+conf["defaultdb"]+"' host='"+host+"' user='"+conf["user"]+"' password='"+conf["password"]+"'")
		self.conn.set_isolation_level(0)
		if D: log.info("Successfuly connected to PostgreSQL.")
		q="SET search_path TO "+globals.dbschema+", public"
		if conf.has_key("schema"):
			q+=", "+conf["schema"]
		#TODO lazy query - should be executed with first query
		if D: log.warning("Internal database query. Change debug level to 'debug' to see details.")
		try:
			self.query(q)
		except:
			pass

	def query(self,sql,dictionary=True):
		if D: log.debug("Executing with sql='%s' and dictionary=%s",sql,dictionary)
		#if globals.config.has_key("debug") and globals.config["debug"]:
		#	import time
		#	t=time.time()
		cursor=self.conn.cursor()
		try:
			cursor.execute(sql)
			#self.conn.commit()
		except Exception ,e:
			if D: log.error("SQLError %s",str(e))
			raise Error("SQLError",escapeQuotes(str(e))+": "+sql)
		if D: log.info("Query returned rows (there was SELECT)")
		#this is most most memory efficient structure but needs to be replaced with yield by merging it with pygresql
		try:
			d={
				"rows":cursor.fetchall(),
{'fraction_non_alphanumeric': 0.13426021996264786, 'fraction_numerical': 0.006640381821954763, 'mean_word_length': 4.6242707117852975, 'pattern_counts': {'":': 6, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7170607', 'n_tokens_mistral': 1626, 'n_tokens_neox': 1501, 'n_words': 519}