DatabaseProcApplicationCreatedLinks
sybsystemprocssp_addobjectdef  31 Aug 14Defects Dependencies

1     
2     /* Sccsid = "%Z% generic/sproc/src/%M% %I% %G%" */
3     
4     /*
5     ** Omni only
6     **
7     ** Messages for "sp_addobjectdef"	18299
8     **
9     ** 17240, "'%1!' is not a valid name."
10    ** 18299, "Unknown object type '%1!'."
11    ** 18300, "A server name is not permitted in the local object_name."
12    ** 18301, "Database name '%1!' is not the current database."
13    ** 18302, "User '%1!' is not a valid user in the '%2!' database."
14    ** 18303, "You must be the System Administrator (sa) or Database Owner (dbo) to add a definition for another user's table."
15    ** 18304, "Object '%1!' has already been defined."
16    ** 18305, "Table '%1!' has already been created."
17    ** 18306, "The format for the object_loc parameter is invalid."
18    ** 18307, "Server name '%1!' does not exist in sysservers."
19    ** 18308, "The server name in object_loc must be a remote server. '%1!' is the local server."
20    */
21    create procedure sp_addobjectdef
22        @tablename varchar(1023), /* local table name */
23        @objectdef varchar(1023),
24        /* remote object to which local object will be mapped */
25        @objecttype varchar(10) = "table"
26    /* object type (file, table/view, rpc) */
27    as
28        begin
29    
30            declare @uid int,
31                @server varchar(255),
32                @dbname varchar(255),
33                @owner varchar(255),
34                @object varchar(255),
35                @r_object varchar(255),
36                @retcode int,
37                @type smallint, /* type */
38                @srvclass smallint,
39                @srvid smallint,
40                @dso_class smallint,
41                @attrib smallint,
42                @objectdef_tmp varchar(1023),
43                @maxlen int,
44                @dummy int
45    
46    
47            set nocount on
48    
49            /*
50            ** We cannot add an Omni proxy table to a database used as an IQ catalog
51            ** database.  Check for sysIQ_objects.
52            */
53            select @dbname = db_name()
54            exec @retcode = sp_iqdbcheck @dbname
55            if @retcode = 1
56            begin
57                return 1
58            end
59            select @dbname = ""
60    
61    
62            /*
63            ** Get a value @type from @objecttype
64            */
65            select @type = number from master.dbo.spt_values
66            where type = 'Y' and name = lower(@objecttype)
67    
68            if @@rowcount = 0 or lower(@objecttype) = 'directory'
69            begin
70                /*
71                ** 18299, "Unknown object type '%1!'."
72                */
73                raiserror 18299, @objecttype
74                return (1)
75            end
76    
77            /*
78            ** Check that @tablename refers to object in current database,
79            ** and determine correct user id.
80            */
81            exec @retcode = sp_namecrack @tablename, @server output, @dbname output,
82                @owner output, @object output
83            if @retcode != 0
84            begin
85                /*
86                ** 17240, "'%1!' is not a valid name."
87                */
88                raiserror 17240, @tablename
89                return (1)
90    
91            end
92    
93            if @server is not null
94            begin
95                /*
96                **  18300, "A server name is not permitted in the local object_name." 
97                */
98                raiserror 18300
99                return (1)
100           end
101   
102           if @dbname is not null
103           begin
104               if @dbname != db_name()
105               begin
106                   /*
107                   ** 18301, "Database name '%1!' is not your current database."
108                   */
109                   raiserror 18301, @dbname
110                   return (1)
111               end
112           end
113   
114           if @owner is NULL
115           begin
116               select @uid = user_id()
117           end
118           else
119           begin
120               select @uid = user_id(@owner)
121               if @uid is NULL
122               begin
123                   select @dbname = db_name()
124                   /*
125                   ** 18302, "User '%1!' is not a valid user in the '%2!' database."
126                   */
127                   raiserror 18302, @owner, @dbname
128                   return (1)
129               end
130           end
131   
132           /*
133           ** Only sa or dbo can add a definition for another user's table.
134           */
135           if @uid != user_id()
136           BEGIN
137               /*
138               ** check if user has sa role, proc_role will
139               ** also do auditing if required. proc_role will also
140               ** print error message if required.
141               */
142               if (charindex("sa_role", show_role()) = 0)
143               BEGIN
144                   select @dummy = proc_role("sa_role")
145                   return (1)
146               END
147           END
148   
149           /*
150           ** Check that tabname is valid
151           */
152           if (@object is not null)
153           begin
154               select @maxlen = length from syscolumns
155               where id = object_id("sysobjects") and name = "name"
156   
157               if valid_name(@object, @maxlen) = 0
158               begin
159                   /*
160                   ** 17240, "'%1!' is not a valid name."
161                   */
162                   raiserror 17240, @object
163                   return 1
164               end
165           end
166   
167           /*
168           ** Strip out quotes from table name if quoted identifier is
169           ** on
170           */
171           if @object like '"%"'
172           begin
173               select @object = substring(@object, 2, char_length(@object) - 2)
174           end
175   
176   
177           /*
178           ** See if the object has already been defined.
179           */
180           select @dso_class = 9, @attrib = 1
181   
182           if exists (select * from sysattributes
183                   where class = @dso_class and attribute = @attrib and
184                       object_cinfo = @object and object_info1 = @uid)
185           begin
186               /*
187               ** 18304, "Object '%1!' has already been defined."
188               */
189               raiserror 18304, @object
190               return (1)
191           end
192   
193           /*
194           ** See if the table has already been created.
195           */
196           if @type != 5
197           begin
198               if exists (select * from sysobjects
199                       where type = 'U' and name = @object and uid = @uid)
200               begin
201                   /*
202                   ** 18305, "Table '%1!' has already been created."
203                   */
204                   raiserror 18305, @object
205                   return (1)
206               end
207           end
208   
209           /*
210           ** If we are a table/view or RPC, check table definition accordingly.
211           */
212           if @type = 1 or @type = 3
213           begin
214               execute @retcode = sp_namecrack @objectdef, @server output,
215                   @dbname output, @owner output,
216                   @r_object output
217               if @retcode != 0
218               begin
219                   /*
220                   ** If we are a table or view, see if object specification
221                   ** contains an auxiliary fields:
222                   **
223                   **  	server.db.owner.tbl;auxname
224                   */
225                   if @type = 1
226                   begin
227                       select @objectdef_tmp = substring(@objectdef, 1,
228                               CHARINDEX(";", @objectdef))
229                       execute @retcode = sp_namecrack @objectdef_tmp,
230                           @server output,
231                           @dbname output, @owner output,
232                           @r_object output
233                   end
234                   if @retcode != 0
235                   begin
236                       /*
237                       ** ** 18306, "Format for the object_loc parameter is invalid."
238                       */
239                       raiserror 18306
240                       return (1)
241   
242                   end
243               end
244               select @srvclass = srvclass,
245                   @srvid = srvid
246               from master.dbo.sysservers
247               where srvname = @server
248               if @@rowcount = 0
249               begin
250                   /*
251                   ** 18307, "Server name '%1!' does not exist in sysservers."
252                   */
253                   raiserror 18307, @server
254                   return (1)
255               end
256               else if @srvclass = 0
257               begin
258                   /*
259                   ** 18308, "The server name in object_loc must be a remote server; '%1!' is the local server."
260                   */
261                   raiserror 18308, @server
262                   return (1)
263               end
264           end
265           else if @type = 2 or @type = 4 or @type = 5
266           begin
267               select @srvid = 0
268           end
269   
270           insert into sysattributes
271           (class, attribute, object_type, object_cinfo, object_info1,
272               object_info2, object_info3, char_value)
273           values (@dso_class, @attrib, "OD", @object, @uid, @type, @srvid, @objectdef)
274   
275           return (0)
276       end
277   
278   


exec sp_procxmode 'sp_addobjectdef', 'AnyMode'
go

Grant Execute on sp_addobjectdef to public
go
DEFECTS
 MEST 4 Empty String will be replaced by Single Space 59
 MINU 4 Unique Index with nullable columns sybsystemprocs..sysattributes sybsystemprocs..sysattributes
 MTYP 4 Assignment type mismatch @dbname: varchar(30) = varchar(255) 54
 MTYP 4 Assignment type mismatch @type: smallint = int 65
 MTYP 4 Assignment type mismatch char_value: varchar(768) = varchar(1023) 273
 QPUI 4 Join or Sarg with Un-Rooted Partial Index Use SARG Candidate index: spt_values.spt_valuesclust clustered
(number, type)
Intersection: {type}
66
 TNOU 4 Table with no unique index master..spt_values master..spt_values
 MGTP 3 Grant to public master..spt_values  
 MGTP 3 Grant to public master..sysservers  
 MGTP 3 Grant to public sybsystemprocs..sp_addobjectdef  
 MGTP 3 Grant to public sybsystemprocs..sysattributes  
 MGTP 3 Grant to public sybsystemprocs..syscolumns  
 MGTP 3 Grant to public sybsystemprocs..sysobjects  
 MNER 3 No Error Check should check @@error after insert 270
 MUCO 3 Useless Code Useless Begin-End Pair 28
 MUCO 3 Useless Code Useless Brackets 74
 MUCO 3 Useless Code Useless Brackets 89
 MUCO 3 Useless Code Useless Brackets 99
 MUCO 3 Useless Code Useless Brackets 110
 MUCO 3 Useless Code Useless Brackets 128
 MUCO 3 Useless Code Useless Brackets 142
 MUCO 3 Useless Code Useless Brackets 145
 MUCO 3 Useless Code Useless Brackets 152
 MUCO 3 Useless Code Useless Brackets 190
 MUCO 3 Useless Code Useless Brackets 205
 MUCO 3 Useless Code Useless Brackets 240
 MUCO 3 Useless Code Useless Brackets 254
 MUCO 3 Useless Code Useless Brackets 262
 MUCO 3 Useless Code Useless Brackets 275
 QAFM 3 Var Assignment from potentially many rows 65
 QAFM 3 Var Assignment from potentially many rows 154
 QIWC 3 Insert with not all columns specified missing 7 columns out of 15 271
 QPRI 3 Join or Sarg with Rooted Partial Index Use SARG Candidate index: syscolumns.csyscolumns unique clustered
(id, number, colid)
Intersection: {id}
155
 QPRI 3 Join or Sarg with Rooted Partial Index Use SARG Candidate index: sysattributes.csysattributes unique clustered
(class, attribute, object_type, object, object_info1, object_info2, object_info3, object_cinfo)
Intersection: {object_info1, object_cinfo, attribute, class}
183
 VNRD 3 Variable is not read @dummy 144
 VNRD 3 Variable is not read @dbname 231
 VNRD 3 Variable is not read @owner 231
 VNRD 3 Variable is not read @r_object 232
 MSUB 2 Subquery Marker 182
 MSUB 2 Subquery Marker 198
 MTR1 2 Metrics: Comments Ratio Comments: 41% 21
 MTR2 2 Metrics: Cyclomatic Complexity Cyclo: 22 = 34dec - 14exi + 2 21
 MTR3 2 Metrics: Query Complexity Complexity: 114 21

DEPENDENCIES
PROCS AND TABLES USED
calls proc sybsystemprocs..sp_namecrack  
read_writes table sybsystemprocs..sysattributes  
reads table sybsystemprocs..sysobjects  
reads table master..sysservers (1)  
reads table sybsystemprocs..syscolumns  
calls proc sybsystemprocs..sp_iqdbcheck  
reads table master..spt_values (1)  

CALLERS
called by proc sybsystemprocs..sp_addtabledef