<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2013 <NAME> <<EMAIL>>
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.

from trac.core import *
from trac.config import Option
from trac.core import Component, implements
from trac.db.api import IDatabaseConnector
from trac.db.util import ConnectionWrapper
from trac.env import IEnvironmentSetupParticipant, ISystemInfoProvider
from trac.env import BackupError
import re

try:
    import pyodbc
except ImportError:
    pyodbc = None

# force enables this plugin in trac-admin initenv
#enabled = BoolOption("components", "mssql_backend.*", "enabled")

# Mapping from "abstract" SQL types to DB-specific types
_type_map = {
    'int64': 'bigint',
    'text': 'nvarchar(512)',
}

# TODO: You cannot use MS Access because column name 'value' can seems not use via odbc.
_column_map = {
    'key': '"key"',
#    'value': '"value"'
}

re_limit = re.compile(" LIMIT (\d+)( OFFSET (\d+))?", re.IGNORECASE)
re_order_by = re.compile("ORDER BY ", re.IGNORECASE)
re_where = re.compile("WHERE ", re.IGNORECASE)
re_equal = re.compile("(\w+)\s*=\s*(['\w]+|\?)", re.IGNORECASE)
re_isnull = re.compile("(\w+) IS NULL", re.IGNORECASE)
re_select = re.compile('SELECT( DISTINCT)?( TOP)?', re.IGNORECASE)
re_coalesce_equal = re.compile("(COALESCE\([^)]+\))=([^,]+)", re.IGNORECASE)

class MSSQLConnector(Component):
	implements(IDatabaseConnector, IEnvironmentSetupParticipant,
			   ISystemInfoProvider)

	required = False

	def __init__(self):
		self._mssql_version = None

	# ISystemInfoProvider methods

	def get_system_info(self):
		if self.required:
			yield 'pymssql', self._mssql_version

	# IDatabaseConnector methods

	def get_supported_schemes(self):
		yield ('mssql', 1)

def _to_sql(table):
    sql = ["CREATE TABLE %s (" % table.name]
    coldefs = []
    for column in table.columns:
        column.name = _column_map.get(column.name, column.name)
        ctype = column.type.lower()
        ctype = _type_map.get(ctype, ctype)
        #  for SQL Server, patch for "enum" table, value is not text, use int instead.
        if table.name == 'enum' and column.name == 'value':
            ctype = 'int'
        if (table.name, column.name) in [
                ('wiki', 'text'),
                ('report', 'query'),
                ('report', 'description'),
                ('milestone', 'description'),
                ('version', 'description'),
            ]:
            ctype = 'nvarchar(MAX)'
        if (table.name, column.name) in [
                ('ticket', 'description'),
                ('ticket_change', 'oldvalue'),
                ('ticket_change', 'newvalue'),
                ('ticket_custom', 'value'),
                ('session_attribute', 'value')<commit_msg>Added init_db to remove "AttributeError: 'MSSQLConnector' object has no attribute 'init_db'"<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2013 <NAME> <<EMAIL>>
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.

from trac.core import *
from trac.config import Option
from trac.core import Component, implements
from trac.db.api import IDatabaseConnector
from trac.db.util import ConnectionWrapper
from trac.env import IEnvironmentSetupParticipant, ISystemInfoProvider
from trac.env import BackupError
import re

try:
    import pyodbc
except ImportError:
    pyodbc = None

# force enables this plugin in trac-admin initenv
#enabled = BoolOption("components", "mssql_backend.*", "enabled")

# Mapping from "abstract" SQL types to DB-specific types
_type_map = {
    'int64': 'bigint',
    'text': 'nvarchar(512)',
}

# TODO: You cannot use MS Access because column name 'value' can seems not use via odbc.
_column_map = {
    'key': '"key"',
#    'value': '"value"'
}

re_limit = re.compile(" LIMIT (\d+)( OFFSET (\d+))?", re.IGNORECASE)
re_order_by = re.compile("ORDER BY ", re.IGNORECASE)
re_where = re.compile("WHERE ", re.IGNORECASE)
re_equal = re.compile("(\w+)\s*=\s*(['\w]+|\?)", re.IGNORECASE)
re_isnull = re.compile("(\w+) IS NULL", re.IGNORECASE)
re_select = re.compile('SELECT( DISTINCT)?( TOP)?', re.IGNORECASE)
re_coalesce_equal = re.compile("(COALESCE\([^)]+\))=([^,]+)", re.IGNORECASE)

class MSSQLConnector(Component):
	implements(IDatabaseConnector, IEnvironmentSetupParticipant,
			   ISystemInfoProvider)

	required = False

	def __init__(self):
		self._mssql_version = None

	# ISystemInfoProvider methods

	def get_system_info(self):
		if self.required:
			yield 'pymssql', self._mssql_version

	# IDatabaseConnector methods

	def get_supported_schemes(self):
		yield ('mssql', 1)

	def init_db(self, path, schema=None, log=None, user=None, password=<PASSWORD>,
				host=None, port=None, params={}):
		cnx = self.get_connection(path, log, user, password, host, port,
								  params)
		cursor = cnx.cursor()
		if schema is None:
			from trac.db_default import schema
		for table in schema:
			for stmt in self.to_sql(table):
				cursor.execute(stmt)
		cnx.commit()


def _to_sql(table):
    sql = ["CREATE TABLE %s (" % table.name]
    coldefs = []
    for column in table.columns:
        column.name = _column_map.get(column.name, column.name)
        ctype = column.type.lower()
        ctype = _type_map.get(ctype, ctype)
        #  for SQL Server, patch for "enum" table, value is not text, use int instead.
        if table.name == 'enum' and column.name == 'value':
            ctype = 'int'
        if (table.name, column.name) in [
                ('wiki', 'text'),
                ('report', 'query'),
                ('report', 'description'),
                ('milestone', 'description'),
                ('version', 'description'),
            ]:
            ctype = 'nvarchar(MAX)'
        if (table.name, column.name) in [
                ('ticket', 'description'),
                ('ticket_change', 'oldvalue'),
                ('ticket_change', 'newvalue'),
                ('ticket_custom', 'value'),
                ('session_attribute', 'value')
{'fraction_non_alphanumeric': 0.14227839896289093, 'fraction_numerical': 0.0035650623885918, 'mean_word_length': 3.2216142270861834, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '946393', 'n_tokens_mistral': 2083, 'n_tokens_neox': 1938, 'n_words': 537}