First, what is the chain-type operation
Return the object of the next action that needs to be done in the previous step. Make certain functionality complete and sustainable.
Second, the advantages of chain-operated operation
The code is more streamlined and elegant. Chain operation can greatly reduce the amount of code, a number of operations a line of code one go, done;
Chained operation scenarios In addition to the front-end jquery Operation Dom, back-end web framework, in the development of ORM-related frameworks are often used
Three, a variety of language chain operation to achieve the following based on a simple implementation of database query similar implementation of some web framework orm. Query the various demos of the class
1, JavaScript chain operation we are most familiar with, but the entire framework of jquery is the chain operation implementation, I wrote here the demo is not consistent with the idea of jquery implementation, jquery concrete implementation of the way can look at the jquery source code.
varLink =function(table) { This. sql = { "Table": "", "WHERE": "1=1", "Limit": 0, "Field": "" } This. sql.table =table;} Link.prototype={where:function(where) { This. Sql.where + = "and" +where; return This; }, Field:function(field) { This. Sql.field =field; return This; }, Top:function(limit) { This. Sql.limit =limit; return This; }, query:function () { varSQLSTR = "Select" + This. sql.field+ "from" + This. Sql.table+ "WHERE" + This. sql.where+ "LIMIT 0," + This. Sql.limit; returnsqlstr; } }vardb =NewLink ("User");var$val = Db.where ("id = 1"). Top (10). Field ("ID, username, password"). query (); Console.log ($val);
2, C # Implementation chain, mainly through its. NET Framework provides extension methods, the code is as follows
usingSystem;namespaceconsoleapplication1{ Public classSqlhelp { PublicSqlhelp (stringtable) { This. Table =table; } Public string where=""; Public stringfield =""; Public intLimit =0; Public stringTable =""; } Static classlinkextended { Public StaticSqlhelp Where ( ThisSqlhelp H,string where) {H.where+=" and"+where; returnh; } Public StaticSqlhelp Top ( ThisSqlhelp H,intlimit) {H.limit=limit; returnh; } Public StaticSqlhelp Field ( ThisSqlhelp H,stringfield) {H.field=field; returnh; } Public Static stringQuery ( Thissqlhelp h) { stringsql =string. Format ("SELECT {0} from {1} WHERE 1=1 {2} LIMIT 0, {3}", H.field, H.table, H.where, H.limit); returnSQL; } } classProgram {Static voidMain (string[] args) {Sqlhelp db=NewSqlhelp ("User"); stringsql = db. Top (Ten) . Field ("ID, username, password") . Where ("ID =1") . Query (); Console.WriteLine (SQL); Console.ReadLine (); } }}
3, PHP implementation chain operation can use the Magic method of PHP _call. But I am here to achieve or to start with the idea of returning to the object itself is not used to _call.
<?PHPclassLink {Static Public $_garr=Array( ' table ' = ' = ', ' Limit ' =>0, ' where ' = ' 1=1 ', ' field ' = ' ); function__construct ($table) { self::$_garr[' table '] =$table ; } Public functionwhere$where= ' ') { self::$_garr[' WHERE '].= ' and '.$where ; return $this; } Public functionField$field) { self::$_garr[' field '] =$field ; return $this; } Public functionTop$top) { self::$_garr[' limit '] =$top; return $this; } Public functionquery () {$sql _arr= Self::$_garr; $sql= "SELECT {$sql _arr[' Field ']} from {$sql _arr[' Table ']} WHERE {$sql _arr[' WHERE ']} LIMIT 0, {$sql _arr[' Limit ']} "; return $sql; }};$db=NewLink ("User");$sql=$db->top (10) ->field ("ID, username, password") ->where ("id = 1") -query ();Print($sql);?>
4, ASP (VBScript) This I have been searching for information on the internet has not been written, I think I wrote here is the whole network of the first (of course, I can not find it, hehe so narcissistic), because VBScript itself is very weak class, but
It also provides easy-to-use features such as Defalut and me, so it is not difficult to implement a chain (if a friend of the skilled VBScript can think of with. End with language in fact, the language itself a little chain-like flavor, but the idea is not called chain-type operation)
ClassLink'declaring a global dictionary DimSqld Public Default functionconstruct (table)SetSqld =CreateObject("Scripting.Dictionary") PagerSqld.add ("Table", table)Setconstruct =Me End function Public Functionwhere (WHERESTR)PagerSqld.add ("where", Wherestr)SetWhere =Me End Function Public FunctionTop (limit)PagerSqld.add ("Limit", limit)Settop =Me End Function Public Functionfield (FIELDSTR)PagerSqld.add ("Field", Fieldstr)Setfield =Me End Function Public Functionquery ()DimSQL SQL="SELECT"& Sqld.item ("Field") &" from"&sqld.item ("Table") &"Wehre"&sqld.item ("where") Query=SQLEnd functionEnd ClassDimdb, SQLSetdb = (NewLink) ("User") SQL= Db.top (Ten). Field ("ID, username, password"). Where ("id = 1"). Query ()MsgBoxSql
5, Python implementation chain is very convenient and simple
classLink:def __init__(self, table): Self.table=Tabledefwhere (self, where): Self.where=' and'+wherereturnSelf ; deffield (Self, field): Self.field=Fieldreturn SelfdefTop (self, top): Self.limit=TopreturnSelf ; defquery (self): SQL='SELECT'+self.field+' from'+self.table+'WHERE 1=1'+ self.where+'Limit 0,'+Self.limitreturnSQLP= Link ('User') SQL= P.top (str). Field ('ID, username, password'). WHERE ('id=1'). Query ()Print(SQL)
Iv. other needs to be continued
Echosong in the yard farming career, temporarily only use the above language, so at present also only realized the above language chain operation, welcome everybody to supplement other language chain-operated demo
Implementation of chained operations in Javascript, C #, PHP, ASP, Python, and other languages