本文整理汇总了Python中skytools.quoting.quote_ident函数的典型用法代码示例。如果您正苦于以下问题:Python quote_ident函数的具体用法?Python quote_ident怎么用?Python quote_ident使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了quote_ident函数的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: mk_update_sql
def mk_update_sql(row, tbl, pkey_list, field_map = None):
"""Generate UPDATE statement from dict data."""
if len(pkey_list) < 1:
raise Exception("update needs pkeys")
set_list = []
whe_list = []
pkmap = {}
for k in pkey_list:
pkmap[k] = 1
new_k = field_map and field_map[k] or k
col = quote_ident(new_k)
val = quote_literal(row[k])
whe_list.append("%s = %s" % (col, val))
if field_map:
for src, dst in field_map.iteritems():
if src not in pkmap:
col = quote_ident(dst)
val = quote_literal(row[src])
set_list.append("%s = %s" % (col, val))
else:
for col, val in row.iteritems():
if col not in pkmap:
col = quote_ident(col)
val = quote_literal(val)
set_list.append("%s = %s" % (col, val))
return "update %s set %s where %s;" % (quote_fqident(tbl),
", ".join(set_list), " and ".join(whe_list))
示例2: mk_update_sql
def mk_update_sql(row, tbl, pkey_list, field_map = None):
r"""Generate UPDATE statement from dict data.
>>> mk_update_sql({'id': 0, 'id2': '2', 'data': 'str\\'}, 'Table', ['id', 'id2'])
'update only public."Table" set data = E\'str\\\\\' where id = \'0\' and id2 = \'2\';'
"""
if len(pkey_list) < 1:
raise Exception("update needs pkeys")
set_list = []
whe_list = []
pkmap = {}
for k in pkey_list:
pkmap[k] = 1
new_k = field_map and field_map[k] or k
col = quote_ident(new_k)
val = quote_literal(row[k])
whe_list.append("%s = %s" % (col, val))
if field_map:
for src, dst in field_map.iteritems():
if src not in pkmap:
col = quote_ident(dst)
val = quote_literal(row[src])
set_list.append("%s = %s" % (col, val))
else:
for col, val in row.iteritems():
if col not in pkmap:
col = quote_ident(col)
val = quote_literal(val)
set_list.append("%s = %s" % (col, val))
return "update only %s set %s where %s;" % (quote_fqident(tbl),
", ".join(set_list), " and ".join(whe_list))
示例3: get_create_sql
def get_create_sql(self, curs, new_table_name=None):
fmt = "ALTER TABLE ONLY %s ADD CONSTRAINT %s %s;"
if new_table_name:
name = self.name
if self.contype in ('p', 'u'):
name = find_new_name(curs, self.name)
qtbl = quote_fqident(new_table_name)
qname = quote_ident(name)
else:
qtbl = quote_fqident(self.table_name)
qname = quote_ident(self.name)
sql = fmt % (qtbl, qname, self.defn)
return sql
示例4: get_create_sql
def get_create_sql(self, curs, new_table_name=None):
"""Generate creation SQL."""
# no ONLY here as table with childs (only case that matters)
# cannot have contraints that childs do not have
fmt = "ALTER TABLE %s ADD CONSTRAINT %s %s;"
if new_table_name:
name = self.name
if self.contype in ('p', 'u'):
name = find_new_name(curs, self.name)
qtbl = quote_fqident(new_table_name)
qname = quote_ident(name)
else:
qtbl = quote_fqident(self.table_name)
qname = quote_ident(self.name)
sql = fmt % (qtbl, qname, self.defn)
return sql
示例5: magic_insert
def magic_insert(curs, tablename, data, fields = None, use_insert = 0, quoted_table = False):
r"""Copy/insert a list of dict/list data to database.
If curs == None, then the copy or insert statements are returned
as string. For list of dict the field list is optional, as its
possible to guess them from dict keys.
Example:
>>> magic_insert(None, 'tbl', [[1, '1'], [2, '2']], ['col1', 'col2'])
'COPY public.tbl (col1,col2) FROM STDIN;\n1\t1\n2\t2\n\\.\n'
"""
if len(data) == 0:
return
# decide how to process
if hasattr(data[0], 'keys'):
if fields == None:
fields = data[0].keys()
if use_insert:
row_func = _gen_dict_insert
else:
row_func = _gen_dict_copy
else:
if fields == None:
raise Exception("Non-dict data needs field list")
if use_insert:
row_func = _gen_list_insert
else:
row_func = _gen_list_copy
qfields = [quote_ident(f) for f in fields]
if quoted_table:
qtablename = tablename
else:
qtablename = quote_fqident(tablename)
# init processing
buf = StringIO()
if curs == None and use_insert == 0:
fmt = "COPY %s (%s) FROM STDIN;\n"
buf.write(fmt % (qtablename, ",".join(qfields)))
# process data
for row in data:
buf.write(row_func(qtablename, row, fields, qfields))
buf.write("\n")
# if user needs only string, return it
if curs == None:
if use_insert == 0:
buf.write("\\.\n")
return buf.getvalue()
# do the actual copy/inserts
if use_insert:
curs.execute(buf.getvalue())
else:
buf.seek(0)
hdr = "%s (%s)" % (qtablename, ",".join(qfields))
curs.copy_from(buf, hdr)
示例6: full_copy
def full_copy(tablename, src_curs, dst_curs, column_list = [], condition = None):
"""COPY table from one db to another."""
qtable = quote_fqident(tablename)
if column_list:
qfields = ",".join([quote_ident(f) for f in column_list])
src = dst = "%s (%s)" % (qtable, qfields)
else:
qfields = '*'
src = dst = qtable
if condition:
src = "(SELECT %s FROM %s WHERE %s)" % (qfields, qtable, condition)
if hasattr(src_curs, 'copy_expert'):
sql_to = "COPY %s TO stdout" % src
sql_from = "COPY %s FROM stdin" % dst
buf = CopyPipe(dst_curs, sql_from = sql_from)
src_curs.copy_expert(sql_to, buf)
else:
if condition:
# regular psycopg copy_to generates invalid sql for subselect copy
raise Exception('copy_expert() is needed for conditional copy')
buf = CopyPipe(dst_curs, dst)
src_curs.copy_to(buf, src)
buf.flush()
return (buf.total_bytes, buf.total_rows)
示例7: __init__
def __init__(self, curs, table_name):
"""Initializes class by loading info about table_name from database."""
BaseStruct.__init__(self, curs, table_name)
self.table_name = table_name
# fill args
schema, name = fq_name_parts(table_name)
args = {
'schema': schema,
'table': name,
'fqname': self.fqname,
'fq2name': quote_literal(self.fqname),
'oid': get_table_oid(curs, table_name),
'pg_class_oid': get_table_oid(curs, 'pg_catalog.pg_class'),
}
# load table struct
self.col_list = self._load_elem(curs, self.name, args, TColumn)
self.object_list = [ TTable(table_name, self.col_list) ]
self.seq_list = []
# load seqs
for col in self.col_list:
if col.seqname:
owner = self.fqname + '.' + quote_ident(col.name)
seq_args = { 'fqname': col.seqname, 'owner': quote_literal(owner) }
self.seq_list += self._load_elem(curs, col.seqname, seq_args, TSeq)
self.object_list += self.seq_list
# load additional objects
to_load = [TConstraint, TIndex, TTrigger, TRule, TGrant, TOwner, TParent]
for eclass in to_load:
self.object_list += self._load_elem(curs, self.name, args, eclass)
示例8: get_create_sql
def get_create_sql(self, curs, new_table_name = None):
"""Generate creation SQL."""
if new_table_name:
# fixme: seems broken
iname = find_new_name(curs, self.name)
tname = new_table_name
pnew = "INDEX %s ON %s " % (quote_ident(iname), quote_fqident(tname))
rx = r"\bINDEX[ ][a-z0-9._]+[ ]ON[ ][a-z0-9._]+[ ]"
sql = rx_replace(rx, self.defn, pnew)
else:
sql = self.defn
iname = self.local_name
tname = self.table_name
if self.is_clustered:
sql += ' ALTER TABLE ONLY %s\n CLUSTER ON %s;' % (
quote_fqident(tname), quote_ident(iname))
return sql
示例9: mk_insert_sql
def mk_insert_sql(row, tbl, pkey_list = None, field_map = None):
"""Generate INSERT statement from dict data."""
col_list = []
val_list = []
if field_map:
for src, dst in field_map.iteritems():
col_list.append(quote_ident(dst))
val_list.append(quote_literal(row[src]))
else:
for c, v in row.iteritems():
col_list.append(quote_ident(c))
val_list.append(quote_literal(v))
col_str = ", ".join(col_list)
val_str = ", ".join(val_list)
return "insert into %s (%s) values (%s);" % (
quote_fqident(tbl), col_str, val_str)
示例10: mk_delete_sql
def mk_delete_sql(row, tbl, pkey_list, field_map = None):
"""Generate DELETE statement from dict data."""
if len(pkey_list) < 1:
raise Exception("delete needs pkeys")
whe_list = []
for k in pkey_list:
new_k = field_map and field_map[k] or k
col = quote_ident(new_k)
val = quote_literal(row[k])
whe_list.append("%s = %s" % (col, val))
whe_str = " and ".join(whe_list)
return "delete from %s where %s;" % (quote_fqident(tbl), whe_str)
示例11: full_copy
def full_copy(tablename, src_curs, dst_curs, column_list=[]):
"""COPY table from one db to another."""
qtable = quote_fqident(tablename)
if column_list:
qfields = [quote_ident(f) for f in column_list]
hdr = "%s (%s)" % (qtable, ",".join(qfields))
else:
hdr = qtable
if hasattr(src_curs, "copy_expert"):
sql_to = "COPY %s TO stdout" % hdr
sql_from = "COPY %s FROM stdout" % hdr
buf = CopyPipe(dst_curs, sql_from=sql_from)
src_curs.copy_expert(sql_to, buf)
else:
buf = CopyPipe(dst_curs, hdr)
src_curs.copy_to(buf, hdr)
buf.flush()
return (buf.total_bytes, buf.total_rows)
示例12: get_drop_sql
def get_drop_sql(self, curs):
sql_list = []
for user, acl, who in self.acl_list:
sql = "REVOKE ALL FROM %s ON %s;" % (quote_ident(user), quote_fqident(self.name))
sql_list.append(sql)
return "\n".join(sql_list)
示例13: get_drop_sql
def get_drop_sql(self, curs):
return "ALTER TABLE %s ALTER COLUMN %s\n DROP DEFAULT;" % (
quote_fqident(self.table_name), quote_ident(self.name))