sapling/edenscm/hgext/extlib/mysqlutil.py
Jun Wu c12e300bb8 codemod: move Python packages to edenscm
Summary:
Move top-level Python packages `mercurial`, `hgext` and `hgdemandimport` to
a new top-level package `edenscm`. This allows the Python packages provided by
the upstream Mercurial to be installed side-by-side.

To maintain compatibility, `edenscm/` gets added to `sys.path` in
`mercurial/__init__.py`.

Reviewed By: phillco, ikostia

Differential Revision: D13853115

fbshipit-source-id: b296b0673dc54c61ef6a591ebc687057ff53b22e
2019-01-28 18:35:41 -08:00

53 lines
1.4 KiB
Python

# mysqlutil.py - useful utility methods for accessing mysql from server-side hg
#
# Copyright 2017 Facebook, Inc.
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
class InvalidConnectionString(Exception):
pass
def parseconnectionstring(connstr):
"""
Parses connection string in format 'IP:PORT:DB_NAME:USER:PASSWORD' and return
parameters for mysql.connection module
"""
try:
host, port, db, user, password = connstr.rsplit(":", 4)
return {
"host": host,
"port": port,
"database": db,
"user": user,
"password": password,
}
except ValueError:
raise InvalidConnectionString()
def insert(sqlconn, tablename, argsdict):
"""
Inserts new row into a table, given a name of a table and a mapping
column name -> column value
"""
sqlcursor = sqlconn.cursor()
items = list(argsdict.items())
columns = ", ".join(
("{column_name}".format(column_name=column_name) for column_name, _ in items)
)
placeholders = ", ".join(("%s" for _ in items))
insertstmt = "INSERT INTO {table} ({columns}) VALUES ({placeholders})".format(
table=tablename, columns=columns, placeholders=placeholders
)
sqlcursor.execute(insertstmt, params=[value for _, value in items])
sqlconn.commit()