Upvise Client Library: Query Class (.NET)
Download Upvise Client Library
Overview
The Query class of the Upvise Client Library allows you to make direct calls to the UpviseJS Cloud Database, the same way the native app does, but from any client program. runnning on a desktop or server machine.
- First use the Query.login(email, password), static method to login into your account and obtain a token
- Then create an instance of the Query class with the token as the constructor parameter
- To select records, use the select(table, where) or selectId(table, recordid)
- To insert or a new record, use insert(table, values)
- To update one record, use updateId(table, id, values)
- To delete a record, use deleteId(table, id)
- If you plan to insert, update or delete multiple records in a loop, use the Batch operation to group all request into one unique HTTPS Call.
- Start with a call to beginBatch, then make all the requests and finish with a call to CommitBatch() call
- table names : always use the appid.tablename complete name for the table string.
Requirements and Setup
- .NET client requires Microsoft .NET 4.6.1 or .NET Core 2.0 or higher
- Download and unzip the Upvise Client Library
- For .NET client, add a reference to UpviseClient.dll in your Visual Studio Project
- The namespace of the library is UpviseClient
Obtaining a token
using UpviseClient; ... String token = null; try { token = Query.Login("youremail", "yourpassword"); } catch (UnknownHostException e) { System.out.println("No Internet Connection or Upvise Server unavailable"); return; } catch (Exception e) { System.out.println(e.getMessage()); return; } if (token == null) { System.out.println("token null : bad email or password"); return; }
Selecting records
Creates an instance of the Query class, the token string parameter comes from a previous call to login(email, password) static method
Use the select(table, where) method of the Query class to retrieve records mathing a set of conditions on a specific table.
Query query = new Query(token); String where = "status=1 AND date>=" + new Date().getTime(); JSONbject[] records = query.select("myapp.mytable", where); foreach (JSON record in records) { Console.WriteLine(record.getString("id")); Console.WriteLine(record.getString("name")); }
Selecting new or modified records since a given date
If you query the Upvise Cloud on a regular basis, you should query the modified or newly created records since the last time you call the samy query for maxiumn performance. Use selectSince(table, where, sinceDate) method. The sinceDate value should come from the last query.lastServerDate value obtained after the previous call to selectSince. You should save persistantly and update the last sinceDate value.
JavaQuery query = new Query(token); String where = "status=1 AND date>=" + new Date().getTime(); // This returns all records because sinceDate = 0 long sinceDate = 0; JSONbject[] records = query.selectSince("myapp.mytable", where, sinceDate); Console.WriteLine("Number of records: " + records.length); // Store the date we did the last selectQuery // In a real situation, this value would be store persistantly and reloaded sinceDate = query.lastServerDate; // Perform the same query again immediately, it should return no records records = query.selectSince("myapp.mytable", where, sinceDate); Console.WriteLine("New reocrds since last select: " + records.length);
Inserting or replacing a record
JSONObject values = new JSONObject(); values.put("id", "56754"); values.put("name", "My Task"); values.put("status", "0"); Query query = new Query(token); query.insert("myapp.mytable", values);
Updating one record
Use updateId(table, id, values) method
Query query = new Query(token); JSONObject values = new JSONObject(); values.put("name", "Publish the web site"); values.put("status", 0); values.putDate("duedate", Date.now.AddDays(2)); query.updateId("Tasks.tasks", "56754", values);
Deleting one record
Query query = new Query(token); query.deleteId("myapp.mytable", "56754");Batch Operation
// Create a bunch of tasks using Batch operation query.beginBatch(); for (int i = 0; i < 10; i ++) { JSONObject task = new JSONObject(); task.put("id", "TASKID_" + i); task.put("name", "My Task " + i); task.putDate("duedate", DateTime.Now.AddDays(2)); query.insert("Tasks.tasks", task); } // Execute one batch HTTPS call for all insert requests query.commitBatch();
File Operation
Select & download all files linked to a given record C#using UpviseClient; .... string recordId = "aRecordId"; // obtain actual value from a previous database call Query fileQuery = new Query(token); JSONOject where = new JSONObject(); FileInfo[] list = query.selectFiles(null, "aRecordId"); foreach(FileInfo info in list) { byte[] content = query.downloadFile(info.id); Files.writeAllBytes(@"c:\\temp\\" + info.name, content); }Upload a new File linked to an existing Database record C#
using UpviseClient; .... string recordId = "aRecordId"; // obtain actual value from a previous database call Query query = new Query(token); FileInfo fileInfo = new FileInfo(); fileInfo.id= "TESTFILE1"; fileInfo.name = "testFile.pdf"; fileInfo.mime = "application/pdf"; fileInfo.linkedid = recordId; byte[] content = File.ReadAllBytes(@"c:\\temp\testFile.pdf"); query.uploadFile(fileInfo, content);
static string Login(string email, string password)
Performs a login into the Upvise account identified by its email and password.
- email is the email of an existing user in Upvise Cloud, and password its password
- return a non null token if success or null if the email/password do not match
- throws an Exception if there is no network connection or if the UpviseJS cloud is not available
Query(String token) Constructor
Creates an instance of the Query class, the token string parameter comes from a previous call to Query.login(email, password) static method.
JSONObject[] select(string table, string where)
Performs a select query on the specified table.
- table is the fully qualified name of the table to query, in the format appid.tablename. It must correspond to one of your application tables or to an already installed Upvise app table.
- where is a string containing a valid SQL where clause (for example "status=0" or "name='Test'")
- The return value is an array of JSONObject containing all records.
JSONObject[] selectSince(string table, string where, long sinceDate)
Performs a select query on the specified table but only returns records created or modified after the sinceDate.
- table is the fully qualified name of the table to query, in the format appid.tablename. It must correspond to one of your application tables or to an already installed Upvise app table.
- where is a string containing a valid SQL where clause (for example "status=0" or "name='Test'")
- sinceDate is long value which must be obtained from the lastServerDate property following a previous call to selectSince()
- The return value is an array of JSONObject containing all records.
JSONObject[] selectArchivedOrDeletedSince(string table, string where, long sinceDate)
Performs a select query on the specified table but only returns records archived or deleted after the sinceDate.
- table is the fully qualified name of the table to query, in the format appid.tablename. It must correspond to one of your application tables or to an already installed Upvise app table.
- where is a string containing a valid SQL where clause (for example "status=0" or "name='Test'")
- sinceDate is long value which must be obtained from the lastServerDate property following a previous call to selectSince()
- The return value is an array of JSONObject containing only the id field for each record. This will return only deleted or archived records from the last 7 days.
JSONObject selectId(string table, string id)
Return the record identified by its table and primary key id
- table is the fully qualified name of the table to query, in the format appid.tablename. It must correspond to one of your application tables or to an already installed Upvise app table.
- id is the primary key of the record to select
- Return a JSONObject matching the primary key or null if not founf
void insert(string table, JSONObject values)
Inserts a new record into the table.
- table is the fully qualified name of the table to query, in the format appid.tablename. It must correspond to one of your application tables or to an already installed Upvise app table.
- values is a JSONObject containing the values to add. Each property name must correspond to a column name in the table
void updateId(string table, string id, JSONObject values)
Updates one record identified by its table and primary key id with values
- table is the fully qualified name of the table to query, in the format appid.tablename. It must correspond to one of your application tables or to an already installed Upvise app table.
- id is the primary key of the record to update
- values is a JSONObject containing the values to update the record with. Each property name must correspond to a column name in the table
void deleteId(string table, string id)
Delete one record identified by its table and primary key
- table is the fully qualified name of the table to query, in the format appid.tablename. It must correspond to one of your application tables or to an already installed Upvise app table.
- id is the primary key of the record to update
void beginBatch()
Start a new batch operation.
- All further calls to insert(), updateId() & deleteId() will be grouped together
- Call commitBatch() method to perform the actual HTTPS web service call to execure all queries
void commitBatch()
Commit the batch operation and peform a unique HTTPS web service call to execute all previous insert, update and delete queries from beginBatch()
long lastServerDate
Readonly value containing the server date stamp
- This value is updated after each call to Select, SelectSince() or SelectId() call.
- You must call one of these methode BEFORE accessing this property, if not, it will return 0
- < Use this value to perform delta sync using the SelectSince() call./li>
JSONObject[] selectDeleted(string table, string where)
Performs a select query on the specified table returning deleted records
- table is the fully qualified name of the table to query, in the format appid.tablename. It must correspond to one of your application tables or to an already installed Upvise app table.
- where is a string containing a valid SQL where clause (for example "status=0" or "name='Test'")
- The return value is an array of JSONObject containing deleted records.
JSONObject[] selectArchived(string table, string where)
Performs a select query on the specified table returning archived records
- table is the fully qualified name of the table to query, in the format appid.tablename. It must correspond to one of your application tables or to an already installed Upvise app table.
- where is a string containing a valid SQL where clause (for example "status=0" or "name='Test'")
- The return value is an array of JSONObject containing archived records.
void restoreId(string table, string id)
Restores one previously deleted record identified by its table and primary key
- table is the fully qualified name of the table to query, in the format appid.tablename. It must correspond to one of your application tables or to an already installed Upvise app table.
- id is the primary key of the record to restore
void archiveId(string table, string id)
Archives one record identified by its table and primary key
- table is the fully qualified name of the table to query, in the format appid.tablename. It must correspond to one of your application tables or to an already installed Upvise app table.
- id is the primary key of the record to archive
File[] selectFiles(string linkedtable, string linkedid)
Returns a list of files attached to the record identified by its table (linkedtable) and id (linkedid)
- linkedtable is the fully qualified name of the linked record. For example for Jobs records, it is "jobs.jobs".
- linkedid is the primary key of the record for which you want to get the linked files.
- returns an array of File objects
byte[] downloadFile(string id)
Returns a the file content for an given file id primary key
- id is the primary key of the file record.
- returns the file content as a byte[] or null
void insertfile(File file, byte[] content)
insert a new file and link it to an existing record
- file is the file metadata of type File. file properties such as name and mime MUST be filled in
- to link the new file to an existing record, set file.linkedtable and file.linkedid properties as well
- content is the file content as a byte[]
using UpviseClient; .... string jobid = "aRecordId"; // obtain actual value from a previous database call Query query = new Query(token); File file = new File(); file.name = "testFile.pdf"; file.mime = "application/pdf"; file.linkedid = jobid; file.linkedtable = "jobs.jobs"; byte[] content = System.IO.File.ReadAllBytes(@"c:\\temp\testFile.pdf"); query.uploadFile(file, content);