Webpy: Web. database with SQLite

Source: Internet
Author: User
Tags rowcount

In the whole framework of webpy, I think the most unreasonable and most failed is the encapsulation of this web. database.
As far as I understand it, webpy's database encapsulation should not be at the Django or sqlalchemy level, at least it should be consistent with the interface, but what does our webpy look like? For example, initialize a database:
For SQLite:

 
DB = web. Database (DBN ="SQLite",
DB ="./DB. SQLite")

However, PostgreSQL looks like this:

 DB = web. database (DBN =  '  Postgres   ', 
host = " 127.0.0.1 " ,
Port = 5432,
User = " S " ,
Password = " Postgres " ,
database = " test " )

of course, SQLite can also be passed as database = "./DB. SQLite", but can we just save some of our features? Does this violate the Zen that python has always advocated? There shoshould be one -- and preferably only one -- obvious way to do it .??
for the uniformity of excuses, let's not talk about it. Let's talk about another disgusting thing in Web. Database, sqlitedb!
for most Programs , the common Code may look like this:

User = dB. Select ('Auth_user',
Where ='User_login = $ Login',
Vars = {'Login':"Admin"}
)
If NotLen (User ):
ReturnNone

HoweverSqlitedbThe program will say that the user's _ Len _ method does not exist. view the webpy source code, DB. the type of the object returned by select () is iterbetter. the database DB: Query () method has clearly stated _ Len _. The Code is as follows:

IfDb_cursor.description:
Names = [x [0]ForXInDb_cursor.description]
DefIterwrapper ():
Row = db_cursor.fetchone ()
WhileRow:
YieldStorage (dict (ZIP (names, row )))
Row = db_cursor.fetchone ()
Out = iterbetter (iterwrapper ())
Out._ Len __=Lambda: Int (db_cursor.rowcount)
Out. List =Lambda: [Storage (dict (ZIP (names, x )))\
ForXInDb_cursor.fetchall ()]

However, in sqlitedb: Query (),DB: Query ()The method process is overloaded and the Len () method is deleted. The Code is as follows:

DefQuery (self, * a, ** kW ):
Out = dB. Query (self, * a, ** kW)
IfIsinstance (Out, iterbetter ):
DelOut._ Len __
ReturnOut

The reason may be that the author considers that db_cursor.rowcount of SQLite during query has always returned-1 ,__ Len _ () =-1, which is meaningless to the application.
However, the consequence is that if the developer wants to use the web. database is compatible with several databases. For example, if both SQLite and PostgreSQL are supported, Len (User) operations cannot be used becauseIterbetterThe LEN () method exists,SqlitedbReturnsIterbetterHowever, there is no Len () method. In this way, unless the program shows to distinguish Postgres from SQLite, or use the following work und:

 
User = user. List ()
If NotLen (User ):
ReturnNone

Just for a small Len operator, is it so much a bend?
In fact, it is very easy to correct this issue in webpy, but it seems that there is no correction trend yet. At least 0.35 and 0.36 have not corrected this issue.
If developers do not need to consider the general issues of the database, do not use webpy web. database.Web. DatabaseThere is no practical value. If you want to consider compatibility between databases, you can use sqlalchemy

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.