|
| 1 | +--- |
| 2 | +sidebar_label: 'Fortran' |
| 3 | +keywords: ['Fortran', 'driver', 'language client'] |
| 4 | +slug: /integrations/language-client/fortran |
| 5 | +description: 'The official Fortran client for connecting to ClickHouse.' |
| 6 | +title: 'ClickHouse Fortran driver' |
| 7 | +doc_type: 'guide' |
| 8 | +integration: |
| 9 | + - support_level: 'core' |
| 10 | + - category: 'language_client' |
| 11 | + - website: 'https://github.com/ClickHouse/fortran' |
| 12 | +--- |
| 13 | + |
| 14 | +import Tabs from '@theme/Tabs'; |
| 15 | +import TabItem from '@theme/TabItem'; |
| 16 | + |
| 17 | +# ClickHouse Fortran driver |
| 18 | + |
| 19 | +Key Features: |
| 20 | + |
| 21 | +- Native TCP protocol support for optimal performance |
| 22 | +- Column-oriented data transfer compatible with Fortran arrays |
| 23 | +- Support for modern Fortran standards (Fortran 90/95/2003/2008) |
| 24 | +- Efficient handling of [numerical data types](/sql-reference/data-types/int-int) commonly used in scientific computing |
| 25 | +- TLS/SSL connection support |
| 26 | +- Parameterized query execution |
| 27 | +- Batch insert operations optimized for large datasets |
| 28 | + |
| 29 | +## Installation |
| 30 | + |
| 31 | +```bash |
| 32 | +fpm install clickhouse-fortran |
| 33 | +``` |
| 34 | + |
| 35 | +## Fortran-facts |
| 36 | + |
| 37 | +<Tabs> |
| 38 | + |
| 39 | + <TabItem value="Fact 1" label="Fact 1" default> |
| 40 | +From its creation in 1954 and its commercial release in 1957 as the progenitor of software, Fortran (short for formula translation) became the first computer language standard. |
| 41 | +<br/> |
| 42 | + </TabItem> |
| 43 | + |
| 44 | + <TabItem value="Fact 2" label="Fact 2" default> |
| 45 | +Originally developed for performing high computation mathematical calculations, NASA is currently deploying Fortran in climate model, aerodynamics applications and orbital mechanism projects. |
| 46 | +<br/> |
| 47 | + </TabItem> |
| 48 | + |
| 49 | +</Tabs> |
| 50 | + |
| 51 | +<details> |
| 52 | +<summary>Did you know?</summary> |
| 53 | + |
| 54 | +People use Fortran for many important tasks. |
| 55 | +These include predicting the weather, designing airplanes, and understanding how liquids flow. |
| 56 | +It's also used in computational physics and chemistry. |
| 57 | +Fortran is a top choice for high-performance computing. |
| 58 | +This means it runs programs on the world's fastest supercomputers. |
| 59 | +</details> |
| 60 | + |
| 61 | +## Usage |
| 62 | + |
| 63 | +<VerticalStepper headerLevel="h3"> |
| 64 | + |
| 65 | +### Ordinary codeblock |
| 66 | + |
| 67 | +Create a connection using a connection string with the imaginary code below: |
| 68 | + |
| 69 | +```fortran |
| 70 | +use clickhouse_driver |
| 71 | +type(ClickHouseClient) :: client |
| 72 | +call client%connect('localhost', 9000) |
| 73 | +call client%execute('SELECT * FROM measurements') |
| 74 | +``` |
| 75 | + |
| 76 | +### Codeblock from a file in docs repo |
| 77 | + |
| 78 | +Here is some basic connect and query code from a file in this repository: |
| 79 | + |
| 80 | +```fortran file=code_snippets/integrations/language_clients/example.f90 |
| 81 | +program clickhouse_example |
| 82 | + use clickhouse_driver |
| 83 | + implicit none |
| 84 | +
|
| 85 | + type(ClickHouseClient) :: client |
| 86 | + type(QueryResult) :: result |
| 87 | + integer :: status, i |
| 88 | +
|
| 89 | + ! Initialize and connect to ClickHouse |
| 90 | + call client%init('localhost', port=9000, database='default', & |
| 91 | + user='default', password='') |
| 92 | +
|
| 93 | + status = client%connect() |
| 94 | + if (status /= CH_SUCCESS) then |
| 95 | + print *, 'Connection failed!' |
| 96 | + stop |
| 97 | + end if |
| 98 | +
|
| 99 | + print *, 'Connected to ClickHouse successfully' |
| 100 | +
|
| 101 | + ! Execute a simple query |
| 102 | + call client%execute('SELECT version()', result) |
| 103 | + call result%print() |
| 104 | +
|
| 105 | + ! Cleanup |
| 106 | + call result%free() |
| 107 | + call client%disconnect() |
| 108 | +
|
| 109 | +end program clickhouse_example |
| 110 | +``` |
| 111 | + |
| 112 | +### Code block from an external URL |
| 113 | + |
| 114 | +Here is some code from a file in GitHub: |
| 115 | + |
| 116 | +```fortran url=https://raw.githubusercontent.com/scivision/fortran2018-examples/refs/heads/main/src/system/play_sound.f90 |
| 117 | +program play_sound |
| 118 | +!! recommend using all lower case filenames and no spaces. |
| 119 | +!! plays sound in Fortran 2003+ |
| 120 | +
|
| 121 | +implicit none |
| 122 | +
|
| 123 | +! configure ffplay -- could make if/else to allow other players |
| 124 | +character(*),parameter :: playexe='ffplay' |
| 125 | +! -autoexit clips off the end of the sound slightly, but otherwise thread hangs open even after Fortran program ends. |
| 126 | +character(*),parameter :: cmdopts='-autoexit -loglevel warning -nodisp' |
| 127 | +
|
| 128 | +character(:), allocatable :: pcmd, buf |
| 129 | +logical :: fexist |
| 130 | +integer :: ierr, istat, L |
| 131 | +
|
| 132 | +valgrind : block |
| 133 | +
|
| 134 | +call get_command_argument(1, length=L, status=ierr) |
| 135 | +if (ierr /= 0) error stop "please specify sound file" |
| 136 | +allocate(character(L) :: buf) |
| 137 | +call get_command_argument(1, value=buf) |
| 138 | +
|
| 139 | +inquire(file=buf, exist=fexist) |
| 140 | +
|
| 141 | +if (.not. fexist) error stop 'did not find FILE ' // buf |
| 142 | +
|
| 143 | +pcmd = playexe//' '//cmdopts//' '//trim(buf) |
| 144 | +
|
| 145 | +call execute_command_line(pcmd, cmdstat=ierr, exitstat=istat) |
| 146 | +if(ierr /= 0) error stop 'could not open player' |
| 147 | +if(istat /= 0) error stop 'problem playing file' |
| 148 | +
|
| 149 | +end block valgrind |
| 150 | +
|
| 151 | +end program |
| 152 | +
|
| 153 | +``` |
| 154 | + |
| 155 | +</VerticalStepper> |
0 commit comments