How to pass a datastore from one user object to another - sybase

Can someone share how to pass a datastore initiated as an instance variable on the window and create it from a datawindow on a user object and pass it to another user object.
Thanks for any help in advance

Don't forget that datastore variables, arguments, etc... are just pointers to instantiated datastores. So, something like this:
UserObject1
Instance Variable:
datastore ids_One
userobject2 inv_Two
Function of_CreateDS ()
ids_One = CREATE datastore
inv_Two = CREATE userobject2
inv_Two.of_ShareDS (ids_One)
UserObject2
Instance Variable:
datastore ids_Two
Function of_ShareDS (datastore ads_Share)
ids_Two = ads_Share
By the time you get to the end of UserObject1.of_CreateDS(), both UserObject1 and UserObject2 will be pointing at the same instance of one datastore. The tricky part when multiple variables point to the same datastore could be deciding when to DESTROY the instance of the datastore.
Good luck,
Terry.

To pass a datastore from a userobject to another you can use directly the datastore type: a procedure or function can have a datastore argument or return the datastore type.
if you need to establish a link between two datawindows or a datawindow and a datastore you can use the ShareData() method: any modification on one of the objects (InsertRow(), SetFilter(), Update(), ...) is reflected to the other.

Related

Delphi Indy 10 HTTPServer bind database instance to session

Is there any best practice to bind database connection instances to INDY HTTP Server sessions?
I store usual session data in ARequestInfo.Session.Content.Values but this is only for strings. My current approach for database objects is (TDatabaseis just an example class):
Create a TDictionary<String,TDatabase>.
Create TDatabase instances for every session and store references along with the session id in the dictionary.
Access the dictionary enclosed in critical sections within the session processing to be thread safe.
Destroy TDatabase instances when sessions are destroyed.
I suspect that my approach is overhead and there are much more elegant ways to achieve what I want. If this is the case - Tips are very welcome.
The Session.Content property is a TStrings, which can hold strings AND TObject pointers. You don't need a separate TDictionary to map strings to objects, you can store them together in the Content itself.
Alternatively, you can derive a new class from TIdHTTPSession, add your database connection to that class, and then use the TIdHTTPServer.OnCreateSession event to create instances of that class. Then, to access the database connection, simply typecast any TIdHTTPSession object to your class type.

When using an object database, how do you handle significant changes to your object model?

If you use an object database, what happens when you need to change the structure of your object model?
For instance, I'm playing around with the Google App Engine. While I'm developing my app, I've realized that in some cases, I mis-named a class, and I want to change the name. And I have two classes that I think I need to consolidate.
However,I don't think I can, because the name of the class in intuitively tied into the datastore, and there is actual data stored under those class names.
I suppose the good thing about the "old way" of abstracting the object model from the data storage is that the data storage doesn't know anything about the object model --it's just data. So, you can change your object model and just load the data out of the datastore differently.
So, in general, when using a datastore which is intimate with your data model...how do you change things around?
If it's just class naming you're concerned about, you can change the class name without changing the kind (the identifier that is used in the datastore):
class Foo(db.Model):
#classmethod
def kind(cls):
return 'Bar'
If you want to rename your class, just implement the kind() method as above, and have it return the old kind name.
If you need to make changes to the actual representation of data in the datastore, you'll have to run a mapreduce to update the old data.
The same way you do it in relational databases, except without a nice simple SQL script: http://code.google.com/appengine/articles/update_schema.html
Also, just like the old days, objects without properties don't automatically get defaults and properties that don't exist in the schema still hang around as phantoms in the objects.
To rename a property, I expect you can remove the old property (the phantom hangs around) add the new name, populate the data with a copy from the old (phantom) property. The re-written object will only have the new property
You may be able to do it the way we are doing it in our project:
Before we update the object-model (schema), we export our data to a file or blob in json format using a custom export function and version tag on top. After the schema has been updated we import the json with another custom function which creates new entities and populates them with old data. Of course the import version needs to know the json format associated with each version number.

GAE for Java Best Practice. How to store sorted List<string> in a most optimal way

What is the best way to store lists (collections) of simple types in Google App Engine for Java?
I usually use something like that:
public class Person {
// ....
#Persistent(serialized = "true")
private List<Date> DatesList;
// ....
}
I am not sure how it is stored in DataStore, atomically or as a list of references? Hence wanted to get your advice on implementing these kinds of collections in a best way for performance of a query.
Like in the example above, when I load the object, will the DateList will be loaded with it. Or it will be loaded when accessed fro first time?
Does #defaultFetchGroup attribute affect this collection?
Thanks
The list will be returned/loaded by the datastore along with the rest of the object, not when it is first referenced.
GAE JDO Collections
If a field is declared as a List, objects returned by the datastore have an ArrayList value. If a field is declared as a Set, the datastore returns a HashSet. If a field is declared as a SortedSet, the datastore returns a TreeSet.

Updating Model Schema in Google App Engine?

Google is proposing changing one entry at a time to the default values ....
http://code.google.com/appengine/articles/update_schema.html
I have a model with a million rows and doing this with a web browser will take me ages. Another option is to run this using task queues but this will cost me a lot of cpu time
any easy way to do this?
Because the datastore is schema-less, you do literally have to add or remove properties on each instance of the Model. Using Task Queues should use the exact same amount of CPU as doing it any other way, so go with that.
Before you go through all of that work, make sure that you really need to do it. As noted in the article that you link to, it is not the case that all entities of a particular model need to have the same set of properties. Why not change your Model class to check for the existence of new or removed properties and update the entity whenever you happen to be writing to it anyhow.
Instead of what the docs suggest, I would suggest to use low level GAE API to migrate.
The following code will migrate all the items of type DbMyModel:
new_attribute will be added if does not exits.
old_attribute will be deleted if exists.
changed_attribute will be converted from boolean to string (True to Priority 1, False to Priority 3)
Please note that query.Run returns iterator returning Entity objects. Entity objects behave simply like dicts:
from google.appengine.api.datastore import Query, Put
query = Query("DbMyModel")
for item in query.Run():
if not 'new_attribute' in item:
item['attribute'] = some_value
if 'old_attribute' in item:
del item['old_attribute']
if ['changed_attribute'] is True:
item['changed_attribute'] = 'Priority 1'
elif ['changed_attribute'] is False:
item['changed_attribute'] = 'Priority 3'
#and so on...
#Put the item to the db:
Put(item)
In case you need to select only some records, see the google.appengine.api.datastore module's source code for extensive documentation and examples how to create filtered query.
Using this approach it is simpler to remove/add properties and avoid issues when you have already updated your application model than in GAE's suggested approach.
For example, now-required fields might not exist (yet) causing errors while migrating. And deleting fields does not work for static properties.
This doesn't help OP but may help googlers with a tiny app: I did what Alex suggested, but simpler. Obviously this isn't appropriate for production apps.
deploy App Engine Console
write code right inside the web interpreter against your live datastore
like so:
from models import BlogPost
for item in BlogPost.all():
item.attr="defaultvalue"
item.put()

Google App Engine: Saving a list of objects?

I need to save in my model a list of objects from a certain class on the datastore.
Is there any simple way to archive this with ListProperty and custom property's without going into pickled/simplejson blob data?
I just want something like this:
class Test:
pass
class model(db.Model):
list = db.ListProperty(Test)
Looking at GAE documentation I can't really tell if this is impossible with the current version or not.
I was trying to avoid pickling because it's slow and has size limits.
You can only store a limited set of types directly in the datastore. To store your own types, you need to convert them into one of the accepted types in some manner - pickling is one common approach, as is serializing it as JSON.
The size limit isn't unique to pickling - 1MB is the largest Entity you can insert regardless of the fields and types.
You could save your Test objects in the datastore directly, by making a Test model/entity type. Otherwise you will have to serialize them somehow (using something like pickle or json)
You could have a list of keys
or you could give 'Test' entities a parent that is a entity of your 'model' class

Resources