Thursday, 26 December 2013

Class in Javascript

3 ways to define a JavaScript class

September 29th, 2006. Tagged: JavaScript

Introduction

JavaScript is a very flexible object-oriented language when it comes to syntax. In this article you can find three ways of defining and instantiating an object. Even if you have already picked your favorite way of doing it, it helps to know some alternatives in order to read other people's code.
It's important to note that there are no classes in JavaScript. Functions can be used to somewhat simulate classes, but in general JavaScript is a class-less language. Everything is an object. And when it comes to inheritance, objects inherit from objects, not classes from classes as in the "class"-ical languages.

1. Using a function

This is probably one of the most common ways. You define a normal JavaScript function and then create an object by using the new keyword. To define properties and methods for an object created using function(), you use the this keyword, as seen in the following example.
function Apple (type) {
    this.type = type;
    this.color = "red";
    this.getInfo = getAppleInfo;
}
 
// anti-pattern! keep reading...
function getAppleInfo() {
    return this.color + ' ' + this.type + ' apple';
}
To instantiate an object using the Apple constructor function, set some properties and call methods you can do the following:
var apple = new Apple('macintosh');
apple.color = "reddish";
alert(apple.getInfo());

1.1. Methods defined internally

In the example above you see that the method getInfo() of the Apple "class" was defined in a separate function getAppleInfo(). While this works fine, it has one drawback – you may end up defining a lot of these functions and they are all in the "global namespece". This means you may have naming conflicts if you (or another library you are using) decide to create another function with the same name. The way to prevent pollution of the global namespace, you can define your methods within the constructor function, like this:
function Apple (type) {
    this.type = type;
    this.color = "red";
    this.getInfo = function() {
        return this.color + ' ' + this.type + ' apple';
    };
}
Using this syntax changes nothing in the way you instantiate the object and use its properties and methods.

1.2. Methods added to the prototype

A drawback of 1.1. is that the method getInfo() is recreated every time you create a new object. Sometimes that may be what you want, but it's rare. A more inexpensive way is to add getInfo() to the prototype of the constructor function.
function Apple (type) {
    this.type = type;
    this.color = "red";
}
 
Apple.prototype.getInfo = function() {
    return this.color + ' ' + this.type + ' apple';
};
Again, you can use the new objects exactly the same way as in 1. and 1.1.

2. Using object literals

Literals are shorter way to define objects and arrays in JavaScript. To create an empty object using you can do:
var o = {};
instead of the "normal" way:
var o = new Object();
For arrays you can do:
var a = [];
instead of:
var a = new Array();
So you can skip the class-like stuff and create an instance (object) immediately. Here's the same functionality as described in the previous examples, but using object literal syntax this time:
var apple = {
    type: "macintosh",
    color: "red",
    getInfo: function () {
        return this.color + ' ' + this.type + ' apple';
    }
}
In this case you don't need to (and cannot) create an instance of the class, it already exists. So you simply start using this instance.
apple.color = "reddish";
alert(apple.getInfo());
Such an object is also sometimes called singleton. It "classical" languages such as Java, singleton means that you can have only one single instance of this class at any time, you cannot create more objects of the same class. In JavaScript (no classes, remember?) this concept makes no sense anymore since all objects are singletons to begin with.

3. Singleton using a function

Again with the singleton, eh? :)
The third way presented in this article is a combination of the other two you already saw. You can use a function to define a singleton object. Here's the syntax:
var apple = new function() {
    this.type = "macintosh";
    this.color = "red";
    this.getInfo = function () {
        return this.color + ' ' + this.type + ' apple';
    };
}
So you see that this is very similar to 1.1. discussed above, but the way to use the object is exactly like in 2.
apple.color = "reddish";
alert(apple.getInfo());
new function(){...} does two things at the same time: define a function (an anonymous constructor function) and invoke it with new. It might look a bit confusing if you're not used to it and it's not too common, but hey, it's an option, when you really want a constructor function that you'll use only once and there's no sense of giving it a name.

Tuesday, 24 December 2013

Views in Sql server

Different Types of SQL Server Views

Posted By : Shailendra Chauhan, 10 Mar 2011
Updated On : 18 Dec 2012
  Version Support : SQL Server 2005,2008,2012
Keywords : Types of Sql View pdf, Simple View, Complex View, Sql Server View, Modify View, Advantage of Views, Use of Views
Views are virtual tables that are compiled at run time. The data associated with views are not physically stored in the view, but it is stored in the base tables of the view. A view can be made over one or more database tables. Generally we put those columns in view that we need to retrieve/query again and again. Once you have created the view, you can query view like as table. We can make index, trigger on view.
In Sql Server we make views for security purpose since it restricts the user to view some columns/fields of the table(s). Views show only those columns that are present in the query which is used to make view.One more advantage of Views is, data abstraction since the end user is not aware of all the data present in database table.
Syntax for View
  1. CREATE VIEW view_name
  2. AS
  3. select_statement[]

Types of Views

In Sql Server we have two types of views.
  1. System Defined Views

    System defined Views are predefined Views that already exist in the Master database of Sql Server. These are also used as template Views for all newly created databases. These system Views will be automatically attached to any user defined database.

    We have following types of system defined views.
    1. Information Schema View

      In Sql Server we have twenty different schema views. These are used to display information of a database, like as tables and columns. This type of view starts with INFORMATION_SCHEMA and after this view name.
      1. --Create a table
      2. create table Employee_Test
      3. (
      4. Emp_ID int identity,
      5. Emp_Name varchar(55),
      6. Emp_Technology varchar(55),
      7. Emp_Sal decimal (10,2),
      8. Emp_Designation varchar(20)
      9. )
      10. --To view detailed information of the columns of table Employee_Test
      11. SELECT * FROM INFORMATION_SCHEMA.COLUMNS
      12. where TABLE_NAME='Employee_Test'
    2. Catalog View

      Catalog Views were introduced with SQL Server 2005. These are used to show database self describing information.
      1. select * from sys.tables
    3. Dynamic Management View

      Dynamic Management Views were introduced in SQL Server 2005. These Views give the administrator information of the database about the current state of the SQL Server machine. These values help the administrator to analyze problems and tune the server for optimal performance. These are of two types
      1. Server-scoped Dynamic Management View

        These are stored only in the Master database.
      2. Database-scoped Dynamic Management View

        These are stored in each database.
      1. --To see all SQL Server connections
      2. SELECT connection_id,session_id,client_net_address,auth_scheme
      3. FROM sys.dm_exec_connections
  2. User Defined Views

    These types of view are defined by users. We have two types of user defined views.
    1. Simple View

      When we create a view on a single table, it is called simple view.
      1. --Now Insert data to table Employee_Test
      2. Insert into Employee_Test values ('Amit','PHP',12000,'SE');
      3. Insert into Employee_Test values ('Mohan','ASP.NET',15000,'TL');
      4. Insert into Employee_Test values ('Avin','C#',14000,'SE');
      5. Insert into Employee_Test values ('Manoj','JAVA',22000,'SSE');
      6. Insert into Employee_Test values ('Riyaz','VB',18000,'TH');
      7. -- Now create view on single table Employee_Test
      8. create VIEW vw_Employee_Test
      9. AS
      10. Select Emp_ID ,Emp_Name ,Emp_Designation
      11. From Employee_Test
      1. -- Query view like as table
      2. Select * from vw_Employee_Test
      In simple view we can insert, update, delete data. We can only insert data in simple view if we have primary key and all not null fields in the view.
      1. -- Insert data to view vw_Employee_Test
      2. insert into vw_Employee_Test(Emp_Name, Emp_Designation) values ('Shailu','SSE')
      3. -- Now see the affected view
      4. Select * from vw_Employee_Test
      1. -- Update data to view vw_Employee_Test
      2. Update vw_Employee_Test set Emp_Name = 'Pawan' where Emp_ID = 6
      3. -- Now see the affected view
      4. Select * from vw_Employee_Test
      1. -- Delete data from view vw_Employee_Test
      2. delete from vw_Employee_Test where Emp_ID = 6
      3. -- Now see the affected view
      4. Select * from vw_Employee_Test
    2. Complex View

      When we create a view on more than one table, it is called complex view.
      1. --Create another table
      2. create table Personal_Info
      3. (
      4. Emp_Name varchar(55),
      5. FName varchar(55),
      6. DOB varchar(55),
      7. Address varchar(55),
      8. Mobile int,
      9. State varchar(55)
      10. )
      11. -- Now Insert data
      12. Insert into Personal_Info values ('G.Chaudary','22-10-1985','Ghaziabad',96548922,'UP');
      13. Insert into Personal_Info values ('B.S.Chauhan','02-07-1986','Haridwar',96548200,'UK');
      14. Insert into Personal_Info values ('A.Panwar','30-04-1987','Noida',97437821,'UP');
      15. Insert into Personal_Info values ('H.C.Patak','20-07-1986','Rampur',80109747,'UP');
      16. Insert into Personal_Info values ('M.Shekh','21-10-1985','Delhi',96547954,'Delhi');
      17. -- Now create view on two tables Employee_Test and Personal_Info
      18. Create VIEW vw_Employee_Personal_Info
      19. As
      20. Select e.Emp_ID, e.Emp_Name,e.Emp_Designation,p.DOB,p.Mobile
      21. From Employee_Test e INNER JOIN Personal_Info p
      22. On e.Emp_Name = p. Emp_Name
      1. -- Now Query view like as table
      2. Select * from vw_Employee_Personal_Info
      We can only update data in complex view.We can't insert,update data in complex view.
      1. --Update view
      2. update vw_Employee_Personal_Info set Emp_Designation = 'SSE' where Emp_ID = 3
      3. -- See affected view
      4. Select * from vw_Employee_Personal_Info

Note

  1. We make views for security purpose since it restricts the user to view some columns/fields of the table(s).
  2. One more advantage of Views is, data abstraction since the end user is not aware of all the data present in database table
Summary
In this article I try to explain the view in sql server with example. I hope after reading this article your sql views concepts will be strong. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.