Author | Message | Time |
---|---|---|
St0rm.iD | Well, I've finally got a pretty stressful web programming job. I have many, many PHP horror stories to tell you (use templates!!!), but I'll spare them for now. It would be a hell of a lot easier if I could serialize objects to SQL and read them back again. I can already create tables and insert data, but reading the data back is a problem. It's easy to read back until you hit another object or an array, at which point you have to perform a join. My question is...how can I take a standard sql SELECT resultset (key/value pairs, however, you don't know which column belongs to which table) and deserialize that into objects? Each object class has metadata embedded in it, that is, it knows what its variable names are and their types. Any suggestions? I can port from most semi-high level languages, pseudo, and plain old advice would just be great. Thanks a ton. | August 24, 2003, 3:48 AM |
Camel | Your question as is appears to be a little vague so I'm not sure if this is what you want; perhaps you could post specificly what you're trying to do or even your workaround code, it would help me to understand the problem better. For a standard array, you might try INSERTing as as string that can be read back through an eval() statement. For example, INSERT INTO table SET something = "Some => 'Thing', Blah => 'Whatever'"; and then after you SELECT, you can do eval('$array = array(' . $row[column] . ')'); You could then write a function to break down an array into a string in said form. | August 24, 2003, 7:29 AM |
St0rm.iD | That's a good idea, however, I'd kind of like this to look as "natural" as possible, y'know? | August 24, 2003, 9:55 PM |
Camel | Fine, I'll try again. [code]<sql> <table name="arrays" primary_key="id"> <!-- This is the table for all of your arrays, minus the data. --> <column name="id" type=number /> <column name="name" type=text /> </table> <table name="arraydata"> <!-- Array data goes in this table. --> <column name="id" type=number link="[arrays].[id]" /> <column name="name" type=text /> <column name="value" type=text /> </table> </sql>[/code] You could could subclass this further using the same concept. [edit] Sp. | August 27, 2003, 1:01 AM |
St0rm.iD | Interesting idea...perhaps I will do it that way except express them as Python objects... If I combined Python, web programming, databases, and XML...I'd have a buzzword...and I don't want that :) | August 27, 2003, 4:15 AM |
Camel | How would XML have anything to do with it? I only used XML to simplify the explanation... | August 27, 2003, 7:46 AM |
Adron | So you mean that xml description can't be directly read into a tool that produces the application? How eww! ;) | August 27, 2003, 7:21 PM |
St0rm.iD | I was thinking of an api along the lines of: [code] class SQLObjectL: ___def fromResultset(self,resultset): _______"Deserialize ourself from a resultset. Call this on any complex members of this class, unless marked otherwise." _______pass ____def insertStatement(self): _______"Get a SQL INSERT statement. Call on all complex members and return a semicolon-separated list of INSERTS." _______pass ____def generateCreate(self): _______"Make a CREATE TABLE statement" _______pass [/code] | August 27, 2003, 8:18 PM |