MCPcopy Create free account
hub / github.com/ElementsProject/lightning / colname_htable

Function colname_htable

devtools/sql-rewrite.py:102–126  ·  view source on GitHub ↗
(query)

Source from the content-addressed store, hash-verified

100
101
102def colname_htable(query):
103 assert query.upper().startswith("SELECT")
104 colquery = query[6:query.upper().index(" FROM ")]
105 colnames = colquery.split(',')
106
107 # If split caused unbalanced brackets, it's complex: assume
108 # a single field!
109 if any([colname.count('(') != colname.count(')') for colname in colnames]):
110 return [('"' + colquery.strip() + '"', 0)]
111
112 # 50% density htable
113 tablesize = len(colnames) * 2 - 1
114 table = [("NULL", -1)] * tablesize
115 for colnum, colname in enumerate(colnames):
116 colname = colname.strip()
117 # SELECT xxx AS yyy -> Y
118 as_clause = colname.upper().rfind(" AS ")
119 if as_clause != -1:
120 colname = colname[as_clause + 4:].strip()
121
122 pos = hash_djb2(colname) % tablesize
123 while table[pos][0] != "NULL":
124 pos = (pos + 1) % tablesize
125 table[pos] = ('"' + colname + '"', colnum)
126 return table
127
128
129template = Template("""#ifndef LIGHTNINGD_WALLET_GEN_DB_${f.upper()}

Callers 1

extract_queriesFunction · 0.85

Calls 2

hash_djb2Function · 0.70
anyClass · 0.50

Tested by

no test coverage detected