Skip to content

Commit 93e2ceb

Browse files
committed
Added data classification features and examples
1 parent 7b56c7e commit 93e2ceb

3 files changed

Lines changed: 208 additions & 36 deletions

File tree

‎docs/connect/php/pdostatement-getcolumnmeta.md‎

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "PDOStatement::getColumnMeta | Microsoft Docs"
33
ms.custom: ""
4-
ms.date: "01/19/2017"
4+
ms.date: "01/31/2020"
55
ms.prod: sql
66
ms.prod_service: connectivity
77
ms.reviewer: ""
@@ -63,6 +63,68 @@ print $metadata['name'];
6363
?>
6464
```
6565

66+
## Sensitivity Data Classification Metadata
67+
68+
Beginning with version 5.8.0, a new statement attribute `PDO::SQLSRV_ATTR_DATA_CLASSIFICATION` is available for users to access the [sensitivity data classification metadata](https://docs.microsoft.com/sql/relational-databases/security/sql-data-discovery-and-classification?view=sql-server-ver15&tabs=t-sql#subheading-4) in Microsoft SQL Server 2019 using `PDOStatement::getColumnMeta`, which requires Microsoft ODBC Driver 17.4.2 or above.
69+
70+
Note the attribute `PDO::SQLSRV_ATTR_DATA_CLASSIFICATION` is `false` by default, but when set to `true`, the aforementioned array field, `flags`, will be populated with the sensitivity data classification metadata, if exists.
71+
72+
Take a Patients table for example:
73+
74+
```
75+
CREATE TABLE Patients
76+
[PatientId] int identity,
77+
[SSN] char(11),
78+
[FirstName] nvarchar(50),
79+
[LastName] nvarchar(50),
80+
[BirthDate] date)
81+
```
82+
83+
We can classify the SSN and BirthDate columns as shown below:
84+
85+
```
86+
ADD SENSITIVITY CLASSIFICATION TO [Patients].SSN WITH (LABEL = 'Highly Confidential - GDPR', INFORMATION_TYPE = 'Credentials')
87+
ADD SENSITIVITY CLASSIFICATION TO [Patients].BirthDate WITH (LABEL = 'Confidential Personal Data', INFORMATION_TYPE = 'Birthdays')
88+
```
89+
90+
To access the metadata, use `PDOStatement::getColumnMeta` by setting `PDO::SQLSRV_ATTR_DATA_CLASSIFICATION` to true, as shown in the snippet below:
91+
92+
```
93+
$options = array(PDO::SQLSRV_ATTR_DATA_CLASSIFICATION => true);
94+
$tableName = 'Patients';
95+
$tsql = "SELECT * FROM $tableName";
96+
$stmt = $conn->prepare($tsql, $options);
97+
$stmt->execute();
98+
$numCol = $stmt->columnCount();
99+
100+
for ($i = 0; $i < $numCol; $i++) {
101+
$metadata = $stmt->getColumnMeta($i);
102+
$jstr = json_encode($metadata);
103+
echo $jstr . PHP_EOL;
104+
}
105+
```
106+
107+
The output of metadata for all columns is:
108+
109+
```
110+
{"flags":{"Data Classification":[]},"sqlsrv:decl_type":"int identity","native_type":"string","table":"","pdo_type":2,"name":"PatientId","len":10,"precision":0}
111+
{"flags":{"Data Classification":[{"Label":{"name":"Highly Confidential - GDPR","id":""},"Information Type":{"name":"Credentials","id":""}}]},"sqlsrv:decl_type":"char","native_type":"string","table":"","pdo_type":2,"name":"SSN","len":11,"precision":0}
112+
{"flags":{"Data Classification":[]},"sqlsrv:decl_type":"nvarchar","native_type":"string","table":"","pdo_type":2,"name":"FirstName","len":50,"precision":0}
113+
{"flags":{"Data Classification":[]},"sqlsrv:decl_type":"nvarchar","native_type":"string","table":"","pdo_type":2,"name":"LastName","len":50,"precision":0}
114+
{"flags":{"Data Classification":[{"Label":{"name":"Confidential Personal Data","id":""},"Information Type":{"name":"Birthdays","id":""}}]},"sqlsrv:decl_type":"date","native_type":"string","table":"","pdo_type":2,"name":"BirthDate","len":10,"precision":0}
115+
```
116+
117+
If we modify the above snippet by setting `PDO::SQLSRV_ATTR_DATA_CLASSIFICATION` to `false` (the default case), the `flags` field will always be `0` as before, like this:
118+
119+
```
120+
{"flags":0,"sqlsrv:decl_type":"int identity","native_type":"string","table":"","pdo_type":2,"name":"PatientId","len":10,"precision":0}
121+
{"flags":0,"sqlsrv:decl_type":"char","native_type":"string","table":"","pdo_type":2,"name":"SSN","len":11,"precision":0}
122+
{"flags":0,"sqlsrv:decl_type":"nvarchar","native_type":"string","table":"","pdo_type":2,"name":"FirstName","len":50,"precision":0}
123+
{"flags":0,"sqlsrv:decl_type":"nvarchar","native_type":"string","table":"","pdo_type":2,"name":"LastName","len":50,"precision":0}
124+
{"flags":0,"sqlsrv:decl_type":"date","native_type":"string","table":"","pdo_type":2,"name":"BirthDate","len":10,"precision":0}
125+
```
126+
127+
66128
## See Also
67129
[PDOStatement Class](../../connect/php/pdostatement-class.md)
68130

‎docs/connect/php/pdostatement-setattribute.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "PDOStatement::setAttribute | Microsoft Docs"
33
ms.custom: ""
4-
ms.date: "04/22/2019"
4+
ms.date: "01/31/2020"
55
ms.prod: sql
66
ms.prod_service: connectivity
77
ms.reviewer: ""
@@ -36,6 +36,7 @@ The following table contains the list of available attributes:
3636
|Attribute|Values|Description|
3737
|-------------|----------|---------------|
3838
|PDO::SQLSRV_ATTR_CLIENT_BUFFER_MAX_KB_SIZE|1 to the PHP memory limit.|Configures the size of the buffer that holds the result set for a client-side cursor.<br /><br />The default is 10,240 KB (10 MB).<br /><br />For more information about client-side cursors, see [Cursor Types &#40;PDO_SQLSRV Driver&#41;](../../connect/php/cursor-types-pdo-sqlsrv-driver.md).|
39+
|PDO::SQLSRV_ATTR_DATA_CLASSIFICATION|true or false|Specifies whether to retrieve data classification metadata when calling [PDOStatement::getColumnMeta](../../connect/php/pdostatement-getcolumnmeta.md). The default is false.|
3940
|PDO::SQLSRV_ATTR_DECIMAL_PLACES|Integer between 0 and 4 (inclusive)|Specifies the number of decimal places when formatting fetched money values.<br /><br />Any negative integer or value more than 4 will be ignored.<br /><br />This option works only when PDO::SQLSRV_ATTR_FORMAT_DECIMALS is true.<br /><br />This option may also be set at the connection level. If so, then this option overrides the connection level option.<br /><br />For more information, see [Formatting Decimal Strings and Money Values (PDO_SQLSRV Driver)](../../connect/php/formatting-decimals-pdo-sqlsrv-driver.md).|
4041
|PDO::SQLSRV_ATTR_ENCODING|Integer<br /><br />PDO::SQLSRV_ENCODING_UTF8 (Default)<br /><br />PDO::SQLSRV_ENCODING_SYSTEM<br /><br />PDO::SQLSRV_ENCODING_BINARY|Sets the character set encoding to be used by the driver to communicate with the server.|
4142
|PDO::SQLSRV_ATTR_FETCHES_DATETIME_TYPE|true or false|Specifies whether to retrieve date and time types as [PHP DateTime](http://php.net/manual/en/class.datetime.php) objects. If left false, the default behavior is to return them as strings.<br /><br />This option may also be set at the connection level. If so, then this option overrides the connection level option.<br /><br />For more information, see [How to: Retrieve Date and Time Types as PHP DateTime Objects Using the PDO_SQLSRV Driver](../../connect/php/how-to-retrieve-datetime-objects-using-pdo-sqlsrv-driver.md).|

‎docs/connect/php/sqlsrv-field-metadata.md‎

Lines changed: 143 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "sqlsrv_field_metadata | Microsoft Docs"
33
ms.custom: ""
4-
ms.date: "01/19/2017"
4+
ms.date: "01/31/2020"
55
ms.prod: sql
66
ms.prod_service: connectivity
77
ms.reviewer: ""
@@ -69,6 +69,7 @@ The following table gives more information on the keys for each sub-array (see t
6969
|smalldatetime|SQL_TYPE_TIMESTAMP (93)|16/16|0/0||
7070
|smallint|SQL_SMALLINT (5)|||2 bytes|
7171
|Smallmoney|SQL_DECIMAL (3)|10/10|4/4||
72+
|sql_variant|SQL_SS_VARIANT (-150)|||variable|
7273
|text|SQL_LONGVARCHAR (-1)|||2 GB|
7374
|time|SQL_SS_TIME2 (-154)|8/16|0/7||
7475
|timestamp|SQL_BINARY (-2)|||8 bytes|
@@ -86,42 +87,150 @@ The Nullable key can either be yes or no.
8687
## Example
8788
The following example creates a statement resource, then retrieves and displays the field metadata. The example assumes that SQL Server and the [AdventureWorks](https://github.com/Microsoft/sql-server-samples/tree/master/samples/databases/adventure-works) database are installed on the local computer. All output is written to the console when the example is run from the command line.
8889

89-
```
90-
<?php
91-
/* Connect to the local server using Windows Authentication and
92-
specify the AdventureWorks database as the database in use. */
93-
$serverName = "(local)";
94-
$connectionInfo = array( "Database"=>"AdventureWorks");
95-
$conn = sqlsrv_connect( $serverName, $connectionInfo);
96-
if( $conn === false )
97-
{
98-
echo "Could not connect.\n";
99-
die( print_r( sqlsrv_errors(), true));
100-
}
101-
102-
/* Prepare the statement. */
103-
$tsql = "SELECT ReviewerName, Comments FROM Production.ProductReview";
104-
$stmt = sqlsrv_prepare( $conn, $tsql);
105-
106-
/* Get and display field metadata. */
107-
foreach( sqlsrv_field_metadata( $stmt) as $fieldMetadata)
108-
{
109-
foreach( $fieldMetadata as $name => $value)
110-
{
111-
echo "$name: $value\n";
112-
}
113-
echo "\n";
90+
```
91+
<?php
92+
/* Connect to the local server using Windows Authentication and
93+
specify the AdventureWorks database as the database in use. */
94+
$serverName = "(local)";
95+
$connectionInfo = array("Database"=>"AdventureWorks");
96+
$conn = sqlsrv_connect($serverName, $connectionInfo);
97+
if ($conn === false) {
98+
echo "Could not connect.\n";
99+
die( print_r( sqlsrv_errors(), true));
100+
}
101+
102+
/* Prepare the statement. */
103+
$tsql = "SELECT ReviewerName, Comments FROM Production.ProductReview";
104+
$stmt = sqlsrv_prepare( $conn, $tsql);
105+
106+
/* Get and display field metadata. */
107+
foreach( sqlsrv_field_metadata( $stmt) as $fieldMetadata) {
108+
foreach( $fieldMetadata as $name => $value) {
109+
echo "$name: $value\n";
110+
}
111+
echo "\n";
114112
}
115113
116-
/* Note: sqlsrv_field_metadata can be called on any statement
117-
resource, pre- or post-execution. */
118-
119-
/* Free statement and connection resources. */
120-
sqlsrv_free_stmt( $stmt);
121-
sqlsrv_close( $conn);
122-
?>
123-
```
114+
/* Note: sqlsrv_field_metadata can be called on any statement
115+
resource, pre- or post-execution. */
124116
117+
/* Free statement and connection resources. */
118+
sqlsrv_free_stmt($stmt);
119+
sqlsrv_close($conn);
120+
?>
121+
```
122+
123+
## Sensitivity Data Classification Metadata
124+
125+
A new option `DataClassification` is introduced in version 5.8.0 for users to access the [sensitivity data classification metadata](https://docs.microsoft.com/sql/relational-databases/security/sql-data-discovery-and-classification?view=sql-server-ver15&tabs=t-sql#subheading-4) in Microsoft SQL Server 2019 using `sqlsrv_field_metadata`, which requires Microsoft ODBC Driver 17.4.2 or above.
126+
127+
By default, the option `DataClassification` is `false`, but when set to `true`, the array returned by `sqlsrv_field_metadata` will be populated with the sensitivity data classification metadata, if exists.
128+
129+
Take a Patients table for example:
130+
131+
```
132+
CREATE TABLE Patients
133+
[PatientId] int identity,
134+
[SSN] char(11),
135+
[FirstName] nvarchar(50),
136+
[LastName] nvarchar(50),
137+
[BirthDate] date)
138+
```
139+
140+
We can classify the SSN and BirthDate columns as shown below:
141+
142+
```
143+
ADD SENSITIVITY CLASSIFICATION TO [Patients].SSN WITH (LABEL = 'Highly Confidential - GDPR', INFORMATION_TYPE = 'Credentials')
144+
ADD SENSITIVITY CLASSIFICATION TO [Patients].BirthDate WITH (LABEL = 'Confidential Personal Data', INFORMATION_TYPE = 'Birthdays')
145+
```
146+
147+
To access the metadata, invoke `sqlsrv_field_metadata` as shown in the snippet below:
148+
149+
```
150+
$tableName = 'Patients';
151+
$tsql = "SELECT * FROM $tableName";
152+
$stmt = sqlsrv_prepare($conn, $tsql, array(), array('DataClassification' => true));
153+
if (sqlsrv_execute($stmt)) {
154+
$fieldmeta = sqlsrv_field_metadata($stmt);
155+
156+
foreach ($fieldmeta as $f) {
157+
if (count($f['Data Classification']) > 0) {
158+
echo $f['Name'] . ": \n";
159+
print_r($f['Data Classification']);
160+
}
161+
}
162+
}
163+
```
164+
165+
The output will be:
166+
167+
```
168+
SSN:
169+
Array
170+
(
171+
[0] => Array
172+
(
173+
[Label] => Array
174+
(
175+
[name] => Highly Confidential - GDPR
176+
[id] =>
177+
)
178+
179+
[Information Type] => Array
180+
(
181+
[name] => Credentials
182+
[id] =>
183+
)
184+
185+
)
186+
187+
)
188+
BirthDate:
189+
Array
190+
(
191+
[0] => Array
192+
(
193+
[Label] => Array
194+
(
195+
[name] => Confidential Personal Data
196+
[id] =>
197+
)
198+
199+
[Information Type] => Array
200+
(
201+
[name] => Birthdays
202+
[id] =>
203+
)
204+
205+
)
206+
207+
)
208+
```
209+
210+
If the user wants to use `sqlsrv_query` instead of `sqlsrv_prepare`, the above snippet can be modified, like this:
211+
212+
```
213+
$tableName = 'Patients';
214+
$tsql = "SELECT * FROM $tableName";
215+
$stmt = sqlsrv_query($conn, $tsql, array(), array('DataClassification' => true));
216+
$fieldmeta = sqlsrv_field_metadata($stmt);
217+
218+
foreach ($fieldmeta as $f) {
219+
$jstr = json_encode($f);
220+
echo $jstr . PHP_EOL;
221+
}
222+
```
223+
224+
As you can see in the JSON representation below, the data classification metadata is shown if associated with the columns:
225+
226+
```
227+
{"Name":"PatientId","Type":4,"Size":null,"Precision":10,"Scale":null,"Nullable":0,"Data Classification":[]}
228+
{"Name":"SSN","Type":1,"Size":11,"Precision":null,"Scale":null,"Nullable":1,"Data Classification":[{"Label":{"name":"Highly Confidential - GDPR","id":""},"Information Type":{"name":"Credentials","id":""}}]}
229+
{"Name":"FirstName","Type":-9,"Size":50,"Precision":null,"Scale":null,"Nullable":1,"Data Classification":[]}
230+
{"Name":"LastName","Type":-9,"Size":50,"Precision":null,"Scale":null,"Nullable":1,"Data Classification":[]}
231+
{"Name":"BirthDate","Type":91,"Size":null,"Precision":10,"Scale":0,"Nullable":1,"Data Classification":[{"Label":{"name":"Confidential Personal Data","id":""},"Information Type":{"name":"Birthdays","id":""}}]}
232+
```
233+
125234
## See Also
126235
[SQLSRV Driver API Reference](../../connect/php/sqlsrv-driver-api-reference.md)
127236

0 commit comments

Comments
 (0)