/// <summary> /// Creates a pattern of ("primaryKeyFieldName":primaryKeyValue) for the input instance - can be passed to the server when inserting or updating an entity /// </summary> /// <param name="input"></param> /// <returns></returns> private string getPrimaryKeyPattern(object input) { JsonDropTableData tableData = getTableData(input); string fieldName = string.Format("\"{0}\"", tableData.PrimaryKey.Name); string fieldValue = tableData.PrimaryKey.FieldType == typeof(String) ? string.Format("\"{0}\"", tableData.PrimaryKey.GetValue(input).ToString()) : tableData.PrimaryKey.GetValue(input).ToString(); return(string.Format("{0}:{1}", fieldName, fieldValue)); }
/// <summary> /// Write data to the database - jsnPut command /// </summary> /// <param name="input"></param> /// <returns></returns> public JsonDropResult create(object input) { JsonDropResult output; try { JsonDropTableData tableData = getTableData(input); // jsnPut parameters are (Registered table id),(primary key value),(object as JSON) string queryParameters = string.Format("{0},{1},{2}", tableData.ID, getPrimaryKeyPattern(input), JsonUtility.ToJson(input)); string queryResponse = queryServer("jsnPut", queryParameters); output = new JsonDropResult(JsonUtility.FromJson <JsnDropMessage>(queryResponse).Message == "OK", queryResponse); } catch (Exception e) { output = new JsonDropResult(false, "There was an error entering the data"); } return(output); }
/// <summary> /// Find an entity by its primary key - jsnGet command /// </summary> /// <typeparam name="T"></typeparam> /// <param name="inputKey"></param> /// <returns></returns> public T[] read <T>(string inputKey) { T[] output; try { JsonDropTableData tableData = getTableData(typeof(T)); // jsnGet parameters are (Registered table id),([primary key value]) - primary key value is optional string queryParameters = string.Format("{0},{1}", tableData.ID, getFieldPattern(tableData.PrimaryKey, inputKey)); string queryResponse = queryServer("jsnGet", queryParameters); output = jsnGetToArray <T>(queryResponse); } catch (Exception e) { // Log the error and then return an empty array Debug.Log(e.Message); output = new T[] { }; } return(output); }
/// <summary> /// Find entities given a field and a value - jsnGet command /// </summary> /// <typeparam name="T"></typeparam> /// <param name="inputField"></param> /// <param name="inputValue"></param> /// <returns></returns> public T[] read <T>(string inputField, string inputValue) { T[] output; try { JsonDropTableData tableData = getTableData(typeof(T)); // Make sure the field the user is check is a field of the table bool hasField = false; FieldInfo fieldToFind = null; foreach (FieldInfo f in typeof(T).GetFields()) { if (f.Name == inputField) { hasField = true; fieldToFind = f; break; } } if (!hasField) { throw new Exception("Couldn't find the field asked for"); // Force the catch block to run } else { // jsnGet parameters are (Registered table id),([primary key value]) - primary key value is optional string queryParameters = string.Format("{0},{1}", tableData.ID, getFieldPattern(fieldToFind, inputValue)); string queryResponse = queryServer("jsnGet", queryParameters); output = jsnGetToArray <T>(queryResponse); } } catch (Exception e) { // Log the error and then return an empty array Debug.Log(e.Message); output = new T[] { }; } return(output); }