Skip to content

Instantly share code, notes, and snippets.

@mheadd
Last active August 29, 2015 14:25
Show Gist options
  • Save mheadd/b8d0d6d4a5d8d6e30263 to your computer and use it in GitHub Desktop.
Save mheadd/b8d0d6d4a5d8d6e30263 to your computer and use it in GitHub Desktop.
Example of how to use SQL Search API to rename columns

HTTP GET on this:

http://www.civicdata.com/api/action/datastore_search_sql?sql=SELECT "PermitNum", 
"Description", "AppliedDate", "IssuedDate", "CompletedDate", 
"OriginalAddress1", "OriginalCity", "OriginalState", "OriginalZip", "PermitClass",
"PermitClassMapped","StatusCurrent","StatusCurrentMapped","WorkClass","WorkClassMapped",
"PermitType","PermitTypeMapped","PermitTypeDesc", "StatusDate","TotalSqFt","Link",
"EstProjectCost","HousingUnits","PIN","ContractorCompanyName","ContractorTrade",
"ContractorTradeMapped","ContractorLicNum","ContractorStateLic","ContractorFullName",
"ContractorCompanyDesc","ContractorPhone","ContractorAddress1","ContractorAddress2",
"ContractorCity","ContractorState","ContractorZip","ContractorEmail","ProposedUsed",
"AddedSqFt","MasterPermitNum","ExpiresDate","COIssuedDate","Publisher","Fee","LastUpdated",
"LAT" AS "Latitude", "LON" AS "Longitude" FROM "f51d567c-07f7-41d4-842d-c2b165722b4b" 
LIMIT 1

Returns:

{
    "result": {
        "sql": "SELECT \"PermitNum\", \"Description\", \"AppliedDate\", \"IssuedDate\", \"CompletedDate\", \"OriginalAddress1\", \"OriginalCity\", \"OriginalState\", \"OriginalZip\", \"PermitClass\", \"PermitClassMapped\",\"StatusCurrent\",\"StatusCurrentMapped\",\"WorkClass\",\"WorkClassMapped\",\"PermitType\",\"PermitTypeMapped\",\"PermitTypeDesc\" \"StatusDate\",\"TotalSqFt\",\"Link\",\"EstProjectCost\",\"HousingUnits\",\"PIN\",\"ContractorCompanyName\",\"ContractorTrade\",\"ContractorTradeMapped\",\"ContractorLicNum\",\"ContractorStateLic\",\"ContractorFullName\",\"ContractorCompanyDesc\",\"ContractorPhone\",\"ContractorAddress1\",\"ContractorAddress2\",\"ContractorCity\",\"ContractorState\",\"ContractorZip\",\"ContractorEmail\",\"ProposedUsed\",\"AddedSqFt\",\"MasterPermitNum\",\"ExpiresDate\",\"COIssuedDate\",\"Publisher\",\"Fee\",\"LastUpdated\",\"LAT\" AS \"Latitude\", \"LON\" AS \"Longitude\" FROM \"f51d567c-07f7-41d4-842d-c2b165722b4b\" LIMIT 1",
        "records": [
            {
                "LastUpdated": "2015-06-08T10:34:00.000",
                "ContractorAddress1": "3055 CLEARVIEW WAY",
                "ContractorAddress2": "ATTN ZOE STEELE",
                "OriginalZip": "92127",
                "AppliedDate": "2015-06-08",
                "PermitClassMapped": "Residential",
                "ContractorTradeMapped": "",
                "WorkClass": "Existing",
                "StatusCurrentMapped": "Permit Issued",
                "ProposedUsed": "CROSS STREET: 4S Ranch Pkwy \n\nDescription of Work: HRA (Online Permit) ROOF MOUNT RESIDENTIAL SOLAR PV FOR EXISTING SFD (No Meter Upgrade).\n \nPerSection 4620 (i) of the zoning ordinance, roof mounted photovoltaic systems shall not extend more than 5 ft. above the highest point of the existing roof.",
                "PermitClass": "Residential Alteration-Addn",
                "StatusDate": "8002 - REN - (Online) RES Roof Mt Solar PV",
                "Longitude": "-117.11057752450346",
                "ContractorCompanyName": "SOLARCITY CORPORATION",
                "ContractorPhone": "650 963-5630",
                "PermitNum": "PDS2015-RESALT-004851",
                "IssuedDate": "2015-06-08",
                "ContractorEmail": "TFRAHN@SOLARCITY.COM",
                "ContractorTrade": "CSLB Contractor",
                "OriginalState": "CA",
                "OriginalCity": "SAN DIEGO",
                "HousingUnits": "0",
                "ContractorFullName": "Pete R Rive ",
                "Fee": "0",
                "ContractorStateLic": "CA",
                "ContractorCompanyDesc": "",
                "ContractorState": "CA",
                "PermitType": "Residential Alteration or Addition - Plan Check-Permit",
                "MasterPermitNum": "",
                "OriginalAddress1": "10537 RICHARD RD ",
                "Latitude": "33.032628197015022",
                "Description": "CROSS STREET: 4S Ranch Pkwy \r\n\r\nDescription of Work: HRA (Online Permit) ROOF MOUNT RESIDENTIAL SOLAR PV FOR EXISTING SFD (No Meter Upgrade).\r\n \r\nPerSection 4620 (i) of the zoning ordinance, roof mounted photovoltaic systems shall not extend more than 5 ft. above the highest point of the existing roof.",
                "PIN": "",
                "StatusCurrent": "Issued",
                "ExpiresDate": "",
                "COIssuedDate": "",
                "ContractorLicNum": "888104",
                "Link": "https://publicservices.sdcounty.ca.gov/citizenaccess/Cap/CapDetail.aspx?Module=Building&TabName=Building&capID1=15CAP&capID2=00000&capID3=014R0",
                "TotalSqFt": "",
                "EstProjectCost": "",
                "AddedSqFt": "",
                "PermitTypeMapped": "Electrical",
                "CompletedDate": "",
                "ContractorZip": "94402",
                "Publisher": "San Diego - Prod 7.2",
                "WorkClassMapped": "Existing",
                "ContractorCity": "SAN MATEO"
            }
        ],
        "private": false,
        "fields": [
            {
                "id": "PermitNum",
                "type": "text"
            },
            {
                "id": "Description",
                "type": "text"
            },
            {
                "id": "AppliedDate",
                "type": "text"
            },
            {
                "id": "IssuedDate",
                "type": "text"
            },
            {
                "id": "CompletedDate",
                "type": "text"
            },
            {
                "id": "OriginalAddress1",
                "type": "text"
            },
            {
                "id": "OriginalCity",
                "type": "text"
            },
            {
                "id": "OriginalState",
                "type": "text"
            },
            {
                "id": "OriginalZip",
                "type": "text"
            },
            {
                "id": "PermitClass",
                "type": "text"
            },
            {
                "id": "PermitClassMapped",
                "type": "text"
            },
            {
                "id": "StatusCurrent",
                "type": "text"
            },
            {
                "id": "StatusCurrentMapped",
                "type": "text"
            },
            {
                "id": "WorkClass",
                "type": "text"
            },
            {
                "id": "WorkClassMapped",
                "type": "text"
            },
            {
                "id": "PermitType",
                "type": "text"
            },
            {
                "id": "PermitTypeMapped",
                "type": "text"
            },
            {
                "id": "StatusDate",
                "type": "text"
            },
            {
                "id": "TotalSqFt",
                "type": "text"
            },
            {
                "id": "Link",
                "type": "text"
            },
            {
                "id": "EstProjectCost",
                "type": "text"
            },
            {
                "id": "HousingUnits",
                "type": "text"
            },
            {
                "id": "PIN",
                "type": "text"
            },
            {
                "id": "ContractorCompanyName",
                "type": "text"
            },
            {
                "id": "ContractorTrade",
                "type": "text"
            },
            {
                "id": "ContractorTradeMapped",
                "type": "text"
            },
            {
                "id": "ContractorLicNum",
                "type": "text"
            },
            {
                "id": "ContractorStateLic",
                "type": "text"
            },
            {
                "id": "ContractorFullName",
                "type": "text"
            },
            {
                "id": "ContractorCompanyDesc",
                "type": "text"
            },
            {
                "id": "ContractorPhone",
                "type": "text"
            },
            {
                "id": "ContractorAddress1",
                "type": "text"
            },
            {
                "id": "ContractorAddress2",
                "type": "text"
            },
            {
                "id": "ContractorCity",
                "type": "text"
            },
            {
                "id": "ContractorState",
                "type": "text"
            },
            {
                "id": "ContractorZip",
                "type": "text"
            },
            {
                "id": "ContractorEmail",
                "type": "text"
            },
            {
                "id": "ProposedUsed",
                "type": "text"
            },
            {
                "id": "AddedSqFt",
                "type": "text"
            },
            {
                "id": "MasterPermitNum",
                "type": "text"
            },
            {
                "id": "ExpiresDate",
                "type": "text"
            },
            {
                "id": "COIssuedDate",
                "type": "text"
            },
            {
                "id": "Publisher",
                "type": "text"
            },
            {
                "id": "Fee",
                "type": "text"
            },
            {
                "id": "LastUpdated",
                "type": "text"
            },
            {
                "id": "Latitude",
                "type": "text"
            },
            {
                "id": "Longitude",
                "type": "text"
            }
        ],
        "resource_id": "f51d567c-07f7-41d4-842d-c2b165722b4b"
    },
    "success": true,
    "help": "Execute SQL queries on the DataStore.\n\n    The datastore_search_sql action allows a user to search data in a resource\n    or connect multiple resources with join expressions. The underlying SQL\n    engine is the\n    `PostgreSQL engine <http://www.postgresql.org/docs/9.1/interactive/sql/.html>`_.\n    There is an enforced timeout on SQL queries to avoid an unintended DOS.\n    DataStore resource that belong to a private CKAN resource cannot be searched with\n    this action. Use :meth:`~ckanext.datastore.logic.action.datastore_search` instead.\n\n    .. note:: This action is only available when using PostgreSQL 9.X and using a read-only user on the database.\n        It is not available in :ref:`legacy mode<legacy-mode>`.\n\n    :param sql: a single SQL select statement\n    :type sql: string\n\n    **Results:**\n\n    The result of this action is a dictionary with the following keys:\n\n    :rtype: A dictionary with the following keys\n    :param fields: fields/columns and their extra metadata\n    :type fields: list of dictionaries\n    :param records: list of matching results\n    :type records: list of dictionaries\n\n    "
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment