Generate unique name everytime like "Windows folder structure" behaviour - objective-c

I want to create folder structure logic like windows.I am using SQlite database for that. I want to generate a unique name everytime. For e.g. if user enters text with name "New". And if again he enter same name "New" then it should be New(1). if again he enter same name "New" then it should be "New(2)". If user delete "New(1)" entry and enter "New" then "New(1)" should be placed in between "New" and "New(2)".
Could anyone suggest logic for that? Any help is appreciated?
Thanks in advance
Tejas

I consider you will have a database with id and foldername as fields. You just need to retrieve all records from database. In where of query use like operator. Once you get list go in a loop and add add new entry by comparing number. Try this.

Here's the logic I use to do something similar in an app:
Declare an NSString called prefix and an NSInteger called number.
Check if your name has a number on the end of it:
If it does, set number to the value of that number and prefix to the name without the number.
If it does not, set number to 1 and prefix to the name.
Loop from 1 up to some maximum number, doing the following:
Construct a proposedName from base and number:
If number is 1, use the bare prefix.
Otherwise, concatenate the prefix and number in the format you're looking to generate.
Check if proposedName is already in use.*
If it is in use, increment number and loop again.
If it is not in use, proposedName is your new name.
Given that you're using a SQLite database, you can probably speed this up a bit by pre-fetching names that begin with your prefix and loading them into an NSMutableSet. That'll be faster than performing a query for each individual name.
* When you do this test, you should probably exclude the object you're already looking at. This will allow you to try to "uniquify" the name of an existing object without actually changing it.

Related

PDI /Kettle - Passing data from previous hop to database query

I'm new to PDI and Kettle, and what I thought was a simple experiment to teach myself some basics has turned into a lot of frustration.
I want to check a database to see if a particular record exists (i.e. vendor). I would like to get the name of the vendor from reading a flat file (.CSV).
My first hurdle selecting only the vendor name from 8 fields in the CSV
The second hurdle is how to use that vendor name as a variable in a database query.
My third issue is what type of step to use for the database lookup.
I tried a dynamic SQL query, but I couldn't determine how to build the query using a variable, then how to pass the desired value to the variable.
The database table (VendorRatings) has 30 fields, one of which is vendor. The CSV also has 8 fields, one of which is also vendor.
My best effort was to use a dynamic query using:
SELECT * FROM VENDORRATINGS WHERE VENDOR = ?
How do I programmatically assign the desired value to "?" in the query? Specifically, how do I link the output of a specific field from Text File Input to the "vendor = ?" SQL query?
The best practice is a Stream lookup. For each record in the main flow (VendorRating) lookup in the reference file (the CSV) for the vendor details (lookup fields), based on its identifier (possibly its number or name or firstname+lastname).
First "hurdle" : Once the path of the csv file defined, press the Get field button.
It will take the first line as header to know the field names and explore the first 100 (customizable) record to determine the field types.
If the name is not on the first line, uncheck the Header row present, press the Get field button, and then change the name on the panel.
If there is more than one header row or other complexities, use the Text file input.
The same is valid for the lookup step: use the Get lookup field button and delete the fields you do not need.
Due to the fact that
There is at most one vendorrating per vendor.
You have to do something if there is no match.
I suggest the following flow:
Read the CSV and for each row look up in the table (i.e.: the lookup table is the SQL table rather that the CSV file). And put default upon not matching. I suggest something really visible like "--- NO MATCH ---".
Then, in case of no match, the filter redirect the flow to the alternative action (here: insert into the SQL table). Then the two flows and merged into the downstream flow.

Choosing id one by one from a variable in jmeter

I want to delete mappings in DB using DataService.
For this purpose
I run search query for all ids in first thread group.
Using this method I put my Ids into the property.
Now property has view like that b69243ee6e9efdf66114200dc93881ac,b69243ee6e9efdf66114200dc90f5ba4,b69243ee6e9efdf66114200dc90e2184
I want to all delete mapping using this Ids from property one by one.
For this purpose I need run BeanShell Pre-Processor and choose first id and put it into variable. Please, help me with this script.
I believe ForEach Controller is the solution. But in my experience, I use regular expression extractor to grab the values and put it in one variable then loop it using ForEach Controller. I think these step will help you:
Add Regular Expression Extractor to grab the ids.
Make sure you fill field "Match No" with negative number (i.e. -1). Put the extracted value into one variable (i.e. IdVar. Fill Reference Name with IdVar). This step will grab all matched ids and put it into IdVar variable.
Then Add ForEach Controller to process each ids from IdVar variable.
For the details, just download this sample and try to run it.
I hope this will help you. :-)

Accessing a datatable via a string variable

This is probably one of those that is so simple I can't see it. I have a string variable called Market. The variable is user chosen and is the exact same name as one of many tables in my dataset. Basically I am having the user choose which table they want in a combobox, then I want to use that variable to access the table. So if the user picks "Market1" then I want to open the table named Market1.
I am simplifying here, but need to know how to open:
For ds.<variable here>.rows.count - 1
'perform steps
Next
How do I inject the variable correctly? Thanks ahead of time!
Dataset has Tables property, which accepts table name as a parameter. So in your case if string variable Market holds table name, you can access the table by referring to ds.Tables(Market)

Create a report that could be one page or two, depending on what field was modified

In an alternate application, the user has the ability to update their address and phone number. When these are changed, three fields will update: Old Value, New Value, and Field Changed. If the Field Changed was the address, I need to create two report pages - one with the old address and one with the new. However, if the Field Changed was the phone number, I only need to create one report page for the current address.
My initial plan was to do a Union that would have one record with the Old Value and another with the New Value. This should work when only the Address has changed. However, it won't whenever the Phone Number has changed. I assume I need to do some sort of case statement, but I'm not really sure if this is the right approach. Sorry if the data is a little confusing (I didn't design the data structure. This was provided by our professor's assistant). If you need more information, I'll try to provide it.
I'm not looking for exact SQL, but I am wondering if I'm approaching this the correct way.
What do you mean by a 1 or 2 page report? Are you outputting to a CSV, PDF, XLSX or something eles?
If you need to do this through "pure" sql I would recommend a stored procedure that is given a value stating whether it's the address or phone number that is being updated. It can then do the update and you can simply do an if statement which determines which report to run and return.
I'd recommend handling it programatically if possible. Have your code run the sql update and then call the appropriate function within your code to get the report you need. You can then easily re-use the code for that report in other ways.

How do I create a Identity starting with a Letter first then numbers after?

I want to create data that contains info about my supplier making it auto generate his ID.
For example, SupplierID I want it to appear as - SID001, SID002 all to auto generate after each other.
How do I do this with SQL?
Ask yourself this: what are the costs of doing this? In particular, what is required to compare two strings versus comparing two numbers? To generate strings from numbers?
Then ask yourself, what value is added by having an id of 'SID0001' rather than just 001?
Then ask yourself, is there an easy way to display a prefix without redundantly storing it for each row? (Answer: yes, with a database view).
You could just use a sequence or identity column and store the prefix in a separate column, or alternately, if the table in question will only ever have suppliers with a prefix of SID, don't store the SID part at all and simply add it at the application level.
You could check for the next ID available and concatenate your prefix with that value. The result should be inserted in the SupplierID column.
Some databases(Oracle, postgres etc) support a sequence for number part. Some(mysql) have an auto increment feature, so you get new number when inserting.
You could then concatenate with string to generate string based IDs.