relpipe-data/examples-in-sql-selecting-existing-database.xml
branchv_0
changeset 279 de1b49ba06f1
child 297 192b0059a6c4
equal deleted inserted replaced
278:ae17db13569c 279:de1b49ba06f1
       
     1 <stránka
       
     2 	xmlns="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/strana"
       
     3 	xmlns:m="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/makro">
       
     4 	
       
     5 	<nadpis>SELECTing from SQLite databases</nadpis>
       
     6 	<perex>read from an existing SQLite file or update it</perex>
       
     7 	<m:pořadí-příkladu>03300</m:pořadí-příkladu>
       
     8 
       
     9 	<text xmlns="http://www.w3.org/1999/xhtml">
       
    10 		
       
    11 		<p>
       
    12 			Both the <code>relpipe-tr-sql</code> and <code>relpipe-in-sql</code>
       
    13 			support the option <code>--file</code> and can store data in a file instead of just in-memory.
       
    14 			Primary purpose of this option is to offload to HDD and be able to process more data whithout consuming too much RAM.
       
    15 			So such file is usually a temporary one and is deleted immediately after the transformation.
       
    16 			But we can also direct it to an existing file and append new relations to it.
       
    17 			Or we can create a new file and do not delete it – using the <code>--file-keep true</code> option.
       
    18 		</p>
       
    19 		
       
    20 		<p>
       
    21 			Thus we can use the <code>relpipe-in-sql</code> as a database client to access existing SQLite files and SELECT from them (and then convert the results to any supported format).
       
    22 			An we can use the <code>relpipe-tr-sql</code> as an output filter which converts relational data to a SQLite file that can be queried later.
       
    23 		</p>
       
    24 		
       
    25 		<p>
       
    26 			SQLite is a popular format used by many applications to store structured data.
       
    27 			Version control systems (VCS) like <a href="https://www.monotone.ca/">Monotone</a> or <a href="https://fossil-scm.org/">Fossil</a>
       
    28 			use it as a repository format and put whole history, data and metadata into it.
       
    29 		</p>
       
    30 		
       
    31 		<p>
       
    32 			So if we clone a Fossil repository (the SQLite one in this case):
       
    33 		</p>
       
    34 		
       
    35 		<m:pre jazyk="bash"><![CDATA[fossil clone http://www.sqlite.org/cgi/src sqlite.fossil]]></m:pre>
       
    36 		
       
    37 		<p>
       
    38 			We can then query it using <m:name/> tools:
       
    39 		</p>
       
    40 		
       
    41 		<m:pre jazyk="bash"><![CDATA[relpipe-in-sql \
       
    42 	--file sqlite.fossil \
       
    43 	--relation tickets "
       
    44 		SELECT 
       
    45 			type,
       
    46 			status,
       
    47 			count(*) AS count
       
    48 		FROM ticket 
       
    49 		GROUP BY type, status
       
    50 		ORDER BY count DESC;
       
    51 	" | relpipe-out-tabular]]></m:pre>
       
    52 	
       
    53 		<p>and get some aggregated statistics:</p>
       
    54 		
       
    55 		<m:pre jazyk="text"><![CDATA[tickets:
       
    56  ╭───────────────────┬─────────────────┬────────────────╮
       
    57  │ type     (string) │ status (string) │ count (string) │
       
    58  ├───────────────────┼─────────────────┼────────────────┤
       
    59  │ Code_Defect       │ Fixed           │ 394            │
       
    60  │ Code_Defect       │ Closed          │ 115            │
       
    61  │ Feature_Request   │ Closed          │ 36             │
       
    62  │ Feature_Request   │ Fixed           │ 16             │
       
    63  │ Build_Problem     │ Closed          │ 10             │
       
    64  │ Documentation     │ Fixed           │ 10             │
       
    65  │ Documentation     │ Closed          │ 8              │
       
    66  │ Incident          │ Closed          │ 4              │
       
    67  │ Build_Problem     │ Fixed           │ 3              │
       
    68  │ Performance_Issue │ Closed          │ 3              │
       
    69  │ Support_Request   │ Closed          │ 3              │
       
    70  │ Code_Defect       │ Open            │ 2              │
       
    71  │ Feature_Request   │ Open            │ 2              │
       
    72  │ Performance_Issue │ Fixed           │ 2              │
       
    73  │ Code_Defect       │ Deferred        │ 1              │
       
    74  │ Compiler_Warning  │ Closed          │ 1              │
       
    75  │ Compiler_Warning  │ Fixed           │ 1              │
       
    76  │ Not_A_Bug         │ Closed          │ 1              │
       
    77  │ Performance_Issue │ Open            │ 1              │
       
    78  │ Portability       │ Fixed           │ 1              │
       
    79  ╰───────────────────┴─────────────────┴────────────────╯
       
    80 Record count: 20]]></m:pre>
       
    81 		
       
    82 		<p>
       
    83 			Accessing internal data structures of another application might be bit tricky (if the application does not guarantee it as a stable API),
       
    84 			but it can be used for ad-hoc digging and even in regular use, there are chances that such „API“ will not break for a reasonable period of time.
       
    85 		</p>
       
    86 		
       
    87 		<p>
       
    88 			However, best results can be achieved, if the database scheme is under our control or defined as a stable API by someone else.
       
    89 		</p>
       
    90 		
       
    91 		<p>
       
    92 			All relations or their subset can be dumped even without writing any SELECTs: just use <code>--copy '.*'</code>
       
    93 			or e.g. <code>--copy 'tickets|tag|evet'</code> or <code>--copy-renamed 'tickets|tag|evet' 'copy_of_$0'</code> and get exported some relations under modified names (using regular expressions).
       
    94 			Such export can be converted e.g. to Recfile or XML format using corresponding output filter.
       
    95 		</p>
       
    96 		
       
    97 	</text>
       
    98 
       
    99 </stránka>