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">REM =======================================================================================================================
4 REM === The Access2Base library is a part of the LibreOffice project. ===
5 REM === Full documentation is available on http://www.access2base.com ===
6 REM =======================================================================================================================
13 REM -----------------------------------------------------------------------------------------------------------------------
14 REM --- CLASS ROOT FIELDS ---
15 REM -----------------------------------------------------------------------------------------------------------------------
17 Private _Type As String
' Must be TABLEDEF or QUERYDEF
18 Private _Name As String
' For tables: [[Catalog.]Schema.]Table
19 Private _ParentDatabase As Object
20 Private _ReadOnly As Boolean
21 Private Table As Object
' com.sun.star.sdb.dbaccess.ODBTable
22 Private CatalogName As String
23 Private SchemaName As String
24 Private TableName As String
25 Private Query As Object
' com.sun.star.sdb.dbaccess.OQuery
26 Private TableDescriptor As Object
' com.sun.star.sdb.dbaccess.ODBTable
27 Private TableFieldsCount As Integer
28 Private TableKeysCount As Integer
30 REM -----------------------------------------------------------------------------------------------------------------------
31 REM --- CONSTRUCTORS / DESTRUCTORS ---
32 REM -----------------------------------------------------------------------------------------------------------------------
33 Private Sub Class_Initialize()
36 Set _ParentDatabase = Nothing
39 CatalogName =
""
40 SchemaName =
""
41 TableName =
""
43 Set TableDescriptor = Nothing
46 End Sub
' Constructor
48 REM -----------------------------------------------------------------------------------------------------------------------
49 Private Sub Class_Terminate()
50 On Local Error Resume Next
51 Call Class_Initialize()
52 End Sub
' Destructor
54 REM -----------------------------------------------------------------------------------------------------------------------
56 Call Class_Terminate()
57 End Sub
' Explicit destructor
59 REM -----------------------------------------------------------------------------------------------------------------------
60 REM --- CLASS GET/LET/SET PROPERTIES ---
61 REM -----------------------------------------------------------------------------------------------------------------------
63 Property Get Name() As String
64 Name = _PropertyGet(
"Name
")
65 End Property
' Name (get)
67 REM -----------------------------------------------------------------------------------------------------------------------
68 Property Get ObjectType() As String
69 ObjectType = _PropertyGet(
"ObjectType
")
70 End Property
' ObjectType (get)
72 REM -----------------------------------------------------------------------------------------------------------------------
73 Property Get SQL() As Variant
74 SQL = _PropertyGet(
"SQL
")
75 End Property
' SQL (get)
77 Property Let SQL(ByVal pvValue As Variant)
78 Call _PropertySet(
"SQL
", pvValue)
79 End Property
' SQL (set)
81 REM -----------------------------------------------------------------------------------------------------------------------
82 Public Function pType() As Integer
83 pType = _PropertyGet(
"Type
")
84 End Function
' Type (get)
86 REM -----------------------------------------------------------------------------------------------------------------------
87 REM --- CLASS METHODS ---
88 REM -----------------------------------------------------------------------------------------------------------------------
90 Public Function CreateField(ByVal Optional pvFieldName As Variant _
91 , ByVal optional pvType As Variant _
92 , ByVal optional pvSize As Variant _
93 , ByVal optional pvAttributes As variant _
95 'Return a Field object
96 Const cstThisSub =
"TableDef.CreateField
"
97 Utils._SetCalledSub(cstThisSub)
99 If _ErrorHandler() Then On Local Error Goto Error_Function
101 Dim oTable As Object, oNewField As Object, oKeys As Object, oPrimaryKey As Object, oColumn As Object
102 Const cstMaxKeyLength =
30
104 CreateField = Nothing
105 If _ParentDatabase._DbConnect
<> DBCONNECTBASE Then Goto Error_NotApplicable
106 If IsMissing(pvFieldName) Then Call _TraceArguments()
107 If Not Utils._CheckArgument(pvFieldName,
1, vbString) Then Goto Exit_Function
108 If pvFieldName =
"" Then Call _TraceArguments()
109 If IsMissing(pvType) Then Call _TraceArguments()
110 If Not Utils._CheckArgument(pvType,
1, Utils._AddNumeric( _
111 dbInteger, dbLong, dbBigInt, dbFloat, vbSingle, dbDouble _
112 , dbNumeric, dbDecimal, dbText, dbChar, dbMemo _
113 , dbDate, dbTime, dbTimeStamp _
114 , dbBinary, dbVarBinary, dbLongBinary, dbBoolean _
115 )) Then Goto Exit_Function
116 If IsMissing(pvSize) Then pvSize =
0
117 If pvSize
< 0 Then pvSize =
0
118 If Not Utils._CheckArgument(pvSize,
1, Utils._AddNumeric()) Then Goto Exit_Function
119 If IsMissing(pvAttributes) Then pvAttributes =
0
120 If Not Utils._CheckArgument(pvAttributes,
1, Utils._AddNumeric(), Array(
0, dbAutoIncrField)) Then Goto Exit_Function
122 If _Type
<> OBJTABLEDEF Then Goto Error_NotApplicable
123 If IsNull(Table) And IsNull(TableDescriptor) Then Goto Error_NotApplicable
125 If _ReadOnly Then Goto Error_NoUpdate
127 Set oNewField = New Field
131 ._ParentType = OBJTABLEDEF
132 If IsNull(Table) Then Set oTable = TableDescriptor Else Set oTable = Table
133 Set .Column = oTable.Columns.createDataDescriptor()
135 With oNewField.Column
138 Case dbInteger : .Type = com.sun.star.sdbc.DataType.TINYINT
139 Case dbLong : .Type = com.sun.star.sdbc.DataType.INTEGER
140 Case dbBigInt : .Type = com.sun.star.sdbc.DataType.BIGINT
141 Case dbFloat : .Type = com.sun.star.sdbc.DataType.FLOAT
142 Case dbSingle : .Type = com.sun.star.sdbc.DataType.REAL
143 Case dbDouble : .Type = com.sun.star.sdbc.DataType.DOUBLE
144 Case dbNumeric, dbCurrency : .Type = com.sun.star.sdbc.DataType.NUMERIC
145 Case dbDecimal : .Type = com.sun.star.sdbc.DataType.DECIMAL
146 Case dbText : .Type = com.sun.star.sdbc.DataType.CHAR
147 Case dbChar : .Type = com.sun.star.sdbc.DataType.VARCHAR
148 Case dbMemo : .Type = com.sun.star.sdbc.DataType.LONGVARCHAR
149 Case dbDate : .Type = com.sun.star.sdbc.DataType.DATE
150 Case dbTime : .Type = com.sun.star.sdbc.DataType.TIME
151 Case dbTimeStamp : .Type = com.sun.star.sdbc.DataType.TIMESTAMP
152 Case dbBinary : .Type = com.sun.star.sdbc.DataType.BINARY
153 Case dbVarBinary : .Type = com.sun.star.sdbc.DataType.VARBINARY
154 Case dbLongBinary : .Type = com.sun.star.sdbc.DataType.LONGVARBINARY
155 Case dbBoolean : .Type = com.sun.star.sdbc.DataType.BOOLEAN
157 .Precision = Int(pvSize)
158 If pvType = dbNumeric Or pvType = dbDecimal Or pvType = dbCurrency Then .Scale = Int(pvSize *
10) - Int(pvSize) *
10
159 .IsNullable = com.sun.star.sdbc.ColumnValue.NULLABLE
160 If Utils._hasUNOProperty(oNewField.Column,
"CatalogName
") Then .CatalogName = CatalogName
161 If Utils._hasUNOProperty(oNewField.Column,
"SchemaName
") Then .SchemaName = SchemaName
162 If Utils._hasUNOProperty(oNewField.Column,
"TableName
") Then .TableName = TableName
163 If Not IsNull(TableDescriptor) Then TableFieldsCount = TableFieldsCount +
1
164 If pvAttributes = dbAutoIncrField Then
165 If Not IsNull(Table) Then Goto Error_Sequence
' Do not accept adding an AutoValue field when table exists
166 Set oKeys = oTable.Keys
167 Set oPrimaryKey = oKeys.createDataDescriptor()
168 Set oColumn = oPrimaryKey.Columns.createDataDescriptor()
169 oColumn.Name = pvFieldName
170 oColumn.CatalogName = CatalogName
171 oColumn.SchemaName = SchemaName
172 oColumn.TableName = TableName
173 oColumn.IsAutoIncrement = True
174 oColumn.IsNullable = com.sun.star.sdbc.ColumnValue.NO_NULLS
175 oPrimaryKey.Columns.appendByDescriptor(oColumn)
176 oPrimaryKey.Name = Left(
"PK_
" & Join(Split(TableName,
" "),
"_
")
& "_
" & Join(Split(pvFieldName,
" "),
"_
"), cstMaxKeyLength)
177 oPrimaryKey.Type = com.sun.star.sdbcx.KeyType.PRIMARY
178 oKeys.appendByDescriptor(oPrimaryKey)
179 .IsAutoIncrement = True
180 .IsNullable = com.sun.star.sdbc.ColumnValue.NO_NULLS
183 .IsAutoIncrement = False
186 oTable.Columns.appendByDescriptor(oNewfield.Column)
188 Set CreateField = oNewField
191 Utils._ResetCalledSub(cstThisSub)
194 TraceError(TRACEABORT, Err, cstThisSub, Erl)
197 TraceError(TRACEFATAL, ERRMETHOD, Utils._CalledSub(),
0,
1, cstThisSub)
200 TraceError(TRACEFATAL, ERRFIELDCREATION, Utils._CalledSub(),
0,
1, pvFieldName)
203 TraceError(TRACEFATAL, ERRNOTUPDATABLE, Utils._CalledSub(),
0)
205 End Function
' CreateField V1.1
.0
207 REM -----------------------------------------------------------------------------------------------------------------------
208 Public Function Execute(ByVal Optional pvOptions As Variant) As Boolean
209 'Execute a stored query. The query must be an ACTION query.
211 Dim cstThisSub As String
212 cstThisSub = Utils._PCase(_Type)
& ".Execute
"
213 Utils._SetCalledSub(cstThisSub)
214 On Local Error Goto Error_Function
217 If _Type
<> OBJQUERYDEF Then Goto Trace_Method
218 If IsMissing(pvOptions) Then
221 If Not Utils._CheckArgument(pvOptions,
1, Utils._AddNumeric(), dbSQLPassThrough) Then Goto Exit_Function
224 'Check action query
225 Dim oStatement As Object, vResult As Variant
226 Dim iType As Integer, sSql As String
228 If ( (iType And DBQAction) =
0 ) And ( (iType And DBQDDL) =
0 ) Then Goto Trace_Action
230 'Execute action query
231 Set oStatement = _ParentDatabase.Connection.createStatement()
233 If pvOptions = dbSQLPassThrough Then oStatement.EscapeProcessing = False _
234 Else oStatement.EscapeProcessing = Query.EscapeProcessing
235 On Local Error Goto SQL_Error
236 vResult = oStatement.executeUpdate(_ParentDatabase._ReplaceSquareBrackets(sSql))
237 On Local Error Goto Error_Function
242 Utils._ResetCalledSub(cstThisSub)
245 TraceError(TRACEFATAL, ERRMETHOD, cstThisSub,
0, , cstThisSub)
248 TraceError(TRACEFATAL, ERRNOTACTIONQUERY, cstThisSub,
0, , _Name)
251 TraceError(TRACEFATAL, ERRSQLSTATEMENT, Utils._CalledSub(),
0, , sSql)
254 TraceError(TRACEABORT, Err, cstThisSub, Erl)
256 End Function
' Execute V1.1
.0
258 REM -----------------------------------------------------------------------------------------------------------------------
259 Public Function Fields(ByVal Optional pvIndex As variant) As Object
261 If _ErrorHandler() Then On Local Error Goto Error_Function
262 Dim cstThisSub As String
263 cstThisSub = Utils._PCase(_Type)
& ".Fields
"
264 Utils._SetCalledSub(cstThisSub)
267 If Not IsMissing(pvIndex) Then
268 If Not Utils._CheckArgument(pvIndex,
1, Utils._AddNumeric(vbString)) Then Goto Exit_Function
271 Dim sObjects() As String, sObjectName As String, oObject As Object
272 Dim i As Integer, bFound As Boolean, oFields As Object
274 If _Type = OBJTABLEDEF Then Set oFields = Table.getColumns() Else Set oFields = Query.getColumns()
275 sObjects = oFields.ElementNames()
277 Case IsMissing(pvIndex)
278 Set oObject = New Collect
279 oObject._CollType = COLLFIELDS
280 oObject._ParentType = _Type
281 oObject._ParentName = _Name
282 Set oObject._ParentDatabase = _ParentDatabase
283 oObject._Count = UBound(sObjects) +
1
285 Case VarType(pvIndex) = vbString
287 ' Check existence of object and find its exact (case-sensitive) name
288 For i =
0 To UBound(sObjects)
289 If UCase(pvIndex) = UCase(sObjects(i)) Then
290 sObjectName = sObjects(i)
295 If Not bFound Then Goto Trace_NotFound
296 Case Else
' pvIndex is numeric
297 If pvIndex
< 0 Or pvIndex
> UBound(sObjects) Then Goto Trace_IndexError
298 sObjectName = sObjects(pvIndex)
301 Set oObject = New Field
302 oObject._Name = sObjectName
303 Set oObject.Column = oFields.getByName(sObjectName)
304 oObject._ParentName = _Name
305 oObject._ParentType = _Type
306 Set oObject._ParentDatabase = _ParentDatabase
310 Set oObject = Nothing
311 Utils._ResetCalledSub(cstThisSub)
314 TraceError(TRACEABORT, Err, cstThisSub, Erl)
317 TraceError(TRACEFATAL, ERROBJECTNOTFOUND, Utils._CalledSub(),
0, , Array(_GetLabel(
"FIELD
"), pvIndex))
320 TraceError(TRACEFATAL, ERRCOLLECTION, Utils._CalledSub(),
0)
322 End Function
' Fields
324 REM -----------------------------------------------------------------------------------------------------------------------
325 Public Function getProperty(Optional ByVal pvProperty As Variant) As Variant
326 ' Return property value of psProperty property name
328 Dim cstThisSub As String
329 cstThisSub = Utils._PCase(_Type)
& ".getProperty
"
330 Utils._SetCalledSub(cstThisSub)
331 If IsMissing(pvProperty) Then Call _TraceArguments()
332 getProperty = _PropertyGet(pvProperty)
333 Utils._ResetCalledSub(cstThisSub)
335 End Function
' getProperty
337 REM -----------------------------------------------------------------------------------------------------------------------
338 Public Function hasProperty(ByVal Optional pvProperty As Variant) As Boolean
339 ' Return True if object has a valid property called pvProperty (case-insensitive comparison !)
341 Dim cstThisSub As String
342 cstThisSub = Utils._PCase(_Type)
& ".hasProperty
"
343 Utils._SetCalledSub(cstThisSub)
344 If IsMissing(pvProperty) Then hasProperty = PropertiesGet._hasProperty(_Type, _PropertiesList()) Else hasProperty = PropertiesGet._hasProperty(_Type, _PropertiesList(), pvProperty)
345 Utils._ResetCalledSub(cstThisSub)
348 End Function
' hasProperty
350 REM -----------------------------------------------------------------------------------------------------------------------
351 Public Function OpenRecordset(ByVal Optional pvType As Variant, ByVal Optional pvOptions As Variant, ByVal Optional pvLockEdit As Variant) As Object
352 'Return a Recordset object based on current table- or querydef object
354 Dim cstThisSub As String
355 cstThisSub = Utils._PCase(_Type)
& ".OpenRecordset
"
356 Utils._SetCalledSub(cstThisSub)
358 Dim lCommandType As Long, sCommand As String, oObject As Object,bPassThrough As Boolean
360 Set oObject = Nothing
361 If IsMissing(pvType) Then
364 If Not Utils._CheckArgument(pvType,
1, Utils._AddNumeric(), dbOpenForwardOnly) Then Goto Exit_Function
366 If IsMissing(pvOptions) Then
369 If Not Utils._CheckArgument(pvOptions,
2, Utils._AddNumeric(), dbSQLPassThrough) Then Goto Exit_Function
371 If IsMissing(pvLockEdit) Then
374 If Not Utils._CheckArgument(pvLockEdit,
3, Utils._AddNumeric(), dbReadOnly) Then Goto Exit_Function
379 lCommandType = com.sun.star.sdb.CommandType.TABLE
382 lCommandType = com.sun.star.sdb.CommandType.QUERY
384 If pvOptions = dbSQLPassThrough Then bPassThrough = True Else bPassThrough = Not Query.EscapeProcessing
387 Set oObject = New Recordset
389 ._CommandType = lCommandType
393 ._ForwardOnly = ( pvType = dbOpenForwardOnly )
394 ._PassThrough = bPassThrough
395 ._ReadOnly = ( (pvLockEdit = dbReadOnly) Or _ReadOnly )
396 Set ._ParentDatabase = _ParentDatabase
401 .RecordsetMax = .RecordsetMax +
1
402 oObject._Name = Format(.RecordsetMax,
"0000000")
403 .RecordsetsColl.Add(oObject, UCase(oObject._Name))
406 If Not ( oObject._BOF And oObject._EOF ) Then oObject.MoveFirst()
' Do nothing if resultset empty
409 Set OpenRecordset = oObject
410 Set oObject = Nothing
411 Utils._ResetCalledSub(cstThisSub)
414 TraceError(TRACEABORT, Err, cstThisSub, Erl)
415 Set oObject = Nothing
417 End Function
' OpenRecordset V1.1
.0
419 REM -----------------------------------------------------------------------------------------------------------------------
420 Public Function Properties(ByVal Optional pvIndex As Variant) As Variant
422 ' a Collection object if pvIndex absent
423 ' a Property object otherwise
425 Dim vProperty As Variant, vPropertiesList() As Variant, sObject As String
426 Dim cstThisSub As String
427 cstThisSub = Utils._PCase(_Type)
& ".Properties
"
428 Utils._SetCalledSub(cstThisSub)
429 vPropertiesList = _PropertiesList()
430 sObject = Utils._PCase(_Type)
431 If IsMissing(pvIndex) Then
432 vProperty = PropertiesGet._Properties(sObject, _Name, vPropertiesList)
434 vProperty = PropertiesGet._Properties(sObject, _Name, vPropertiesList, pvIndex)
435 vProperty._Value = _PropertyGet(vPropertiesList(pvIndex))
437 Set vProperty._ParentDatabase = _ParentDatabase
440 Set Properties = vProperty
441 Utils._ResetCalledSub(cstThisSub)
443 End Function
' Properties
445 REM -----------------------------------------------------------------------------------------------------------------------
446 Public Function setProperty(ByVal Optional psProperty As String, ByVal Optional pvValue As Variant) As Boolean
447 ' Return True if property setting OK
448 Dim cstThisSub As String
449 cstThisSub = Utils._PCase(_Type)
& ".getProperty
"
450 Utils._SetCalledSub(cstThisSub)
451 setProperty = _PropertySet(psProperty, pvValue)
452 Utils._ResetCalledSub(cstThisSub)
455 REM -----------------------------------------------------------------------------------------------------------------------
456 REM --- PRIVATE FUNCTIONS ---
457 REM -----------------------------------------------------------------------------------------------------------------------
459 REM -----------------------------------------------------------------------------------------------------------------------
460 Private Function _PropertiesList() As Variant
464 _PropertiesList = Array(
"Name
",
"ObjectType
")
466 _PropertiesList = Array(
"Name
",
"ObjectType
",
"SQL
",
"Type
")
470 End Function
' _PropertiesList
472 REM -----------------------------------------------------------------------------------------------------------------------
473 Private Function _PropertyGet(ByVal psProperty As String) As Variant
474 ' Return property value of the psProperty property name
476 If _ErrorHandler() Then On Local Error Goto Error_Function
477 Dim cstThisSub As String
478 cstThisSub = Utils._PCase(_Type)
479 Utils._SetCalledSub(cstThisSub
& ".get
" & psProperty)
480 Dim vEMPTY As Variant, sSql As String, sVerb As String, iType As Integer
481 _PropertyGet = vEMPTY
482 If Not hasProperty(psProperty) Then Goto Trace_Error
484 Select Case UCase(psProperty)
485 Case UCase(
"Name
")
487 Case UCase(
"ObjectType
")
489 Case UCase(
"SQL
")
490 _PropertyGet = Query.Command
491 Case UCase(
"Type
")
493 sSql = Trim(UCase(Query.Command))
494 sVerb = Split(sSql,
" ")(
0)
495 If sVerb =
"SELECT
" Then iType = iType + dbQSelect
496 If sVerb =
"SELECT
" And InStr(sSql,
" INTO
")
> 0 Then iType = iType + dbQMakeTable
497 If sVerb =
"SELECT
" And InStr(sSql,
" UNION
")
> 0 Then iType = iType + dbQSetOperation
498 If Not Query.EscapeProcessing Then iType = iType + dbQSQLPassThrough
499 If sVerb =
"INSERT
" Then iType = iType + dbQAppend
500 If sVerb =
"DELETE
" Then iType = iType + dbQDelete
501 If sVerb =
"UPDATE
" Then iType = iType + dbQUpdate
502 If sVerb =
"CREATE
" _
503 Or sVerb =
"ALTER
" _
504 Or sVerb =
"DROP
" _
505 Or sVerb =
"RENAME
" _
506 Or sVerb =
"TRUNCATE
" _
507 Then iType = iType + dbQDDL
508 ' dbQAction implied by dbQMakeTable, dbQAppend, dbQDelete and dbQUpdate
509 ' To check Type use: If (iType And dbQxxx)
<> 0 Then ...
516 Utils._ResetCalledSub(cstThisSub
& ".get
" & psProperty)
519 TraceError(TRACEWARNING, ERRPROPERTY, Utils._CalledSub(),
0, , psProperty)
520 _PropertyGet = vEMPTY
523 TraceError(TRACEABORT, Err, cstThisSub
& "._PropertyGet
", Erl)
524 _PropertyGet = vEMPTY
526 End Function
' _PropertyGet
528 REM -----------------------------------------------------------------------------------------------------------------------
529 Private Function _PropertySet(ByVal psProperty As String, ByVal pvValue As Variant) As Boolean
530 ' Return True if property setting OK
532 If _ErrorHandler() Then On Local Error Goto Error_Function
533 Dim cstThisSub As String
534 cstThisSub = Utils._PCase(_Type)
535 Utils._SetCalledSub(cstThisSub
& ".set
" & psProperty)
538 Dim iArgNr As Integer
541 Select Case UCase(_A2B_.CalledSub)
542 Case UCase(
"setProperty
") : iArgNr =
3
543 Case UCase(cstThisSub
& ".setProperty
") : iArgNr =
2
544 Case UCase(cstThisSub
& ".set
" & psProperty) : iArgNr =
1
547 If Not hasProperty(psProperty) Then Goto Trace_Error
549 If _ReadOnly Then Goto Error_NoUpdate
551 Select Case UCase(psProperty)
552 Case UCase(
"SQL
")
553 If Not Utils._CheckArgument(pvValue, iArgNr, vbString, , False) Then Goto Trace_Error_Value
554 Query.Command = pvValue
560 Utils._ResetCalledSub(cstThisSub
& ".set
" & psProperty)
563 TraceError(TRACEFATAL, ERRPROPERTY, Utils._CalledSub(),
0, , psProperty)
567 TraceError(TRACEFATAL, ERRPROPERTYVALUE, Utils._CalledSub(),
0,
1, Array(pvValue, psProperty))
571 TraceError(TRACEFATAL, ERRNOTUPDATABLE, Utils._CalledSub(),
0)
574 TraceError(TRACEABORT, Err, cstThisSub
& "._PropertySet
", Erl)
577 End Function
' _PropertySet