Changeset 413 for GearsORM/branches
- Timestamp:
- 03/22/08 13:32:22 (8 months ago)
- Location:
- GearsORM/branches/JsORM
- Files:
-
- 17 modified
-
JsORM.Events.js (modified) (4 diffs)
-
JsORM.Fields.js (modified) (20 diffs)
-
JsORM.Introspection.js (modified) (4 diffs)
-
JsORM.Model.js (modified) (19 diffs)
-
JsORM.ResultIterator.js (modified) (4 diffs)
-
JsORM.Sql.js (modified) (9 diffs)
-
JsORM.Transaction.js (modified) (3 diffs)
-
JsORM.js (modified) (7 diffs)
-
examples/new_demo.html (modified) (1 diff)
-
tests/test_events.html (modified) (1 diff)
-
tests/test_inner_relations.html (modified) (1 diff)
-
tests/test_introspection.html (modified) (2 diffs)
-
tests/test_m2m.html (modified) (1 diff)
-
tests/test_relations.html (modified) (1 diff)
-
tests/test_simple.html (modified) (1 diff)
-
tests/test_transaction.html (modified) (3 diffs)
-
tests/unit_test.js (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
GearsORM/branches/JsORM/JsORM.Events.js
r412 r413 1 1 /* 2 Script: GearsORM.Events.js3 Event support for GearsORM2 Script: JsORM.Events.js 3 Event support for JsORM 4 4 5 5 License: … … 8 8 9 9 /* 10 Class: GearsORM.Events10 Class: JsORM.Events 11 11 add event support to a class 12 12 … … 14 14 15 15 */ 16 GearsORM.Events = function()16 JsORM.Events = function() 17 17 { 18 18 this.$events = {}; 19 19 } 20 GearsORM.Events.prototype =20 JsORM.Events.prototype = 21 21 { 22 22 /* … … 75 75 object - the object who fire the events 76 76 */ 77 GearsORM.Events.wrapFunction=function(fn,name,object)77 JsORM.Events.wrapFunction=function(fn,name,object) 78 78 { 79 79 return function() -
GearsORM/branches/JsORM/JsORM.Fields.js
r412 r413 1 1 /* 2 Script: GearsORM.Fields.js2 Script: JsORM.Fields.js 3 3 Contains the fields to be used in the models. 4 4 … … 7 7 */ 8 8 9 GearsORM.Field = {};10 /* 11 Class: GearsORM.Field.Primitive9 JsORM.Field = {}; 10 /* 11 Class: JsORM.Field.Primitive 12 12 a meta class for basic field types like varchar,integer,real and timestamp 13 13 … … 17 17 sqlType - the sql string for the type 18 18 */ 19 GearsORM.Field.Primitive = function(sqlType)19 JsORM.Field.Primitive = function(sqlType) 20 20 { 21 21 var field = function(options) … … 37 37 isRelation:false 38 38 }; 39 field.constructor = GearsORM.Field.Primitive;39 field.constructor = JsORM.Field.Primitive; 40 40 return field; 41 41 }; … … 43 43 a object holding all the field types(including relations) 44 44 */ 45 GearsORM.Fields =46 { 47 String:new GearsORM.Field.Primitive("VARCHAR"),48 Integer:new GearsORM.Field.Primitive("INTEGER"),49 Float:new GearsORM.Field.Primitive("REAL"),50 TimeStamp:new GearsORM.Field.Primitive("TIMESTAMP")51 }; 52 53 /* 54 Class: GearsORM.Fields.ManyToOne45 JsORM.Fields = 46 { 47 String:new JsORM.Field.Primitive("VARCHAR"), 48 Integer:new JsORM.Field.Primitive("INTEGER"), 49 Float:new JsORM.Field.Primitive("REAL"), 50 TimeStamp:new JsORM.Field.Primitive("TIMESTAMP") 51 }; 52 53 /* 54 Class: JsORM.Fields.ManyToOne 55 55 56 56 represent a ManyToOne relation,this isn`t a actual table field. … … 66 66 } 67 67 */ 68 GearsORM.Fields.ManyToOne = function(options)68 JsORM.Fields.ManyToOne = function(options) 69 69 { 70 70 this.options = options; 71 71 }; 72 72 73 GearsORM.Fields.ManyToOne.prototype =73 JsORM.Fields.ManyToOne.prototype = 74 74 { 75 75 /* … … 79 79 copy:function() 80 80 { 81 return new GearsORM.Fields.ManyToOne(this.options);81 return new JsORM.Fields.ManyToOne(this.options); 82 82 }, 83 83 /* … … 119 119 getRelatedClass:function() 120 120 { 121 return GearsORM._models[this.options.related];121 return JsORM._models[this.options.related]; 122 122 }, 123 123 _selectRemove:function(whereExpression,params,method) … … 134 134 135 135 /* 136 Class: GearsORM.Fields.OneToMany136 Class: JsORM.Fields.OneToMany 137 137 represent a forigen key relation 138 138 … … 148 148 } 149 149 */ 150 GearsORM.Fields.OneToMany = function(options)150 JsORM.Fields.OneToMany = function(options) 151 151 { 152 152 this.options = options; 153 153 }; 154 154 155 GearsORM.Fields.OneToMany.prototype =155 JsORM.Fields.OneToMany.prototype = 156 156 { 157 157 /* … … 161 161 copy:function() 162 162 { 163 return new GearsORM.Fields.OneToMany(this.options);163 return new JsORM.Fields.OneToMany(this.options); 164 164 }, 165 165 /* … … 169 169 toSql:function() 170 170 { 171 return GearsORM.Sql.oneToManySql(this.options.related,this.options.allowNull);171 return JsORM.Sql.oneToManySql(this.options.related,this.options.allowNull); 172 172 }, 173 173 /* … … 177 177 getRelatedClass:function() 178 178 { 179 return GearsORM._models[this.options.related];179 return JsORM._models[this.options.related]; 180 180 }, 181 181 isBackwardRelation:false, … … 183 183 }; 184 184 /* 185 Class: GearsORM.Fields.ManyToMany185 Class: JsORM.Fields.ManyToMany 186 186 represent a many to many relation between two tables using a many to many relation table 187 187 … … 196 196 } 197 197 */ 198 GearsORM.Fields.ManyToMany = function(options)198 JsORM.Fields.ManyToMany = function(options) 199 199 { 200 200 this.options = options; 201 201 }; 202 202 203 GearsORM.Fields.ManyToMany.prototype =203 JsORM.Fields.ManyToMany.prototype = 204 204 { 205 205 _m2mModel:null, … … 210 210 copy:function() 211 211 { 212 return new GearsORM.Fields.ManyToMany(this.options);212 return new JsORM.Fields.ManyToMany(this.options); 213 213 }, 214 214 /* … … 225 225 226 226 var fields = {}; 227 fields[relatedModelName] = new GearsORM.Fields.OneToMany({related:relatedModelName,onDeleteCascade:true});228 fields[thisModelName] = new GearsORM.Fields.OneToMany({related:thisModelName,onDeleteCascade:true});227 fields[relatedModelName] = new JsORM.Fields.OneToMany({related:relatedModelName,onDeleteCascade:true}); 228 fields[thisModelName] = new JsORM.Fields.OneToMany({related:thisModelName,onDeleteCascade:true}); 229 229 230 this._m2mModel = new GearsORM.Model({230 this._m2mModel = new JsORM.Model({ 231 231 name:"m2m_"+ thisModelName + "_"+ relatedModelName, 232 232 fields:fields … … 266 266 { 267 267 //create a new table for the many-to-many relation only if related class is defined 268 if( GearsORM._models[this.options.related])268 if(JsORM._models[this.options.related]) 269 269 { 270 270 this.getRelationModel().createTable(); … … 283 283 var m2mTableName = this.getRelationModel().options.name; 284 284 var relatedClass = this.getRelatedClass(); 285 return new GearsORM.ResultIterator(GearsORM.execute(GearsORM.Sql.selectWithManyToMany(this.modelClass,relatedClass,m2mTableName,whereExpression,this.instance.rowid),params),relatedClass);285 return new JsORM.ResultIterator(JsORM.execute(JsORM.Sql.selectWithManyToMany(this.modelClass,relatedClass,m2mTableName,whereExpression,this.instance.rowid),params),relatedClass); 286 286 }, 287 287 /* … … 311 311 getRelatedClass:function() 312 312 { 313 return GearsORM._models[this.options.related];313 return JsORM._models[this.options.related]; 314 314 }, 315 315 isBackwardRelation:true, -
GearsORM/branches/JsORM/JsORM.Introspection.js
r412 r413 1 1 /* 2 Script: GearsORM.Introspection.js3 Contains the GearsORM.Introspection utility class,2 Script: JsORM.Introspection.js 3 Contains the JsORM.Introspection utility class, 4 4 which contain also a model for sqlite_master table 5 5 … … 9 9 10 10 /* 11 Class: GearsORM.Introspection11 Class: JsORM.Introspection 12 12 a utility class to do database introspection. 13 13 */ 14 GearsORM.Introspection =14 JsORM.Introspection = 15 15 { 16 16 /* 17 Property: GearsORM.Introspection.sqliteMasterModel17 Property: JsORM.Introspection.sqliteMasterModel 18 18 a model to work with sqlite_master(provide information on the schema) table 19 19 sqlite_master definition from sqlite site: … … 26 26 ); 27 27 */ 28 sqliteMasterModel:new GearsORM.Model({28 sqliteMasterModel:new JsORM.Model({ 29 29 name:"sqlite_master", 30 30 fields: 31 31 { 32 type:new GearsORM.Fields.String(),33 name:new GearsORM.Fields.String(),34 tbl_name:new GearsORM.Fields.String(),35 rootpage:new GearsORM.Fields.Integer(),36 sql:new GearsORM.Fields.String()32 type:new JsORM.Fields.String(), 33 name:new JsORM.Fields.String(), 34 tbl_name:new JsORM.Fields.String(), 35 rootpage:new JsORM.Fields.Integer(), 36 sql:new JsORM.Fields.String() 37 37 } 38 38 }), … … 56 56 var query = ["sqlite_master.type =? AND sqlite_master.name IN (",qmarks.join(","),")"].join(""); 57 57 58 return GearsORM.Introspection.sqliteMasterModel58 return JsORM.Introspection.sqliteMasterModel 59 59 .count(query,vals) == argsLength; 60 60 } -
GearsORM/branches/JsORM/JsORM.Model.js
r412 r413 1 1 /* 2 Script: GearsORM.Model.js3 Contains GearsORM.Model a metaclass used to define models2 Script: JsORM.Model.js 3 Contains JsORM.Model a metaclass used to define models 4 4 5 5 License: … … 8 8 9 9 /* 10 Class: GearsORM.Model10 Class: JsORM.Model 11 11 a metaclass to define models that represent database tables. 12 12 a metaclass is a class in which the instances are also classes. … … 21 21 fields: 22 22 { 23 name_of_the_field:a_instance_of_ GearsORM.Fields.*23 name_of_the_field:a_instance_of_JsORM.Fields.* 24 24 } 25 25 } 26 26 */ 27 GearsORM.Model = function(options)27 JsORM.Model = function(options) 28 28 { 29 29 if(!options || !options.fields || !options.name) … … 32 32 } 33 33 /* 34 Class: GearsORM.Model.model34 Class: JsORM.Model.model 35 35 36 36 represent a model instance … … 55 55 else 56 56 { 57 GearsORM.extend(this,valuesOrRowID);57 JsORM.extend(this,valuesOrRowID); 58 58 } 59 59 } … … 81 81 { 82 82 if(this.rowid) 83 GearsORM.executeAndClose(GearsORM.Sql.deleteRowByIdAndTable(options.name),[this.rowid]);83 JsORM.executeAndClose(JsORM.Sql.deleteRowByIdAndTable(options.name),[this.rowid]); 84 84 85 85 this.rowid = null; … … 103 103 _updateInsert:function(update) 104 104 { 105 var query = update ? GearsORM.Sql.updateByModel(model) : GearsORM.Sql.insertRowByModel(model);105 var query = update ? JsORM.Sql.updateByModel(model) : JsORM.Sql.insertRowByModel(model); 106 106 107 107 var values = []; … … 129 129 } 130 130 if(update)values.push(this.rowid); 131 GearsORM.executeAndClose(query,values);132 if(!update)this.rowid = GearsORM.getDB().lastInsertRowId;131 JsORM.executeAndClose(query,values); 132 if(!update)this.rowid = JsORM.getDB().lastInsertRowId; 133 133 return this; 134 134 }, … … 198 198 options.fields[fieldName].modelClass = model; 199 199 } 200 GearsORM.extend(model,this);201 GearsORM.extend(model,new GearsORM.Events);200 JsORM.extend(model,this); 201 JsORM.extend(model,new JsORM.Events); 202 202 203 203 for(var i in model.prototype)//add events to save,remove,refresh 204 204 if(i.charAt(0) != "_")//wrap only public functions 205 model.prototype[i] = GearsORM.Events.wrapFunction(model.prototype[i],i.charAt(0).toUpperCase()+i.substring(1),model);205 model.prototype[i] = JsORM.Events.wrapFunction(model.prototype[i],i.charAt(0).toUpperCase()+i.substring(1),model); 206 206 207 model.constructor = GearsORM.Model;207 model.constructor = JsORM.Model; 208 208 //register the model 209 GearsORM._models[options.name] = model;209 JsORM._models[options.name] = model; 210 210 return model; 211 211 }; 212 212 /* 213 class methods of GearsORM.Model213 class methods of JsORM.Model 214 214 this reffer to the model class 215 215 */ 216 GearsORM.Model.prototype =216 JsORM.Model.prototype = 217 217 { 218 218 /* … … 232 232 count:function(whereExpression,params) 233 233 { 234 var rs = GearsORM.execute(GearsORM.Sql.selectCount(this.options.name,whereExpression),params);234 var rs = JsORM.execute(JsORM.Sql.selectCount(this.options.name,whereExpression),params); 235 235 var count = rs.fieldByName("c"); 236 236 rs.close(); … … 253 253 select:function(whereExpression,params) 254 254 { 255 return new GearsORM.ResultIterator(GearsORM.execute(GearsORM.Sql.selectWithForigenKeys(this,whereExpression),params),this);255 return new JsORM.ResultIterator(JsORM.execute(JsORM.Sql.selectWithForigenKeys(this,whereExpression),params),this); 256 256 }, 257 257 /* … … 267 267 remove:function(whereExpression,params) 268 268 { 269 GearsORM.executeAndClose(GearsORM.Sql.deleteFromTableWhere(this.options.name,whereExpression),params);269 JsORM.executeAndClose(JsORM.Sql.deleteFromTableWhere(this.options.name,whereExpression),params); 270 270 }, 271 271 /* … … 276 276 createTable:function() 277 277 { 278 GearsORM.Transaction(function(){279 GearsORM.executeAndClose(GearsORM.Sql.createTableByModelClass(this));278 JsORM.Transaction(function(){ 279 JsORM.executeAndClose(JsORM.Sql.createTableByModelClass(this)); 280 280 var fields = this.options.fields; 281 281 for(var fieldName in fields) … … 295 295 dropTable:function() 296 296 { 297 GearsORM.Transaction(function(){298 GearsORM.executeAndClose(GearsORM.Sql.dropTableByName(this.options.name));297 JsORM.Transaction(function(){ 298 JsORM.executeAndClose(JsORM.Sql.dropTableByName(this.options.name)); 299 299 this.dropTriggers(); 300 300 },this); … … 317 317 } 318 318 //if one of the tables doesn`t exist don`t create the triggers 319 if(! GearsORM.Introspection.doesTableExist.apply(this,tableNames))319 if(!JsORM.Introspection.doesTableExist.apply(this,tableNames)) 320 320 return; 321 321 }; 322 322 var fields = this.options.fields; 323 323 var name = this.options.name; 324 GearsORM.Transaction(function(){324 JsORM.Transaction(function(){ 325 325 for(var fieldName in fields) 326 326 { … … 329 329 { 330 330 //on insert,check that forigen key is valid since sqlite doesn`t enforce forigenkeys 331 GearsORM.executeAndClose(GearsORM.Sql.createInsertForigenKeyTrigger(name,field.getRelatedClass().options.name,field.options.allowNull));331 JsORM.executeAndClose(JsORM.Sql.createInsertForigenKeyTrigger(name,field.getRelatedClass().options.name,field.options.allowNull)); 332 332 //on update,check that forigen key is valid since sqlite doesn`t enforce forigenkeys 333 GearsORM.executeAndClose(GearsORM.Sql.createUpdateForigenKeyTrigger(name,field.getRelatedClass().options.name,field.options.allowNull));333 JsORM.executeAndClose(JsORM.Sql.createUpdateForigenKeyTrigger(name,field.getRelatedClass().options.name,field.options.allowNull)); 334 334 335 335 if(field.options.onDeleteCascade) 336 336 //on delete from parent table,delete all rows that reference the row that is going to be deleted 337 GearsORM.executeAndClose(GearsORM.Sql.createDeleteCascadeTrigger(name,field.getRelatedClass().options.name));337 JsORM.executeAndClose(JsORM.Sql.createDeleteCascadeTrigger(name,field.getRelatedClass().options.name)); 338 338 else 339 339 //on delete from parent table,if there is a row that reference the row that is going to be deleted throw a exception 340 GearsORM.executeAndClose(GearsORM.Sql.createDeleteRestrictTrigger(name,field.getRelatedClass().options.name));340 JsORM.executeAndClose(JsORM.Sql.createDeleteRestrictTrigger(name,field.getRelatedClass().options.name)); 341 341 } 342 342 } … … 352 352 var fields = this.options.fields; 353 353 var name = this.options.name; 354 GearsORM.Transaction(function(){354 JsORM.Transaction(function(){ 355 355 for(var fieldName in fields) 356 356 { … … 359 359 { 360 360 //INSERT 361 GearsORM.executeAndClose(GearsORM.Sql.dropForigenKeyInsertTrigger(name,field.getRelatedClass().options.name));361 JsORM.executeAndClose(JsORM.Sql.dropForigenKeyInsertTrigger(name,field.getRelatedClass().options.name)); 362 362 //UPDATE 363 GearsORM.executeAndClose(GearsORM.Sql.dropForigenKeyUpdateTrigger(name,field.getRelatedClass().options.name));363 JsORM.executeAndClose(JsORM.Sql.dropForigenKeyUpdateTrigger(name,field.getRelatedClass().options.name)); 364 364 //DELETE 365 GearsORM.executeAndClose(GearsORM.Sql.dropForigenKeyDeleteTrigger(name,field.getRelatedClass().options.name));365 JsORM.executeAndClose(JsORM.Sql.dropForigenKeyDeleteTrigger(name,field.getRelatedClass().options.name)); 366 366 } 367 367 else if(field._m2mModel) … … 398 398 } 399 399 } 400 if(save) GearsORM.Transaction(map);400 if(save)JsORM.Transaction(map); 401 401 else map(); 402 402 return mappedObjects; -
GearsORM/branches/JsORM/JsORM.ResultIterator.js
r412 r413 1 1 /* 2 Script: GearsORM.ResultIterator.js2 Script: JsORM.ResultIterator.js 3 3 Contains the ResultIterator class. 4 4 … … 8 8 9 9 /* 10 Class: GearsORM.ResultIterator10 Class: JsORM.ResultIterator 11 11 a iterator on a Google Gears ResultSet 12 12 … … 15 15 modelClass - the model class used to map the results 16 16 */ 17 GearsORM.ResultIterator = function(result,modelClass)17 JsORM.ResultIterator = function(result,modelClass) 18 18 { 19 19 this.result = result; … … 22 22 }; 23 23
