• Embed Doc
  • Readcast
  • Collections
  • 1
    CommentGo Back
Download
 
Domain Specific Languages with SQL and Table oriented programming*Suman Karumuri*Contents* Introductiono SQLo DSLo TOP* The Combination* Employee Payrollo Requirementso Datao Designo Tax Calculationo Acid Test* The Summaryo Advantageso Disadvantages* Conclusion* BibliographyIntroductionThe ever increasing complexity of present day software systems has madedesigning a robust and flexible system a difficult job .Unfortunately,there are no guidelines or frameworks that would ensure that the currentdesign would result in a robust and flexible system to meet future needs.But Domain Specific Languages(DSL's) do provide some promise though weare yet to see some practical languages on that front.As futuresolutions do not solve present day problems, i will describe a techniquefor data processing applications which form a major chunk ofapplications today. In this article i will describe how we can use SQLand a technique called Table Oriented Programming (TOP) to build a"Domain specific language for programmers" for any domain.Before jumping into the details, we define the terms SQL, TOP andDSL.This will be followed by an example of a Payroll system which willbe followed by the pros and cons of using this approach.SQLSQL stands for Structured Query Language[1<dsl_top_sql.html#postgresql:doc>] which is the defacto language foraccessing databases.SQL inherently is declarative and procedural. Butthe wrappers around it like PL/SQL, pl/pgsql which come with variousdatabases make imperative programing possible. These languages usuallyprovide conditionals, loops and variables thus making them Turing complete.
 
For example, consider the statement Select emp_name from Employees wherenet_pay $>$ '50000'.This query selects all the employees whose netsalary is greater then $50,000.DSLThe acronym DSL stands for Domain Specific language.DSL's arespecialised languages which are designed for a specific applications.For example lex,yacc,latex are domain specific languages used forgenerating lexers, parsers and formatted documents respectively. Theadvantages of a DSL are:* DSL's heavily aid in improving programmer productivity.* DSL's are closer to the domain and usually are declarative innature, hence using them results in bug-free code.On the other hand DSL's also come with their unique set of problems.* Code generated from DSL's is very difficult to understand andmostly impossible to debug.* DSL's usually are used for applications which have a standard wayof doing things as in lex and yacc. But since real worldapplications tend to change a lot over time a designing a DSL forthem is very difficult.* DSL's usually are difficult to extend to adapt for changingrequirements which discourages their adopters .* Unless very carefully designed,DSL's cannot be tuned for optimalperformance as it requires exposing their internals which defeatstheir purpose.In a real world situation, the cost of building a DSL usually outweightheir advantages even for large applications. Hence conventionalprogramming is preferred over DSL's.In this article, we will see how aDSL can be made out of SQL using a technique called "Table orientedprogramming"(TOP).But before we start please keep these points in mind:* The term DSL is used here loosely. We are talking about DSL forprogrammers working in a domain. This is not a DSL which wouldpass the elbow test.* For brevity and clarity, i will only use the basic features ofPostgreSQL in this article.* A Payroll processing system with a limited feature set is chosenas an example to suit the article and audience.TOPTable Oriented Programming is a technique of designing systems in whichall the rules(also called business rules) are present in a table(here adatabase table).These rules are also referred as soft coding. In shortTOP is about saving rules and processes in databases in addition to thedata.For example, take the scenario of a bank where the processing of thetransaction depends on the type of the account.Consider the case, wherein we have a we have a single function which performs the processing fora certain type of account. Conventionally, the mapping of the account
 
type and function name is usually written in a switch-case statement inlogic. In TOP, these mappings exist in a database table. Now if we wantto add a new type of account and a new type of transaction processing wesimply add another row to this table and we are done without touchingany of the old logic. But if this logic was hardcoded in the program,any change would require a lot of testing of the logic. This has otheradvantages like instant access to the mapping instead of wading throughcomplex code which implements this mapping. This also requires lesstesting since only the soft coding is changed leaving the code untouched.The CombinationBefore going further let me remind you that the kind of DSL we arewriting here is a DSL aimed at programmers who have some knowledge ofthe domain in which the system will be used. These forms of DSL's willmake the development very faster once the programmer understands whatneeds to be done.Though SQL and TOP are independent things, their true utility and magiccomes to the fore only when both are used. For example, in anapplication that manages employee payroll, the tax calculations can bedone in a program when the rules are present in the database tables. Butin the following example i will show how the combination can yield anice DSL for processing employee payroll.Employee PayrollRequirementsThe HR Department of SmartWork, a rapidly growing software company, hasdecided to automate the manual process of payroll processing to improvethe operational efficiency .The requirements are as follows:* The pay package of each employee has three components: BasicSalary, House rent allowance(HRA) and Bonus.There can also be somedeductions that will be made from the employees account forvarious reasons.* The Employees of SmartWork depending on their designation areclassified into 3 levels. While joining the employees are offereda net package which is distributed among the 3 components.* Since SmartWork is a reputed company the employees come fromvarious states in the country. Each state levies a "Living Tax"and an "Income Tax".The percentages of the taxes can vary acrossstates.* The Deductions are calculated monthly and are fed into the systemmanually before the taxes are calculated. None of the deductionssave any taxes.* Income Tax is a percent of the income earned by the employee.HRAdoesn't constitute income. The percentage varies from state to state.* Living Tax is a percentage of the HRA of the employee decided byindividual state.Data
of 00

Leave a Comment

You must be to leave a comment.
Submit
Characters: ...

this kind of article is great and gives me more idea on this. on this blog gives more knowledge. thanks so much on this.

You must be to leave a comment.
Submit
Characters: ...