MCPcopy Index your code
hub / github.com/clips/pattern / connect

Method connect

pattern/db/__init__.py:432–484  ·  view source on GitHub ↗
(self, unicode=True)

Source from the content-addressed store, hash-verified

430 self.relations = []
431
432 def connect(self, unicode=True):
433 # Connections for threaded applications work differently,
434 # see http://tools.cherrypy.org/wiki/Databases
435 # (have one Database object for each thread).
436 if self._connection is not None:
437 return
438 # MySQL
439 if self.type == MYSQL:
440 try:
441 self._connection = MySQLdb.connect(self.host, self.username, self.password, self.name, port=self.port, use_unicode=unicode)
442 self._connection.autocommit(False)
443 except Exception, e:
444 # Create the database if it doesn't exist yet.
445 if "unknown database" not in str(e).lower():
446 raise DatabaseConnectionError, e[1] # Wrong host, username and/or password.
447 connection = MySQLdb.connect(self.host, self.username, self.password)
448 cursor = connection.cursor()
449 cursor.execute("create database if not exists `%s`;" % self.name)
450 cursor.close()
451 connection.close()
452 self._connection = MySQLdb.connect(self.host, self.username, self.password, self.name, port=self.port, use_unicode=unicode)
453 self._connection.autocommit(False)
454 if unicode:
455 self._connection.set_character_set("utf8")
456 # SQLite
457 if self.type == SQLITE:
458 self._connection = sqlite.connect(self.name, detect_types=sqlite.PARSE_DECLTYPES)
459 # Create functions that are not natively supported by the engine.
460 # Aggregate functions (for grouping rows) + date functions.
461 self._connection.create_aggregate("first", 1, sqlite_first)
462 self._connection.create_aggregate("last", 1, sqlite_last)
463 self._connection.create_aggregate("group_concat", 1, sqlite_group_concat)
464 self._connection.create_function("year", 1, sqlite_year)
465 self._connection.create_function("month", 1, sqlite_month)
466 self._connection.create_function("day", 1, sqlite_day)
467 self._connection.create_function("hour", 1, sqlite_hour)
468 self._connection.create_function("minute", 1, sqlite_minute)
469 self._connection.create_function("second", 1, sqlite_second)
470 # Map field type INTEGER to int (not long(), e.g., 1L).
471 # Map field type BOOLEAN to bool.
472 # Map field type DATE to str, yyyy-mm-dd hh:mm:ss.
473 if self.type == MYSQL:
474 type = MySQLdb.constants.FIELD_TYPE
475 self._connection.converter[type.LONG] = int
476 self._connection.converter[type.LONGLONG] = int
477 self._connection.converter[type.DECIMAL] = float
478 self._connection.converter[type.NEWDECIMAL] = float
479 self._connection.converter[type.TINY] = bool
480 self._connection.converter[type.TIMESTAMP] = date
481 if self.type == SQLITE:
482 sqlite.converters["TINYINT(1)"] = bool # See Binary() why this is necessary:
483 sqlite.converters["BLOB"] = lambda data: str(data).decode("string-escape")
484 sqlite.converters["TIMESTAMP"] = date
485
486 def disconnect(self):
487 if self._connection is not None:

Callers 4

__init__Method · 0.95
dropMethod · 0.95
parse_xmlFunction · 0.80
test_databaseMethod · 0.80

Calls 4

strFunction · 0.85
executeMethod · 0.45
closeMethod · 0.45
decodeMethod · 0.45

Tested by 1

test_databaseMethod · 0.64