Advertisement
freswinn

GDScript: 2shady4u GODOT SQLite, update_rows workaround

Jan 7th, 2023 (edited)
2,225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. func update_values(table : String, criteria : Dictionary, values_to_change : Dictionary):
  2. #   This function translates the inputs that are more convenient to provide into the ones that work with the plugin's update_rows method.
  3. #   EXAMPLE ORIGINAL SYNTAX: DB.db.update_rows("'Home Town'", "\"Name of Town\"=\"Maybe Nowhere\" and \"Town ID\"=1", {"'Name of Town'":"Everything"})
  4. #   table:              would need ' ' around table names with spaces in them
  5. #   criteria:           would need \" \" around column names with spaces in them
  6. #                       would need \" \" around cell data strings with spaces in them
  7. #                   ALSO: "and" has to be between each criterium
  8. #   values_to_change:   would need ' ' around column names with spaces in them
  9. #                       does not actually need anything extra around values if there are spaces in them
  10. #   EXAMPLE NEW SYNTAX: DB.update_values("Home Town", {"Name of Town" : "Maybe Nowhere", "Town ID" : 1}, {"Name of Town" : "Everything"})
  11.  
  12.     var xtable : String = table
  13.     var xcriteria : String = "" # input criteria is dictionary, but output is string, as noted above
  14.     var xvalues : Dictionary = {}
  15.  
  16.     #formatting xtable
  17.     if " " in xtable:
  18.         if !xtable.begins_with("'"): xtable = "'" + xtable
  19.         if !xtable.ends_with("'"): xtable += "'"
  20.  
  21.     #formatting xcriteria
  22.     var ckeys : Array = criteria.keys() #array of original keys, acquired
  23.     var cvals : Array = []
  24.     var xckeys : Array = []
  25.     var xcvals : Array = []
  26.  
  27.     for i in ckeys:
  28.         cvals.append(criteria[i]) #array of original values, acquired
  29.         if i.begins_with("'"): i = i.right(1)
  30.         if i.ends_with("'"): i = i.left( len(i)-1 )
  31.         if !i.begins_with("\""): i = "\"" + i
  32.         if !i.ends_with("\""): i += "\""
  33.         xckeys.append(i) #array of corrected keys, acquired
  34.    
  35.     for i in cvals:
  36.         if i is String and " " in i:
  37.             if i.begins_with("'"): i = i.right(1)
  38.             if i.ends_with("'"): i = i.left( len(i)-1 )
  39.             if !i.begins_with("\""): i = "\"" + i
  40.             if !i.ends_with("\""): i += "\""
  41.         elif i is bool: #not entirely sure this is necessary, but it can't hurt
  42.             match i:
  43.                 true: i = 1
  44.                 false: i = 0
  45.         xcvals.append(i) #array of corrected values, acquired
  46.  
  47.     for k in len(xckeys):
  48.         var add_and = false
  49.         if len(xcriteria) != 0: add_and = true
  50.         if add_and: xcriteria += " and "
  51.         xcriteria += "%s=%s" % [xckeys[k], xcvals[k]]
  52.  
  53.     #formatting xvalues
  54.     var vkeys : Array = values_to_change.keys() #array of original keys, acquired
  55.     var vvals : Array = []
  56.     var xvkeys : Array = []
  57.     var xvvals : Array = [] #this variable doesn't really need to exist, but for clarity I'm including it
  58.  
  59.     for i in vkeys:
  60.         vvals.append(values_to_change[i]) #array of original values, acquired
  61.         if " " in i:
  62.             if !i.begins_with("'"): i = "'" + i
  63.             if !i.ends_with("'"): i += "'"
  64.         xvkeys.append(i) #array of corrected keys, acquired
  65.    
  66.     for i in vvals: #yet again, I don't think this is strictly necessary, but it can't hurt
  67.         if i is bool:
  68.             match i:
  69.                 true: xvvals.append(1)
  70.                 false: xvvals.append(0)
  71.         else: xvvals.append(i)
  72.  
  73.     for k in len(xvkeys):
  74.         xvalues.merge( {xvkeys[k] : xvvals[k]} )
  75.  
  76.     #finally!
  77.     db.update_rows(xtable, xcriteria, xvalues)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement