1 <?xml version=
"1.0" encoding=
"UTF-8"?>
2 <!DOCTYPE script:module PUBLIC
"-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
3 <script:module xmlns:
script=
"http://openoffice.org/2000/script" script:
name=
"DataDef" script:
language=
"StarBasic">
4 REM =======================================================================================================================
5 REM === The Access2Base library is a part of the LibreOffice project. ===
6 REM === Full documentation is available on http://www.access2base.com ===
7 REM =======================================================================================================================
14 REM -----------------------------------------------------------------------------------------------------------------------
15 REM --- CLASS ROOT FIELDS ---
16 REM -----------------------------------------------------------------------------------------------------------------------
18 Private _Type As String
' Must be TABLEDEF or QUERYDEF
19 Private _This As Object
' Workaround for absence of This builtin function
20 Private _Parent As Object
21 Private _Name As String
' For tables: [[Catalog.]Schema.]Table
22 Private _ParentDatabase As Object
23 Private _ReadOnly As Boolean
24 Private Table As Object
' com.sun.star.sdb.dbaccess.ODBTable
25 Private CatalogName As String
26 Private SchemaName As String
27 Private TableName As String
28 Private Query As Object
' com.sun.star.sdb.dbaccess.OQuery
29 Private TableDescriptor As Object
' com.sun.star.sdb.dbaccess.ODBTable
30 Private TableFieldsCount As Integer
31 Private TableKeysCount As Integer
33 REM -----------------------------------------------------------------------------------------------------------------------
34 REM --- CONSTRUCTORS / DESTRUCTORS ---
35 REM -----------------------------------------------------------------------------------------------------------------------
36 Private Sub Class_Initialize()
41 Set _ParentDatabase = Nothing
44 CatalogName =
""
45 SchemaName =
""
46 TableName =
""
48 Set TableDescriptor = Nothing
51 End Sub
' Constructor
53 REM -----------------------------------------------------------------------------------------------------------------------
54 Private Sub Class_Terminate()
55 On Local Error Resume Next
56 Call Class_Initialize()
57 End Sub
' Destructor
59 REM -----------------------------------------------------------------------------------------------------------------------
61 Call Class_Terminate()
62 End Sub
' Explicit destructor
64 REM -----------------------------------------------------------------------------------------------------------------------
65 REM --- CLASS GET/LET/SET PROPERTIES ---
66 REM -----------------------------------------------------------------------------------------------------------------------
68 Property Get Name() As String
69 Name = _PropertyGet(
"Name
")
70 End Property
' Name (get)
72 REM -----------------------------------------------------------------------------------------------------------------------
73 Property Get ObjectType() As String
74 ObjectType = _PropertyGet(
"ObjectType
")
75 End Property
' ObjectType (get)
77 REM -----------------------------------------------------------------------------------------------------------------------
78 Property Get SQL() As Variant
79 SQL = _PropertyGet(
"SQL
")
80 End Property
' SQL (get)
82 Property Let SQL(ByVal pvValue As Variant)
83 Call _PropertySet(
"SQL
", pvValue)
84 End Property
' SQL (set)
86 REM -----------------------------------------------------------------------------------------------------------------------
87 Public Function pType() As Integer
88 pType = _PropertyGet(
"Type
")
89 End Function
' Type (get)
91 REM -----------------------------------------------------------------------------------------------------------------------
92 REM --- CLASS METHODS ---
93 REM -----------------------------------------------------------------------------------------------------------------------
95 Public Function CreateField(ByVal Optional pvFieldName As Variant _
96 , ByVal optional pvType As Variant _
97 , ByVal optional pvSize As Variant _
98 , ByVal optional pvAttributes As Variant _
100 'Return a Field object
101 Const cstThisSub =
"TableDef.CreateField
"
102 Utils._SetCalledSub(cstThisSub)
104 If _ErrorHandler() Then On Local Error Goto Error_Function
106 Dim oTable As Object, oNewField As Object, oKeys As Object, oPrimaryKey As Object, oColumn As Object
107 Const cstMaxKeyLength =
30
109 CreateField = Nothing
110 If _ParentDatabase._DbConnect
<> DBCONNECTBASE Then Goto Error_NotApplicable
111 If IsMissing(pvFieldName) Then Call _TraceArguments()
112 If Not Utils._CheckArgument(pvFieldName,
1, vbString) Then Goto Exit_Function
113 If pvFieldName =
"" Then Call _TraceArguments()
114 If IsMissing(pvType) Then Call _TraceArguments()
115 If Not Utils._CheckArgument(pvType,
1, Utils._AddNumeric( _
116 dbInteger, dbLong, dbBigInt, dbFloat, vbSingle, dbDouble _
117 , dbNumeric, dbDecimal, dbText, dbChar, dbMemo _
118 , dbDate, dbTime, dbTimeStamp _
119 , dbBinary, dbVarBinary, dbLongBinary, dbBoolean _
120 )) Then Goto Exit_Function
121 If IsMissing(pvSize) Then pvSize =
0
122 If pvSize
< 0 Then pvSize =
0
123 If Not Utils._CheckArgument(pvSize,
1, Utils._AddNumeric()) Then Goto Exit_Function
124 If IsMissing(pvAttributes) Then pvAttributes =
0
125 If Not Utils._CheckArgument(pvAttributes,
1, Utils._AddNumeric(), Array(
0, dbAutoIncrField)) Then Goto Exit_Function
127 If _Type
<> OBJTABLEDEF Then Goto Error_NotApplicable
128 If IsNull(Table) And IsNull(TableDescriptor) Then Goto Error_NotApplicable
130 If _ReadOnly Then Goto Error_NoUpdate
132 Set oNewField = New Field
137 ._ParentType = OBJTABLEDEF
138 If IsNull(Table) Then Set oTable = TableDescriptor Else Set oTable = Table
139 Set .Column = oTable.Columns.createDataDescriptor()
141 With oNewField.Column
144 Case dbInteger : .Type = com.sun.star.sdbc.DataType.TINYINT
145 Case dbLong : .Type = com.sun.star.sdbc.DataType.INTEGER
146 Case dbBigInt : .Type = com.sun.star.sdbc.DataType.BIGINT
147 Case dbFloat : .Type = com.sun.star.sdbc.DataType.FLOAT
148 Case dbSingle : .Type = com.sun.star.sdbc.DataType.REAL
149 Case dbDouble : .Type = com.sun.star.sdbc.DataType.DOUBLE
150 Case dbNumeric, dbCurrency : .Type = com.sun.star.sdbc.DataType.NUMERIC
151 Case dbDecimal : .Type = com.sun.star.sdbc.DataType.DECIMAL
152 Case dbText : .Type = com.sun.star.sdbc.DataType.CHAR
153 Case dbChar : .Type = com.sun.star.sdbc.DataType.VARCHAR
154 Case dbMemo : .Type = com.sun.star.sdbc.DataType.LONGVARCHAR
155 Case dbDate : .Type = com.sun.star.sdbc.DataType.DATE
156 Case dbTime : .Type = com.sun.star.sdbc.DataType.TIME
157 Case dbTimeStamp : .Type = com.sun.star.sdbc.DataType.TIMESTAMP
158 Case dbBinary : .Type = com.sun.star.sdbc.DataType.BINARY
159 Case dbVarBinary : .Type = com.sun.star.sdbc.DataType.VARBINARY
160 Case dbLongBinary : .Type = com.sun.star.sdbc.DataType.LONGVARBINARY
161 Case dbBoolean : .Type = com.sun.star.sdbc.DataType.BOOLEAN
163 .Precision = Int(pvSize)
164 If pvType = dbNumeric Or pvType = dbDecimal Or pvType = dbCurrency Then .Scale = Int(pvSize *
10) - Int(pvSize) *
10
165 .IsNullable = com.sun.star.sdbc.ColumnValue.NULLABLE
166 If Utils._hasUNOProperty(oNewField.Column,
"CatalogName
") Then .CatalogName = CatalogName
167 If Utils._hasUNOProperty(oNewField.Column,
"SchemaName
") Then .SchemaName = SchemaName
168 If Utils._hasUNOProperty(oNewField.Column,
"TableName
") Then .TableName = TableName
169 If Not IsNull(TableDescriptor) Then TableFieldsCount = TableFieldsCount +
1
170 If pvAttributes = dbAutoIncrField Then
171 If Not IsNull(Table) Then Goto Error_Sequence
' Do not accept adding an AutoValue field when table exists
172 Set oKeys = oTable.Keys
173 Set oPrimaryKey = oKeys.createDataDescriptor()
174 Set oColumn = oPrimaryKey.Columns.createDataDescriptor()
175 oColumn.Name = pvFieldName
176 oColumn.CatalogName = CatalogName
177 oColumn.SchemaName = SchemaName
178 oColumn.TableName = TableName
179 oColumn.IsAutoIncrement = True
180 oColumn.IsNullable = com.sun.star.sdbc.ColumnValue.NO_NULLS
181 oPrimaryKey.Columns.appendByDescriptor(oColumn)
182 oPrimaryKey.Name = Left(
"PK_
" & Join(Split(TableName,
" "),
"_
")
& "_
" & Join(Split(pvFieldName,
" "),
"_
"), cstMaxKeyLength)
183 oPrimaryKey.Type = com.sun.star.sdbcx.KeyType.PRIMARY
184 oKeys.appendByDescriptor(oPrimaryKey)
185 .IsAutoIncrement = True
186 .IsNullable = com.sun.star.sdbc.ColumnValue.NO_NULLS
189 .IsAutoIncrement = False
192 oTable.Columns.appendByDescriptor(oNewfield.Column)
194 Set CreateField = oNewField
197 Utils._ResetCalledSub(cstThisSub)
200 TraceError(TRACEABORT, Err, cstThisSub, Erl)
203 TraceError(TRACEFATAL, ERRMETHOD, Utils._CalledSub(),
0,
1, cstThisSub)
206 TraceError(TRACEFATAL, ERRFIELDCREATION, Utils._CalledSub(),
0,
1, pvFieldName)
209 TraceError(TRACEFATAL, ERRNOTUPDATABLE, Utils._CalledSub(),
0)
211 End Function
' CreateField V1.1
.0
213 REM -----------------------------------------------------------------------------------------------------------------------
214 Public Function Execute(ByVal Optional pvOptions As Variant) As Boolean
215 'Execute a stored query. The query must be an ACTION query.
217 Dim cstThisSub As String
218 cstThisSub = Utils._PCase(_Type)
& ".Execute
"
219 Utils._SetCalledSub(cstThisSub)
220 On Local Error Goto Error_Function
223 If _Type
<> OBJQUERYDEF Then Goto Trace_Method
224 If IsMissing(pvOptions) Then
227 If Not Utils._CheckArgument(pvOptions,
1, Utils._AddNumeric(), dbSQLPassThrough) Then Goto Exit_Function
230 'Check action query
231 Dim oStatement As Object, vResult As Variant
232 Dim iType As Integer, sSql As String
234 If ( (iType And DBQAction) =
0 ) And ( (iType And DBQDDL) =
0 ) Then Goto Trace_Action
236 'Execute action query
237 Set oStatement = _ParentDatabase.Connection.createStatement()
239 If pvOptions = dbSQLPassThrough Then oStatement.EscapeProcessing = False _
240 Else oStatement.EscapeProcessing = Query.EscapeProcessing
241 On Local Error Goto SQL_Error
242 vResult = oStatement.executeUpdate(_ParentDatabase._ReplaceSquareBrackets(sSql))
243 On Local Error Goto Error_Function
248 Utils._ResetCalledSub(cstThisSub)
251 TraceError(TRACEFATAL, ERRMETHOD, cstThisSub,
0, , cstThisSub)
254 TraceError(TRACEFATAL, ERRNOTACTIONQUERY, cstThisSub,
0, , _Name)
257 TraceError(TRACEFATAL, ERRSQLSTATEMENT, Utils._CalledSub(),
0, , sSql)
260 TraceError(TRACEABORT, Err, cstThisSub, Erl)
262 End Function
' Execute V1.1
.0
264 REM -----------------------------------------------------------------------------------------------------------------------
265 Public Function Fields(ByVal Optional pvIndex As Variant) As Object
267 If _ErrorHandler() Then On Local Error Goto Error_Function
268 Dim cstThisSub As String
269 cstThisSub = Utils._PCase(_Type)
& ".Fields
"
270 Utils._SetCalledSub(cstThisSub)
273 If Not IsMissing(pvIndex) Then
274 If Not Utils._CheckArgument(pvIndex,
1, Utils._AddNumeric(vbString)) Then Goto Exit_Function
277 Dim sObjects() As String, sObjectName As String, oObject As Object
278 Dim i As Integer, bFound As Boolean, oFields As Object
280 If _Type = OBJTABLEDEF Then Set oFields = Table.getColumns() Else Set oFields = Query.getColumns()
281 sObjects = oFields.ElementNames()
283 Case IsMissing(pvIndex)
284 Set oObject = New Collect
285 Set oObject._This = oObject
286 oObject._CollType = COLLFIELDS
287 Set oObject._Parent = _This
288 oObject._Count = UBound(sObjects) +
1
290 Case VarType(pvIndex) = vbString
292 ' Check existence of object and find its exact (case-sensitive) name
293 For i =
0 To UBound(sObjects)
294 If UCase(pvIndex) = UCase(sObjects(i)) Then
295 sObjectName = sObjects(i)
300 If Not bFound Then Goto Trace_NotFound
301 Case Else
' pvIndex is numeric
302 If pvIndex
< 0 Or pvIndex
> UBound(sObjects) Then Goto Trace_IndexError
303 sObjectName = sObjects(pvIndex)
306 Set oObject = New Field
307 Set oObject._This = oObject
308 oObject._Name = sObjectName
309 Set oObject.Column = oFields.getByName(sObjectName)
310 oObject._ParentName = _Name
311 oObject._ParentType = _Type
312 Set oObject._ParentDatabase = _ParentDatabase
316 Set oObject = Nothing
317 Utils._ResetCalledSub(cstThisSub)
320 TraceError(TRACEABORT, Err, cstThisSub, Erl)
323 TraceError(TRACEFATAL, ERROBJECTNOTFOUND, Utils._CalledSub(),
0, , Array(_GetLabel(
"FIELD
"), pvIndex))
326 TraceError(TRACEFATAL, ERRCOLLECTION, Utils._CalledSub(),
0)
328 End Function
' Fields
330 REM -----------------------------------------------------------------------------------------------------------------------
331 Public Function getProperty(Optional ByVal pvProperty As Variant) As Variant
332 ' Return property value of psProperty property name
334 Dim cstThisSub As String
335 cstThisSub = Utils._PCase(_Type)
& ".getProperty
"
336 Utils._SetCalledSub(cstThisSub)
337 If IsMissing(pvProperty) Then Call _TraceArguments()
338 getProperty = _PropertyGet(pvProperty)
339 Utils._ResetCalledSub(cstThisSub)
341 End Function
' getProperty
343 REM -----------------------------------------------------------------------------------------------------------------------
344 Public Function hasProperty(ByVal Optional pvProperty As Variant) As Boolean
345 ' Return True if object has a valid property called pvProperty (case-insensitive comparison !)
347 Dim cstThisSub As String
348 cstThisSub = Utils._PCase(_Type)
& ".hasProperty
"
349 Utils._SetCalledSub(cstThisSub)
350 If IsMissing(pvProperty) Then hasProperty = PropertiesGet._hasProperty(_Type, _PropertiesList()) Else hasProperty = PropertiesGet._hasProperty(_Type, _PropertiesList(), pvProperty)
351 Utils._ResetCalledSub(cstThisSub)
354 End Function
' hasProperty
356 REM -----------------------------------------------------------------------------------------------------------------------
357 Public Function OpenRecordset(ByVal Optional pvType As Variant, ByVal Optional pvOptions As Variant, ByVal Optional pvLockEdit As Variant) As Object
358 'Return a Recordset object based on current table- or querydef object
360 Dim cstThisSub As String
361 cstThisSub = Utils._PCase(_Type)
& ".OpenRecordset
"
362 Utils._SetCalledSub(cstThisSub)
364 Dim lCommandType As Long, sCommand As String, oObject As Object,bPassThrough As Boolean
365 Dim iType As Integer, iOptions As Integer, iLockEdit As Integer
368 Set oObject = Nothing
369 If VarType(pvType) = vbError Then
371 ElseIf IsMissing(pvType) Then
374 If Not Utils._CheckArgument(pvType,
1, Utils._AddNumeric(), Array(cstNull, dbOpenForwardOnly)) Then Goto Exit_Function
377 If VarType(pvOptions) = vbError Then
379 ElseIf IsMissing(pvOptions) Then
382 If Not Utils._CheckArgument(pvOptions,
2, Utils._AddNumeric(), Array(cstNull, dbSQLPassThrough)) Then Goto Exit_Function
385 If VarType(pvLockEdit) = vbError Then
387 ElseIf IsMissing(pvLockEdit) Then
390 If Not Utils._CheckArgument(pvLockEdit,
3, Utils._AddNumeric(), Array(cstNull, dbReadOnly)) Then Goto Exit_Function
391 iLockEdit = pvLockEdit
396 lCommandType = com.sun.star.sdb.CommandType.TABLE
399 lCommandType = com.sun.star.sdb.CommandType.QUERY
401 If iOptions = dbSQLPassThrough Then bPassThrough = True Else bPassThrough = Not Query.EscapeProcessing
404 Set oObject = New Recordset
406 ._CommandType = lCommandType
410 ._ForwardOnly = ( iType = dbOpenForwardOnly )
411 ._PassThrough = bPassThrough
412 ._ReadOnly = ( (iLockEdit = dbReadOnly) Or _ReadOnly )
413 Set ._ParentDatabase = _ParentDatabase
418 .RecordsetMax = .RecordsetMax +
1
419 oObject._Name = Format(.RecordsetMax,
"0000000")
420 .RecordsetsColl.Add(oObject, UCase(oObject._Name))
423 If Not ( oObject._BOF And oObject._EOF ) Then oObject.MoveFirst()
' Do nothing if resultset empty
426 Set OpenRecordset = oObject
427 Set oObject = Nothing
428 Utils._ResetCalledSub(cstThisSub)
431 TraceError(TRACEABORT, Err, cstThisSub, Erl)
432 Set oObject = Nothing
434 End Function
' OpenRecordset V1.1
.0
436 REM -----------------------------------------------------------------------------------------------------------------------
437 Public Function Properties(ByVal Optional pvIndex As Variant) As Variant
439 ' a Collection object if pvIndex absent
440 ' a Property object otherwise
442 Dim vProperty As Variant, vPropertiesList() As Variant, sObject As String
443 Dim cstThisSub As String
444 cstThisSub = Utils._PCase(_Type)
& ".Properties
"
445 Utils._SetCalledSub(cstThisSub)
446 vPropertiesList = _PropertiesList()
447 sObject = Utils._PCase(_Type)
448 If IsMissing(pvIndex) Then
449 vProperty = PropertiesGet._Properties(sObject, _This, vPropertiesList)
451 vProperty = PropertiesGet._Properties(sObject, _This, vPropertiesList, pvIndex)
452 vProperty._Value = _PropertyGet(vPropertiesList(pvIndex))
454 Set vProperty._ParentDatabase = _ParentDatabase
457 Set Properties = vProperty
458 Utils._ResetCalledSub(cstThisSub)
460 End Function
' Properties
462 REM -----------------------------------------------------------------------------------------------------------------------
463 Public Function setProperty(ByVal Optional psProperty As String, ByVal Optional pvValue As Variant) As Boolean
464 ' Return True if property setting OK
465 Dim cstThisSub As String
466 cstThisSub = Utils._PCase(_Type)
& ".setProperty
"
467 Utils._SetCalledSub(cstThisSub)
468 setProperty = _PropertySet(psProperty, pvValue)
469 Utils._ResetCalledSub(cstThisSub)
472 REM -----------------------------------------------------------------------------------------------------------------------
473 REM --- PRIVATE FUNCTIONS ---
474 REM -----------------------------------------------------------------------------------------------------------------------
476 REM -----------------------------------------------------------------------------------------------------------------------
477 Private Function _PropertiesList() As Variant
481 _PropertiesList = Array(
"Name
",
"ObjectType
")
483 _PropertiesList = Array(
"Name
",
"ObjectType
",
"SQL
",
"Type
")
487 End Function
' _PropertiesList
489 REM -----------------------------------------------------------------------------------------------------------------------
490 Private Function _PropertyGet(ByVal psProperty As String) As Variant
491 ' Return property value of the psProperty property name
493 If _ErrorHandler() Then On Local Error Goto Error_Function
494 Dim cstThisSub As String
495 cstThisSub = Utils._PCase(_Type)
496 Utils._SetCalledSub(cstThisSub
& ".get
" & psProperty)
497 Dim sSql As String, sVerb As String, iType As Integer
499 If Not hasProperty(psProperty) Then Goto Trace_Error
501 Select Case UCase(psProperty)
502 Case UCase(
"Name
")
504 Case UCase(
"ObjectType
")
506 Case UCase(
"SQL
")
507 _PropertyGet = Query.Command
508 Case UCase(
"Type
")
510 sSql = Utils._Trim(UCase(Query.Command))
511 sVerb = Split(sSql,
" ")(
0)
512 If sVerb =
"SELECT
" Then iType = iType + dbQSelect
513 If sVerb =
"SELECT
" And InStr(sSql,
" INTO
")
> 0 _
514 Or sVerb =
"CREATE
" And InStr(sSql,
" TABLE
")
> 0 _
515 Then iType = iType + dbQMakeTable
516 If sVerb =
"SELECT
" And InStr(sSql,
" UNION
")
> 0 Then iType = iType + dbQSetOperation
517 If Not Query.EscapeProcessing Then iType = iType + dbQSQLPassThrough
518 If sVerb =
"INSERT
" Then iType = iType + dbQAppend
519 If sVerb =
"DELETE
" Then iType = iType + dbQDelete
520 If sVerb =
"UPDATE
" Then iType = iType + dbQUpdate
521 If sVerb =
"CREATE
" _
522 Or sVerb =
"ALTER
" _
523 Or sVerb =
"DROP
" _
524 Or sVerb =
"RENAME
" _
525 Or sVerb =
"TRUNCATE
" _
526 Then iType = iType + dbQDDL
527 ' dbQAction implied by dbQMakeTable, dbQAppend, dbQDelete and dbQUpdate
528 ' To check Type use: If (iType And dbQxxx)
<> 0 Then ...
535 Utils._ResetCalledSub(cstThisSub
& ".get
" & psProperty)
538 TraceError(TRACEWARNING, ERRPROPERTY, Utils._CalledSub(),
0, , psProperty)
542 TraceError(TRACEABORT, Err, cstThisSub
& "._PropertyGet
", Erl)
545 End Function
' _PropertyGet
547 REM -----------------------------------------------------------------------------------------------------------------------
548 Private Function _PropertySet(ByVal psProperty As String, ByVal pvValue As Variant) As Boolean
549 ' Return True if property setting OK
551 If _ErrorHandler() Then On Local Error Goto Error_Function
552 Dim cstThisSub As String
553 cstThisSub = Utils._PCase(_Type)
554 Utils._SetCalledSub(cstThisSub
& ".set
" & psProperty)
557 Dim iArgNr As Integer
560 Select Case UCase(_A2B_.CalledSub)
561 Case UCase(
"setProperty
") : iArgNr =
3
562 Case UCase(cstThisSub
& ".setProperty
") : iArgNr =
2
563 Case UCase(cstThisSub
& ".set
" & psProperty) : iArgNr =
1
566 If Not hasProperty(psProperty) Then Goto Trace_Error
568 If _ReadOnly Then Goto Error_NoUpdate
570 Select Case UCase(psProperty)
571 Case UCase(
"SQL
")
572 If Not Utils._CheckArgument(pvValue, iArgNr, vbString, , False) Then Goto Trace_Error_Value
573 Query.Command = pvValue
579 Utils._ResetCalledSub(cstThisSub
& ".set
" & psProperty)
582 TraceError(TRACEFATAL, ERRPROPERTY, Utils._CalledSub(),
0, , psProperty)
586 TraceError(TRACEFATAL, ERRPROPERTYVALUE, Utils._CalledSub(),
0,
1, Array(pvValue, psProperty))
590 TraceError(TRACEFATAL, ERRNOTUPDATABLE, Utils._CalledSub(),
0)
593 TraceError(TRACEABORT, Err, cstThisSub
& "._PropertySet
", Erl)
596 End Function
' _PropertySet