Advertisement
freswinn

GDScript: 2shady4u GODOT SQLite, update_rows workaround

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